Skip to content

Commit

Permalink
Don't instrument code if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 4, 2023
1 parent 8b0b32e commit ff40629
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/init/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ function evalIndirect(code, fileProps, tracker) {
// No need to wrap eval code in a further `eval()` call the way there is for direct eval with
// prefix change, because any local declaration of a var called `eval` will be renamed.
// So `eval` in injected statement will always refer to `global.eval`.
let codeWasInstrumented = false;
const addInternalVarsToAst = (ast, {internalVarsPrefixNum: insidePrefixNum}) => {
codeWasInstrumented = true;

ast.program.body.unshift(
t.variableDeclaration('const', [t.variableDeclarator(
t.arrayPattern([trackerVarNode(insidePrefixNum), getScopeIdVarNode(insidePrefixNum)]),
Expand All @@ -162,10 +165,12 @@ function evalIndirect(code, fileProps, tracker) {
// 1. Array containing `tracker` and `getScopeId` functions
// - for `const [livepack_tracker, livepack_getScopeId] = eval;`.
// 2. Revert to returning the usual shimmed `eval`.
getEval = () => {
getEval = getShimmedEval;
return [tracker, getScopeId];
};
if (codeWasInstrumented) {
getEval = () => {
getEval = getShimmedEval;
return [tracker, getScopeId];
};
}

// `eval()` code without external scope
return nativeEval(code);
Expand Down Expand Up @@ -270,6 +275,7 @@ function evalDirect(
// const livepack1_tracker = livepack_tracker.createTracker(1),
// livepack1_getScopeId = livepack_getScopeId;
// ```
// Also create var for `livepack_localEval` if required.
// The additional wrapping in another `eval()` is in case this code defines a var which clashes
// with the external `livepack_tracker` or `livepack_getScopeId` var.
let prefixNumIsChanged = false;
Expand Down Expand Up @@ -413,15 +419,19 @@ function compile(code, fileProps, state, isStrict, outsidePrefixNum, addInternal
modifyAst(ast, filename, false, isStrict, undefined, state);
const instrumentationState = state.state;

// Update next block ID for file
fileProps.nextBlockId = instrumentationState.nextBlockId;
// Only substitute instrumented code if instrumentation is necessary.
// Plain calculation e.g. `x + 2` doesn't require instrumentation.
if (instrumentationState.fileContainsFunctionsOrEval) {
// Update next block ID for file
fileProps.nextBlockId = instrumentationState.nextBlockId;

// If indirect `eval`, or direct `eval()` with different prefix nums inside and outside `eval()`,
// inject `livepack_tracker` and `livepack_getScopeId`
addInternalVarsToAst(ast, instrumentationState);
// If indirect `eval`, or direct `eval()` with different prefix nums inside and outside `eval()`,
// inject `livepack_tracker` and `livepack_getScopeId`
addInternalVarsToAst(ast, instrumentationState);

// Return instrumented code
code = generateCode(ast); // eslint-disable-line no-use-before-define
// Generate instrumented code
code = generateCode(ast); // eslint-disable-line no-use-before-define
}

if (DEBUG) {
/* eslint-disable no-console */
Expand Down

0 comments on commit ff40629

Please sign in to comment.