Skip to content

Commit

Permalink
Allow member expression as lib function arg
Browse files Browse the repository at this point in the history
  • Loading branch information
steeelydan committed Oct 5, 2023
1 parent 0649e75 commit 9705e00
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { identifier } from '../../identifier/identifier.js';
import { unaryExpression } from '../../unaryExpression/unaryExpression.js';
import { binaryExpression } from '../../binaryExpression/binaryExpression.js';
import { callExpression as compileCallExpression } from '../callExpression.js';
import { memberExpression } from '../../memberExpression/memberExpression.js';
import { evaluateLibraryFunctionCall } from '../utils/evaluateLibraryFunctionCall.js';

import type { CallExpression } from 'estree';
Expand All @@ -16,7 +17,8 @@ const defaultNumericArgAllowedValues: FunctionCallAllowedValues = [
{ nodeType: 'Identifier' },
{ nodeType: 'BinaryExpression' },
{ nodeType: 'UnaryExpression', validationSchema: Joi.number() },
{ nodeType: 'CallExpression' }
{ nodeType: 'CallExpression' },
{ nodeType: 'MemberExpression' }
];

export const eelLibraryFunctionCall = (
Expand Down Expand Up @@ -176,6 +178,10 @@ export const eelLibraryFunctionCall = (
argsSrc += compileCallExpression(arg, instance);
break;
}
case 'MemberExpression': {
argsSrc += memberExpression(null, arg, instance);
break;
}
default: {
instance.error('TypeError', `Argument type ${arg.type} not allowed`, arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ParsedFunctionArgument,
ValidatableFunctionCallAllowedValue
} from '../../../types.js';
import { memberExpression } from '../../memberExpression/memberExpression.js';

type ValidatedArgs<ArgName extends string> = {
[argName in ArgName]: ParsedFunctionArgument;
Expand Down Expand Up @@ -200,12 +201,18 @@ export const evaluateLibraryFunctionCall = <ArgName extends string>(

break;
}
case 'MemberExpression': {
value = '';
rawValue = '';

break;
}
// FIXME: Should be caught further up at arg validation
/* c8 ignore start */
default: {
errors.push({
type: 'TypeError',
msg: `${callee.name}: argument type ${givenArg.type} not allowed`,
msg: `evaluateLibraryFunctionCall(): ${callee.name}: argument type ${givenArg.type} not allowed`,
node: functionCallExpression
});
value = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,39 @@ R__S1__0 = someVar;
myVar__S3 = R__S1__0;
`)
);
expect(result.errors.length).to.equal(0);
expect(result.warnings.length).to.equal(1);
expect(result.warnings[0].type).to.equal('SymbolUnusedWarning');
});

it('member expression as argument', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'functions', inChannels: 2, outChannels: 2 });
const myObj = { prop: 1 };
const myVar = floor(myObj.prop);
`);
expect(result.success).to.equal(true);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.10.0 */
desc:functions
in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1
@init
myObj__prop = 1;
myVar = floor(myObj__prop);
`)
);
expect(result.errors.length).to.equal(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export const memberExpressionCall = (
if (!('object' in callee)) {
instance.error(
'TypeError',
'SimpleCallExpression not allowed. Parent CallExpression is of the wrong type: ' +
parentCallExpression.type,
`SimpleCallExpression not allowed. Parent CallExpression ${
'name' in parentCallExpression.callee
? parentCallExpression.callee.name
: '[anonymous]'
} is of the wrong type: ${parentCallExpression.type}`,
parentCallExpression
);

Expand Down

0 comments on commit 9705e00

Please sign in to comment.