-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmodule-namespace.js
320 lines (304 loc) · 20.5 KB
/
module-namespace.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// ES6 Module syntax tests -- verifies syntax of import and export statements
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
function testModuleScript(source, message, shouldFail = false) {
let testfunc = () => testRunner.LoadModule(source, undefined, shouldFail);
if (shouldFail) {
let caught = false;
// We can't use assert.throws here because the SyntaxError used to construct the thrown error
// is from a different context so it won't be strictly equal to our SyntaxError.
try {
testfunc();
} catch(e) {
caught = true;
// Compare toString output of SyntaxError and other context SyntaxError constructor.
assert.areEqual(e.constructor.toString(), SyntaxError.toString(), message);
}
assert.isTrue(caught, `Expected error not thrown: ${message}`);
} else {
assert.doesNotThrow(testfunc, message);
}
}
var tests = [
{
name: "Issue3249: Namespace object's property attributes",
body: function () {
testModuleScript(`
function verifyPropertyDesc(obj, prop, desc, propName) {
var actualDesc = Object.getOwnPropertyDescriptor(obj, prop);
if (typeof propName === "undefined") { propName = prop; }
assert.areEqual(desc.configurable, actualDesc.configurable, propName+"'s attribute: configurable");
assert.areEqual(desc.enumerable, actualDesc.enumerable, propName+"'s attribute: enumerable");
assert.areEqual(desc.writable, actualDesc.writable, propName+"'s attribute: writable");
}
import * as foo from "ValidExportStatements.js";
assert.areEqual("Module", foo[Symbol.toStringTag], "@@toStringTag is the String value'Module'");
verifyPropertyDesc(foo, Symbol.toStringTag, {configurable:false, enumerable: false, writable: false}, "Symbol.toStringTag");
verifyPropertyDesc(foo, "default", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var7", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var6", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var4", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var3", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "var1", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "foo4", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "bar2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "foobar", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "foo3", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "baz2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "foo2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "baz", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "bar", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "foo", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "const6", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "const5", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "const4", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "const3", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "const2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let7", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let6", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let5", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let4", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "let1", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "cl2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "cl1", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "gn2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "gn1", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "fn2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo, "fn1", {configurable:false, enumerable: true, writable: true});
import * as foo1 from "ValidReExportStatements.js";
verifyPropertyDesc(foo1, "foo", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo1, "bar", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo1, "baz", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo1, "foo2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo1, "bar2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo1, "foo3", {configurable:false, enumerable: true, writable: true});
import * as foo2 from "ModuleComplexReexports.js";
verifyPropertyDesc(foo2, "ModuleComplexReexports_foo", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "bar2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "localfoo2", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "bar", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "localfoo", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "baz", {configurable:false, enumerable: true, writable: true});
verifyPropertyDesc(foo2, "foo", {configurable:false, enumerable: true, writable: true});
`, '', false);
}
},
{
name: "Basic import namespace",
body: function () {
testModuleScript(`
import * as foo from "ValidExportStatements.js";
assert.areEqual("default", foo.default, "default");
assert.areEqual(undefined, foo.var7, "var7");
assert.areEqual(undefined, foo.var6, "var6");
assert.areEqual(undefined, foo.var5, "var5");
assert.areEqual(undefined, foo.var4, "var4");
assert.areEqual(5, foo.var3, "var3");
assert.areEqual(undefined, foo.var2, "var2");
assert.areEqual("string", foo.var1, "var1");
assert.areEqual("function foo() { }", foo.foo4.toString(), "foo4");
assert.areEqual("class bar { }", foo.bar2.toString(), "bar2");
assert.areEqual("function foobar() { }", foo.foobar.toString(), "foobar");
assert.areEqual("function foo() { }", foo.foo3.toString(), "foo3");
assert.areEqual("function* baz() { }", foo.baz2.toString(), "baz2");
assert.areEqual("function foo() { }", foo.foo2.toString(), "foo2");
assert.areEqual("function* baz() { }", foo.baz.toString(), "baz");
assert.areEqual("class bar { }", foo.bar.toString(), "bar");
assert.areEqual("function foo() { }", foo.foo.toString(), "foo");
assert.areEqual([], foo.const6, "const6");
assert.areEqual({}, foo.const5, "const5");
assert.areEqual(4, foo.const4, "const4");
assert.areEqual(3, foo.const3, "const3");
assert.areEqual("str", foo.const2, "const2");
assert.areEqual([], foo.let7, "let7");
assert.areEqual({}, foo.let6, "let6");
assert.areEqual(undefined, foo.let5, "let5");
assert.areEqual(undefined, foo.let4, "let4");
assert.areEqual(undefined, foo.let3, "let3");
assert.areEqual(2, foo.let2, "let2");
assert.areEqual(undefined, foo.let1, "let1");
assert.areEqual("class cl2 { }", foo.cl2.toString(), "cl2");
assert.areEqual("class cl1 { }", foo.cl1.toString(), "cl1");
assert.areEqual("function* gn2 () { }", foo.gn2.toString(), "gn2");
assert.areEqual("function* gn1 () { }", foo.gn1.toString(), "gn1");
assert.areEqual("function fn2 () { }", foo.fn2.toString(), "fn2");
assert.areEqual("function fn1 () { }", foo.fn1.toString(), "fn1");
`, '', false);
}
},
{
name: "import namespace with verification",
body: function () {
testModuleScript(`
import * as foo from "moduleExport1.js";
assert.areEqual("default", foo.default, "default");
assert.areEqual(undefined, foo.var7, "var7");
assert.areEqual(undefined, foo.var6, "var6");
assert.areEqual(undefined, foo.var5, "var5");
assert.areEqual(undefined, foo.var4, "var4");
assert.areEqual(5, foo.var3, "var3");
assert.areEqual(undefined, foo.var2, "var2");
assert.areEqual("string", foo.var1, "var1");
assert.areEqual("function foo() { }", foo.foo4.toString(), "foo4");
assert.areEqual("class bar { }", foo.bar2.toString(), "bar2");
assert.areEqual("function foobar() { }", foo.foobar.toString(), "foobar");
assert.areEqual("function foo() { }", foo.foo3.toString(), "foo3");
assert.areEqual("function* baz() { }", foo.baz2.toString(), "baz2");
assert.areEqual("function foo() { }", foo.foo2.toString(), "foo2");
assert.areEqual("function* baz() { }", foo.baz.toString(), "baz");
assert.areEqual("class bar { }", foo.bar.toString(), "bar");
assert.areEqual("function foo() { }", foo.foo.toString(), "foo");
assert.areEqual([], foo.const6, "const6");
assert.areEqual({}, foo.const5, "const5");
assert.areEqual(4, foo.const4, "const4");
assert.areEqual(3, foo.const3, "const3");
assert.areEqual("str", foo.const2, "const2");
assert.areEqual([], foo.let7, "let7");
assert.areEqual({}, foo.let6, "let6");
assert.areEqual(undefined, foo.let5, "let5");
assert.areEqual(undefined, foo.let4, "let4");
assert.areEqual(undefined, foo.let3, "let3");
assert.areEqual(2, foo.let2, "let2");
assert.areEqual(undefined, foo.let1, "let1");
assert.areEqual("class cl2 { }", foo.cl2.toString(), "cl2");
assert.areEqual("class cl1 { }", foo.cl1.toString(), "cl1");
assert.areEqual("function* gn2 () { }", foo.gn2.toString(), "gn2");
assert.areEqual("function* gn1 () { }", foo.gn1.toString(), "gn1");
assert.areEqual("function fn2 () { }", foo.fn2.toString(), "fn2");
assert.areEqual("function fn1 () { }", foo.fn1.toString(), "fn1");
foo.verifyNamespace(foo);
foo.changeContext();
foo.verifyNamespace(foo);
`, '', false);
}
},
{
name: "reexport only",
body: function () {
testModuleScript(`
import * as foo from "ValidReExportStatements.js";
assert.areEqual("function foo() { }", foo.foo.toString(), "foo");
assert.areEqual("class bar { }", foo.bar.toString(), "bar");
assert.areEqual("function* baz() { }", foo.baz.toString(), "baz");
assert.areEqual("function foo() { }", foo.foo2.toString(), "foo2");
assert.areEqual("class bar { }", foo.bar2.toString(), "bar2");
assert.areEqual("function foo() { }", foo.foo3.toString(), "foo3");
`, '', false);
}
},
{
name: "complex reexport",
body: function () {
testModuleScript(`import * as fooComplex from "ModuleComplexReexports.js";
assert.areEqual("function bar() { return 'bar'; }", fooComplex.ModuleComplexReexports_foo.toString(), "ModuleComplexReexports_foo");
assert.areEqual(undefined, fooComplex.switch, "switch");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.bar2.toString(), "bar2");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.localfoo2.toString(), "localfoo2");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.bar.toString(), "bar");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.localfoo.toString(), "localfoo");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.baz.toString(), "baz");
assert.areEqual("function foo() { return 'foo'; }", fooComplex.foo.toString(), "foo");
`, '', false);
}
},
{
name: "namespace as prototype",
body: function () {
testModuleScript(`import * as foo from "ValidExportStatements.js";
var childObj = Object.create(foo);
assert.areEqual("default", childObj.default, "default");
assert.areEqual(undefined, childObj.var7, "var7");
assert.areEqual(undefined, childObj.var6, "var6");
assert.areEqual(undefined, childObj.var5, "var5");
assert.areEqual(undefined, childObj.var4, "var4");
assert.areEqual(5, childObj.var3, "var3");
assert.areEqual(undefined, childObj.var2, "var2");
assert.areEqual("string", childObj.var1, "var1");
assert.areEqual("function foo() { }", childObj.foo4.toString(), "foo4");
assert.areEqual("class bar { }", childObj.bar2.toString(), "bar2");
assert.areEqual("function foobar() { }", childObj.foobar.toString(), "foobar");
assert.areEqual("function foo() { }", childObj.foo3.toString(), "foo3");
assert.areEqual("function* baz() { }", childObj.baz2.toString(), "baz2");
assert.areEqual("function foo() { }", childObj.foo2.toString(), "foo2");
assert.areEqual("function* baz() { }", childObj.baz.toString(), "baz");
assert.areEqual("class bar { }", childObj.bar.toString(), "bar");
assert.areEqual("function foo() { }", childObj.foo.toString(), "foo");
assert.areEqual([], childObj.const6, "const6");
assert.areEqual({}, childObj.const5, "const5");
assert.areEqual(4, childObj.const4, "const4");
assert.areEqual(3, childObj.const3, "const3");
assert.areEqual("str", childObj.const2, "const2");
assert.areEqual([], childObj.let7, "let7");
assert.areEqual({}, childObj.let6, "let6");
assert.areEqual(undefined, childObj.let5, "let5");
assert.areEqual(undefined, childObj.let4, "let4");
assert.areEqual(undefined, childObj.let3, "let3");
assert.areEqual(2, childObj.let2, "let2");
assert.areEqual(undefined, childObj.let1, "let1");
assert.areEqual("class cl2 { }", childObj.cl2.toString(), "cl2");
assert.areEqual("class cl1 { }", childObj.cl1.toString(), "cl1");
assert.areEqual("function* gn2 () { }", childObj.gn2.toString(), "gn2");
assert.areEqual("function* gn1 () { }", childObj.gn1.toString(), "gn1");
assert.areEqual("function fn2 () { }", childObj.fn2.toString(), "fn2");
assert.areEqual("function fn1 () { }", childObj.fn1.toString(), "fn1");
`, '', false);
}
},
{
name: "namespace internal operations",
body: function () {
let functionBody =
`import * as foo from "ValidExportStatements.js";
assert.areEqual(null, Object.getPrototypeOf(foo), 'namespace prototype is null');
assert.areEqual(false, Object.isExtensible(foo), 'namespace is not extensible');
assert.areEqual(false, Reflect.set(foo, "non_existing", 20), '[[set]] returns false ');
assert.areEqual(undefined, foo.non_existing, 'namespace object is immutable');
assert.areEqual(false, Reflect.set(foo, "4", 20), 'cannot set item in namespace obect');
assert.areEqual(undefined, foo[4], 'cannot export item in namespace obect');
assert.areEqual(false, Reflect.deleteProperty(foo, "var1"), 'cannot delete export in namespace obect');
assert.areEqual(true, Reflect.deleteProperty(foo, "nonevar"), 'cannot delete export in namespace obect');
assert.areEqual(undefined, foo[6], 'cannot get item in namespace obect');
assert.areEqual(false, Reflect.set(foo, Symbol.species, 20), 'no species property');
assert.areEqual(undefined, foo[Symbol.species], 'namespace is not contructor');
assert.areEqual("Module", foo[Symbol.toStringTag], 'namespace toStringTag');
assert.areEqual("[object Module]", Object.prototype.toString.call(foo), 'Object.prototype.toString uses the module namespace @@toStringTag value');
var symbols = Object.getOwnPropertySymbols(foo);
assert.areEqual(1, symbols.length, "one symbol");
assert.areEqual(symbols[0], Symbol.toStringTag, "first symbol is toStringTag");
assert.isFalse(Object.prototype.hasOwnProperty.call(foo, Symbol.iterator), "Module namespace object should not have Symbol.iterator property");
assert.throws( function() {Object.setPrototypeOf(foo, Object)}, TypeError, 'Cannot create property for a non-extensible object');
assert.areEqual(true, Reflect.preventExtensions(foo), '[[PreventExtensions]] for namespace object returns true');`;
testModuleScript(functionBody, "Test importing as different binding identifiers", false);
}
},
{
name: "Issue3246: namespace property names are sorted",
body: function () {
let functionBody =
`
import * as ns from 'ValidExportStatements2.js';
var p = new Proxy(ns, {});
var names = ["sym0","default","$","$$","_","\u03bb","aa","A","a","zz","z","\u03bc","Z","za","__","az","\u03c0"].sort();
var verifyNamespaceOwnProperty = function(obj, objKind) {
assert.areEqual(names, Object.getOwnPropertyNames(obj), objKind+" getOwnPropertyNames()");
var propDesc = Object.getOwnPropertyDescriptors(obj);
assert.areEqual('{"value":"Module","writable":false,"enumerable":false,"configurable":false}', JSON.stringify(propDesc[Symbol.toStringTag]),
objKind+" getOwnPropertyDescriptors() @@toStringTag");
assert.areEqual(names, Object.keys(propDesc), "ModuleNamespace", objKind+" getOwnPropertyDescriptors()");
};
verifyNamespaceOwnProperty(ns, "ModuleNamespace");
verifyNamespaceOwnProperty(p, "Proxy");
var propEn = [];
for (var k in ns) { propEn.push(k); }
assert.areEqual(names, propEn, "ModuleNamespace enumerator");
`;
testModuleScript(functionBody, "Test importing as different binding identifiers", false);
}
},
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });