-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathmodule-instance.js
497 lines (450 loc) · 14.6 KB
/
module-instance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/** @import {ModuleExportsNamespace} from '../types.js' */
import { assert } from './error/assert.js';
import { getDeferredExports } from './module-proxy.js';
import {
ReferenceError,
SyntaxError,
TypeError,
arrayForEach,
arrayIncludes,
arrayPush,
arraySome,
arraySort,
create,
defineProperty,
entries,
freeze,
isArray,
keys,
mapGet,
weakmapGet,
reflectHas,
assign,
} from './commons.js';
import { compartmentEvaluate } from './compartment-evaluate.js';
const { quote: q } = assert;
export const makeVirtualModuleInstance = (
compartmentPrivateFields,
moduleSource,
compartment,
moduleAliases,
moduleSpecifier,
resolvedImports,
) => {
const { exportsProxy, exportsTarget, activate } = getDeferredExports(
compartment,
weakmapGet(compartmentPrivateFields, compartment),
moduleAliases,
moduleSpecifier,
);
const notifiers = create(null);
if (moduleSource.exports) {
if (
!isArray(moduleSource.exports) ||
arraySome(moduleSource.exports, name => typeof name !== 'string')
) {
throw TypeError(
`SES virtual module source "exports" property must be an array of strings for module ${moduleSpecifier}`,
);
}
arrayForEach(moduleSource.exports, name => {
let value = exportsTarget[name];
const updaters = [];
const get = () => value;
const set = newValue => {
value = newValue;
for (const updater of updaters) {
updater(newValue);
}
};
defineProperty(exportsTarget, name, {
get,
set,
enumerable: true,
configurable: false,
});
notifiers[name] = update => {
arrayPush(updaters, update);
update(value);
};
});
// This is enough to support import * from cjs - the '*' field doesn't need to be in exports nor exportsTarget because import will only ever access it via notifiers
notifiers['*'] = update => {
update(exportsTarget);
};
}
const localState = {
activated: false,
};
return freeze({
notifiers,
exportsProxy,
execute() {
if (reflectHas(localState, 'errorFromExecute')) {
throw localState.errorFromExecute;
}
if (!localState.activated) {
activate();
localState.activated = true;
try {
// eslint-disable-next-line @endo/no-polymorphic-call
moduleSource.execute(exportsTarget, compartment, resolvedImports);
} catch (err) {
localState.errorFromExecute = err;
throw err;
}
}
},
});
};
// `makeModuleInstance` takes a module's compartment record, the live import
// namespace, and a global object; and produces a module instance.
// The module instance carries the proxied module exports namespace (the
// "exports"), notifiers to update the module's internal import namespace, and
// an idempotent execute function.
// The module exports namespace is a proxy to the proxied exports namespace
// that the execution of the module instance populates.
export const makeModuleInstance = (
privateFields,
moduleAliases,
moduleRecord,
importedInstances,
) => {
const {
compartment,
moduleSpecifier,
moduleSource,
importMeta: moduleRecordMeta,
} = moduleRecord;
const {
reexports: exportAlls = [],
__syncModuleProgram__: functorSource,
__fixedExportMap__: fixedExportMap = {},
__liveExportMap__: liveExportMap = {},
__reexportMap__: reexportMap = {},
__needsImport__: needsImport = false,
__needsImportMeta__: needsImportMeta = false,
__syncModuleFunctor__,
} = moduleSource;
const compartmentFields = weakmapGet(privateFields, compartment);
const { __shimTransforms__, resolveHook, importMetaHook, compartmentImport } =
compartmentFields;
const { exportsProxy, exportsTarget, activate } = getDeferredExports(
compartment,
compartmentFields,
moduleAliases,
moduleSpecifier,
);
// {_exportName_: getter} module exports namespace
// object (eventually proxied).
const exportsProps = create(null);
// {_localName_: accessor} proxy traps for moduleLexicals and live bindings.
// The moduleLexicals object is frozen and the corresponding properties of
// moduleLexicals must be immutable, so we copy the descriptors.
const moduleLexicals = create(null);
// {_localName_: init(initValue) -> initValue} used by the
// rewritten code to initialize exported fixed bindings.
const onceVar = create(null);
// {_localName_: update(newValue)} used by the rewritten code to
// both initialize and update live bindings.
const liveVar = create(null);
const importMeta = create(null);
if (moduleRecordMeta) {
assign(importMeta, moduleRecordMeta);
}
if (needsImportMeta && importMetaHook) {
importMetaHook(moduleSpecifier, importMeta);
}
/** @type {(fullSpecifier: string) => Promise<ModuleExportsNamespace>} */
let dynamicImport;
if (needsImport) {
/** @param {string} importSpecifier */
dynamicImport = async importSpecifier =>
compartmentImport(resolveHook(importSpecifier, moduleSpecifier));
}
// {_localName_: [{get, set, notify}]} used to merge all the export updaters.
const localGetNotify = create(null);
// {[importName: string]: notify(update(newValue))} Used by code that imports
// one of this module's exports, so that their update function will
// be notified when this binding is initialized or updated.
const notifiers = create(null);
arrayForEach(entries(fixedExportMap), ([fixedExportName, [localName]]) => {
let fixedGetNotify = localGetNotify[localName];
if (!fixedGetNotify) {
// fixed binding state
let value;
let tdz = true;
/** @type {null | Array<(value: any) => void>} */
let optUpdaters = [];
// tdz sensitive getter
const get = () => {
if (tdz) {
throw ReferenceError(`binding ${q(localName)} not yet initialized`);
}
return value;
};
// leave tdz once
const init = freeze(initValue => {
// init with initValue of a declared const binding, and return
// it.
if (!tdz) {
throw TypeError(
`Internal: binding ${q(localName)} already initialized`,
);
}
value = initValue;
const updaters = optUpdaters;
optUpdaters = null;
tdz = false;
for (const updater of updaters || []) {
updater(initValue);
}
return initValue;
});
// If still tdz, register update for notification later.
// Otherwise, update now.
const notify = updater => {
if (updater === init) {
// Prevent recursion.
return;
}
if (tdz) {
arrayPush(optUpdaters || [], updater);
} else {
updater(value);
}
};
// Need these for additional exports of the local variable.
fixedGetNotify = {
get,
notify,
};
localGetNotify[localName] = fixedGetNotify;
onceVar[localName] = init;
}
exportsProps[fixedExportName] = {
get: fixedGetNotify.get,
set: undefined,
enumerable: true,
configurable: false,
};
notifiers[fixedExportName] = fixedGetNotify.notify;
});
arrayForEach(
entries(liveExportMap),
([liveExportName, [localName, setProxyTrap]]) => {
let liveGetNotify = localGetNotify[localName];
if (!liveGetNotify) {
// live binding state
let value;
let tdz = true;
const updaters = [];
// tdz sensitive getter
const get = () => {
if (tdz) {
throw ReferenceError(
`binding ${q(liveExportName)} not yet initialized`,
);
}
return value;
};
// This must be usable locally for the translation of initializing
// a declared local live binding variable.
//
// For reexported variable, this is also an update function to
// register for notification with the downstream import, which we
// must assume to be live. Thus, it can be called independent of
// tdz but always leaves tdz. Such reexporting creates a tree of
// bindings. This lets the tree be hooked up even if the imported
// module instance isn't initialized yet, as may happen in cycles.
const update = freeze(newValue => {
value = newValue;
tdz = false;
for (const updater of updaters) {
updater(newValue);
}
});
// tdz sensitive setter
const set = newValue => {
if (tdz) {
throw ReferenceError(`binding ${q(localName)} not yet initialized`);
}
value = newValue;
for (const updater of updaters) {
updater(newValue);
}
};
// Always register the updater function.
// If not in tdz, also update now.
const notify = updater => {
if (updater === update) {
// Prevent recursion.
return;
}
arrayPush(updaters, updater);
if (!tdz) {
updater(value);
}
};
liveGetNotify = {
get,
notify,
};
localGetNotify[localName] = liveGetNotify;
if (setProxyTrap) {
defineProperty(moduleLexicals, localName, {
get,
set,
enumerable: true,
configurable: false,
});
}
liveVar[localName] = update;
}
exportsProps[liveExportName] = {
get: liveGetNotify.get,
set: undefined,
enumerable: true,
configurable: false,
};
notifiers[liveExportName] = liveGetNotify.notify;
},
);
const notifyStar = update => {
update(exportsTarget);
};
notifiers['*'] = notifyStar;
// Per the calling convention for the moduleFunctor generated from
// an ESM, the `imports` function gets called once up front
// to populate or arrange the population of imports and reexports.
// The generated code produces an `updateRecord`: the means for
// the linker to update the imports and exports of the module.
// The updateRecord must conform to moduleAnalysis.imports
// updateRecord = Map<specifier, importUpdaters>
// importUpdaters = Map<importName, [update(newValue)*]>
function imports(updateRecord) {
// By the time imports is called, the importedInstances should already be
// initialized with module instances that satisfy
// imports.
// importedInstances = Map[_specifier_, { notifiers, module, execute }]
// notifiers = { [importName: string]: notify(update(newValue))}
// export * cannot export default.
const candidateAll = create(null);
candidateAll.default = false;
for (const [specifier, importUpdaters] of updateRecord) {
const instance = mapGet(importedInstances, specifier);
// The module instance object is an internal literal, does not bind this,
// and never revealed outside the SES shim.
// There are two instantiation sites for instances and they are both in
// this module.
// eslint-disable-next-line @endo/no-polymorphic-call
instance.execute(); // bottom up cycle tolerant
const { notifiers: importNotifiers } = instance;
for (const [importName, updaters] of importUpdaters) {
const importNotify = importNotifiers[importName];
if (!importNotify) {
throw SyntaxError(
`The requested module '${specifier}' does not provide an export named '${importName}'`,
);
}
for (const updater of updaters) {
importNotify(updater);
}
}
if (arrayIncludes(exportAlls, specifier)) {
// Make all these imports candidates.
// Note names don't change in reexporting all
for (const [importAndExportName, importNotify] of entries(
importNotifiers,
)) {
if (candidateAll[importAndExportName] === undefined) {
candidateAll[importAndExportName] = importNotify;
} else {
// Already a candidate: remove ambiguity.
candidateAll[importAndExportName] = false;
}
}
}
if (reexportMap[specifier]) {
// Make named reexports candidates too.
for (const [localName, exportedName] of reexportMap[specifier]) {
candidateAll[exportedName] = importNotifiers[localName];
}
}
}
for (const [exportName, notify] of entries(candidateAll)) {
if (!notifiers[exportName] && notify !== false) {
notifiers[exportName] = notify;
// exported live binding state
let value;
const update = newValue => (value = newValue);
notify(update);
exportsProps[exportName] = {
get() {
return value;
},
set: undefined,
enumerable: true,
configurable: false,
};
}
}
// Sort the module exports namespace as per spec.
// The module exports namespace will be wrapped in a module namespace
// exports proxy which will serve as a "module exports namespace exotic
// object".
// Sorting properties is not generally reliable because some properties may
// be symbols, and symbols do not have an inherent relative order, but
// since all properties of the exports namespace must be keyed by a string
// and the string must correspond to a valid identifier, sorting these
// properties works for this specific case.
arrayForEach(arraySort(keys(exportsProps)), k =>
defineProperty(exportsTarget, k, exportsProps[k]),
);
freeze(exportsTarget);
activate();
}
let optFunctor;
if (__syncModuleFunctor__ !== undefined) {
optFunctor = __syncModuleFunctor__;
} else {
optFunctor = compartmentEvaluate(compartmentFields, functorSource, {
globalObject: compartment.globalThis,
transforms: __shimTransforms__,
__moduleShimLexicals__: moduleLexicals,
});
}
let didThrow = false;
let thrownError;
function execute() {
if (optFunctor) {
// uninitialized
const functor = optFunctor;
optFunctor = null;
// initializing - call with `this` of `undefined`.
try {
functor(
freeze({
imports: freeze(imports),
onceVar: freeze(onceVar),
liveVar: freeze(liveVar),
import: dynamicImport,
importMeta,
}),
);
} catch (e) {
didThrow = true;
thrownError = e;
}
// initialized
}
if (didThrow) {
throw thrownError;
}
}
return freeze({
notifiers,
exportsProxy,
execute,
});
};