Skip to content

Commit

Permalink
fix(ses)!: Remove evaluate endowments option (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jul 13, 2020
1 parent 591d2c6 commit e7b7b6e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 108 deletions.
8 changes: 2 additions & 6 deletions packages/ses-integration-test/test/sanity.test.js
Expand Up @@ -5,12 +5,8 @@ import "ses";

test("sanity", t => {
t.equal(lockdown(), true, "lockdown runs successfully");
const c = new Compartment();
const c = new Compartment({ abc: 456 });
t.equal(c.evaluate("123"), 123, "simple evaluate succeeds");
t.equal(
c.evaluate("abc", { endowments: { abc: 456 } }),
456,
"endowment succeeds"
);
t.equal(c.evaluate("abc"), 456, "endowment succeeds");
t.end();
});
5 changes: 5 additions & 0 deletions packages/ses/NEWS.md
Expand Up @@ -2,6 +2,11 @@ User-visible changes in SES:

## Next release

* BREAKING CHANGE: The compartment `evaluate` method no longer accepts an
`endowments` option.
Use `compartment.globalThis`, `endowments`, or `globalLexicals`.
If per-evaluation `globalLexicals` or `endowments` are necessary,
each evaluation will need a fresh `Compartment`.
* The Compartment constructor now accepts a `globalLexicals` option.
The own enumerable properties of the global lexicals are captured
and presented as constants in the scope of all calls to `evaluate` and all
Expand Down
25 changes: 3 additions & 22 deletions packages/ses/src/compartment-shim.js
Expand Up @@ -13,15 +13,7 @@ import * as babel from '@agoric/babel-standalone';
// Both produce:
// Error: 'default' is not exported by .../@agoric/babel-standalone/babel.js
import { makeModuleAnalyzer } from '@agoric/transform-module';
import {
assign,
create,
defineProperties,
entries,
getOwnPropertyNames,
getOwnPropertyDescriptors,
freeze,
} from './commons.js';
import { assign, entries, getOwnPropertyNames, freeze } from './commons.js';
import { createGlobalObject } from './global-object.js';
import { performEval } from './evaluate.js';
import { getCurrentRealmRec } from './realm-rec.js';
Expand Down Expand Up @@ -185,7 +177,6 @@ export class Compartment {
/**
* @param {string} source is a JavaScript program grammar construction.
* @param {{
* endowments: Object<name:string, endowment:any>,
* transforms: Array<Transform>,
* sloppyGlobalsMode: bool,
* }} options.
Expand All @@ -197,11 +188,7 @@ export class Compartment {
}

// Extract options, and shallow-clone transforms.
const {
endowments = {},
transforms = [],
sloppyGlobalsMode = false,
} = options;
const { transforms = [], sloppyGlobalsMode = false } = options;
const localTransforms = [...transforms];

const {
Expand All @@ -211,13 +198,7 @@ export class Compartment {
} = privateFields.get(this);
const realmRec = getCurrentRealmRec();

// TODO just pass globalLexicals as localObject
// https://github.com/Agoric/SES-shim/issues/365
const localObject = create(null);
defineProperties(localObject, getOwnPropertyDescriptors(globalLexicals));
defineProperties(localObject, getOwnPropertyDescriptors(endowments));

return performEval(realmRec, source, globalObject, localObject, {
return performEval(realmRec, source, globalObject, globalLexicals, {
globalTransforms,
localTransforms,
sloppyGlobalsMode,
Expand Down
14 changes: 5 additions & 9 deletions packages/ses/test/compartment.test.js
Expand Up @@ -48,8 +48,8 @@ test('SES compartment also has compartments', t => {
// });

test('SES compartment has harden', t => {
const c = new Compartment();
const obj = c.evaluate(`harden({a})`, { endowments: { a: 123 } });
const c = new Compartment({ a: 123 });
const obj = c.evaluate(`harden({a})`);
t.equal(obj.a, 123, `expected object`);
t.throws(() => (obj.a = 'ignored'));
t.equal(obj.a, 123, `hardened object retains value`);
Expand Down Expand Up @@ -88,7 +88,6 @@ test('SES compartment has harden', t => {
// });

test('main use case', t => {
const c = new Compartment();
function power(a) {
return a + 1;
}
Expand All @@ -98,15 +97,12 @@ test('main use case', t => {
}
return power(arg);
}
const attenuatedPower = c.evaluate(`(${attenuate})`, {
endowments: { power },
});
const attenuatedPower = new Compartment({ power }).evaluate(`(${attenuate})`);
function use(arg) {
return power(arg);
}
const user = c.evaluate(`(${use})`, {
endowments: { power: attenuatedPower },
});
const c = new Compartment({ power: attenuatedPower });
const user = c.evaluate(`(${use})`);
t.equal(user(1), 2);
t.throws(() => user(-1), c.globalThis.TypeError);
t.end();
Expand Down
62 changes: 0 additions & 62 deletions packages/ses/test/leak-endowments.test.js

This file was deleted.

14 changes: 5 additions & 9 deletions packages/ses/test/ses.test.js
Expand Up @@ -120,8 +120,8 @@ test('SES compartment also has compartments', t => {
// });

test('SES compartment has harden', t => {
const c = new Compartment();
const obj = c.evaluate(`harden({a})`, { endowments: { a: 123 } });
const c = new Compartment({ a: 123 });
const obj = c.evaluate(`harden({a})`);
t.equal(obj.a, 123, `expected object`);
t.throws(() => (obj.a = 'ignored'));
t.equal(obj.a, 123, `hardened object retains value`);
Expand Down Expand Up @@ -160,7 +160,6 @@ test('SES compartment has harden', t => {
// });

test('main use case', t => {
const c = new Compartment();
function power(a) {
return a + 1;
}
Expand All @@ -170,15 +169,12 @@ test('main use case', t => {
}
return power(arg);
}
const attenuatedPower = c.evaluate(`(${attenuate})`, {
endowments: { power },
});
const attenuatedPower = new Compartment({ power }).evaluate(`(${attenuate})`);
function use(arg) {
return power(arg);
}
const user = c.evaluate(`(${use})`, {
endowments: { power: attenuatedPower },
});
const c = new Compartment({ power: attenuatedPower });
const user = c.evaluate(`(${use})`);
t.equal(user(1), 2);
t.throws(() => user(-1), c.globalThis.TypeError);
t.end();
Expand Down

0 comments on commit e7b7b6e

Please sign in to comment.