This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
/
register-loader.js
495 lines (393 loc) · 14.7 KB
/
register-loader.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
import { Loader, Module, InternalModuleNamespace as ModuleNamespace } from './loader-polyfill.js';
import { resolveUrlToParentIfNotPlain } from './resolve.js';
import { addToError, global } from './common.js';
export default RegisterLoader;
export var emptyModule = new ModuleNamespace({});
/*
* Register Loader
*
* Builds directly on top of loader polyfill to provide:
* - loader.register support
* - hookable higher-level normalize with metadata argument
* - instantiate hook with metadata arugment returning a ModuleNamespace or undefined for es module loading
* - loader error behaviour as in HTML and loader specs, clearing failed modules from registration cache synchronously
* - build tracing support by providing a .trace=true and .loads object format
*/
function RegisterLoader(baseKey) {
Loader.apply(this, arguments);
// System.register registration cache
this._registerCache = {};
// last anonymous System.register call
this._registeredLastAnon = undefined;
// in-flight es module load records
this._registerRegistry = {};
// tracing
this.trace = false;
// trace load objects when tracing
this.loads = {};
}
RegisterLoader.prototype = Object.create(Loader.prototype);
RegisterLoader.prototype.constructor = RegisterLoader;
// these are implementation specific
// this allows a v2 migration path into symbols so normalize and instantiate
// aren't exposed to end-users
RegisterLoader.normalize = 'normalize';
RegisterLoader.instantiate = 'instantiate';
RegisterLoader.createMetadata = 'createMetadata';
// default normalize is the WhatWG style normalizer
RegisterLoader.prototype.normalize = function(key, parentKey, metadata) {
return resolveUrlToParentIfNotPlain(key, parentKey);
};
RegisterLoader.prototype.instantiate = function(key, metadata) {};
// this function is an optimization to allow loader extensions to
// implement it to set the metadata object shape upfront to ensure
// it can run as a single hidden class throughout the normalize
// and instantiate pipeline hooks in the js engine
RegisterLoader.prototype.createMetadata = function() {
return {};
};
var RESOLVE = Loader.resolve;
RegisterLoader.prototype[RESOLVE] = function(key, parentKey) {
var loader = this;
// normalization shortpath if already in the registry or loading
if (key && (loader.registry.has(key) || loader._registerRegistry[key]))
return Promise.resolve(key);
var metadata = this.createMetadata();
return Promise.resolve(loader.normalize(key, parentKey, metadata))
.then(function(resolvedKey) {
if (typeof resolvedKey !== 'string') {
if (resolvedKey === undefined)
throw new RangeError('No resolution normalizing "' + key + '" to ' + parentKey);
// allow a non-string resolve for use cases like conditionals
return resolvedKey;
}
// we create the in-progress load record already here to store the normalization metadata
if (!loader.registry.has(resolvedKey))
getOrCreateLoadRecord(loader, resolvedKey, metadata);
return resolvedKey;
});
};
// provides instantiate promise cache
// we need to first wait on instantiate which will tell us if it is ES or not
// this record represents that waiting period, and when set, we then populate
// the esLinkRecord record into this load record.
// instantiate is a promise for a module namespace or undefined
function getOrCreateLoadRecord(loader, key, metadata) {
return loader._registerRegistry[key] || (loader._registerRegistry[key] = {
key: key,
metadata: metadata,
instantiatePromise: undefined,
module: undefined,
// es-specific
esLinkRecord: undefined,
importerSetters: undefined
});
}
RegisterLoader.prototype[Loader.instantiate] = function(key) {
var loader = this;
return instantiate(this, key)
.then(function(instantiated) {
if (instantiated instanceof ModuleNamespace)
return Promise.resolve(instantiated);
return instantiateAllDeps(loader, instantiated, [])
.then(function() {
if (loader.execute)
var err = ensureEvaluated(loader, instantiated, []);
if (err)
return Promise.reject(err);
if (loader.trace)
traceLoadRecord(loader, instantiated, []);
return instantiated.module || emptyModule;
})
.catch(function(err) {
clearLoadErrors(loader, instantiated);
throw err;
});
})
};
// instantiates the given module name
// returns the load record for es or the namespace object for dynamic
// setting the dynamic namespace into the registry
function instantiate(loader, key) {
var load = loader._registerRegistry[key];
// this is impossible assuming resolve always runs before instantiate
if (!load)
throw new TypeError('Internal error, load record not created');
return load.instantiatePromise || (load.instantiatePromise = Promise.resolve(loader.instantiate(key, load.metadata))
.then(function(instantiation) {
// dynamic module
if (instantiation !== undefined) {
loader.registry.set(key, instantiation);
loader._registerRegistry[key] = undefined;
return instantiation;
}
// run the cached loader.register declaration if there is one
ensureRegisterLinkRecord.call(loader, load);
// metadata no longer needed
if (!loader.trace)
load.metadata = undefined;
return load;
})
.catch(function(err) {
err = addToError(err, 'Instantiating ' + load.key);
// immediately clear the load record for an instantiation error
if (loader._registerRegistry[load.key] === load)
loader._registerRegistry[load.key] = undefined;
throw err;
}));
}
// this only applies to load records with load.esLinkRecord set
function instantiateAllDeps(loader, load, seen) {
// skip if already linked
if (load.module)
return Promise.resolve();
var esLinkRecord = load.esLinkRecord;
// no dependencies shortpath
if (!esLinkRecord.dependencies.length)
return Promise.resolve();
// assumes seen does not contain load already
seen.push(load);
var instantiateDepsPromises = Array(esLinkRecord.dependencies.length);
// normalize dependencies
for (var i = 0; i < esLinkRecord.dependencies.length; i++) (function(i) {
// this resolve can potentially be cached on the link record, should be a measured optimization
instantiateDepsPromises[i] = loader[RESOLVE](esLinkRecord.dependencies[i], load.key)
.catch(function(err) {
throw addToError(err, 'Resolving ' + esLinkRecord.dependencies[i] + ' to ' + load.key);
})
.then(function(resolvedDepKey) {
var existingNamespace = loader.registry.get(resolvedDepKey);
if (existingNamespace) {
esLinkRecord.dependencyInstantiations[i] = existingNamespace;
// run setter to reference the module
if (esLinkRecord.setters[i])
esLinkRecord.setters[i](existingNamespace);
return Promise.resolve();
}
if (loader.trace) {
esLinkRecord.depMap = esLinkRecord.depMap || {};
esLinkRecord.depMap[esLinkRecord.dependencies[i]] = resolvedDepKey;
}
return instantiate(loader, resolvedDepKey)
.then(function(instantiation) {
// instantiation is either a load record or a module namespace
esLinkRecord.dependencyInstantiations[i] = instantiation;
// dynamic module
if (instantiation instanceof ModuleNamespace) {
if (esLinkRecord.setters[i])
esLinkRecord.setters[i](instantiation);
return Promise.resolve();
}
// register setter with dependency
instantiation.importerSetters.push(esLinkRecord.setters[i]);
// run setter now to pick up the first bindings from the dependency
if (esLinkRecord.setters[i])
esLinkRecord.setters[i](instantiation.esLinkRecord.moduleObj);
// circular
if (seen.indexOf(instantiation) !== -1)
return Promise.resolve();
// es module load
// if not already linked, instantiate dependencies
if (instantiation.esLinkRecord)
return instantiateAllDeps(loader, instantiation, seen);
});
})
})(i);
return Promise.all(instantiateDepsPromises)
.catch(function(err) {
err = addToError(err, 'Loading ' + load.key);
// throw up the instantiateAllDeps stack
// loads are then synchonously cleared at the top-level through the helper below
// this then ensures avoiding partially unloaded tree states
esLinkRecord.error = err;
throw err;
});
}
// clears an errored load and all its errored dependencies from the loads registry
function clearLoadErrors(loader, load) {
// clear from loads
if (loader._registerRegistry[load.key] === load)
loader._registerRegistry[load.key] = undefined;
var esLinkRecord = load.esLinkRecord;
if (!esLinkRecord)
return;
esLinkRecord.dependencyInstantiations.forEach(function(depLoad, index) {
if (!depLoad || depLoad instanceof ModuleNamespace)
return;
if (depLoad.esLinkRecord && depLoad.esLinkRecord.error) {
// unregister setters for es dependency load records
var setterIndex = depLoad.importerSetters.indexOf(esLinkRecord.setters[index]);
depLoad.importerSetters.splice(setterIndex, 1);
// provides a circular reference check
if (loader._registerRegistry[depLoad.key] === depLoad)
clearLoadErrors(loader, depLoad);
}
});
}
function createESLinkRecord(dependencies, setters, module, moduleObj, execute) {
return {
dependencies: dependencies,
error: undefined,
// will be the dependency ES load record, or a module namespace
dependencyInstantiations: Array(dependencies.length),
setters: setters,
module: module,
moduleObj: moduleObj,
execute: execute
};
}
/*
* System.register
* Places the status into the registry and a load into the loads list
*/
RegisterLoader.prototype.register = function(key, deps, declare) {
// anonymous modules get stored as lastAnon
if (declare === undefined)
this._registeredLastAnon = [key, deps];
// everything else registers into the register cache
else
this._registerCache[key] = [deps, declare];
};
RegisterLoader.prototype.processRegisterContext = function(contextKey) {
if (!this._registeredLastAnon)
return;
this._registerCache[contextKey] = this._registeredLastAnon;
this._registeredLastAnon = undefined;
};
function ensureRegisterLinkRecord(load) {
// ensure we already have a link record
if (load.esLinkRecord)
return;
var key = load.key;
var registrationPair = this._registerCache[key];
if (!registrationPair)
throw new TypeError('Module instantiation did not call an anonymous or correctly named System.register');
this._registerCache[key] = undefined;
var importerSetters = [];
var moduleObj = {};
var locked = false;
var declared = registrationPair[1].call(global, function(name, value) {
// export setter propogation with locking to avoid cycles
if (locked)
return;
if (typeof name == 'object') {
for (var p in name)
moduleObj[p] = name[p];
}
else {
moduleObj[name] = value;
}
if (importerSetters.length) {
locked = true;
for (var i = 0; i < importerSetters.length; i++)
// this object should be a defined module object
// but in order to do that we need the exports returned by declare
// for now we assume no exports in the implementation
importerSetters[i](moduleObj);
locked = false;
}
return value;
}, new ContextualLoader(this, key));
var setters, execute;
if (typeof declared !== 'function') {
setters = declared.setters;
execute = declared.execute;
}
else {
setters = [],
execute = declared;
}
// TODO, pass module when we can create it here already via exports
load.importerSetters = importerSetters;
load.esLinkRecord = createESLinkRecord(registrationPair[0], setters, undefined, moduleObj, execute);
}
// ContextualLoader class
// backwards-compatible with previous System.register context argument by exposing .id
function ContextualLoader(loader, key) {
this.loader = loader;
this.key = this.id = key;
}
ContextualLoader.prototype.constructor = function() {
throw new TypeError('Cannot subclass the contextual loader only Reflect.Loader.');
};
ContextualLoader.prototype.import = function(key) {
return this.loader.import(key, this.key)
};
ContextualLoader.prototype.resolve = function(key) {
return this.loader[Loader.resolve](key, this.key);
};
ContextualLoader.prototype.load = function(key) {
return this.loader.load(key, this.key);
};
// ensures the given es load is evaluated
// returns the error if any
function ensureEvaluated(loader, load, seen) {
var esLinkRecord = load.esLinkRecord;
// no esLinkRecord means evaluated
if (!esLinkRecord)
return;
// assumes seen does not contain load already
seen.push(load);
var err, depLoad;
for (var i = 0; i < esLinkRecord.dependencies.length; i++) {
depLoad = esLinkRecord.dependencyInstantiations[i];
// non ES load
// it is the responsibility of the executor to remove the module from the registry on failure
if (depLoad instanceof ModuleNamespace)
err = namespaceEvaluate(depLoad);
// ES load
else if (seen.indexOf(depLoad) === -1)
err = ensureEvaluated(loader, depLoad, seen);
if (err)
return addToError(err, 'Evaluating ' + load.key);
}
// es load record evaluation
err = esEvaluate(esLinkRecord);
if (err) {
loader.registry.delete(load.key);
if (loader._registerRegistry[load.key] === load)
loader._registerRegistry[load.key] = undefined;
return addToError(err, 'Evaluating ' + load.key);
}
load.module = new ModuleNamespace(esLinkRecord.moduleObj);
loader.registry.set(load.key, load.module);
// can clear link record now
if (!loader.trace)
load.esLinkRecord = undefined;
}
function esEvaluate(esLinkRecord) {
try {
// {} is the closest we can get to call(undefined)
// this should really be blocked earlier though
esLinkRecord.execute.call({});
}
catch(err) {
return err;
}
}
function namespaceEvaluate(namespace) {
try {
Module.evaluate(namespace);
}
catch(err) {
return err;
}
}
function traceLoadRecord(loader, load, seen) {
// its up to dynamic instantiate layers to ensure their own traces are present
if (load instanceof ModuleNamespace)
return;
seen.push(load);
if (!load.esLinkRecord || load.esLinkRecord.dependencies.length && !load.esLinkRecord.depMap)
throw new Error('Tracing error, ensure loader.trace is set before loading begins');
loader.loads[load.key] = {
key: load.key,
dependencies: load.esLinkRecord.dependencies,
depMap: load.esLinkRecord.depMap || {},
metadata: load.metadata
};
load.esLinkRecord.dependencies.forEach(function(dep) {
if (seen.indexOf(dep) === -1)
traceLoadRecord(loader, dep, seen);
});
}