Skip to content

Commit

Permalink
Merge pull request #28 from superfaceai/fix/visit-call-statement-args
Browse files Browse the repository at this point in the history
fix: inline call and call statement `args` variables
  • Loading branch information
lukas-valenta authored Jan 26, 2021
2 parents 90a9fe4 + 0567a9f commit 0b48ded
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [Unreleased]

### Fixed
* Array handling in mergeVariables function
* Inline call and call statement not correctly handling call stack arguments

## [0.0.6] - 2021-01-11
### Changed
Expand Down Expand Up @@ -49,7 +51,9 @@
* CI/CD workflows


[Unreleased]: https://github.com/superfaceai/sdk-js/compare/v0.0.4...HEAD
[Unreleased]: https://github.com/superfaceai/sdk-js/compare/v0.0.6...HEAD
[0.0.6]: https://github.com/superfaceai/sdk-js/compare/v0.0.5...v0.0.6
[0.0.5]: https://github.com/superfaceai/sdk-js/compare/v0.0.6...v0.0.5
[0.0.4]: https://github.com/superfaceai/sdk-js/compare/v0.0.3...v0.0.4
[0.0.3]: https://github.com/superfaceai/sdk-js/compare/v0.0.1...v0.0.3
[0.0.1]: https://github.com/superfaceai/sdk-js/releases/tag/v0.0.1
32 changes: 25 additions & 7 deletions src/internal/interpreter/map-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ describe('MapInterpreter', () => {
terminateFlow: true,
isError: false,
value: {
kind: 'PrimitiveLiteral',
value: 12,
kind: 'JessieExpression',
expression: 'args.foo',
},
},
],
Expand All @@ -172,7 +172,16 @@ describe('MapInterpreter', () => {
key: ['result'],
value: {
kind: 'InlineCall',
arguments: [],
arguments: [
{
kind: 'Assignment',
key: ['foo'],
value: {
kind: 'PrimitiveLiteral',
value: 12,
},
},
],
operationName: 'TestOp',
},
},
Expand Down Expand Up @@ -201,8 +210,8 @@ describe('MapInterpreter', () => {
isError: false,
terminateFlow: true,
value: {
kind: 'PrimitiveLiteral',
value: 5,
kind: 'JessieExpression',
expression: 'args.hey.now.length',
},
},
],
Expand All @@ -215,7 +224,16 @@ describe('MapInterpreter', () => {
{
kind: 'CallStatement',
operationName: 'TestOp',
arguments: [],
arguments: [
{
kind: 'Assignment',
key: ['hey', 'now'],
value: {
kind: 'PrimitiveLiteral',
value: 'you are a rock star',
},
},
],
statements: [
{
kind: 'OutcomeStatement',
Expand All @@ -233,7 +251,7 @@ describe('MapInterpreter', () => {
],
});

expect(result.isOk() && result.value).toEqual(12);
expect(result.isOk() && result.value).toEqual(26);
});

it('should correctly resolve scope', async () => {
Expand Down
37 changes: 22 additions & 15 deletions src/internal/interpreter/map-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
return this.constructObject(node.key, result);
}

async visitInlineCallNode(
node: InlineCallNode
private async visitCallCommon(
node: InlineCallNode | CallStatementNode
): Promise<Variables | undefined> {
const operation = this.operations[node.operationName];
if (!operation) {
Expand All @@ -207,27 +207,32 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
});
}

debug('Calling operation:', operation.name);

this.newStack('operation');
let args: Variables = {};
for (const assignment of node.arguments) {
args = mergeVariables(args, await this.visit(assignment));
}
this.addVariableToStack({ args });

const result = await this.visit(operation);
this.popStack();

return result;
}

async visitCallStatementNode(node: CallStatementNode): Promise<void> {
const operation = this.operations[node.operationName];

if (!operation) {
throw new MapASTError(`Operation not found: ${node.operationName}`, {
node,
ast: this.ast,
});
}
async visitInlineCallNode(
node: InlineCallNode
): Promise<Variables | undefined> {
return this.visitCallCommon(node);
}

debug('Calling operation:', operation.name);
async visitCallStatementNode(node: CallStatementNode): Promise<void> {
const result = await this.visitCallCommon(node);

this.newStack('operation');
const result = await this.visit(operation);
this.addVariableToStack({ outcome: { data: result } });

const secondResult = await this.processStatements(node.statements);
this.popStack(secondResult);
}
Expand Down Expand Up @@ -535,6 +540,8 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
this.stackTop.variables,
variables
);

debug('Updated stack:', this.stackTop);
}

private constructObject(keys: string[], value: Variables): NonPrimitive {
Expand Down Expand Up @@ -564,7 +571,7 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
this.stackTop.result = result ?? last.variables['result'];
}

debug('Popping stack:', last);
debug('Popped stack:', last);
}

private get stackTop(): Stack {
Expand Down

0 comments on commit 0b48ded

Please sign in to comment.