Skip to content

Commit

Permalink
WIP 2
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 24, 2023
1 parent 4ccba0b commit f5d47ef
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 35 deletions.
4 changes: 1 addition & 3 deletions lib/instrument/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ function createBindingWithoutNameCheck(block, varName, props) {
* @returns {Object} - Binding object
*/
function createThisBinding(block) {
// `isFrozenName: true` because it cannot be renamed within function in which it's created.
// If `this` has to be substituted for a temp var for transpiled `super()`,
// `isFrozenName` will be set to false.
// `isFrozenName: true` because it cannot be renamed within function in which it's created
return createBindingWithoutNameCheck(
// eslint-disable-next-line no-use-before-define
block, 'this', {varNode: thisNode, isConst: true, isFrozenName: true}
Expand Down
17 changes: 3 additions & 14 deletions lib/instrument/visitors/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const t = require('@babel/types');

// Imports
const {
activateSuperBinding, getParentFullFunction, createInternalVarForThis, createExternalVarForThis,
activateSuperBinding, getParentFullFunction, createExternalVarForThis,
setSuperIsProtoOnFunctions
} = require('./super.js'),
{getOrCreateExternalVar, activateBlock, activateBinding} = require('../blocks.js'),
{createTempVarNode} = require('../internalVars.js'),
{copyLocAndComments} = require('../utils.js'),
{isReservedWord, getProp} = require('../../shared/functions.js');
{isReservedWord} = require('../../shared/functions.js');

// Exports

Expand Down Expand Up @@ -141,13 +141,11 @@ function instrumentEvalCall(callNode, block, fn, isStrict, canUseSuper, superIsP
// Freeze binding to prevent var being renamed when used internally in a function.
// All vars in scope need to be frozen, even if not accessible to `eval()`
// because if they're renamed, they would become accessible to `eval()` when they weren't before.
// `this` should not be frozen internally, as it can be replaced with a variable
// in a class constructor when `super()` is transpiled.
// For `super` and `new.target`, the var name can't actually be frozen, but flagged here
// as frozen anyway, to indicate that replacement var name should be prefixed with `_`.
// Frozen `new.target` is not currently supported.
// https://github.com/overlookmotel/livepack/issues/448
if (varName !== 'this' && !binding.isFrozenName) {
if (!binding.isFrozenName) {
binding.isFrozenName = true;
binding.trails.length = 0;
}
Expand Down Expand Up @@ -286,15 +284,6 @@ function activateSuperIfIsUsable(fn, state) {
if (fullFnType === 'FunctionDeclaration' || fullFnType === 'FunctionExpression') return false;
}
createExternalVarForThis(fn, state);
} else if (fnType === 'ClassDeclaration' || fnType === 'ClassExpression') {
const {trail} = state;
if (trail.length > 6) {
const maybeConstructorNode = getProp(fn.node, trail, 3);
if (maybeConstructorNode.type === 'ClassMethod' && maybeConstructorNode.kind === 'constructor') {
// In class constructor - need internal var for `this` as `eval()` code might include `super()`
createInternalVarForThis(fn, state);
}
}
}

setSuperIsProtoOnFunctions(superBlock, fn, state);
Expand Down
15 changes: 0 additions & 15 deletions lib/instrument/visitors/super.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
Super,
recordSuper,
activateSuperBinding,
createInternalVarForThis,
createExternalVarForThis,
setSuperIsProtoOnFunctions,
getSuperVarNode,
Expand Down Expand Up @@ -95,20 +94,6 @@ function activateSuperBinding(superBlock, state) {
return binding;
}

/**
* Create internal var for `this` on function.
* @param {Object} fn - Function object
* @param {Object} state - State object
* @returns {undefined}
*/
function createInternalVarForThis(fn, state) {
const binding = state.currentThisBlock.bindings.this;
if (binding.isFrozenName) {
binding.isFrozenName = false;
fn.bindings.push(binding);
}
}

/**
* Create external var for `this` on function.
* @param {Object} fn - Function object
Expand Down
5 changes: 2 additions & 3 deletions lib/serialize/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,8 @@ module.exports = {
// Function names and vars which are frozen by `eval()` aren't included in `internalVars`.
const transformVarName = this.createVarNameTransform(fnReservedVarNames);
for (const varName in internalVars) {
// `this` is only included in `internalVars` if it's the faux `this` created for transpiled
// `super()`. If function contains `eval()`, make sure it's prefixed with "_".
const newName = transformVarName(varName, fnContainsEval && varName === 'this');
// NB: Frozen vars are not included in `internalVars`
const newName = transformVarName(varName, false);
if (newName !== varName) {
for (const varNode of internalVars[varName]) {
varNode.name = newName;
Expand Down

0 comments on commit f5d47ef

Please sign in to comment.