-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
254543b
commit f388403
Showing
5 changed files
with
81 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* -------------------- | ||
* livepack module | ||
* Code instrumentation visitor for `with` statements | ||
* ------------------*/ | ||
|
||
'use strict'; | ||
|
||
// Export | ||
module.exports = WithStatement; | ||
|
||
// Modules | ||
const t = require('@babel/types'); | ||
|
||
// Imports | ||
const Expression = require('./expression.js'), | ||
Statement = require('./statement.js'), | ||
{ | ||
createAndEnterBlock, createBindingWithoutNameCheck, createBlockTempVar, activateBlock | ||
} = require('../blocks.js'), | ||
{createTrackerVarNode} = require('../internalVars.js'), | ||
{visitKey} = require('../visit.js'); | ||
|
||
// Exports | ||
|
||
/** | ||
* Visitor for `with () {}` statement. | ||
* @param {Object} node - Statement AST node | ||
* @param {Object} state - State object | ||
* @returns {undefined} | ||
*/ | ||
function WithStatement(node, state) { | ||
// Visit object i.e. expression inside `with (...)` | ||
visitKey(node, 'object', Expression, state); | ||
|
||
// Create block for `with` object | ||
const parentBlock = state.currentBlock; | ||
const block = createAndEnterBlock('with', false, state); | ||
const tempVarNode = createBlockTempVar(block, state); | ||
createBindingWithoutNameCheck(block, 'with', {isConst: true, varNode: tempVarNode}, state); | ||
// TODO: Should this only happen if var is accessed? | ||
activateBlock(block, state); | ||
|
||
// Visit body | ||
visitKey(node, 'body', Statement, state); | ||
|
||
// Exit block | ||
state.currentBlock = parentBlock; | ||
|
||
// Queue action to wrap `with` object | ||
state.secondPass(instrumentWithObj, node, tempVarNode, state); | ||
} | ||
|
||
function instrumentWithObj(node, tempVarNode, state) { | ||
// `with (o) {}` -> `with ( livepack_tracker.wrapWith(livepack_temp2 = o) ) {}` | ||
node.object = t.callExpression( | ||
t.memberExpression(createTrackerVarNode(state), t.identifier('wrapWith')), | ||
[t.assignmentExpression('=', tempVarNode, node.object)] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters