Skip to content

Commit

Permalink
Instrument: Store bindings in external vars [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 3, 2023
1 parent b83e7ee commit 96bf1f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 3 additions & 5 deletions lib/instrument/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ let newTargetNode;
* @param {Map} externalVars - External vars for function. Map keyed by block object.
* @param {Object} block - Block object
* @param {string} varName - Var name
* @param {Object} bindingOrExternalVar - Binding object or external var object
* @param {Object} binding - Binding object
* @returns {Object} - External var object
*/
function getOrCreateExternalVar(externalVars, block, varName, bindingOrExternalVar) {
function getOrCreateExternalVar(externalVars, block, varName, binding) {
let blockVars = externalVars.get(block),
externalVar;
if (blockVars) {
Expand All @@ -197,11 +197,9 @@ function getOrCreateExternalVar(externalVars, block, varName, bindingOrExternalV

if (!externalVar) {
externalVar = {
varNode: bindingOrExternalVar.varNode,
binding,
isReadFrom: false,
isAssignedTo: false,
isFunction: bindingOrExternalVar.isFunction,
argNames: bindingOrExternalVar.argNames,
trails: []
};
blockVars[varName] = externalVar;
Expand Down
10 changes: 5 additions & 5 deletions lib/instrument/visitors/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ function instrumentFunctionOrClassConstructor(node, fn, paramsBlock, bodyBlock,
const {id: parentFunctionId, externalVars: parentExternalVars} = parentFunction;
for (const {block, vars} of scopes) {
if (block.id >= parentFunctionId) break;
for (const varName in vars) {
getOrCreateExternalVar(parentExternalVars, block, varName, vars[varName]);
for (const [varName, {binding}] of Object.entries(vars)) {
getOrCreateExternalVar(parentExternalVars, block, varName, binding);
}
}
}
Expand Down Expand Up @@ -666,7 +666,7 @@ function createTrackerCall(scopes, fnInfoVarNode, state) {
t.arrowFunctionExpression([], t.arrayExpression(
scopes.map(scope => t.arrayExpression([
scope.block.varsBlock.scopeIdVarNode,
...Object.values(scope.vars).map(blockVar => blockVar.varNode)
...Object.values(scope.vars).map(blockVar => blockVar.binding.varNode)
]))
))
]);
Expand Down Expand Up @@ -743,11 +743,11 @@ function createFunctionInfoFunction(fn, scopes, astJson, fnInfoVarNode, state) {
blockId: block.id,
blockName: block.name,
vars: mapValues(vars, (varProps, varName) => {
if (varName === 'arguments') argNames = varProps.argNames;
if (varName === 'arguments') argNames = varProps.binding.argNames;
return {
isReadFrom: varProps.isReadFrom || undefined,
isAssignedTo: varProps.isAssignedTo || undefined,
isFunction: varProps.isFunction || undefined,
isFunction: varProps.binding.isFunction || undefined,
trails: varProps.trails
};
})
Expand Down

0 comments on commit 96bf1f3

Please sign in to comment.