refactor(ses): rename and add option bag to global object init
MODIFY
packages/ses/src/compartment-shim.js
packages/ses/src/compartment-shim.js
@@ -21,8 +21,8 @@ import {
weaksetHas,
} from './commons.js';
import {
- initGlobalObjectConstants,
- initGlobalObjectProperties,
+ setGlobalObjectConstantProperties,
+ setGlobalObjectMutableProperties,
} from './global-object.js';
import { isValidIdentifierName } from './scope-constants.js';
import { sharedGlobalPropertyNames } from './whitelist.js';
@@ -272,7 +272,12 @@ export const makeCompartmentConstructor = (
const globalObject = {};
- initGlobalObjectConstants(globalObject);
+ // We must initialize all constant properties first because
+ // `makeSafeEvaluator` may use them to create optimized bindings
+ // in the evaluator.
+ // TODO: consider merging into a single initialization if internal
+ // evaluator is no longer eagerly created
+ setGlobalObjectConstantProperties(globalObject);
const knownScopeProxies = new WeakSet();
const { safeEvaluate } = makeSafeEvaluator({
@@ -283,15 +288,13 @@ export const makeCompartmentConstructor = (
knownScopeProxies,
});
- initGlobalObjectProperties(
- globalObject,
+ setGlobalObjectMutableProperties(globalObject, {
intrinsics,
- sharedGlobalPropertyNames,
- targetMakeCompartmentConstructor,
- this.constructor.prototype,
+ newGlobalPropertyNames: sharedGlobalPropertyNames,
+ makeCompartmentConstructor: targetMakeCompartmentConstructor,
safeEvaluate,
markVirtualizedNativeFunction,
- );
+ });
assign(globalObject, endowments);

MODIFY
packages/ses/src/global-object.js
packages/ses/src/global-object.js
@@ -4,15 +4,14 @@ import { makeFunctionConstructor } from './make-function-constructor.js';
import { constantProperties, universalPropertyNames } from './whitelist.js';
/**
- * initGlobalObjectConstants()
- * Create new global object using a process similar to ECMA specifications
- * (portions of SetRealmGlobalObject and SetDefaultGlobalBindings).
- * `newGlobalPropertyNames` should be either `initialGlobalPropertyNames` or
- * `sharedGlobalPropertyNames`.
+ * setGlobalObjectConstantProperties()
+ * Initializes a new global object using a process similar to ECMA specifications
+ * (SetDefaultGlobalBindings). This process is split between this function and
+ * `setGlobalObjectMutableProperties`.
*
* @param {Object} globalObject
*/
-export const initGlobalObjectConstants = globalObject => {
+export const setGlobalObjectConstantProperties = globalObject => {
for (const [name, constant] of entries(constantProperties)) {
defineProperty(globalObject, name, {
value: constant,
@@ -24,28 +23,29 @@ export const initGlobalObjectConstants = globalObject => {
};
/**
- * initGlobalObjectProperties()
+ * setGlobalObjectMutableProperties()
* Create new global object using a process similar to ECMA specifications
* (portions of SetRealmGlobalObject and SetDefaultGlobalBindings).
* `newGlobalPropertyNames` should be either `initialGlobalPropertyNames` or
* `sharedGlobalPropertyNames`.
*
* @param {Object} globalObject
- * @param {Object} intrinsics
- * @param {Object} newGlobalPropertyNames
- * @param {Function} makeCompartmentConstructor
- * @param {Object} compartmentPrototype
- * @param {(string, Object?) => any} safeEvaluate
- * @param {(Object) => void} markVirtualizedNativeFunction
+ * @param {Object} param1
+ * @param {Object} param1.intrinsics
+ * @param {Object} param1.newGlobalPropertyNames
+ * @param {Function} param1.makeCompartmentConstructor
+ * @param {(string, Object?) => any} param1.safeEvaluate
+ * @param {(Object) => void} param1.markVirtualizedNativeFunction
*/
-export const initGlobalObjectProperties = (
+export const setGlobalObjectMutableProperties = (
globalObject,
+ {
intrinsics,
newGlobalPropertyNames,
makeCompartmentConstructor,
- compartmentPrototype,
safeEvaluate,
markVirtualizedNativeFunction,
+ },
) => {
for (const [name, intrinsicName] of entries(universalPropertyNames)) {
if (objectHasOwnProperty(intrinsics, intrinsicName)) {

MODIFY
packages/ses/src/lockdown-shim.js
packages/ses/src/lockdown-shim.js
@@ -25,8 +25,8 @@ import tameRegExpConstructor from './tame-regexp-constructor.js';
import enablePropertyOverrides from './enable-property-overrides.js';
import tameLocaleMethods from './tame-locale-methods.js';
import {
- initGlobalObjectConstants,
- initGlobalObjectProperties,
+ setGlobalObjectConstantProperties,
+ setGlobalObjectMutableProperties,
} from './global-object.js';
import { makeSafeEvaluator } from './make-safe-evaluator.js';
import { initialGlobalPropertyNames } from './whitelist.js';
@@ -293,19 +293,18 @@ export const repairIntrinsics = (
// Initialize the powerful initial global, i.e., the global of the
// start compartment, from the intrinsics.
- initGlobalObjectConstants(globalThis);
+
+ setGlobalObjectConstantProperties(globalThis);
const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
- initGlobalObjectProperties(
- globalThis,
+ setGlobalObjectMutableProperties(globalThis, {
intrinsics,
- initialGlobalPropertyNames,
+ newGlobalPropertyNames: initialGlobalPropertyNames,
makeCompartmentConstructor,
- compartmentPrototype,
safeEvaluate,
markVirtualizedNativeFunction,
- );
+ });
/**
* 3. HARDEN to share the intrinsics.

MODIFY
packages/ses/test/test-global-object.js
packages/ses/test/test-global-object.js
@@ -4,14 +4,11 @@ import '../index.js';
import './lockdown-safe.js';
import test from 'ava';
import {
- initGlobalObjectConstants,
- initGlobalObjectProperties,
+ setGlobalObjectConstantProperties,
+ setGlobalObjectMutableProperties,
} from '../src/global-object.js';
import { sharedGlobalPropertyNames } from '../src/whitelist.js';
-import {
- makeCompartmentConstructor,
- CompartmentPrototype,
-} from '../src/compartment-shim.js';
+import { makeCompartmentConstructor } from '../src/compartment-shim.js';
test('globalObject', t => {
const intrinsics = {
@@ -23,17 +20,16 @@ test('globalObject', t => {
};
const globalObject = {};
- initGlobalObjectConstants(globalObject);
- const evaluate = (_source, _options) => {};
- initGlobalObjectProperties(
- globalObject,
+ setGlobalObjectConstantProperties(globalObject);
+ const safeEvaluate = (_source, _options) => {};
+ const markVirtualizedNativeFunction = _ => {};
+ setGlobalObjectMutableProperties(globalObject, {
intrinsics,
- sharedGlobalPropertyNames,
+ newGlobalPropertyNames: sharedGlobalPropertyNames,
makeCompartmentConstructor,
- CompartmentPrototype,
- evaluate,
- _ => {},
- );
+ safeEvaluate,
+ markVirtualizedNativeFunction,
+ });
t.truthy(globalObject instanceof Object);
t.is(Object.getPrototypeOf(globalObject), Object.prototype);

refactor(ses): rename and add option bag to global object init