Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat called variables as async fns. Fixes #110 #111

Merged
merged 2 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ASYNC_TYPES = [
'doThinkFor',
'doSayFor'
];
const FN_EVAL = ['evaluateCustomBlock', 'reportCallCC', 'evaluate'];
const FN_EVAL = ['reportCallCC', 'evaluate', 'doCallCC'];
class Node extends GenericNode {
constructor(id) {
super();
Expand Down Expand Up @@ -60,6 +60,10 @@ class Node extends GenericNode {
this.finalize();
}

isAsync() {
return !!this.children.find(node => node.isAsync());
}

simplify(allowWarp) {
this.children.forEach(node => node.simplify(allowWarp));
}
Expand Down Expand Up @@ -100,12 +104,13 @@ class Node extends GenericNode {
return Node.from(xmlElement.children[0]);
}

let block;
let block,
childrenLoaded = false;
if (tag === 'script') {
block = new Block();
} else if (xmlElement.tag === 'context') {
// TODO
block = Node.fromContext(xmlElement);
childrenLoaded = true;
} else if (xmlElement.tag === 'custom-block') {
const value = attributes.s;
const id = attributes.collabId;
Expand Down Expand Up @@ -136,11 +141,13 @@ class Node extends GenericNode {
block = new BuiltIn(id, type);
}

for (let i = 0; i < xmlElement.children.length; i++) {
const childTag = xmlElement.children[i].tag;
const child = Node.from(xmlElement.children[i]);
if (child !== null) {
block.addChild(child);
if (!childrenLoaded) {
for (let i = 0; i < xmlElement.children.length; i++) {
const childTag = xmlElement.children[i].tag;
const child = Node.from(xmlElement.children[i]);
if (child !== null) {
block.addChild(child);
}
}
}

Expand All @@ -153,17 +160,17 @@ class Node extends GenericNode {
if (receiverNode && receiverNode.children.length) { // create the context
receiver = receiverNode.children[0];
if (receiver.tag === 'ref') receiver = receiver.target;
//this.parse(receiver);
}

// Add the execution code
const [inputs, variables, fnBody] = element.children;
const isNoOp = !fnBody;
// FIXME: How is "variables" used???
// Could this be used to store variables captured from the closure?
const type = fnBody.tag === 'script' ? 'reifyScript' : 'reifyReporter';

const type = (isNoOp || fnBody.tag === 'script') ? 'reifyScript' : 'reifyReporter';
const node = new BuiltIn(null, type); // TODO: Make these custom types?
//const fnNode = this.createAstNode(fnBody);
const fnNode = Node.from(fnBody);
const fnNode = isNoOp ? null : Node.from(fnBody);
if (fnNode) {
node.addChild(fnNode);
} else {
Expand Down Expand Up @@ -243,9 +250,11 @@ class BuiltIn extends Node { // FIXME: Not the best
}
return true;
} else if (FN_EVAL.includes(this.type)) {
return !!this.children.find(child => child.isAsync());
} else if (this.type === 'context') {
return this.children[0].isAsync();
const fn = this.first();
if (!fn) {
console.log('this', this);
}
return fn.type === 'variable' || super.isAsync();
}

const children = this.children
Expand Down Expand Up @@ -454,7 +463,7 @@ class CallCustomFunction extends BuiltIn {
const parents = this.allParents();
}
assert(this.definition, `No definition found for ${this.name}`);
return this.definition.isAsync();
return this.definition.isAsync() || super.isAsync();
}

addBlockDefinitions(defs) {
Expand Down
7 changes: 7 additions & 0 deletions test/functions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ describe('functions', function() {
// TODO
});
});

describe('async fns in variables', function() {
it('should resolve async fn as variable', async function() {
const result = await utils.compileAndRun('aliased-async-fn');
assert.equal(result, true);
});
});
});
15 changes: 15 additions & 0 deletions test/test-cases/contexts/get-field-context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<context>
<inputs>
<input>row</input>
</inputs>
<variables/>
<block s="reportListItem">
<l>1</l>
<block var="row"/>
</block>
<context>
<inputs/>
<variables/>
</context>
</context>

1 change: 1 addition & 0 deletions test/test-cases/contexts/rpc-example.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/test-cases/contexts/transform.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<context id="12"><inputs><input>row</input></inputs><variables></variables><block collabId="" s="reportListItem"><l>2</l><block collabId="" var="row"/></block><receiver></receiver><context id="18"><inputs></inputs><variables></variables><receiver></receiver></context></context>
1 change: 1 addition & 0 deletions test/test-cases/projects/aliased-async-fn.xml

Large diffs are not rendered by default.