refactor(ses): makeEvaluate -> makeSafeEvaluator
evaluate -> safeEvaluate
MODIFY
packages/ses/src/compartment-evaluate.js
packages/ses/src/compartment-evaluate.js
@@ -11,7 +11,7 @@ import {
evadeImportExpressionTest,
rejectSomeDirectEvalExpressions,
} from './transforms.js';
-import { makeEvaluate } from './evaluate.js';
+import { makeSafeEvaluator } from './make-safe-evaluator.js';
export const compartmentEvaluate = (compartmentFields, source, options) => {
// Perform this check first to avoid unecessary sanitizing.
@@ -41,10 +41,10 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
arrayPush(localTransforms, rejectSomeDirectEvalExpressions);
}
- let evaluate;
+ let safeEvaluate;
if (__moduleShimLexicals__ === undefined && !sloppyGlobalsMode) {
- ({ evaluate } = compartmentFields);
+ ({ safeEvaluate } = compartmentFields);
} else {
// The scope proxy or global lexicals are different from the
// shared evaluator so we need to build a new one
@@ -75,15 +75,15 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
);
}
- evaluate = makeEvaluate({
+ ({ safeEvaluate } = makeSafeEvaluator({
globalObject,
localObject,
globalTransforms,
sloppyGlobalsMode,
knownScopeProxies,
- });
+ }));
}
- return evaluate(source, {
+ return safeEvaluate(source, {
localTransforms,
});
};

MODIFY
packages/ses/src/compartment-shim.js
packages/ses/src/compartment-shim.js
@@ -31,7 +31,7 @@ import { link } from './module-link.js';
import { getDeferredExports } from './module-proxy.js';
import { assert } from './error/assert.js';
import { compartmentEvaluate } from './compartment-evaluate.js';
-import { makeEvaluate } from './evaluate.js';
+import { makeSafeEvaluator } from './make-safe-evaluator.js';
const { quote: q } = assert;
@@ -275,7 +275,7 @@ export const makeCompartmentConstructor = (
initGlobalObjectConstants(globalObject);
const knownScopeProxies = new WeakSet();
- const evaluate = makeEvaluate({
+ const { safeEvaluate } = makeSafeEvaluator({
globalObject,
localObject: globalLexicals,
globalTransforms,
@@ -289,7 +289,7 @@ export const makeCompartmentConstructor = (
sharedGlobalPropertyNames,
targetMakeCompartmentConstructor,
this.constructor.prototype,
- evaluate,
+ safeEvaluate,
markVirtualizedNativeFunction,
);
@@ -301,7 +301,7 @@ export const makeCompartmentConstructor = (
globalObject,
knownScopeProxies,
globalLexicals,
- evaluate,
+ safeEvaluate,
resolveHook,
importHook,
moduleMap,

MODIFY
packages/ses/src/global-object.js
packages/ses/src/global-object.js
@@ -35,7 +35,7 @@ export const initGlobalObjectConstants = globalObject => {
* @param {Object} newGlobalPropertyNames
* @param {Function} makeCompartmentConstructor
* @param {Object} compartmentPrototype
- * @param {(string, Object?) => any} evaluate
+ * @param {(string, Object?) => any} safeEvaluate
* @param {(Object) => void} markVirtualizedNativeFunction
*/
export const initGlobalObjectProperties = (
@@ -44,7 +44,7 @@ export const initGlobalObjectProperties = (
newGlobalPropertyNames,
makeCompartmentConstructor,
compartmentPrototype,
- evaluate,
+ safeEvaluate,
markVirtualizedNativeFunction,
) => {
for (const [name, intrinsicName] of entries(universalPropertyNames)) {
@@ -71,8 +71,8 @@ export const initGlobalObjectProperties = (
const perCompartmentGlobals = {
globalThis: globalObject,
- eval: makeEvalFunction(evaluate),
- Function: makeFunctionConstructor(evaluate),
+ eval: makeEvalFunction(safeEvaluate),
+ Function: makeFunctionConstructor(safeEvaluate),
};
perCompartmentGlobals.Compartment = makeCompartmentConstructor(

MODIFY
packages/ses/src/lockdown-shim.js
packages/ses/src/lockdown-shim.js
@@ -28,7 +28,7 @@ import {
initGlobalObjectConstants,
initGlobalObjectProperties,
} from './global-object.js';
-import { makeEvaluate } from './evaluate.js';
+import { makeSafeEvaluator } from './make-safe-evaluator.js';
import { initialGlobalPropertyNames } from './whitelist.js';
import { tameFunctionToString } from './tame-function-tostring.js';
import { tameDomains } from './tame-domains.js';
@@ -295,7 +295,7 @@ export const repairIntrinsics = (
// start compartment, from the intrinsics.
initGlobalObjectConstants(globalThis);
- const evaluate = makeEvaluate({ globalObject: globalThis });
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
initGlobalObjectProperties(
globalThis,
@@ -303,7 +303,7 @@ export const repairIntrinsics = (
initialGlobalPropertyNames,
makeCompartmentConstructor,
compartmentPrototype,
- evaluate,
+ safeEvaluate,
markVirtualizedNativeFunction,
);

MODIFY
packages/ses/src/make-eval-function.js
packages/ses/src/make-eval-function.js
/*
* makeEvalFunction()
* A safe version of the native eval function which relies on
- * the safety of makeEvaluate for confinement.
+ * the safety of safeEvaluate for confinement.
*/
-export const makeEvalFunction = evaluate => {
+export const makeEvalFunction = safeEvaluate => {
// We use the the concise method syntax to create an eval without a
// [[Construct]] behavior (such that the invocation "new eval()" throws
// TypeError: eval is not a constructor"), but which still accepts a
@@ -17,7 +17,7 @@ export const makeEvalFunction = evaluate => {
// rule. Track.
return source;
}
- return evaluate(source);
+ return safeEvaluate(source);
},
}.eval;

MODIFY
packages/ses/src/make-function-constructor.js
packages/ses/src/make-function-constructor.js
@@ -10,9 +10,9 @@ import { assert } from './error/assert.js';
/*
* makeFunctionConstructor()
* A safe version of the native Function which relies on
- * the safety of makeEvaluate for confinement.
+ * the safety of safeEvaluate for confinement.
*/
-export const makeFunctionConstructor = evaluate => {
+export const makeFunctionConstructor = safeEvaluate => {
// Define an unused parameter to ensure Function.length === 1
const newFunction = function Function(_body) {
// Sanitize all parameters at the entry point.
@@ -52,7 +52,7 @@ export const makeFunctionConstructor = evaluate => {
// TODO: since we create an anonymous function, the 'this' value
// isn't bound to the global object as per specs, but set as undefined.
const src = `(function anonymous(${parameters}\n) {\n${bodyText}\n})`;
- return evaluate(src);
+ return safeEvaluate(src);
};
defineProperties(newFunction, {

RENAME
packages/ses/src/evaluate.js
packages/ses/src/make-safe-evaluator.js
@@ -19,7 +19,7 @@ const { details: d } = assert;
// TODO: rename localObject to scopeObject
/**
- * makeEvaluate()
+ * makeSafeEvaluator()
* Build the low-level operation used by all evaluators:
* eval(), Function(), Compartment.prototype.evaluate().
*
@@ -30,7 +30,7 @@ const { details: d } = assert;
* @param {bool} [options.sloppyGlobalsMode]
* @param {WeakSet} [options.knownScopeProxies]
*/
-export const makeEvaluate = ({
+export const makeSafeEvaluator = ({
globalObject,
localObject = {},
globalTransforms = [],
@@ -59,7 +59,7 @@ export const makeEvaluate = ({
* @param {Object} [options]
* @param {Array<Transform>} [options.localTransforms]
*/
- return (source, { localTransforms = [] } = {}) => {
+ const safeEvaluate = (source, { localTransforms = [] } = {}) => {
// Execute the mandatory transforms last to ensure that any rewritten code
// meets those mandatory requirements.
source = applyTransforms(source, [
@@ -102,4 +102,6 @@ export const makeEvaluate = ({
}
}
};
+
+ return { safeEvaluate };
};

MODIFY
packages/ses/src/scope-handler.js
packages/ses/src/scope-handler.js
@@ -39,7 +39,7 @@ const alwaysThrowHandler = new Proxy(
/*
* createScopeHandler()
* ScopeHandler manages a Proxy which serves as the global scope for the
- * makeEvaluate operation (the Proxy is the argument of a 'with' binding).
+ * safeEvaluate operation (the Proxy is the argument of a 'with' binding).
* As described in createSafeEvaluator(), it has several functions:
* - allow the very first (and only the very first) use of 'eval' to map to
* the real (unsafe) eval function, so it acts as a 'direct eval' and can

MODIFY
packages/ses/test/test-make-eval-function.js
packages/ses/test/test-make-eval-function.js
@@ -4,14 +4,14 @@ import '../index.js';
import './lockdown-safe.js';
import test from 'ava';
import { makeEvalFunction } from '../src/make-eval-function.js';
-import { makeEvaluate } from '../src/evaluate.js';
+import { makeSafeEvaluator } from '../src/make-safe-evaluator.js';
test('makeEvalFunction - leak', t => {
t.plan(8);
const globalObject = {};
- const evaluate = makeEvaluate({ globalObject });
- const safeEval = makeEvalFunction(evaluate);
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject });
+ const safeEval = makeEvalFunction(safeEvaluate);
t.throws(() => safeEval('none'), { instanceOf: ReferenceError });
t.is(safeEval('this.none'), undefined);
@@ -42,8 +42,8 @@ test('makeEvalFunction - globals', t => {
bar: { value: 2, writable: true },
},
);
- const evaluate = makeEvaluate({ globalObject });
- const safeEval = makeEvalFunction(evaluate);
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject });
+ const safeEval = makeEvalFunction(safeEvaluate);
t.is(safeEval('foo'), 1);
t.is(safeEval('bar'), 2);

RENAME
packages/ses/test/test-evaluate.js
packages/ses/test/test-make-evaluator.js
import '../index.js';
import './lockdown-safe.js';
import test from 'ava';
-import { makeEvaluate } from '../src/evaluate.js';
+import { makeSafeEvaluator } from '../src/make-safe-evaluator.js';
-test('makeEvaluate - default (non-sloppy, no localObject)', t => {
+test('safeEvaluate - default (non-sloppy, no localObject)', t => {
t.plan(6);
const globalObject = { abc: 123 };
- const evaluate = makeEvaluate({ globalObject });
+ const { safeEvaluate: evaluate } = makeSafeEvaluator({ globalObject });
t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
@@ -27,11 +27,14 @@ test('makeEvaluate - default (non-sloppy, no localObject)', t => {
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
-test('makeEvaluate - sloppyGlobalsMode', t => {
+test('safeEvaluate - sloppyGlobalsMode', t => {
t.plan(5);
const globalObject = {};
- const evaluate = makeEvaluate({ globalObject, sloppyGlobalsMode: true });
+ const { safeEvaluate: evaluate } = makeSafeEvaluator({
+ globalObject,
+ sloppyGlobalsMode: true,
+ });
t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
t.is(
@@ -45,16 +48,16 @@ test('makeEvaluate - sloppyGlobalsMode', t => {
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
-test('makeEvaluate - endowments', t => {
+test('safeEvaluate - endowments', t => {
t.plan(3);
const globalObject = {};
const endowments = { abc: 123 };
- const endowedEvaluate = makeEvaluate({
+ const { safeEvaluate: endowedEvaluate } = makeSafeEvaluator({
globalObject,
localObject: endowments,
});
- const evaluate = makeEvaluate({ globalObject });
+ const { safeEvaluate: evaluate } = makeSafeEvaluator({ globalObject });
t.is(endowedEvaluate('abc'), 123, 'endowments can be referenced');
t.is(endowedEvaluate('abc += 333'), 456, 'endowments can be mutated');
@@ -65,7 +68,7 @@ test('makeEvaluate - endowments', t => {
);
});
-test('makeEvaluate - transforms - rewrite source', t => {
+test('safeEvaluate - transforms - rewrite source', t => {
t.plan(2);
const globalObject = {};
@@ -89,7 +92,7 @@ test('makeEvaluate - transforms - rewrite source', t => {
},
];
- const evaluate = makeEvaluate({
+ const { safeEvaluate: evaluate } = makeSafeEvaluator({
globalObject,
localObject: endowments,
globalTransforms,

MODIFY
packages/ses/test/test-make-function-constructor.js
packages/ses/test/test-make-function-constructor.js
@@ -2,7 +2,7 @@ import '../index.js';
import './lockdown-safe.js';
import test from 'ava';
import { makeFunctionConstructor } from '../src/make-function-constructor.js';
-import { makeEvaluate } from '../src/evaluate.js';
+import { makeSafeEvaluator } from '../src/make-safe-evaluator.js';
test('functionConstructor', t => {
t.plan(12);
@@ -14,8 +14,8 @@ test('functionConstructor', t => {
bar: { value: 2, writable: true },
},
);
- const evaluate = makeEvaluate({ globalObject });
- const safeFunction = makeFunctionConstructor(evaluate);
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject });
+ const safeFunction = makeFunctionConstructor(safeEvaluate);
t.is(safeFunction('return foo')(), 1);
t.is(safeFunction('return bar')(), 2);

refactor(ses): makeEvaluate -> makeSafeEvaluator
evaluate -> safeEvaluate