rename to makeSESRootRealm, enhance tests
MODIFY
src/bundled/createSES.js
src/bundled/createSES.js
@@ -3,7 +3,7 @@ import { removeProperties } from './removeProperties.js';
import { tamePrimordials } from './tame.js';
export function createSESWithRealmConstructor(creatorStrings, Realm) {
- function makeSESRealm() {
+ function makeSESRootRealm() {
const r = Realm.makeRootRealm();
r.global.SES = r.evaluate(creatorStrings).createSESInThisRealm(creatorStrings);
//removeProperties(r.global);
@@ -16,9 +16,10 @@ export function createSESWithRealmConstructor(creatorStrings, Realm) {
return r;
}
const SES = {
- makeSESRealm,
+ makeSESRootRealm,
confine(code, endowments) {
- const r = makeSESRealm();
+ // todo: pass this to our parent's .evaluate() method
+ const r = makeSESRootRealm();
return r.evaluate(code, endowments);
}
};

MODIFY
test/test_create.js
test/test_create.js
@@ -2,13 +2,57 @@ const test = require('tape');
const SES = require('../index.js').SES;
test('create', function(t) {
- const s = SES.makeSESRealm();
+ const s = SES.makeSESRootRealm();
t.equal(1, 1);
t.equal(s.evaluate('1+1'), 2);
t.end();
});
+test('SESRealm does not see primal realm names', function(t) {
+ let hidden = 1;
+ const s = SES.makeSESRootRealm();
+ t.throws(() => s.evaluate('hidden+1'), ReferenceError);
+ t.end();
+});
+
+test('SESRealm also has SES', function(t) {
+ const s = SES.makeSESRootRealm();
+ t.equal(1, 1);
+ t.equal(s.evaluate('1+1'), 2);
+ t.equal(s.evaluate(`const s2 = SES.makeSESRootRealm(); s2.evaluate('1+2')`), 3);
+ t.end();
+});
+
+test('SESRealm is frozen', function(t) {
+ const s = SES.makeSESRootRealm();
+ t.throws(() => s.evaluate('this.a = 10;'), TypeError);
+ t.equal(s.evaluate('this.a'), undefined);
+ t.end();
+});
+
test('confine', function(t) {
t.equal(SES.confine('1+1'), 2);
t.end();
});
+
+test('main use case', function(t) {
+ const s = SES.makeSESRootRealm();
+ function power(a) {
+ return a + 1;
+ }
+ function attenuate(arg) {
+ if (arg <= 0) {
+ throw new TypeError('only positive numbers');
+ }
+ return power(arg);
+ }
+ const attenuated_power = s.evaluate(`(${attenuate})`, { power });
+ function use(arg) {
+ return power(arg);
+ }
+ const user = s.evaluate(`(${use})`, { power: attenuated_power });
+ t.equal(user(1), 2);
+ t.throws(() => user(-1), s.global.TypeError);
+ t.end();
+});
+

rename to makeSESRootRealm, enhance tests