Add commons.js
ADD
shim/src/commons.js
+// Declare shorthand functions. Sharing these declarations accross modules
+// improves both consitency and minification. Unused declarations are dropped
+// by the tree shaking process.
+
+const {
+ getPrototypeOf,
+ setPrototypeOf,
+ defineProperty,
+ defineProperties,
+ deleteProperty,
+ ownKeys
+} = Reflect;
+
+const {
+ getOwnPropertyDescriptor,
+ getOwnPropertyDescriptors,
+ getOwnPropertyNames,
+ create,
+ assign,
+ freeze
+} = Object;
+
+export {
+ getPrototypeOf,
+ setPrototypeOf,
+ defineProperty,
+ defineProperties,
+ deleteProperty,
+ ownKeys,
+ getOwnPropertyDescriptor,
+ getOwnPropertyDescriptors,
+ getOwnPropertyNames,
+ create,
+ assign,
+ freeze
+};

MODIFY
shim/src/evaluators.js
shim/src/evaluators.js
import { evaluate } from "./evaluate";
+import { setPrototypeOf } from "./commons";
function getEvalEvaluator(sandbox) {
const o = {
@@ -8,7 +9,7 @@ function getEvalEvaluator(sandbox) {
return evaluate(sourceText, sandbox);
}
};
- Object.setPrototypeOf(o.eval, sandbox.Function);
+ setPrototypeOf(o.eval, sandbox.Function);
o.eval.toString = () => 'function eval() { [shim code] }';
return o.eval;
}
@@ -22,7 +23,7 @@ function getFunctionEvaluator(sandbox) {
return evaluate(`(function anonymous(${fnArgs}){\n${sourceText}\n}).bind(this)`, sandbox);
}
f.prototype = confinedWindow.Function.prototype;
- Object.setPrototypeOf(f, f.prototype);
+ setPrototypeOf(f, f.prototype);
f.prototype.constructor = f;
f.toString = () => 'function Function() { [shim code] }';
return f;

MODIFY
shim/src/intrinsics.js
shim/src/intrinsics.js
import Realm from "./realm";
+import { getPrototypeOf } from "./commons";
-const getProto = Object.getPrototypeOf;
const iteratorSymbol = (typeof Symbol && Symbol.iterator) || "@@iterator";
export function getIntrinsics(sandbox) {
const { confinedWindow: _ } = sandbox;
- const anonymousArrayIteratorPrototype = getProto(_.Array(0)[iteratorSymbol]());
- const anonymousStringIteratorPrototype = getProto(_.String()[iteratorSymbol]());
- const anonymousIteratorPrototype = getProto(anonymousArrayIteratorPrototype);
+ const anonymousArrayIteratorPrototype = getPrototypeOf(_.Array(0)[iteratorSymbol]());
+ const anonymousStringIteratorPrototype = getPrototypeOf(_.String()[iteratorSymbol]());
+ const anonymousIteratorPrototype = getPrototypeOf(anonymousArrayIteratorPrototype);
const strictArgumentsGenerator = _.eval('(function*(){"use strict";yield arguments;})');
- const anonymousGenerator = getProto(strictArgumentsGenerator);
- const anonymousGeneratorPrototype = getProto(anonymousGenerator);
+ const anonymousGenerator = getPrototypeOf(strictArgumentsGenerator);
+ const anonymousGeneratorPrototype = getPrototypeOf(anonymousGenerator);
const anonymousGeneratorFunction = anonymousGeneratorPrototype.constructor;
return {

MODIFY
shim/src/proxy.js
shim/src/proxy.js
+import { defineProperty, deleteProperty, getOwnPropertyDescriptor, getOwnPropertyNames } from './commons';
+
// this flag allow us to determine if the eval() call is a controlled eval done by the realm's code
// or if it is user-land invocation, so we can react differently.
let isInternalEvaluation = false;
@@ -23,11 +25,11 @@ export const proxyHandler = {
return true;
},
defineProperty(sandbox, propName, descriptor) {
- Object.defineProperty(sandbox.globalObject, propName, descriptor);
+ defineProperty(sandbox.globalObject, propName, descriptor);
return true;
},
deleteProperty(sandbox, propName) {
- return Reflect.deleteProperty(sandbox.globalObject, propName);
+ return deleteProperty(sandbox.globalObject, propName);
},
has(sandbox, propName) {
if (propName === 'eval' && isInternalEvaluation) {
@@ -41,10 +43,10 @@ export const proxyHandler = {
return false;
},
ownKeys(sandbox) {
- return Object.getOwnPropertyNames(sandbox.globalObject);
+ return getOwnPropertyNames(sandbox.globalObject);
},
getOwnPropertyDescriptor(sandbox, propName) {
- return Object.getOwnPropertyDescriptor(sandbox.globalObject, propName);
+ return getOwnPropertyDescriptor(sandbox.globalObject, propName);
},
isExtensible(sandbox) {
// TODO: can it becomes non-extensible?

MODIFY
shim/src/realm.js
shim/src/realm.js
@@ -3,6 +3,7 @@ import { evaluate } from "./evaluate";
import { getStdLib } from "./stdlib";
import { getIntrinsics } from "./intrinsics";
import { assert, IsCallable } from "./utils";
+import { defineProperties, create } from "./commons";
const RealmRecord = Symbol('Realm Slot');
const Intrinsics = Symbol('Intrinsics Slot');
@@ -50,7 +51,7 @@ function NewGlobalEnvironment(G, thisValue) {
function SetRealmGlobalObject(realmRec, globalObj, thisValue) {
if (globalObj === undefined) {
const intrinsics = realmRec[Intrinsics];
- globalObj = Object.create(intrinsics['ObjectPrototype']);
+ globalObj = create(intrinsics['ObjectPrototype']);
}
assert(typeof globalObj === 'object');
if (thisValue === undefined) thisValue = globalObj;
@@ -66,7 +67,7 @@ function SetDefaultGlobalBindings(realmRec) {
// For each property of the Global Object specified in clause <emu-xref href="#sec-global-object"></emu-xref>, do
// ---> diverging:
const GlobalObjectDescriptors = getStdLib(realmRec[ShimSandbox]);
- Object.defineProperties(global, GlobalObjectDescriptors);
+ defineProperties(global, GlobalObjectDescriptors);
return global;
}

MODIFY
shim/src/sandbox.js
shim/src/sandbox.js
import { sanitize } from "./sanitize";
import { getEvaluators } from "./evaluators";
import { proxyHandler } from "./proxy";
+import { assign } from "./commons";
function createIframe() {
const el = document.createElement("iframe");
@@ -24,7 +25,7 @@ export function createSandbox() {
globalProxy: undefined,
};
sanitize(sandbox);
- Object.assign(sandbox, getEvaluators(sandbox));
+ assign(sandbox, getEvaluators(sandbox));
sandbox.globalProxy = new Proxy(sandbox, proxyHandler);
return sandbox;
}

Add commons.js