refactor(ses): performEval -> makeEvaluate
MODIFY
packages/ses/src/compartment-evaluate.js
packages/ses/src/compartment-evaluate.js
@@ -11,7 +11,7 @@ import {
evadeImportExpressionTest,
rejectSomeDirectEvalExpressions,
} from './transforms.js';
-import { performEval } from './evaluate.js';
+import { makeEvaluate } from './evaluate.js';
export const compartmentEvaluate = (compartmentFields, source, options) => {
// Perform this check first to avoid unecessary sanitizing.
@@ -63,10 +63,15 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
);
}
- return performEval(source, globalObject, localObject, {
+ const evaluate = makeEvaluate({
+ globalObject,
+ localObject,
globalTransforms,
- localTransforms,
sloppyGlobalsMode,
knownScopeProxies,
});
+
+ return evaluate(source, {
+ localTransforms,
+ });
};

MODIFY
packages/ses/src/evaluate.js
packages/ses/src/evaluate.js
@@ -16,31 +16,27 @@ import { assert } from './error/assert.js';
const { details: d } = assert;
+// TODO: rename localObject to scopeObject
+
/**
- * performEval()
- * The low-level operation used by all evaluators:
- * eval(), Function(), Evalutator.prototype.evaluate().
+ * makeEvaluate()
+ * Build the low-level operation used by all evaluators:
+ * eval(), Function(), Compartment.prototype.evaluate().
*
- * @param {string} source
- * @param {Object} globalObject
- * @param {Object} localObject
- * @param {Object} [options]
- * @param {Array<Transform>} [options.localTransforms]
+ * @param {Object} options
+ * @param {Object} options.globalObject
+ * @param {Object} [options.localObject]
* @param {Array<Transform>} [options.globalTransforms]
* @param {bool} [options.sloppyGlobalsMode]
* @param {WeakSet} [options.knownScopeProxies]
*/
-export const performEval = (
- source,
+export const makeEvaluate = ({
globalObject,
localObject = {},
- {
- localTransforms = [],
globalTransforms = [],
sloppyGlobalsMode = false,
knownScopeProxies = new WeakSet(),
- } = {},
-) => {
+} = {}) => {
const {
scopeHandler,
admitOneUnsafeEvalNext,
@@ -58,6 +54,12 @@ export const performEval = (
const evaluateFactory = makeEvaluateFactory(constants);
const evaluate = apply(evaluateFactory, scopeProxy, []);
+ /**
+ * @param {string} source
+ * @param {Object} [options]
+ * @param {Array<Transform>} [options.localTransforms]
+ */
+ return (source, { localTransforms = [] } = {}) => {
// Execute the mandatory transforms last to ensure that any rewritten code
// meets those mandatory requirements.
source = applyTransforms(source, [
@@ -100,3 +102,4 @@ export const performEval = (
}
}
};
+};

MODIFY
packages/ses/src/make-eval-function.js
packages/ses/src/make-eval-function.js
-import { performEval } from './evaluate.js';
+import { makeEvaluate } from './evaluate.js';
/*
* makeEvalFunction()
* A safe version of the native eval function which relies on
- * the safety of performEval for confinement.
+ * the safety of makeEvaluate for confinement.
*/
export const makeEvalFunction = (globalObject, options = {}) => {
// We use the the concise method syntax to create an eval without a
@@ -19,7 +19,8 @@ export const makeEvalFunction = (globalObject, options = {}) => {
// rule. Track.
return source;
}
- return performEval(source, globalObject, {}, options);
+ const evaluate = makeEvaluate({ ...options, globalObject });
+ return evaluate(source);
},
}.eval;

MODIFY
packages/ses/src/make-function-constructor.js
packages/ses/src/make-function-constructor.js
@@ -5,13 +5,13 @@ import {
defineProperties,
getPrototypeOf,
} from './commons.js';
-import { performEval } from './evaluate.js';
+import { makeEvaluate } from './evaluate.js';
import { assert } from './error/assert.js';
/*
* makeFunctionConstructor()
* A safe version of the native Function which relies on
- * the safety of performEval for confinement.
+ * the safety of makeEvaluate for confinement.
*/
export const makeFunctionConstructor = (globalObject, options = {}) => {
// Define an unused parameter to ensure Function.length === 1
@@ -53,7 +53,8 @@ export const makeFunctionConstructor = (globalObject, options = {}) => {
// 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 performEval(src, globalObject, {}, options);
+ const evaluate = makeEvaluate({ ...options, globalObject });
+ return evaluate(src);
};
defineProperties(newFunction, {

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
- * performEval operation (the Proxy is the argument of a 'with' binding).
+ * makeEvaluate 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-evaluate.js
packages/ses/test/test-evaluate.js
import '../index.js';
import './lockdown-safe.js';
import test from 'ava';
-import { performEval } from '../src/evaluate.js';
+import { makeEvaluate } from '../src/evaluate.js';
-test('performEval - default (non-sloppy, no localObject)', t => {
+test('makeEvaluate - default (non-sloppy, no localObject)', t => {
t.plan(6);
const globalObject = { abc: 123 };
- const evaluate = source => performEval(source, globalObject);
+ const evaluate = makeEvaluate({ globalObject });
t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
@@ -27,12 +27,11 @@ test('performEval - default (non-sloppy, no localObject)', t => {
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
-test('performEval - sloppyGlobalsMode', t => {
+test('makeEvaluate - sloppyGlobalsMode', t => {
t.plan(5);
const globalObject = {};
- const evaluate = source =>
- performEval(source, globalObject, {}, { sloppyGlobalsMode: true });
+ const evaluate = makeEvaluate({ globalObject, sloppyGlobalsMode: true });
t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
t.is(
@@ -46,14 +45,16 @@ test('performEval - sloppyGlobalsMode', t => {
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
-test('performEval - endowments', t => {
+test('makeEvaluate - endowments', t => {
t.plan(3);
const globalObject = {};
const endowments = { abc: 123 };
- const endowedEvaluate = source =>
- performEval(source, globalObject, endowments);
- const evaluate = source => performEval(source, globalObject);
+ const endowedEvaluate = makeEvaluate({
+ globalObject,
+ localObject: endowments,
+ });
+ const evaluate = makeEvaluate({ globalObject });
t.is(endowedEvaluate('abc'), 123, 'endowments can be referenced');
t.is(endowedEvaluate('abc += 333'), 456, 'endowments can be mutated');
@@ -64,7 +65,7 @@ test('performEval - endowments', t => {
);
});
-test('performEval - transforms - rewrite source', t => {
+test('makeEvaluate - transforms - rewrite source', t => {
t.plan(2);
const globalObject = {};
@@ -88,9 +89,9 @@ test('performEval - transforms - rewrite source', t => {
},
];
- const evaluate = (source, options = {}) =>
- performEval(source, globalObject, endowments, {
- ...options,
+ const evaluate = makeEvaluate({
+ globalObject,
+ localObject: endowments,
globalTransforms,
});

refactor(ses): performEval -> makeEvaluate