Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 76d3bbf

Browse files
committed
instantiation checks alter
1 parent 887ac27 commit 76d3bbf

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

core/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export var isWindows = typeof process !== 'undefined' && typeof process.platform
88
var envGlobal = typeof self !== 'undefined' ? self : global;
99
export { envGlobal as global }
1010

11+
export var resolvedPromise = Promise.resolve();
12+
1113
/*
1214
* Simple Symbol() shim
1315
*/

core/loader-polyfill.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addToError, createSymbol } from './common.js';
1+
import { addToError, createSymbol, resolvedPromise } from './common.js';
22

33
export { Loader, ModuleNamespace }
44

@@ -45,12 +45,24 @@ function Loader () {
4545
}
4646
// 3.3.1
4747
Loader.prototype.constructor = Loader;
48+
49+
function ensureInstantiated (module) {
50+
if (!(module instanceof ModuleNamespace))
51+
throw new TypeError('Module instantiation did not return a valid namespace object.');
52+
return module;
53+
}
54+
4855
// 3.3.2
4956
Loader.prototype.import = function (key, parent) {
5057
if (typeof key !== 'string')
5158
throw new TypeError('Loader import method must be passed a module key string');
5259
// custom resolveInstantiate combined hook for better perf
53-
return Promise.resolve(this[RESOLVE_INSTANTIATE](key, parent))
60+
var loader = this;
61+
return resolvedPromise
62+
.then(function () {
63+
return loader[RESOLVE_INSTANTIATE](key, parent);
64+
})
65+
.then(ensureInstantiated)
5466
//.then(Module.evaluate)
5567
.catch(function (err) {
5668
throw addToError(err, 'Loading ' + key + (parent ? ' from ' + parent : ''));
@@ -76,10 +88,7 @@ Loader.prototype[RESOLVE_INSTANTIATE] = function (key, parent) {
7688
var loader = this;
7789
return loader.resolve(key, parent)
7890
.then(function (resolved) {
79-
var module = loader.registry.get(resolved);
80-
if (!module)
81-
throw new Error('Module ' + resolved + ' did not instantiate.');
82-
return module;
91+
return loader.registry.get(resolved);
8392
});
8493
};
8594

@@ -91,7 +100,7 @@ function ensureResolution (resolvedKey) {
91100

92101
Loader.prototype.resolve = function (key, parent) {
93102
var loader = this;
94-
return Promise.resolve()
103+
return resolvedPromise
95104
.then(function() {
96105
return loader[RESOLVE](key, parent);
97106
})

test/2-loader-api.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import assert from 'assert';
22
import { Loader, ModuleNamespace } from '../core/loader-polyfill.js';
3+
import RegisterLoader from '../core/register-loader.js';
34
import { pathToFileUrl } from '../core/common.js';
45

5-
describe('Loader Polyfill API', function() {
6-
var loader = new Loader(pathToFileUrl(process.cwd()));
6+
describe('Loader Polyfill API', function () {
7+
var loader = new Loader();
78

8-
it('Should be an instance of itself', function() {
9+
it('Should be an instance of itself', function () {
910
assert(loader instanceof Loader);
1011
});
1112

12-
it('Should support the full registry API', function() {
13+
it('Should support the full registry API', function () {
1314
assert(loader.registry);
1415

1516
loader.registry.set('asdf', new ModuleNamespace({ asdf: 'asdf' }));
@@ -39,7 +40,7 @@ describe('Loader Polyfill API', function() {
3940
assert.equal(loader.registry.has('asdf'), false);
4041
});
4142

42-
it('Should support Module construction, evaluation and mutation', function() {
43+
it('Should support Module construction, evaluation and mutation', function () {
4344
//var evaluated = false;
4445
var mutator = { a: 'asdf' };
4546
var module = new ModuleNamespace(mutator);/*, function() {
@@ -55,4 +56,31 @@ describe('Loader Polyfill API', function() {
5556

5657
assert.equal(module.a, 'b');
5758
});
59+
60+
it('Should throw if instantiate hook doesnt instantiate', function () {
61+
loader[loader.constructor.resolve] = function (x) {
62+
return x;
63+
};
64+
65+
return loader.import('x')
66+
.catch(function (e) {
67+
assert.equal(e.toString().indexOf('Error: Module instantiation did not return a valid namespace object.'), 0);
68+
});
69+
});
70+
});
71+
72+
73+
describe('Register Loader API', function () {
74+
var loader = new RegisterLoader();
75+
76+
loader[RegisterLoader.resolve] = function (x) {
77+
return x;
78+
};
79+
80+
it('Should throw if instantiate doesnt instantiate', function () {
81+
return loader.import('x')
82+
.catch(function (e) {
83+
assert.equal(e.toString().indexOf('Error: Module instantiation did not call an anonymous or correctly named System.register.'), 0);
84+
});
85+
});
5886
});

0 commit comments

Comments
 (0)