Skip to content

Commit

Permalink
with statements WIP 4
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 3, 2023
1 parent a99cc46 commit 6cde2b1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/init/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ function evalDirect(args, tracker, filename, evalDirectLocal) {
currentThisBlock: undefined,
currentSuperBlock: undefined
};
const withVars = [];
const tempVars = [];
for (const [blockId, blockName, scopeId, ...varDefs] of scopeDefs) {
const block = createBlockWithId(blockId, blockName, true, state);
block.scopeIdVarNode = t.numericLiteral(scopeId);
state.currentBlock = block;

for (const [varName, isConst, isSilentConst, argNames, withObj] of varDefs) {
for (const [varName, isConst, isSilentConst, argNames, tempVarValue] of varDefs) {
if (varName === 'this') {
createThisBinding(block);
state.currentThisBlock = block;
Expand All @@ -127,28 +127,28 @@ function evalDirect(args, tracker, filename, evalDirectLocal) {
block, varName, {isConst: !!isConst, isSilentConst: !!isSilentConst, argNames}
);

if (withObj) withVars.push({withObj, binding});
if (tempVarValue) tempVars.push({value: tempVarValue, binding});
}
}
}

const withBindings = withVars.length > 0 ? withVars.map((v => v.binding)) : null;
const tempVarBindings = tempVars.length > 0 ? tempVars.map((v => v.binding)) : null;

// Compile to executable code with tracking code inserted.
// If var names prefix inside code has to be different from outside,
// code is wrapped in an IIFE which renames the tracker/eval functions:
// code is wrapped in an IIFE which renames the tracker/eval functions and any temp vars:
// `() => foo` ->
// `((livepack1_tracker, livepack1_getScopeId) => () => foo)(livepack_tracker, livepack_getScopeId)`
const {
code: codeInstrumented, shouldThrow, prefixNumChanged
} = compile(code, tracker, filename, false, state, withBindings, isStrict);
} = compile(code, tracker, filename, false, state, tempVarBindings, isStrict);

// Call `eval()` with amended code
let res = execEvalCode(execEvalSingleArg, codeInstrumented, shouldThrow, code, evalDirectLocal);

// If was wrapped in a function, inject values into function
const params = prefixNumChanged ? [tracker, getScopeId] : [];
if (withBindings) params.push(...withVars.filter(v => v.binding.varNode).map(v => v.withObj));
if (tempVarBindings) params.push(...tempVars.filter(v => v.binding.varNode).map(v => v.value));
if (params.length > 0) res = res(...params);

return res;
Expand Down

0 comments on commit 6cde2b1

Please sign in to comment.