From f8cd3d378eb58d87e3385de374fb20cdb39719bc Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Thu, 23 Nov 2023 17:34:27 +0000 Subject: [PATCH] Tests WIP 2 --- test/eval.test.js | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/test/eval.test.js b/test/eval.test.js index e85de993..8c5da9a1 100644 --- a/test/eval.test.js +++ b/test/eval.test.js @@ -1925,6 +1925,110 @@ describe('eval', () => { }); }); + describe('freezes linked vars internal to function where one is in scope of eval', () => { + describe('function params + var bindings', () => { + itSerializes('where var binding is frozen', { + in: ` + module.exports = function(param = 1, module, exports) { + var param; + eval('param'); + return param; + }; + `, + out: `(0,eval)(" + (function(param=1,module,exports){var param;eval(\\"param\\");return param}) + ")`, + validate(fn) { + expect(fn).toBeFunction(); + expect(fn()).toBe(1); + } + }); + + itSerializes('where function param is frozen', { + in: ` + module.exports = function(param = {x: 1}, y = eval('param'), module, exports) { + var param; + return [param, y]; + }; + `, + out: `(0,eval)(" + (function(param={x:1},y=eval(\\"param\\"),module,exports){ + var param; + return[param,y] + }) + ")`, + validate(fn) { + expect(fn).toBeFunction(); + const arr = fn(); + expect(arr).toEqual([{x: 1}, {x: 1}]); + expect(arr[1]).toBe(arr[0]); + } + }); + }); + + describe('for statement bindings', () => { + itSerializes('where left binding is frozen', { + in: ` + module.exports = function(module, exports) { + let getLeft, getRight; + for ( + let [x, y = getLeft = () => x, z = () => eval('x')] + of (getRight = () => typeof x, [[1]]) + ) ; + return {getLeft, getRight}; + }; + `, + out: `(0,eval)(" + (function(module,exports){ + let getLeft,getRight; + for(let[x,y=getLeft=()=>x,z=()=>eval(\\"x\\")]of(getRight=()=>typeof x,[[1]])); + return{getLeft,getRight} + }) + ")`, + validate(fn) { + expect(fn).toBeFunction(); + const {getLeft, getRight} = fn(); + expect(getLeft).toBeFunction(); + expect(getLeft()).toBe(1); + expect(getRight).toBeFunction(); + expect(getRight).toThrowWithMessage( + ReferenceError, "Cannot access 'x' before initialization" + ); + } + }); + + itSerializes('where right binding is frozen', { + in: ` + module.exports = function(module, exports) { + let getLeft, getRight; + for ( + let [x, y = getLeft = () => x] + of (getRight = () => typeof x, () => eval('x'), [[1]]) + ) ; + return {getLeft, getRight}; + }; + `, + out: `(0,eval)(" + (function(module,exports){ + let getLeft,getRight; + for(let[x,y=getLeft=()=>x]of(getRight=()=>typeof x,()=>eval(\\"x\\"),[[1]])); + return{getLeft,getRight} + }) + ")`, + validate(fn) { + expect(fn).toBeFunction(); + const {getLeft, getRight} = fn(); + expect(getLeft).toBeFunction(); + expect(getLeft()).toBe(1); + expect(getRight).toBeFunction(); + expect(getRight).toThrowWithMessage( + ReferenceError, "Cannot access 'x' before initialization" + ); + } + }); + }); + }); + describe('prevents shadowed vars in upper scopes being accessible to eval', () => { itSerializes('simple case', { in: `