Skip to content

Commit

Permalink
refactor(ses): Const export all functions
Browse files Browse the repository at this point in the history
This change anticipates using SES's own module transform to build a
bootstrap replacement for Rollup.
  • Loading branch information
kriskowal committed May 10, 2021
1 parent 103e6c5 commit 1007ecc
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 54 deletions.
6 changes: 3 additions & 3 deletions packages/ses/src/error/tame-v8-error-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ export const shortenCallSiteString = callSiteString => {
return callSiteString;
};

export function tameV8ErrorConstructor(
export const tameV8ErrorConstructor = (
OriginalError,
InitialError,
errorTaming,
stackFiltering,
) {
) => {
// const callSiteFilter = _callSite => true;
const callSiteFilter = callSite => {
if (stackFiltering === 'verbose') {
Expand Down Expand Up @@ -272,4 +272,4 @@ export function tameV8ErrorConstructor(
});

return tamedMethods.getStackString;
}
};
6 changes: 3 additions & 3 deletions packages/ses/src/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { details: d } = assert;
* @param {bool} [options.sloppyGlobalsMode]
* @param {WeakSet} [options.knownScopeProxies]
*/
export function performEval(
export const performEval = (
source,
globalObject,
localObject = {},
Expand All @@ -34,7 +34,7 @@ export function performEval(
sloppyGlobalsMode = false,
knownScopeProxies = new WeakSet(),
} = {},
) {
) => {
// Execute the mandatory transforms last to ensure that any rewritten code
// meets those mandatory requirements.
source = applyTransforms(source, [
Expand Down Expand Up @@ -79,4 +79,4 @@ export function performEval(
assert.fail(d`handler did not revoke useUnsafeEvaluator ${err}`);
}
}
}
};
15 changes: 11 additions & 4 deletions packages/ses/src/get-anonymous-intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@ function getConstructorOf(obj) {
return getPrototypeOf(obj).constructor;
}

// getAnonymousIntrinsics uses a utility function to construct an arguments
// object, since it cannot have one of its own and also be a const export.
function makeArguments() {
// eslint-disable-next-line prefer-rest-params
return arguments;
}

/**
* getAnonymousIntrinsics()
* Get the intrinsics not otherwise reachable by named own property
* traversal from the global object.
*
* @returns {Object}
*/
export function getAnonymousIntrinsics() {
export const getAnonymousIntrinsics = () => {
const InertFunction = Function.prototype.constructor;

const SymbolIterator = (typeof Symbol && Symbol.iterator) || '@@iterator';
const SymbolMatchAll = (typeof Symbol && Symbol.matchAll) || '@@matchAll';

// 9.2.4.1 %ThrowTypeError%

// eslint-disable-next-line prefer-rest-params
const ThrowTypeError = getOwnPropertyDescriptor(arguments, 'callee').get;
const ThrowTypeError = getOwnPropertyDescriptor(makeArguments(), 'callee')
.get;

// 21.1.5.2 The %StringIteratorPrototype% Object

Expand Down Expand Up @@ -116,4 +123,4 @@ export function getAnonymousIntrinsics() {
};

return intrinsics;
}
};
4 changes: 2 additions & 2 deletions packages/ses/src/get-source-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const sourceMetaEntriesRegExp = new RegExp(
`(?:\\s*//${sourceMetaEntryRegExp}|/\\*${sourceMetaEntryRegExp}\\s*\\*/)\\s*$`,
);

export function getSourceURL(src) {
export const getSourceURL = src => {
let sourceURL = '<unknown>';

// Our regular expression matches the last one or two comments with key value
Expand Down Expand Up @@ -42,4 +42,4 @@ export function getSourceURL(src) {
}

return sourceURL;
}
};
6 changes: 3 additions & 3 deletions packages/ses/src/global-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { constantProperties, universalPropertyNames } from './whitelist.js';
* @param {Array<Transform>} [options.globalTransforms]
* @param {(Object) => void} [options.nativeBrander]
*/
export function initGlobalObject(
export const initGlobalObject = (
globalObject,
intrinsics,
newGlobalPropertyNames,
makeCompartmentConstructor,
compartmentPrototype,
{ globalTransforms, nativeBrander },
) {
) => {
for (const [name, constant] of entries(constantProperties)) {
defineProperty(globalObject, name, {
value: constant,
Expand Down Expand Up @@ -87,4 +87,4 @@ export function initGlobalObject(
nativeBrander(value);
}
}
}
};
8 changes: 4 additions & 4 deletions packages/ses/src/intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function sampleGlobals(globalObject, newPropertyNames) {
return newIntrinsics;
}

export function makeIntrinsicsCollector() {
export const makeIntrinsicsCollector = () => {
const intrinsics = { __proto__: null };
let pseudoNatives;

Expand Down Expand Up @@ -130,7 +130,7 @@ export function makeIntrinsicsCollector() {
);

return intrinsicsCollector;
}
};

/**
* getGlobalIntrinsics()
Expand All @@ -145,12 +145,12 @@ export function makeIntrinsicsCollector() {
*
* @param {Object} globalObject
*/
export function getGlobalIntrinsics(globalObject) {
export const getGlobalIntrinsics = globalObject => {
const intrinsicsCollector = makeIntrinsicsCollector();

intrinsicsCollector.addIntrinsics(
sampleGlobals(globalObject, sharedGlobalPropertyNames),
);

return intrinsicsCollector.finalIntrinsics();
}
};
6 changes: 3 additions & 3 deletions packages/ses/src/lockdown-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ const alreadyHardenedIntrinsics = () => false;
* @param {LockdownOptions} [options]
* @returns {() => {}} repairIntrinsics
*/
export function repairIntrinsics(
export const repairIntrinsics = (
makeCompartmentConstructor,
compartmentPrototype,
getAnonymousIntrinsics,
options = {},
) {
) => {
// First time, absent options default to 'safe'.
// Subsequent times, absent options default to first options.
// Thus, all present options must agree with first options.
Expand Down Expand Up @@ -314,7 +314,7 @@ export function repairIntrinsics(
}

return hardenIntrinsics;
}
};

/**
* @param {CompartmentConstructorMaker} makeCompartmentConstructor
Expand Down
4 changes: 2 additions & 2 deletions packages/ses/src/make-evaluate-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function buildOptimizer(constants) {
*
* @param {Array<string>} [constants]
*/
export function makeEvaluateFactory(constants = []) {
export const makeEvaluateFactory = (constants = []) => {
const optimizer = buildOptimizer(constants);

// Create a function in sloppy mode, so that we can use 'with'. It returns
Expand Down Expand Up @@ -64,4 +64,4 @@ export function makeEvaluateFactory(constants = []) {
};
}
`);
}
};
4 changes: 2 additions & 2 deletions packages/ses/src/make-function-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const FERAL_FUNCTION = Function;
* A safe version of the native Function which relies on
* the safety of performEval for confinement.
*/
export function makeFunctionConstructor(globaObject, options = {}) {
export const makeFunctionConstructor = (globaObject, options = {}) => {
// Define an unused parameter to ensure Function.length === 1
const newFunction = function Function(_body) {
// Sanitize all parameters at the entry point.
Expand Down Expand Up @@ -82,4 +82,4 @@ export function makeFunctionConstructor(globaObject, options = {}) {
);

return newFunction;
}
};
6 changes: 3 additions & 3 deletions packages/ses/src/module-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { create, entries, keys, freeze, defineProperty } from './commons.js';
// q, for enquoting strings in error messages.
const q = JSON.stringify;

export function makeThirdPartyModuleInstance(
export const makeThirdPartyModuleInstance = (
compartmentPrivateFields,
staticModuleRecord,
compartment,
moduleAliases,
moduleSpecifier,
resolvedImports,
) {
) => {
const { exportsProxy, proxiedExports, activate } = getDeferredExports(
compartment,
compartmentPrivateFields.get(compartment),
Expand Down Expand Up @@ -73,7 +73,7 @@ export function makeThirdPartyModuleInstance(
}
},
});
}
};

// `makeModuleInstance` takes a module's compartment record, the live import
// namespace, and a global object; and produces a module instance.
Expand Down
8 changes: 4 additions & 4 deletions packages/ses/src/scope-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ const identifierPattern = new RegExp('^[a-zA-Z_$][\\w$]*$');
*
* @param {string} name
*/
export function isValidIdentifierName(name) {
export const isValidIdentifierName = name => {
// Ensure we have a valid identifier. We use regexpTest rather than
// /../.test() to guard against the case where RegExp has been poisoned.
return (
name !== 'eval' &&
!arrayIncludes(keywords, name) &&
regexpTest(identifierPattern, name)
);
}
};

/*
* isImmutableDataProperty
Expand Down Expand Up @@ -145,7 +145,7 @@ function isImmutableDataProperty(obj, name) {
* @param {Object} globalObject
* @param {Object} localObject
*/
export function getScopeConstants(globalObject, localObject = {}) {
export const getScopeConstants = (globalObject, localObject = {}) => {
// getOwnPropertyNames() does ignore Symbols so we don't need to
// filter them out.
const globalNames = getOwnPropertyNames(globalObject);
Expand All @@ -169,4 +169,4 @@ export function getScopeConstants(globalObject, localObject = {}) {
);

return [...globalConstants, ...localConstants];
}
};
6 changes: 3 additions & 3 deletions packages/ses/src/scope-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const alwaysThrowHandler = new Proxy(immutableObject, {
* - hide the unsafeGlobal which lives on the scope chain above the 'with'.
* - ensure the Proxy invariants despite some global properties being frozen.
*/
export function createScopeHandler(
export const createScopeHandler = (
globalObject,
localObject = {},
{ sloppyGlobalsMode = false } = {},
) {
) => {
return {
// The scope handler throws if any trap other than get/set/has are run
// (e.g. getOwnPropertyDescriptors, apply, getPrototypeOf).
Expand Down Expand Up @@ -168,4 +168,4 @@ export function createScopeHandler(
return undefined;
},
};
}
};
4 changes: 2 additions & 2 deletions packages/ses/src/tame-function-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let nativeBrander;
* Replace `Function.prototype.toString` with one that recognizes
* shimmed functions as honorary native functions.
*/
export function tameFunctionToString() {
export const tameFunctionToString = () => {
if (nativeBrander === undefined) {
const nativeBrand = new WeakSet();

Expand All @@ -35,4 +35,4 @@ export function tameFunctionToString() {
nativeBrander = freeze(func => nativeBrand.add(func));
}
return nativeBrander;
}
};
Loading

0 comments on commit 1007ecc

Please sign in to comment.