From f5d47ef13739162fa2a5c0ba1e471e9328ad248a Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 24 Nov 2023 20:25:55 +0000 Subject: [PATCH] WIP 2 --- lib/instrument/blocks.js | 4 +--- lib/instrument/visitors/eval.js | 17 +++-------------- lib/instrument/visitors/super.js | 15 --------------- lib/serialize/blocks.js | 5 ++--- 4 files changed, 6 insertions(+), 35 deletions(-) diff --git a/lib/instrument/blocks.js b/lib/instrument/blocks.js index c688f9e1..40df4e3e 100644 --- a/lib/instrument/blocks.js +++ b/lib/instrument/blocks.js @@ -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} diff --git a/lib/instrument/visitors/eval.js b/lib/instrument/visitors/eval.js index e28bd7ae..03df9aba 100644 --- a/lib/instrument/visitors/eval.js +++ b/lib/instrument/visitors/eval.js @@ -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 @@ -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; } @@ -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); diff --git a/lib/instrument/visitors/super.js b/lib/instrument/visitors/super.js index 32664396..032623b9 100644 --- a/lib/instrument/visitors/super.js +++ b/lib/instrument/visitors/super.js @@ -10,7 +10,6 @@ module.exports = { Super, recordSuper, activateSuperBinding, - createInternalVarForThis, createExternalVarForThis, setSuperIsProtoOnFunctions, getSuperVarNode, @@ -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 diff --git a/lib/serialize/blocks.js b/lib/serialize/blocks.js index 4ea9fe97..e525c682 100644 --- a/lib/serialize/blocks.js +++ b/lib/serialize/blocks.js @@ -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;