ses 1.10.0 → 1.11.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.
package/dist/types.d.cts CHANGED
@@ -27,8 +27,14 @@ export interface RepairOptions {
27
27
  reporting?: 'platform' | 'console' | 'none';
28
28
  unhandledRejectionTrapping?: 'report' | 'none';
29
29
  errorTaming?: 'safe' | 'unsafe' | 'unsafe-debug';
30
- dateTaming?: 'safe' | 'unsafe'; // deprecated
31
- mathTaming?: 'safe' | 'unsafe'; // deprecated
30
+ /**
31
+ * @deprecated Deprecated and does nothing. In the future specifying it will be an error.
32
+ */
33
+ dateTaming?: 'safe' | 'unsafe';
34
+ /**
35
+ * @deprecated Deprecated and does nothing. In the future specifying it will be an error.
36
+ */
37
+ mathTaming?: 'safe' | 'unsafe';
32
38
  evalTaming?: 'safeEval' | 'unsafeEval' | 'noEval';
33
39
  stackFiltering?: 'concise' | 'verbose';
34
40
  overrideTaming?: 'moderate' | 'min' | 'severe';
@@ -552,12 +558,12 @@ declare global {
552
558
 
553
559
  // Deprecated:
554
560
  constructor(
555
- globals?: Record<string, any> | undefined,
561
+ globals?: Record<PropertyKey, any> | undefined,
556
562
  modules?: Record<string, ModuleDescriptor>,
557
563
  options?: CompartmentOptions,
558
564
  );
559
565
 
560
- get globalThis(): Record<string, any>;
566
+ get globalThis(): Record<PropertyKey, any>;
561
567
 
562
568
  get name(): string;
563
569
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -38,6 +38,7 @@
38
38
  ".": {
39
39
  "import": {
40
40
  "types": "./types.d.ts",
41
+ "xs": "./src-xs/index.js",
41
42
  "default": "./index.js"
42
43
  },
43
44
  "require": {
@@ -57,8 +58,14 @@
57
58
  },
58
59
  "./tools.js": "./tools.js",
59
60
  "./assert-shim.js": "./assert-shim.js",
60
- "./lockdown-shim.js": "./lockdown-shim.js",
61
- "./compartment-shim.js": "./compartment-shim.js",
61
+ "./lockdown-shim.js": {
62
+ "xs": "./src-xs/lockdown-shim.js",
63
+ "default": "./lockdown-shim.js"
64
+ },
65
+ "./compartment-shim.js": {
66
+ "xs": "./src-xs/compartment-shim.js",
67
+ "default": "./compartment-shim.js"
68
+ },
62
69
  "./console-shim.js": "./console-shim.js",
63
70
  "./package.json": "./package.json"
64
71
  },
@@ -74,16 +81,16 @@
74
81
  "prepare": "npm run clean && npm run build",
75
82
  "qt": "ava",
76
83
  "test": "tsd && ava",
77
- "test:xs": "xst dist/ses.umd.js test/_lockdown-safe.js",
84
+ "test:xs": "xst dist/ses.umd.js test/_lockdown-safe.js && node scripts/generate-test-xs.js && xst tmp/test-xs.js && rm -rf tmp",
78
85
  "postpack": "git clean -f '*.d.ts*' '*.tsbuildinfo'"
79
86
  },
80
87
  "dependencies": {
81
88
  "@endo/env-options": "^1.1.8"
82
89
  },
83
90
  "devDependencies": {
84
- "@endo/compartment-mapper": "^1.4.0",
85
- "@endo/module-source": "^1.1.2",
86
- "@endo/test262-runner": "^0.1.43",
91
+ "@endo/compartment-mapper": "^1.5.0",
92
+ "@endo/module-source": "^1.2.0",
93
+ "@endo/test262-runner": "^0.1.44",
87
94
  "ava": "^6.1.3",
88
95
  "babel-eslint": "^10.1.0",
89
96
  "c8": "^7.14.0",
@@ -203,5 +210,5 @@
203
210
  "typeCoverage": {
204
211
  "atLeast": 81.17
205
212
  },
206
- "gitHead": "5486ed1f238104716b6a8321b977fbc508ef80e1"
213
+ "gitHead": "e0683e0bfdbfc84351af332c9e78813d7b67ef89"
207
214
  }
package/src/commons.js CHANGED
@@ -1,17 +1,19 @@
1
- /* global globalThis */
2
- /* eslint-disable no-restricted-globals */
3
-
4
1
  /**
5
- * commons.js
6
- * Declare shorthand functions. Sharing these declarations across modules
7
- * improves on consistency and minification. Unused declarations are
8
- * dropped by the tree shaking process.
2
+ * @module Captures native intrinsics during initialization, so vetted shims
3
+ * (running between initialization of SES and calling lockdown) are free to
4
+ * modify the environment without compromising the integrity of SES. For
5
+ * example, a vetted shim can modify Object.assign because we capture and
6
+ * export Object and assign here, then never again consult Object to get its
7
+ * assign property.
9
8
  *
10
- * We capture these, not just for brevity, but for security. If any code
11
- * modifies Object to change what 'assign' points to, the Compartment shim
12
- * would be corrupted.
9
+ * This pattern of use is enforced by eslint rules no-restricted-globals and
10
+ * no-polymorphic-call.
11
+ * We maintain the list of restricted globals in ../package.json.
13
12
  */
14
13
 
14
+ /* global globalThis */
15
+ /* eslint-disable no-restricted-globals */
16
+
15
17
  // We cannot use globalThis as the local name since it would capture the
16
18
  // lexical name.
17
19
  const universalThis = globalThis;
@@ -267,12 +269,6 @@ export const finalizationRegistryUnregister =
267
269
  export const getConstructorOf = fn =>
268
270
  reflectGet(getPrototypeOf(fn), 'constructor');
269
271
 
270
- /**
271
- * immutableObject
272
- * An immutable (frozen) empty object that is safe to share.
273
- */
274
- export const immutableObject = freeze(create(null));
275
-
276
272
  /**
277
273
  * isObject tests whether a value is an object.
278
274
  * Today, this is equivalent to:
@@ -305,6 +301,12 @@ export const isObject = value => Object(value) === value;
305
301
  */
306
302
  export const isError = value => value instanceof FERAL_ERROR;
307
303
 
304
+ /**
305
+ * @template T
306
+ * @param {T} x
307
+ */
308
+ export const identity = x => x;
309
+
308
310
  // The original unsafe untamed eval function, which must not escape.
309
311
  // Sample at module initialization time, which is before lockdown can
310
312
  // repair it. Use it only to build powerless abstractions.
@@ -16,4 +16,7 @@ globalThis.Compartment = makeCompartmentConstructor(
16
16
  // See https://github.com/endojs/endo/pull/2624#discussion_r1840979770
17
17
  getGlobalIntrinsics(globalThis, muteReporter),
18
18
  markVirtualizedNativeFunction,
19
+ {
20
+ enforceNew: true,
21
+ },
19
22
  );
@@ -1,3 +1,40 @@
1
+ /**
2
+ * @module Provides the mechanism to create a Compartment constructor that
3
+ * can provide either shim-specific or native XS features depending on
4
+ * the __native__ constructor option.
5
+ * This is necessary because a native Compartment can handle native ModuleSource
6
+ * but cannot handle shim-specific pre-compiled ModuleSources like the JSON
7
+ * representation of a module that Compartment Mapper can put in bundles.
8
+ * Pre-compiling ModuleSource during bundling helps avoid paying the cost
9
+ * of importing Babel and transforming ESM syntax to a form that can be
10
+ * confined by the shim, which is prohibitively expensive for a web runtime
11
+ * and for XS _without this adapter_.
12
+ *
13
+ * Since any invocation of the Compartment constructor may occur standing
14
+ * on a native-flavor or shim-flavor compartment, we create parallel compartment
15
+ * constructor trees for compartments created with the Compartment constructor
16
+ * of a specific compartment.
17
+ *
18
+ * A compartment's importHook, importNowHook, moduleMapHook, and the modules
19
+ * map itself may provide module descriptors that address another compartment,
20
+ * using a compartment instance as a token indicating the compartment the
21
+ * module should be loaded or initialized in.
22
+ * Consequently, the compartment instance must be a suitable token for the
23
+ * underlying native-flavor or shim-flavor compartment.
24
+ * We are not in a position to fidddle with the native compartments behavior,
25
+ * so adapted compartments use the identity of the native compartment.
26
+ * We replace all of the methods of the native compartment prototype with
27
+ * thunks that choose behavior based on whether the compartment was
28
+ * constructed with the __native__ option.
29
+ * The SES shim associates a compartment with its private fields using a weak
30
+ * map exported by ../src/compartment.js and held closely by ses by the
31
+ * enforcement of explicit exports in package.json, since Node.js 12.11.0.
32
+ *
33
+ * Evaluating ./compartment.js does not have global side-effects.
34
+ * We defer modification of the global environment until the evaluation
35
+ * of ./compartment-shim.js.
36
+ */
37
+
1
38
  // @ts-check
2
39
  /* eslint-disable no-underscore-dangle */
3
40
  /// <reference types="ses">
@@ -6,8 +43,10 @@ import {
6
43
  Map,
7
44
  TypeError,
8
45
  WeakMap,
46
+ arrayFlatMap,
9
47
  assign,
10
48
  defineProperties,
49
+ identity,
11
50
  promiseThen,
12
51
  toStringTagSymbol,
13
52
  weakmapGet,
@@ -19,7 +58,7 @@ import {
19
58
  setGlobalObjectMutableProperties,
20
59
  setGlobalObjectEvaluators,
21
60
  } from './global-object.js';
22
- import { assert, assertEqual } from './error/assert.js';
61
+ import { assert, assertEqual, q } from './error/assert.js';
23
62
  import { sharedGlobalPropertyNames } from './permits.js';
24
63
  import { load, loadNow } from './module-load.js';
25
64
  import { link } from './module-link.js';
@@ -27,7 +66,7 @@ import { getDeferredExports } from './module-proxy.js';
27
66
  import { compartmentEvaluate } from './compartment-evaluate.js';
28
67
  import { makeSafeEvaluator } from './make-safe-evaluator.js';
29
68
 
30
- /** @import {ModuleDescriptor} from '../types.js' */
69
+ /** @import {ModuleDescriptor, ModuleExportsNamespace} from '../types.js' */
31
70
 
32
71
  // moduleAliases associates every public module exports namespace with its
33
72
  // corresponding compartment and specifier so they can be used to link modules
@@ -171,7 +210,9 @@ defineProperties(InertCompartment, {
171
210
  * @param {MakeCompartmentConstructor} targetMakeCompartmentConstructor
172
211
  * @param {Record<string, any>} intrinsics
173
212
  * @param {(object: object) => void} markVirtualizedNativeFunction
174
- * @param {Compartment} [parentCompartment]
213
+ * @param {object} [options]
214
+ * @param {Compartment} [options.parentCompartment]
215
+ * @param {boolean} [options.enforceNew]
175
216
  * @returns {Compartment['constructor']}
176
217
  */
177
218
 
@@ -184,7 +225,7 @@ defineProperties(InertCompartment, {
184
225
  // positional arguments, this function detects the temporary sigil __options__
185
226
  // on the first argument and coerces compartments arguments into a single
186
227
  // compartments object.
187
- const compartmentOptions = (...args) => {
228
+ export const compartmentOptions = (...args) => {
188
229
  if (args.length === 0) {
189
230
  return {};
190
231
  }
@@ -229,10 +270,10 @@ export const makeCompartmentConstructor = (
229
270
  targetMakeCompartmentConstructor,
230
271
  intrinsics,
231
272
  markVirtualizedNativeFunction,
232
- parentCompartment = undefined,
273
+ { parentCompartment = undefined, enforceNew = false } = {},
233
274
  ) => {
234
275
  function Compartment(...args) {
235
- if (new.target === undefined) {
276
+ if (enforceNew && new.target === undefined) {
236
277
  throw TypeError(
237
278
  "Class constructor Compartment cannot be invoked without 'new'",
238
279
  );
@@ -252,7 +293,10 @@ export const makeCompartmentConstructor = (
252
293
  importMetaHook,
253
294
  __noNamespaceBox__: noNamespaceBox = false,
254
295
  } = compartmentOptions(...args);
255
- const globalTransforms = [...transforms, ...__shimTransforms__];
296
+ const globalTransforms = arrayFlatMap(
297
+ [transforms, __shimTransforms__],
298
+ identity,
299
+ );
256
300
  const endowments = { __proto__: null, ...endowmentsOption };
257
301
  const moduleMap = { __proto__: null, ...moduleMapOption };
258
302
 
@@ -297,6 +341,43 @@ export const makeCompartmentConstructor = (
297
341
 
298
342
  assign(globalObject, endowments);
299
343
 
344
+ /**
345
+ * In support dynamic import in a module source loaded by this compartment,
346
+ * like `await import(importSpecifier)`, induces this compartment to import
347
+ * a module, returning a promise for the resulting module exports
348
+ * namespace.
349
+ * Unlike `compartment.import`, never creates a box object for the
350
+ * namespace as that behavior is deprecated and inconsistent with the
351
+ * standard behavior of dynamic import.
352
+ * Obliges the caller to resolve import specifiers to their corresponding
353
+ * full specifier.
354
+ * That is, every module must have its own dynamic import function that
355
+ * closes over the surrounding module's full module specifier and calls
356
+ * through to this function.
357
+ * @param {string} fullSpecifier - A full specifier is a key in the
358
+ * compartment's module memo.
359
+ * The method `compartment.import` accepts a full specifier, but dynamic
360
+ * import accepts an import specifier and resolves it to a full specifier
361
+ * relative to the calling module's full specifier.
362
+ * @returns {Promise<ModuleExportsNamespace>}
363
+ */
364
+ const compartmentImport = async fullSpecifier => {
365
+ if (typeof resolveHook !== 'function') {
366
+ throw TypeError(
367
+ `Compartment does not support dynamic import: no configured resolveHook for compartment ${q(name)}`,
368
+ );
369
+ }
370
+ await load(privateFields, moduleAliases, this, fullSpecifier);
371
+ const { execute, exportsProxy } = link(
372
+ privateFields,
373
+ moduleAliases,
374
+ this,
375
+ fullSpecifier,
376
+ );
377
+ execute();
378
+ return exportsProxy;
379
+ };
380
+
300
381
  weakmapSet(privateFields, this, {
301
382
  name: `${name}`,
302
383
  globalTransforms,
@@ -314,6 +395,7 @@ export const makeCompartmentConstructor = (
314
395
  instances,
315
396
  parentCompartment,
316
397
  noNamespaceBox,
398
+ compartmentImport,
317
399
  });
318
400
  }
319
401
 
@@ -28,6 +28,20 @@ import {
28
28
  * @import {ErrorInfo, ErrorInfoKind, LogRecord, NoteCallback, LoggedErrorHandler, MakeCausalConsole, MakeLoggingConsoleKit} from "./internal-types.js";
29
29
  */
30
30
 
31
+ /**
32
+ * Explicitly set a function's name, supporting use of arrow functions for which
33
+ * source text doesn't include a name and no initial name is set by
34
+ * NamedEvaluation
35
+ * https://tc39.es/ecma262/multipage/syntax-directed-operations.html#sec-runtime-semantics-namedevaluation
36
+ * Instead, we hope that tooling uses only the explicit `name` property.
37
+ *
38
+ * @template {Function} F
39
+ * @param {string} name
40
+ * @param {F} fn
41
+ * @returns {F}
42
+ */
43
+ const defineName = (name, fn) => defineProperty(fn, 'name', { value: name });
44
+
31
45
  // For our internal debugging purposes, uncomment
32
46
  // const internalDebugConsole = console;
33
47
 
@@ -159,16 +173,12 @@ export const makeLoggingConsoleKit = (
159
173
 
160
174
  const loggingConsole = fromEntries(
161
175
  arrayMap(consoleMethodPermits, ([name, _]) => {
162
- // Use an arrow function so that it doesn't come with its own name in
163
- // its printed form. Instead, we're hoping that tooling uses only
164
- // the `.name` property set below.
165
176
  /**
166
177
  * @param {...any} args
167
178
  */
168
- const method = (...args) => {
179
+ const method = defineName(name, (...args) => {
169
180
  arrayPush(logArray, [name, ...args]);
170
- };
171
- defineProperty(method, 'name', { value: name });
181
+ });
172
182
  return [name, freeze(method)];
173
183
  }),
174
184
  );
@@ -288,8 +298,10 @@ export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
288
298
  logError(severity, subError);
289
299
  }
290
300
  } finally {
291
- // eslint-disable-next-line @endo/no-polymorphic-call
292
- baseConsole.groupEnd();
301
+ if (baseConsole.groupEnd) {
302
+ // eslint-disable-next-line @endo/no-polymorphic-call
303
+ baseConsole.groupEnd();
304
+ }
293
305
  }
294
306
  };
295
307
 
@@ -368,7 +380,7 @@ export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
368
380
  /**
369
381
  * @param {...any} logArgs
370
382
  */
371
- const levelMethod = (...logArgs) => {
383
+ const levelMethod = defineName(level, (...logArgs) => {
372
384
  const subErrors = [];
373
385
  const argTags = extractErrorArgs(logArgs, subErrors);
374
386
  if (baseConsole[level]) {
@@ -377,8 +389,7 @@ export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
377
389
  }
378
390
  // @ts-expect-error ConsoleProp vs LogSeverity mismatch
379
391
  logSubErrors(level, subErrors);
380
- };
381
- defineProperty(levelMethod, 'name', { value: level });
392
+ });
382
393
  return [level, freeze(levelMethod)];
383
394
  });
384
395
  const otherMethodNames = arrayFilter(
@@ -389,13 +400,12 @@ export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
389
400
  /**
390
401
  * @param {...any} args
391
402
  */
392
- const otherMethod = (...args) => {
403
+ const otherMethod = defineName(name, (...args) => {
393
404
  // @ts-ignore
394
405
  // eslint-disable-next-line @endo/no-polymorphic-call
395
406
  baseConsole[name](...args);
396
407
  return undefined;
397
- };
398
- defineProperty(otherMethod, 'name', { value: name });
408
+ });
399
409
  return [name, freeze(otherMethod)];
400
410
  });
401
411
 
@@ -456,23 +466,21 @@ export const defineCausalConsoleFromLogger = loggedErrorHandler => {
456
466
  }
457
467
  return tlogger(...args);
458
468
  };
459
- const makeNamed = (name, fn) =>
460
- ({ [name]: (...args) => fn(...args) })[name];
461
469
 
462
470
  const baseConsole = fromEntries([
463
471
  ...arrayMap(consoleLevelMethods, ([name]) => [
464
472
  name,
465
- makeNamed(name, logWithIndent),
473
+ defineName(name, (...args) => logWithIndent(...args)),
466
474
  ]),
467
475
  ...arrayMap(consoleOtherMethods, ([name]) => [
468
476
  name,
469
- makeNamed(name, (...args) => logWithIndent(name, ...args)),
477
+ defineName(name, (...args) => logWithIndent(name, ...args)),
470
478
  ]),
471
479
  ]);
472
480
  // https://console.spec.whatwg.org/#grouping
473
481
  for (const name of ['group', 'groupCollapsed']) {
474
482
  if (baseConsole[name]) {
475
- baseConsole[name] = makeNamed(name, (...args) => {
483
+ baseConsole[name] = defineName(name, (...args) => {
476
484
  if (args.length >= 1) {
477
485
  // Prefix the logged data with "group" or "groupCollapsed".
478
486
  logWithIndent(...args);
@@ -482,16 +490,17 @@ export const defineCausalConsoleFromLogger = loggedErrorHandler => {
482
490
  arrayPush(indents, ' ');
483
491
  });
484
492
  } else {
485
- baseConsole[name] = () => {};
493
+ baseConsole[name] = defineName(name, () => {});
486
494
  }
487
495
  }
488
- if (baseConsole.groupEnd) {
489
- baseConsole.groupEnd = makeNamed('groupEnd', (...args) => {
490
- arrayPop(indents);
491
- });
492
- } else {
493
- baseConsole.groupEnd = () => {};
494
- }
496
+ baseConsole.groupEnd = defineName(
497
+ 'groupEnd',
498
+ baseConsole.groupEnd
499
+ ? (...args) => {
500
+ arrayPop(indents);
501
+ }
502
+ : () => {},
503
+ );
495
504
  harden(baseConsole);
496
505
  const causalConsole = makeCausalConsole(
497
506
  /** @type {VirtualConsole} */ (baseConsole),
@@ -516,14 +525,14 @@ export const filterConsole = (baseConsole, filter, _topic = undefined) => {
516
525
  /**
517
526
  * @param {...any} args
518
527
  */
519
- const method = (...args) => {
528
+ const method = defineName(name, (...args) => {
520
529
  // eslint-disable-next-line @endo/no-polymorphic-call
521
530
  if (severity === undefined || filter.canLog(severity)) {
522
531
  // @ts-ignore
523
532
  // eslint-disable-next-line @endo/no-polymorphic-call
524
533
  baseConsole[name](...args);
525
534
  }
526
- };
535
+ });
527
536
  return [name, freeze(method)];
528
537
  });
529
538
  const filteringConsole = fromEntries(methods);
@@ -117,7 +117,10 @@ export const setGlobalObjectMutableProperties = (
117
117
  makeCompartmentConstructor,
118
118
  intrinsics,
119
119
  markVirtualizedNativeFunction,
120
- parentCompartment,
120
+ {
121
+ parentCompartment,
122
+ enforceNew: true,
123
+ },
121
124
  ),
122
125
  );
123
126
 
package/src/lockdown.js CHANGED
@@ -194,8 +194,8 @@ export const repairIntrinsics = (options = {}) => {
194
194
  'safe',
195
195
  ),
196
196
  __hardenTaming__ = getenv('LOCKDOWN_HARDEN_TAMING', 'safe'),
197
- dateTaming = 'safe', // deprecated
198
- mathTaming = 'safe', // deprecated
197
+ dateTaming, // deprecated
198
+ mathTaming, // deprecated
199
199
  ...extraOptions
200
200
  } = options;
201
201
 
@@ -215,6 +215,20 @@ export const repairIntrinsics = (options = {}) => {
215
215
  Fail`lockdown(): non supported option ${q(extraOptionsNames)}`;
216
216
 
217
217
  const reporter = chooseReporter(reporting);
218
+ const { warn } = reporter;
219
+
220
+ if (dateTaming !== undefined) {
221
+ // eslint-disable-next-line no-console
222
+ warn(
223
+ `SES The 'dateTaming' option is deprecated and does nothing. In the future specifying it will be an error.`,
224
+ );
225
+ }
226
+ if (mathTaming !== undefined) {
227
+ // eslint-disable-next-line no-console
228
+ warn(
229
+ `SES The 'mathTaming' option is deprecated and does nothing. In the future specifying it will be an error.`,
230
+ );
231
+ }
218
232
 
219
233
  priorRepairIntrinsics === undefined ||
220
234
  // eslint-disable-next-line @endo/no-polymorphic-call
@@ -289,9 +303,9 @@ export const repairIntrinsics = (options = {}) => {
289
303
 
290
304
  addIntrinsics(tameFunctionConstructors());
291
305
 
292
- addIntrinsics(tameDateConstructor(dateTaming));
306
+ addIntrinsics(tameDateConstructor());
293
307
  addIntrinsics(tameErrorConstructor(errorTaming, stackFiltering));
294
- addIntrinsics(tameMathObject(mathTaming));
308
+ addIntrinsics(tameMathObject());
295
309
  addIntrinsics(tameRegExpConstructor(regExpTaming));
296
310
  addIntrinsics(tameSymbolConstructor());
297
311
  addIntrinsics(shimArrayBufferTransfer());
@@ -1,7 +1,7 @@
1
1
  // Portions adapted from V8 - Copyright 2016 the V8 project authors.
2
2
  // https://github.com/v8/v8/blob/master/src/builtins/builtins-function.cc
3
3
 
4
- import { apply, freeze } from './commons.js';
4
+ import { apply, arrayFlatMap, freeze, identity } from './commons.js';
5
5
  import { strictScopeTerminator } from './strict-scope-terminator.js';
6
6
  import { createSloppyGlobalsScopeTerminator } from './sloppy-globals-scope-terminator.js';
7
7
  import { makeEvalScopeKit } from './eval-scope.js';
@@ -62,11 +62,13 @@ export const makeSafeEvaluator = ({
62
62
 
63
63
  // Execute the mandatory transforms last to ensure that any rewritten code
64
64
  // meets those mandatory requirements.
65
- source = applyTransforms(source, [
66
- ...localTransforms,
67
- ...globalTransforms,
68
- mandatoryTransforms,
69
- ]);
65
+ source = applyTransforms(
66
+ source,
67
+ arrayFlatMap(
68
+ [localTransforms, globalTransforms, [mandatoryTransforms]],
69
+ identity,
70
+ ),
71
+ );
70
72
 
71
73
  let err;
72
74
  try {
@@ -1,3 +1,5 @@
1
+ /** @import {ModuleExportsNamespace} from '../types.js' */
2
+
1
3
  import { assert } from './error/assert.js';
2
4
  import { getDeferredExports } from './module-proxy.js';
3
5
  import {
@@ -131,13 +133,15 @@ export const makeModuleInstance = (
131
133
  __fixedExportMap__: fixedExportMap = {},
132
134
  __liveExportMap__: liveExportMap = {},
133
135
  __reexportMap__: reexportMap = {},
136
+ __needsImport__: needsImport = false,
134
137
  __needsImportMeta__: needsImportMeta = false,
135
138
  __syncModuleFunctor__,
136
139
  } = moduleSource;
137
140
 
138
141
  const compartmentFields = weakmapGet(privateFields, compartment);
139
142
 
140
- const { __shimTransforms__, importMetaHook } = compartmentFields;
143
+ const { __shimTransforms__, resolveHook, importMetaHook, compartmentImport } =
144
+ compartmentFields;
141
145
 
142
146
  const { exportsProxy, exportsTarget, activate } = getDeferredExports(
143
147
  compartment,
@@ -171,6 +175,14 @@ export const makeModuleInstance = (
171
175
  importMetaHook(moduleSpecifier, importMeta);
172
176
  }
173
177
 
178
+ /** @type {(fullSpecifier: string) => Promise<ModuleExportsNamespace>} */
179
+ let dynamicImport;
180
+ if (needsImport) {
181
+ /** @param {string} importSpecifier */
182
+ dynamicImport = async importSpecifier =>
183
+ compartmentImport(resolveHook(importSpecifier, moduleSpecifier));
184
+ }
185
+
174
186
  // {_localName_: [{get, set, notify}]} used to merge all the export updaters.
175
187
  const localGetNotify = create(null);
176
188
 
@@ -462,6 +474,7 @@ export const makeModuleInstance = (
462
474
  imports: freeze(imports),
463
475
  onceVar: freeze(onceVar),
464
476
  liveVar: freeze(liveVar),
477
+ import: dynamicImport,
465
478
  importMeta,
466
479
  }),
467
480
  );