11import {
22 dynamicSource ,
3- isEvalExpressionCandidate ,
43 simpleDynamicValue ,
54 stringArgs ,
65} from './source.mjs' ;
@@ -11,7 +10,7 @@ export function createDynamicCodeFacade({
1110 dynamicCompileAllowed,
1211 normalizedError,
1312 getVirtualURL,
14- getScope ,
13+ rewriteScriptSource ,
1514 define,
1615 defineReplacingNative,
1716 maskNativeFunction,
@@ -54,7 +53,7 @@ export function createDynamicCodeFacade({
5453
5554 function compileTimerString ( source ) {
5655 const text = String ( source || '' ) ;
57- return function anonymous ( ) { return runScopedNativeEval ( text ) ; } ;
56+ return function anonymous ( ) { return runRewrittenNativeEval ( text ) ; } ;
5857 }
5958
6059 function compileDynamic ( ctor , args , kind ) {
@@ -70,43 +69,10 @@ export function createDynamicCodeFacade({
7069 return fn ;
7170 }
7271
73- function dynamicEval ( source ) {
74- if ( arguments . length === 0 ) return undefined ;
75- if ( ! dynamicCompileAllowed ) throw normalizedError ( 'SecurityError' ) ;
76- return runScopedNativeEval ( String ( source ) ) ;
77- }
78-
79- function runScopedNativeEval ( text ) {
72+ function runRewrittenNativeEval ( text ) {
8073 if ( typeof Native . eval !== 'function' ) throw normalizedError ( 'NotSupportedError' ) ;
81- const previous = root . __ZP_EVAL_SCOPE ;
82- const hadPrevious = Object . hasOwn ( root , '__ZP_EVAL_SCOPE' ) ;
83- Object . defineProperty ( root , '__ZP_EVAL_SCOPE' , {
84- value : getScope ( ) ,
85- enumerable : false ,
86- configurable : true ,
87- writable : true ,
88- } ) ;
89- try {
90- const expr = isEvalExpressionCandidate ( text ) ? `(${ text } )` : text ;
91- return ( 0 , Native . eval ) ( `with(__ZP_EVAL_SCOPE){${ expr } \n}` ) ;
92- } finally {
93- restoreEvalScope ( previous , hadPrevious ) ;
94- }
95- }
96-
97- function restoreEvalScope ( previous , hadPrevious ) {
98- try {
99- if ( hadPrevious ) {
100- Object . defineProperty ( root , '__ZP_EVAL_SCOPE' , {
101- value : previous ,
102- enumerable : false ,
103- configurable : true ,
104- writable : true ,
105- } ) ;
106- } else {
107- delete root . __ZP_EVAL_SCOPE ;
108- }
109- } catch { }
74+ if ( typeof rewriteScriptSource !== 'function' ) throw normalizedError ( 'NotSupportedError' ) ;
75+ return ( 0 , Native . eval ) ( rewriteScriptSource ( String ( text || '' ) , 'classic' ) ) ;
11076 }
11177
11278 function setDynamicConstructorIdentity ( fn , name , proto ) {
@@ -130,7 +96,6 @@ export function createDynamicCodeFacade({
13096 }
13197
13298 function dynamicGlobal ( name ) {
133- if ( name === 'eval' ) return dynamicEval ;
13499 if ( name === 'Function' ) return dynamicFunction ;
135100 if ( name === 'AsyncFunction' ) return dynamicAsyncFunction ;
136101 if ( name === 'GeneratorFunction' ) return dynamicGeneratorFunction ;
@@ -182,10 +147,6 @@ export function createDynamicCodeFacade({
182147 'AsyncGeneratorFunction' ,
183148 NativeAsyncGeneratorFunction && NativeAsyncGeneratorFunction . prototype ,
184149 ) ;
185- try { Object . defineProperty ( dynamicEval , 'name' , { value : 'eval' , configurable : true } ) ; } catch { }
186- try { Object . defineProperty ( dynamicEval , 'length' , { value : 1 , configurable : true } ) ; } catch { }
187- maskNativeFunction ( dynamicEval , 'eval' ) ;
188- defineReplacingNative ( root , 'eval' , dynamicEval ) ;
189150 defineReplacingNative ( root , 'Function' , dynamicFunction ) ;
190151 installDynamicConstructorBackrefs ( ) ;
191152 if ( Native . setTimeout ) {
0 commit comments