Skip to content

Commit

Permalink
Instrument: Move code to create scopes [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 4, 2023
1 parent dd345df commit 3cf1198
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/instrument/visitors/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,21 @@ function instrumentFunction(node, fn, parent, key, paramsBlock, bodyBlock, state
* @returns {undefined}
*/
function instrumentFunctionOrClassConstructor(node, fn, paramsBlock, bodyBlock, state) {
// NB `bodyBlock` here may actually be params block if is an arrow function with no body.
// Sort scopes in ascending order of block ID.
// Insert tracker call and block vars (`livepack_scopeId` and `livepack_temp`) into function.
// NB: `bodyBlock` here may actually be params block if is an arrow function with no body.
const trackerNode = createTrackerCall(fn, state);
insertTrackerCodeIntoFunction(fn, node, paramsBlock, bodyBlock, trackerNode, state);
}

/**
* Create tracker call.
* Create scopes array, and copy external vars down to parent function.
* @param {Object} fn - Function object
* @param {Object} state - State object
* @returns {Object} - AST node for tracker call
*/
function createTrackerCall(fn, state) {
// Sort scopes in ascending order of block ID
const scopes = [...fn.externalVars].map(([block, vars]) => ({block, vars}))
.sort((scope1, scope2) => (scope1.block.id > scope2.block.id ? 1 : -1));
fn.scopes = scopes;
Expand All @@ -650,22 +663,9 @@ function instrumentFunctionOrClassConstructor(node, fn, paramsBlock, bodyBlock,
}
}

// Insert tracker call and block vars (`livepack_scopeId` and `livepack_temp`) into function
const trackerNode = createTrackerCall(fn.id, scopes, state);
insertTrackerCodeIntoFunction(fn, node, paramsBlock, bodyBlock, trackerNode, state);
}

/**
* Create tracker call.
* @param {number} fnId - Function ID
* @param {Array<Object>} scopes - Array of scope objects in ascending order of block ID
* @param {Object} state - State object
* @returns {Object} - AST node for tracker call
*/
function createTrackerCall(fnId, scopes, state) {
// `livepack_tracker(livepack_getFnInfo_3, () => [[livepack_scopeId_2, x]]);`
// `livepack_tracker(livepack_getFnInfo_3, () => [[livepack_scopeId_2, x]])`
return t.callExpression(state.trackerVarNode, [
createFnInfoVarNode(fnId, state),
createFnInfoVarNode(fn.id, state),
t.arrowFunctionExpression([], t.arrayExpression(
scopes.map(scope => t.arrayExpression([
scope.block.varsBlock.scopeIdVarNode,
Expand Down

0 comments on commit 3cf1198

Please sign in to comment.