Skip to content

Commit

Permalink
fix(ses): Unravel compartment/lockdown import cycle (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Aug 8, 2020
1 parent e18cd23 commit b931629
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/ses/src/compartment-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getOwnPropertyNames,
keys,
} from './commons.js';
// eslint-disable-next-line import/no-cycle
import { initGlobalObject } from './global-object.js';
import { performEval } from './evaluate.js';
import { load } from './module-load.js';
Expand Down Expand Up @@ -242,6 +241,7 @@ export const makeCompartmentConstructor = (intrinsics, nativeBrander) => {
initGlobalObject(globalObject, intrinsics, sharedGlobalPropertyNames, {
globalTransforms,
nativeBrander,
makeCompartmentConstructor,
});

assign(globalObject, endowments);
Expand Down
12 changes: 8 additions & 4 deletions packages/ses/src/global-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { defineProperty, objectHasOwnProperty, entries } from './commons.js';
import { makeEvalFunction } from './make-eval-function.js';
import { makeFunctionConstructor } from './make-function-constructor.js';
import { constantProperties, universalPropertyNames } from './whitelist.js';
// eslint-disable-next-line import/no-cycle
import { makeCompartmentConstructor } from './compartment-shim.js';

/**
* initGlobalObject()
Expand All @@ -16,7 +14,7 @@ export function initGlobalObject(
globalObject,
intrinsics,
newGlobalPropertyNames,
{ globalTransforms, nativeBrander },
{ globalTransforms, nativeBrander, makeCompartmentConstructor },
) {
for (const [name, constant] of entries(constantProperties)) {
defineProperty(globalObject, name, {
Expand Down Expand Up @@ -57,9 +55,15 @@ export function initGlobalObject(
Function: makeFunctionConstructor(globalObject, {
globalTransforms,
}),
Compartment: makeCompartmentConstructor(intrinsics, nativeBrander),
};

if (makeCompartmentConstructor) {
perCompartmentGlobals.Compartment = makeCompartmentConstructor(
intrinsics,
nativeBrander,
);
}

// TODO These should still be tamed according to the whitelist before
// being made available.
for (const [name, value] of entries(perCompartmentGlobals)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/ses/src/lockdown-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
import { initGlobalObject } from './global-object.js';
import { initialGlobalPropertyNames } from './whitelist.js';
import { tameFunctionToString } from './tame-function-tostring.js';
import { makeCompartmentConstructor } from './compartment-shim.js';

let firstOptions;

Expand Down Expand Up @@ -134,6 +135,7 @@ export function repairIntrinsics(options = {}) {
// start compartment, from the intrinsics.
initGlobalObject(globalThis, intrinsics, initialGlobalPropertyNames, {
nativeBrander,
makeCompartmentConstructor,
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ses/test/global-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('globalObject', t => {
t.notEqual(globalObject, globalThis);
t.equal(globalObject.globalThis, globalObject);

t.equals(Object.getOwnPropertyNames(globalObject).length, 7);
t.equals(Object.getOwnPropertyNames(globalObject).length, 6);

const descs = Object.getOwnPropertyDescriptors(globalObject);
for (const [name, desc] of Object.entries(descs)) {
Expand Down

0 comments on commit b931629

Please sign in to comment.