|
| 1 | +/* global Compartment */ |
| 2 | +import test from 'tape'; |
| 3 | +import '../src/main.js'; |
| 4 | + |
| 5 | +test('endowments own properties are mentionable', t => { |
| 6 | + t.plan(1); |
| 7 | + |
| 8 | + const endowments = { hello: 'World!' }; |
| 9 | + const modules = {}; |
| 10 | + const compartment = new Compartment(endowments, modules); |
| 11 | + |
| 12 | + const whom = compartment.evaluate('hello'); |
| 13 | + t.equal(whom, 'World!'); |
| 14 | +}); |
| 15 | + |
| 16 | +test('endowments own properties are enumerable', t => { |
| 17 | + t.plan(1); |
| 18 | + |
| 19 | + const endowments = { hello: 'World!' }; |
| 20 | + const modules = {}; |
| 21 | + const compartment = new Compartment(endowments, modules); |
| 22 | + |
| 23 | + const keys = compartment.evaluate('Object.keys(globalThis)'); |
| 24 | + t.deepEqual(keys, ['hello']); |
| 25 | +}); |
| 26 | + |
| 27 | +test('endowments prototypically inherited properties are not mentionable', t => { |
| 28 | + t.plan(1); |
| 29 | + |
| 30 | + const endowments = { __proto__: { hello: 'World!' } }; |
| 31 | + const modules = {}; |
| 32 | + const compartment = new Compartment(endowments, modules); |
| 33 | + |
| 34 | + t.throws(() => compartment.evaluate('hello'), /hello is not defined/); |
| 35 | +}); |
| 36 | + |
| 37 | +test('endowments prototypically inherited properties are not enumerable', t => { |
| 38 | + t.plan(1); |
| 39 | + |
| 40 | + const endowments = { __proto__: { hello: 'World!' } }; |
| 41 | + const modules = {}; |
| 42 | + const compartment = new Compartment(endowments, modules); |
| 43 | + |
| 44 | + const keys = compartment.evaluate('Object.keys(globalThis)'); |
| 45 | + t.deepEqual(keys, []); |
| 46 | +}); |
| 47 | + |
| 48 | +test('global lexicals are mentionable', t => { |
| 49 | + t.plan(1); |
| 50 | + |
| 51 | + const endowments = {}; |
| 52 | + const modules = {}; |
| 53 | + const globalLexicals = { hello: 'World!' }; |
| 54 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 55 | + |
| 56 | + const whom = compartment.evaluate('hello'); |
| 57 | + t.equal(whom, 'World!'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('global lexicals are not reachable from global object', t => { |
| 61 | + t.plan(1); |
| 62 | + |
| 63 | + const endowments = {}; |
| 64 | + const modules = {}; |
| 65 | + const globalLexicals = { hello: 'World!' }; |
| 66 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 67 | + |
| 68 | + const keys = compartment.evaluate('Object.keys(globalThis)'); |
| 69 | + t.deepEqual(keys, []); |
| 70 | +}); |
| 71 | + |
| 72 | +test('global lexicals prototypically inherited properties are not mentionable', t => { |
| 73 | + t.plan(1); |
| 74 | + |
| 75 | + const endowments = {}; |
| 76 | + const modules = {}; |
| 77 | + const globalLexicals = { __proto__: { hello: 'World!' } }; |
| 78 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 79 | + |
| 80 | + t.throws(() => compartment.evaluate('hello'), /hello is not defined/); |
| 81 | +}); |
| 82 | + |
| 83 | +test('global lexicals prototypically inherited properties are not enumerable', t => { |
| 84 | + t.plan(1); |
| 85 | + |
| 86 | + const endowments = {}; |
| 87 | + const modules = {}; |
| 88 | + const globalLexicals = { __proto__: { hello: 'World!' } }; |
| 89 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 90 | + |
| 91 | + const keys = compartment.evaluate('Object.keys(globalThis)'); |
| 92 | + t.deepEqual(keys, []); |
| 93 | +}); |
| 94 | + |
| 95 | +test('global lexicals overshadow global object', t => { |
| 96 | + t.plan(1); |
| 97 | + |
| 98 | + const endowments = { hello: 'Your name here' }; |
| 99 | + const modules = {}; |
| 100 | + const globalLexicals = { hello: 'World!' }; |
| 101 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 102 | + |
| 103 | + const whom = compartment.evaluate('hello'); |
| 104 | + t.equal(whom, 'World!'); |
| 105 | +}); |
| 106 | + |
| 107 | +test('global lexicals are constant', t => { |
| 108 | + t.plan(1); |
| 109 | + |
| 110 | + const endowments = {}; |
| 111 | + const modules = {}; |
| 112 | + const globalLexicals = { hello: 'World!' }; |
| 113 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 114 | + |
| 115 | + t.throws( |
| 116 | + () => compartment.evaluate('hello = "Dave."'), |
| 117 | + /Assignment to constant/, |
| 118 | + ); |
| 119 | +}); |
| 120 | + |
| 121 | +test('global lexicals are captured on construction', t => { |
| 122 | + t.plan(1); |
| 123 | + |
| 124 | + const endowments = {}; |
| 125 | + const modules = {}; |
| 126 | + const globalLexicals = { hello: 'World!' }; |
| 127 | + const compartment = new Compartment(endowments, modules, { globalLexicals }); |
| 128 | + |
| 129 | + // Psych! |
| 130 | + globalLexicals.hello = 'Something else'; |
| 131 | + |
| 132 | + const whom = compartment.evaluate('hello'); |
| 133 | + t.equal(whom, 'World!'); |
| 134 | +}); |
0 commit comments