restoring to the simpler evaluation routine
MODIFY
shim/src/evaluate.js
shim/src/evaluate.js
@@ -10,10 +10,9 @@ const HookFnName = '$RealmEvaluatorIIFE$';
// via globalProxy.
function addLexicalScopesToSource(sourceText) {
/**
- * We use a `with` statement who uses `argments[1]`, which is the
- * `sandbox.globalProxy` that implements the shadowing mechanism,
- * // and `arguments[0]`, which is the global object reprenseting
- * // the contour corresponding to `sandbox.globalObject`.
+ * We use a `with` statement who uses `argments[0]`, which is the
+ * `sandbox.globalProxy` that implements the shadowing mechanism as well as access to
+ * any global variable.
* Aside from that, the `this` value in sourceText will correspond to `sandbox.thisValue`.
* We have to use `arguments` instead of naming them to avoid name collision.
*/
@@ -21,13 +20,11 @@ function addLexicalScopesToSource(sourceText) {
sourceText = sourceText.replace(/\`/g, '\\`');
return `
function ${HookFnName}() {
- with(arguments[1]) {
- // with(arguments[0]) {
+ with(arguments[0]) {
return (function(){
"use strict";
return eval(\`${sourceText}\`);
}).call(this);
- // }
}
}
`;
@@ -51,10 +48,10 @@ export function evaluate(sourceText, sandbox) {
if (!sourceText) {
return undefined;
}
- sourceText = addLexicalScopesToSource(sourceText);
+ sourceText = addLexicalScopesToSource(sourceText + '');
setInternalEvaluation();
const fn = evalAndReturn(sourceText, sandbox);
- const result = fn.apply(sandbox.thisValue, [sandbox.globalObject, sandbox.globalProxy]);
+ const result = fn.apply(sandbox.thisValue, [sandbox.globalProxy]);
resetInternalEvaluation();
return result;
}
\ No newline at end of file

MODIFY
shim/src/proxy.js
shim/src/proxy.js
-let internalFlag = false;
+// this flag allow us to determine if the eval() call is a controlled eval done by the realm's code
+// or if it is user-land invocation, so we can react differently.
+let isInternalEvaluation = false;
export function setInternalEvaluation() {
- internalFlag = true;
+ isInternalEvaluation = true;
}
export function resetInternalEvaluation() {
- internalFlag = false;
-}
-
-export function isInternalEvaluation() {
- return internalFlag === true;
+ isInternalEvaluation = false;
}
export const proxyHandler = {
get(sandbox, propName) {
- if (propName === 'eval' && internalFlag) {
+ if (propName === 'eval' && isInternalEvaluation) {
resetInternalEvaluation();
return sandbox.confinedWindow.eval;
}
@@ -32,7 +30,7 @@ export const proxyHandler = {
return Reflect.deleteProperty(sandbox.globalObject, propName);
},
has(sandbox, propName) {
- if (propName === 'eval' && internalFlag) {
+ if (propName === 'eval' && isInternalEvaluation) {
return true;
}
if (propName in sandbox.globalObject) {

restoring to the simpler evaluation routine