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

Allow re-evaluation of specific arguments (compiler) #242

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/compiler/compat-block-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CompatibilityLayerBlockUtility extends BlockUtility {
this.thread = thread;
this.sequencer = thread.target.runtime.sequencer;
this._startedBranch = null;
this._forceRevaluationOfArguments = false;
thread.stack[0] = fakeBlockId;
thread.compatibilityStackFrame = stackFrame;
}
Expand Down
36 changes: 32 additions & 4 deletions src/compiler/jsexecute.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const isPromise = value => (
typeof value.then === 'function'
);
const executeInCompatibilityLayer = function*(inputs, blockFunction, isWarp, useFlags, blockId, branchInfo) {
const inputNames = new Set(Object.keys(inputs));
const thread = globalState.thread;
const blockUtility = globalState.blockUtility;
const stackFrame = branchInfo ? branchInfo.stackFrame : {};
Expand All @@ -151,12 +152,39 @@ const executeInCompatibilityLayer = function*(inputs, blockFunction, isWarp, use
return returnValue;
};

const executeBlock = () => {
const inputCache = {};
let firstRun = true;
const executeBlock = function*() {
let callInputs, reEvaluate = new Set();
if (firstRun || blockUtility._forceRevaluationOfArguments) {
if (firstRun) {
blockUtility._forceRevaluationOfArguments = false;
reEvaluate = inputNames;
firstRun = false;
}
if (blockUtility._forceRevaluationOfArguments) {
if (Array.isArray(blockUtility._forceRevaluationOfArguments)) {
reEvaluate = new Set(blockUtility._forceRevaluationOfArguments);
} else {
reEvaluate = inputNames;
}
blockUtility._forceRevaluationOfArguments = false;
callInputs = {};
}
for (const inputName of reEvaluate) {
if (!inputNames.has(inputName)) continue;
const inputValue = (
typeof inputs[inputName] === 'function'
) ? (yield* (inputs[inputName]())) : inputs[inputName];
inputCache[inputName] = inputValue;
if (callInputs) callInputs[inputName] = inputValue;
}
}
blockUtility.init(thread, blockId, stackFrame);
return blockFunction(inputs, blockUtility);
return blockFunction(callInputs || inputCache, blockUtility);
};

let returnValue = executeBlock();
let returnValue = yield* executeBlock();
if (isPromise(returnValue)) {
returnValue = finish(yield* waitPromise(returnValue));
if (useFlags) hasResumedFromPromise = true;
Expand All @@ -183,7 +211,7 @@ const executeInCompatibilityLayer = function*(inputs, blockFunction, isWarp, use
yield;
}

returnValue = executeBlock();
returnValue = yield* executeBlock();
if (isPromise(returnValue)) {
returnValue = finish(yield* waitPromise(returnValue));
if (useFlags) hasResumedFromPromise = true;
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ class JSGenerator {
for (const inputName of Object.keys(node.inputs)) {
const input = node.inputs[inputName];
const compiledInput = this.descendInput(input).asSafe();
result += `"${sanitize(inputName)}":${compiledInput},`;
result += `"${sanitize(inputName)}":(function*(){ return( ${compiledInput} ); }),`;
}
for (const fieldName of Object.keys(node.fields)) {
const field = node.fields[fieldName];
Expand Down Expand Up @@ -1445,7 +1445,8 @@ JSGenerator.unstable_exports = {
ConstantInput,
VariableInput,
Frame,
sanitize
sanitize,
jsexecute
};

// Test hook used by automated snapshot testing.
Expand Down
Loading