refactor(ses): introduce makeEvaluate
RENAME
packages/ses/src/make-evaluate-factory.js
packages/ses/src/make-evaluate.js
-import { FERAL_FUNCTION, arrayJoin } from './commons.js';
+import { FERAL_FUNCTION, arrayJoin, apply } from './commons.js';
+import { getScopeConstants } from './scope-constants.js';
/**
* buildOptimizer()
@@ -87,3 +88,22 @@ export const makeEvaluateFactory = (
}
`);
};
+
+/**
+ * @param {object} context
+ * @param {object} context.evalScope
+ * @param {object} context.globalLexicals
+ * @param {object} context.globalObject
+ * @param {object} context.scopeTerminator
+ */
+export const makeEvaluate = context => {
+ const { globalObjectConstants, globalLexicalConstants } = getScopeConstants(
+ context.globalObject,
+ context.globalLexicals,
+ );
+ const evaluateFactory = makeEvaluateFactory(
+ globalObjectConstants,
+ globalLexicalConstants,
+ );
+ return apply(evaluateFactory, context, []);
+};

MODIFY
packages/ses/src/make-safe-evaluator.js
packages/ses/src/make-safe-evaluator.js
// https://github.com/v8/v8/blob/master/src/builtins/builtins-function.cc
import { apply, freeze } from './commons.js';
-import { getScopeConstants } from './scope-constants.js';
import { strictScopeTerminator } from './strict-scope-terminator.js';
import { createSloppyGlobalsScopeTerminator } from './sloppy-globals-scope-terminator.js';
import { createEvalScope } from './eval-scope.js';
import { applyTransforms, mandatoryTransforms } from './transforms.js';
-import { makeEvaluateFactory } from './make-evaluate-factory.js';
+import { makeEvaluate } from './make-evaluate.js';
import { assert } from './error/assert.js';
const { details: d } = assert;
@@ -34,30 +33,20 @@ export const makeSafeEvaluator = ({
: strictScopeTerminator;
const { evalScope, allowNextEvalToBeUnsafe } = createEvalScope();
+ const evaluateContext = freeze({
+ evalScope,
+ globalLexicals,
+ globalObject,
+ scopeTerminator,
+ });
+
// Defer creating the actual evaluator to first use.
// Creating a compartment should be possible in no-eval environments
// It also allows more global constants to be captured by the optimizer
let evaluate;
- const makeEvaluate = () => {
+ const provideEvaluate = () => {
if (!evaluate) {
- const {
- globalObjectConstants,
- globalLexicalConstants,
- } = getScopeConstants(globalObject, globalLexicals);
- const evaluateFactory = makeEvaluateFactory(
- globalObjectConstants,
- globalLexicalConstants,
- );
- evaluate = apply(
- evaluateFactory,
- freeze({
- evalScope,
- globalLexicals,
- globalObject,
- scopeTerminator,
- }),
- [],
- );
+ evaluate = makeEvaluate(evaluateContext);
}
};
@@ -67,7 +56,7 @@ export const makeSafeEvaluator = ({
* @param {Array<Transform>} [options.localTransforms]
*/
const safeEvaluate = (source, { localTransforms = [] } = {}) => {
- makeEvaluate();
+ provideEvaluate();
// Execute the mandatory transforms last to ensure that any rewritten code
// meets those mandatory requirements.

MODIFY
packages/ses/test/test-make-evaluate-factory.js
packages/ses/test/test-make-evaluate-factory.js
import test from 'ava';
-import { makeEvaluateFactory } from '../src/make-evaluate-factory.js';
+import { makeEvaluateFactory } from '../src/make-evaluate.js';
test('Intrinsics - values', t => {
t.plan(4);

RENAME
packages/ses/test/test-make-evaluator.js
packages/ses/test/test-make-safe-evaluator.js
refactor(ses): introduce makeEvaluate