ses 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,318 @@
1
+ /** @import {ModuleDescriptor} from '../types.js' */
2
+
3
+ import {
4
+ Map,
5
+ Object,
6
+ SyntaxError,
7
+ TypeError,
8
+ WeakMap,
9
+ arrayMap,
10
+ create,
11
+ defineProperty,
12
+ entries,
13
+ fromEntries,
14
+ getOwnPropertyDescriptor,
15
+ globalThis,
16
+ mapDelete,
17
+ mapGet,
18
+ mapSet,
19
+ uncurryThis,
20
+ weakmapGet,
21
+ weakmapSet,
22
+ } from '../src/commons.js';
23
+ import { nativeGetGlobalThis } from './commons.js';
24
+ import {
25
+ makeCompartmentConstructor,
26
+ compartmentOptions,
27
+ } from '../src/compartment.js';
28
+ import { getGlobalIntrinsics } from '../src/intrinsics.js';
29
+ import { tameFunctionToString } from '../src/tame-function-tostring.js';
30
+ import { chooseReporter } from '../src/reporting.js';
31
+ import { makeError } from '../src/error/assert.js';
32
+
33
+ /**
34
+ * @import {CompartmentOptionsArgs, LegacyCompartmentOptionsArgs} from '../src/compartment.js'
35
+ */
36
+
37
+ const muteReporter = chooseReporter('none');
38
+
39
+ export const ShimStartCompartment = makeCompartmentConstructor(
40
+ makeCompartmentConstructor,
41
+ getGlobalIntrinsics(globalThis, muteReporter),
42
+ tameFunctionToString(),
43
+ );
44
+
45
+ export const shimCompartmentPrototype = ShimStartCompartment.prototype;
46
+
47
+ export const shimEvaluate = uncurryThis(shimCompartmentPrototype.evaluate);
48
+ export const shimImport = uncurryThis(shimCompartmentPrototype.import);
49
+ export const shimImportNow = uncurryThis(shimCompartmentPrototype.importNow);
50
+
51
+ /** @type {(compartment: typeof Compartment) => typeof globalThis} */
52
+ export const shimGetGlobalThis = uncurryThis(
53
+ // @ts-expect-error The descriptor will never be undefined.
54
+ getOwnPropertyDescriptor(ShimStartCompartment.prototype, 'globalThis').get,
55
+ );
56
+
57
+ /**
58
+ * @typedef {{
59
+ * name: string,
60
+ * transforms: Array<(source: string) => string>,
61
+ * delegateNative: boolean,
62
+ * noNamespaceBox: boolean,
63
+ * nativeEval: typeof eval,
64
+ * descriptors: Map<string, ModuleDescriptor>,
65
+ * }} PrivateFields
66
+ */
67
+
68
+ /** @type {WeakMap<object, PrivateFields>} */
69
+ export const privateFields = new WeakMap();
70
+
71
+ const adaptVirtualModuleSource = ({
72
+ execute,
73
+ imports = [],
74
+ exports = [],
75
+ reexports = [],
76
+ }) => {
77
+ const resolutions = create(null);
78
+ let i = 0;
79
+ return {
80
+ execute(environment) {
81
+ const fakeCompartment = {
82
+ importNow(specifier) {
83
+ return environment[specifier];
84
+ },
85
+ };
86
+ execute(environment, fakeCompartment, resolutions);
87
+ },
88
+ bindings: [
89
+ ...arrayMap(imports, specifier => {
90
+ const resolved = `_${i}`;
91
+ i += 1;
92
+ resolutions[specifier] = resolved;
93
+ return { importAllFrom: specifier, as: resolved };
94
+ }),
95
+ ...arrayMap(reexports, specifier => ({ exportAllFrom: specifier })),
96
+ ...arrayMap(exports, name => ({ export: name })),
97
+ ],
98
+ };
99
+ };
100
+
101
+ const adaptModuleSource = source => {
102
+ if (source.execute) {
103
+ return adaptVirtualModuleSource(source);
104
+ }
105
+ // eslint-disable-next-line no-underscore-dangle
106
+ if (source.__syncModuleProgram__) {
107
+ throw makeError(
108
+ 'XS native compartments do not support precompiled module sources',
109
+ SyntaxError,
110
+ );
111
+ }
112
+ return source;
113
+ };
114
+
115
+ const adaptModuleDescriptor = (
116
+ descriptor,
117
+ specifier,
118
+ compartment = undefined,
119
+ ) => {
120
+ if (Object(descriptor) !== descriptor) {
121
+ throw makeError('Module descriptor must be an object', TypeError);
122
+ }
123
+ if (descriptor.namespace !== undefined) {
124
+ return descriptor;
125
+ }
126
+ if (descriptor.source !== undefined) {
127
+ return {
128
+ source: adaptModuleSource(descriptor.source),
129
+ importMeta: descriptor.importMeta,
130
+ specifier: descriptor.specifier,
131
+ };
132
+ }
133
+ // Legacy support for record descriptors.
134
+ if (descriptor.record !== undefined) {
135
+ if (
136
+ descriptor.specifier === specifier ||
137
+ descriptor.specifier === undefined
138
+ ) {
139
+ return {
140
+ source: adaptModuleSource(descriptor.record),
141
+ specifier,
142
+ importMeta: descriptor.importMeta,
143
+ };
144
+ } else {
145
+ if (compartment === undefined) {
146
+ throw makeError(
147
+ 'Cannot construct forward reference module descriptor in module map',
148
+ TypeError,
149
+ );
150
+ }
151
+ const compartmentPrivateFields = weakmapGet(privateFields, compartment);
152
+ if (compartmentPrivateFields === undefined) {
153
+ throw makeError(
154
+ 'Module descriptor compartment is not a recognizable compartment',
155
+ TypeError,
156
+ );
157
+ }
158
+ const { descriptors } = compartmentPrivateFields;
159
+ mapSet(descriptors, descriptor.specifier, {
160
+ compartment,
161
+ namespace: specifier,
162
+ });
163
+ return {
164
+ source: adaptModuleSource(descriptor.record),
165
+ specifier: descriptor.specifier,
166
+ importMeta: descriptor.importMeta,
167
+ };
168
+ }
169
+ }
170
+ if (descriptor.specifier !== undefined) {
171
+ return {
172
+ namespace: descriptor.specifier,
173
+ compartment: descriptor.compartment,
174
+ };
175
+ }
176
+ // Legacy support for a source in the place of a descriptor.
177
+ return { source: adaptModuleSource(descriptor) };
178
+ };
179
+
180
+ export const adaptCompartmentConstructors = (
181
+ NativeCompartment,
182
+ ShimCompartment,
183
+ maybeHarden,
184
+ ) => {
185
+ /** @param {CompartmentOptionsArgs|LegacyCompartmentOptionsArgs} args */
186
+ function Compartment(...args) {
187
+ const options = compartmentOptions(...args);
188
+
189
+ const {
190
+ name = undefined,
191
+ globals = {},
192
+ transforms = [],
193
+ resolveHook = () => {
194
+ throw makeError('Compartment requires a resolveHook', TypeError);
195
+ },
196
+ loadHook = undefined,
197
+ loadNowHook = undefined,
198
+ importHook = loadHook,
199
+ importNowHook = loadNowHook,
200
+ moduleMapHook = () => {},
201
+ __native__: delegateNative = false,
202
+ __noNamespaceBox__: noNamespaceBox = false,
203
+ } = options;
204
+
205
+ const modules = delegateNative
206
+ ? fromEntries(
207
+ arrayMap(
208
+ entries(options.modules ?? {}),
209
+ // Uses desctructuring to avoid invoking iterator protocol and deny
210
+ // vetted shims the opportunity to interfere.
211
+ ({ 0: specifier, 1: descriptor }) => [
212
+ specifier,
213
+ adaptModuleDescriptor(descriptor, specifier, undefined),
214
+ ],
215
+ ),
216
+ )
217
+ : {};
218
+
219
+ // side table for references from one descriptor to another
220
+ const descriptors = new Map();
221
+
222
+ let nativeOptions = { globals, modules };
223
+
224
+ if (importHook) {
225
+ /** @param {string} specifier */
226
+ const nativeImportHook = async specifier => {
227
+ await null;
228
+ let descriptor =
229
+ mapGet(descriptors, specifier) ??
230
+ moduleMapHook(specifier) ??
231
+ (await importHook(specifier));
232
+ mapDelete(descriptors, specifier);
233
+ // eslint-disable-next-line no-use-before-define
234
+ descriptor = adaptModuleDescriptor(descriptor, specifier, compartment);
235
+ return descriptor;
236
+ };
237
+ nativeOptions = {
238
+ ...nativeOptions,
239
+ resolveHook,
240
+ importHook: nativeImportHook,
241
+ loadHook: nativeImportHook,
242
+ };
243
+ }
244
+
245
+ if (importNowHook) {
246
+ /** @param {string} specifier */
247
+ const nativeImportNowHook = specifier => {
248
+ let descriptor =
249
+ mapGet(descriptors, specifier) ??
250
+ moduleMapHook(specifier) ??
251
+ importNowHook(specifier);
252
+ mapDelete(descriptors, specifier);
253
+ // eslint-disable-next-line no-use-before-define
254
+ descriptor = adaptModuleDescriptor(descriptor, specifier, compartment);
255
+ return descriptor;
256
+ };
257
+ nativeOptions = {
258
+ ...nativeOptions,
259
+ resolveHook,
260
+ importNowHook: nativeImportNowHook,
261
+ loadNowHook: nativeImportNowHook,
262
+ };
263
+ }
264
+
265
+ const compartment = new NativeCompartment(nativeOptions);
266
+
267
+ const nativeGlobalThis = nativeGetGlobalThis(compartment);
268
+
269
+ const nativeEval = nativeGlobalThis.eval;
270
+
271
+ weakmapSet(privateFields, compartment, {
272
+ name,
273
+ transforms,
274
+ delegateNative,
275
+ noNamespaceBox,
276
+ nativeEval,
277
+ descriptors,
278
+ });
279
+
280
+ const shimOptions = {
281
+ ...options,
282
+ __noNamespaceBox__: true,
283
+ __options__: true,
284
+ };
285
+
286
+ uncurryThis(ShimCompartment)(compartment, shimOptions);
287
+
288
+ const shimGlobalThis = shimGetGlobalThis(compartment);
289
+
290
+ const ChildCompartment = adaptCompartmentConstructors(
291
+ // @ts-expect-error incomplete type information for XS
292
+ nativeGlobalThis.Compartment,
293
+ // @ts-expect-error incomplete type information for XS
294
+ shimGlobalThis.Compartment,
295
+ maybeHarden,
296
+ );
297
+
298
+ defineProperty(nativeGlobalThis, 'Compartment', {
299
+ value: ChildCompartment,
300
+ writable: true,
301
+ configurable: true,
302
+ enumerable: false,
303
+ });
304
+
305
+ defineProperty(shimGlobalThis, 'Compartment', {
306
+ value: ChildCompartment,
307
+ writable: true,
308
+ configurable: true,
309
+ enumerable: false,
310
+ });
311
+
312
+ maybeHarden(compartment);
313
+ return compartment;
314
+ }
315
+
316
+ maybeHarden(Compartment);
317
+ return Compartment;
318
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @module This is an alternate implementation of ../index.js to provide
3
+ * access to the native implementation of Hardened JavaScript on the XS
4
+ * engine, but adapted for backward compatibility with SES.
5
+ * This module can only be reached in the presence of the package export/import
6
+ * condition "xs", and should only be used to bundle an XS-specific version of
7
+ * SES.
8
+ */
9
+ // @ts-nocheck
10
+ /// <refs types="../types.js"/>
11
+
12
+ import { Object, freeze } from '../src/commons.js';
13
+
14
+ // These are the constituent shims in an arbitrary order, but matched
15
+ // to ../index.js to remove doubt.
16
+ import './lockdown-shim.js';
17
+ import './compartment-shim.js';
18
+ import '../src/assert-shim.js';
19
+ import '../src/console-shim.js';
20
+
21
+ // XS Object.freeze takes a second argument to apply freeze transitively, but
22
+ // with slightly different effects than `harden`.
23
+ // We disable this behavior to encourage use of `harden` for portable Hardened
24
+ // JavaScript.
25
+ // The pattern of creating and extracting preserves Object.freeze.name.
26
+ /** @param {object} object */
27
+ Object.freeze = { freeze: object => freeze(object) }.freeze;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @module Alters the XS implementation of Lockdown to be backward compatible
3
+ * with SES, providing Compartment constructors in every Compartment that can
4
+ * be used with either native ModuleSources or module sources pre-compiled for
5
+ * the SES Compartment, depending on the __native__ Compartment constructor
6
+ * option.
7
+ */
8
+ import { globalThis } from '../src/commons.js';
9
+ import { NativeStartCompartment } from './commons.js';
10
+ import { repairIntrinsics } from '../src/lockdown.js';
11
+ import {
12
+ ShimStartCompartment,
13
+ adaptCompartmentConstructors,
14
+ } from './compartment.js';
15
+
16
+ const lockdown = options => {
17
+ const hardenIntrinsics = repairIntrinsics(options);
18
+ hardenIntrinsics();
19
+ // Replace global Compartment with a version that is hardened and hardens
20
+ // transitive child Compartment.
21
+ // @ts-expect-error Incomplete global type on XS.
22
+ globalThis.Compartment = adaptCompartmentConstructors(
23
+ NativeStartCompartment,
24
+ ShimStartCompartment,
25
+ harden,
26
+ );
27
+ };
28
+
29
+ globalThis.lockdown = lockdown;
package/types.d.ts CHANGED
@@ -132,6 +132,10 @@ export type ModuleDescriptor =
132
132
  | PrecompiledModuleSource
133
133
  | string;
134
134
 
135
+ export type StrictModuleDescriptor =
136
+ | SourceModuleDescriptor
137
+ | NamespaceModuleDescriptor;
138
+
135
139
  // Deprecated type aliases:
136
140
  export type PrecompiledStaticModuleInterface = PrecompiledModuleSource;
137
141
  export type ThirdPartyStaticModuleInterface = VirtualModuleSource;