refactor(ses): Clarify name of markVirtualizedNativeFunction
MODIFY
packages/ses/index.js
packages/ses/index.js
@@ -34,12 +34,12 @@ if (getThis()) {
throw new Error(`SES failed to initialize, sloppy mode (SES_NO_SLOPPY)`);
}
-const nativeBrander = tameFunctionToString();
+const markVirtualizedNativeFunction = tameFunctionToString();
const Compartment = makeCompartmentConstructor(
makeCompartmentConstructor,
getGlobalIntrinsics(globalThis),
- nativeBrander,
+ markVirtualizedNativeFunction,
);
assign(globalThis, {

MODIFY
packages/ses/src/compartment-shim.js
packages/ses/src/compartment-shim.js
@@ -219,7 +219,7 @@ defineProperties(InertCompartment, {
* @callback MakeCompartmentConstructor
* @param {MakeCompartmentConstructor} targetMakeCompartmentConstructor
* @param {Record<string, any>} intrinsics
- * @param {(object: Object) => void} nativeBrander
+ * @param {(object: Object) => void} markVirtualizedNativeFunction
* @returns {Compartment['constructor']}
*/
@@ -227,7 +227,7 @@ defineProperties(InertCompartment, {
export const makeCompartmentConstructor = (
targetMakeCompartmentConstructor,
intrinsics,
- nativeBrander,
+ markVirtualizedNativeFunction,
) => {
function Compartment(endowments = {}, moduleMap = {}, options = {}) {
if (new.target === undefined) {
@@ -288,7 +288,7 @@ export const makeCompartmentConstructor = (
this.constructor.prototype,
{
globalTransforms,
- nativeBrander,
+ markVirtualizedNativeFunction,
},
);

MODIFY
packages/ses/src/global-object.js
packages/ses/src/global-object.js
@@ -17,7 +17,7 @@ import { constantProperties, universalPropertyNames } from './whitelist.js';
* @param {Object} compartmentPrototype
* @param {Object} [options]
* @param {Array<Transform>} [options.globalTransforms]
- * @param {(Object) => void} [options.nativeBrander]
+ * @param {(Object) => void} [options.markVirtualizedNativeFunction]
*/
export const initGlobalObject = (
globalObject,
@@ -25,7 +25,7 @@ export const initGlobalObject = (
newGlobalPropertyNames,
makeCompartmentConstructor,
compartmentPrototype,
- { globalTransforms, nativeBrander },
+ { globalTransforms, markVirtualizedNativeFunction },
) => {
for (const [name, constant] of entries(constantProperties)) {
defineProperty(globalObject, name, {
@@ -71,7 +71,7 @@ export const initGlobalObject = (
perCompartmentGlobals.Compartment = makeCompartmentConstructor(
makeCompartmentConstructor,
intrinsics,
- nativeBrander,
+ markVirtualizedNativeFunction,
);
// TODO These should still be tamed according to the whitelist before
@@ -84,7 +84,7 @@ export const initGlobalObject = (
configurable: true,
});
if (typeof value === 'function') {
- nativeBrander(value);
+ markVirtualizedNativeFunction(value);
}
}
};

MODIFY
packages/ses/src/lockdown-shim.js
packages/ses/src/lockdown-shim.js
@@ -70,7 +70,7 @@ const alreadyHardenedIntrinsics = () => false;
* @callback CompartmentConstructorMaker
* @param {CompartmentConstructorMaker} targetMakeCompartmentConstructor
* @param {Object} intrinsics
- * @param {(func: Function) => void} nativeBrander
+ * @param {(func: Function) => void} markVirtualizedNativeFunction
* @returns {CompartmentConstructor}
*/
@@ -250,7 +250,7 @@ export const repairIntrinsics = (
// Replace Function.prototype.toString with one that recognizes
// shimmed functions as honorary native functions.
- const nativeBrander = tameFunctionToString();
+ const markVirtualizedNativeFunction = tameFunctionToString();
/**
* 2. WHITELIST to standardize the environment.
@@ -259,7 +259,7 @@ export const repairIntrinsics = (
// Remove non-standard properties.
// All remaining function encountered during whitelisting are
// branded as honorary native functions.
- whitelistIntrinsics(intrinsics, nativeBrander);
+ whitelistIntrinsics(intrinsics, markVirtualizedNativeFunction);
// Initialize the powerful initial global, i.e., the global of the
// start compartment, from the intrinsics.
@@ -270,7 +270,7 @@ export const repairIntrinsics = (
makeCompartmentConstructor,
compartmentPrototype,
{
- nativeBrander,
+ markVirtualizedNativeFunction,
},
);

MODIFY
packages/ses/src/tame-function-tostring.js
packages/ses/src/tame-function-tostring.js
-import { defineProperty, apply, freeze } from './commons.js';
+import { defineProperty, apply, freeze, weaksetAdd } from './commons.js';
const nativeSuffix = ') { [native code] }';
@@ -6,22 +6,25 @@ const nativeSuffix = ') { [native code] }';
// patching of `Function.prototype.toString` is also globally stateful. We
// use this top level state so that multiple calls to `tameFunctionToString` are
// idempotent, rather than creating redundant indirections.
-let nativeBrander;
+let markVirtualizedNativeFunction;
/**
* Replace `Function.prototype.toString` with one that recognizes
* shimmed functions as honorary native functions.
*/
export const tameFunctionToString = () => {
- if (nativeBrander === undefined) {
- const nativeBrand = new WeakSet();
+ if (markVirtualizedNativeFunction === undefined) {
+ const virtualizedNativeFunctions = new WeakSet();
const originalFunctionToString = Function.prototype.toString;
const tamingMethods = {
toString() {
const str = apply(originalFunctionToString, this, []);
- if (str.endsWith(nativeSuffix) || !nativeBrand.has(this)) {
+ if (
+ str.endsWith(nativeSuffix) ||
+ !virtualizedNativeFunctions.has(this)
+ ) {
return str;
}
return `function ${this.name}() { [native code] }`;
@@ -32,7 +35,9 @@ export const tameFunctionToString = () => {
value: tamingMethods.toString,
});
- nativeBrander = freeze(func => nativeBrand.add(func));
+ markVirtualizedNativeFunction = freeze(func =>
+ weaksetAdd(virtualizedNativeFunctions, func),
+ );
}
- return nativeBrander;
+ return markVirtualizedNativeFunction;
};

MODIFY
packages/ses/src/whitelist-intrinsics.js
packages/ses/src/whitelist-intrinsics.js
@@ -74,9 +74,12 @@ function asStringPropertyName(path, prop) {
* reflectively walking own property chains.
*
* @param {Object} intrinsics
- * @param {(Object) => void} nativeBrander
+ * @param {(Object) => void} markVirtualizedNativeFunction
*/
-export default function whitelistIntrinsics(intrinsics, nativeBrander) {
+export default function whitelistIntrinsics(
+ intrinsics,
+ markVirtualizedNativeFunction,
+) {
// These primities are allowed allowed for permits.
const primitives = ['undefined', 'boolean', 'number', 'string', 'symbol'];
@@ -197,7 +200,7 @@ export default function whitelistIntrinsics(intrinsics, nativeBrander) {
}
if (typeof obj === 'function') {
- nativeBrander(obj);
+ markVirtualizedNativeFunction(obj);
if (hasOwnProperty(FunctionInstance, permitProp)) {
return FunctionInstance[permitProp];
}

MODIFY
packages/ses/test/test-global-object.js
packages/ses/test/test-global-object.js
@@ -27,7 +27,7 @@ test('globalObject', t => {
makeCompartmentConstructor,
CompartmentPrototype,
{
- nativeBrander(_) {},
+ markVirtualizedNativeFunction(_func) {},
},
);

refactor(ses): Clarify name of markVirtualizedNativeFunction