summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_import_shim.js
blob: 3e265414f88258acf2f6fbe1f20217fe261fd5aa (plain)
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
add_task(function test_Cu_import_shim_first() {
  // Load and cache with shim.

  const exports = {};
  const global = Components.utils.import(
    "resource://test/esmified-1.jsm",
    exports
  );
  Assert.equal(global.loadCount, 1);
  Assert.equal(global.obj.value, 10);
  Assert.equal(exports.loadCount, 1);
  Assert.equal(exports.obj.value, 10);
  Assert.ok(exports.obj === global.obj);

  const ns = ChromeUtils.importESModule("resource://test/esmified-1.sys.mjs");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns.obj.value, 10);
  Assert.ok(ns.obj === global.obj);

  const exports2 = {};
  const global2 = Components.utils.import(
    "resource://test/esmified-1.jsm", exports2
  );
  Assert.equal(global2.loadCount, 1);
  Assert.equal(global2.obj.value, 10);
  Assert.equal(exports2.loadCount, 1);
  Assert.equal(exports2.obj.value, 10);
  Assert.ok(exports2.obj === global2.obj);
  Assert.ok(exports2.obj === global.obj);

  // Also test with *.js extension.
  const exports3 = {};
  const global3 = Components.utils.import(
    "resource://test/esmified-1.js", exports3
  );
  Assert.equal(global3.loadCount, 1);
  Assert.equal(global3.obj.value, 10);
  Assert.equal(exports3.loadCount, 1);
  Assert.equal(exports3.obj.value, 10);
  Assert.ok(exports3.obj === global3.obj);
  Assert.ok(exports3.obj === global.obj);

  // Also test with *.jsm.js extension.
  const exports4 = {};
  const global4 = Components.utils.import(
    "resource://test/esmified-1.js", exports4
  );
  Assert.equal(global4.loadCount, 1);
  Assert.equal(global4.obj.value, 10);
  Assert.equal(exports4.loadCount, 1);
  Assert.equal(exports4.obj.value, 10);
  Assert.ok(exports4.obj === global4.obj);
  Assert.ok(exports4.obj === global.obj);
});

add_task(function test_Cu_import_no_shim_first() {
  // Load and cache with importESModule.

  const ns = ChromeUtils.importESModule("resource://test/esmified-2.sys.mjs");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns.obj.value, 10);

  const exports = {};
  const global = Components.utils.import(
    "resource://test/esmified-2.jsm", exports
  );
  Assert.equal(global.loadCount, 1);
  Assert.equal(global.obj.value, 10);
  Assert.equal(exports.loadCount, 1);
  Assert.equal(exports.obj.value, 10);
  Assert.ok(exports.obj === global.obj);
  Assert.ok(ns.obj === global.obj);

  const ns2 = ChromeUtils.importESModule("resource://test/esmified-2.sys.mjs");
  Assert.equal(ns2.loadCount, 1);
  Assert.equal(ns2.obj.value, 10);
});

add_task(function test_ChromeUtils_import_shim_first() {
  // Load and cache with shim.

  const exports = {};
  const global = ChromeUtils.import(
    "resource://test/esmified-3.jsm", exports
  );
  Assert.equal(global.loadCount, 1);
  Assert.equal(global.obj.value, 10);
  Assert.equal(exports.loadCount, 1);
  Assert.equal(exports.obj.value, 10);
  Assert.ok(exports.obj === global.obj);

  const ns = ChromeUtils.importESModule("resource://test/esmified-3.sys.mjs");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns.obj.value, 10);
  Assert.ok(ns.obj === global.obj);

  const exports2 = {};
  const global2 = ChromeUtils.import(
    "resource://test/esmified-3.jsm", exports2
  );
  Assert.equal(global2.loadCount, 1);
  Assert.equal(global2.obj.value, 10);
  Assert.equal(exports2.loadCount, 1);
  Assert.equal(exports2.obj.value, 10);
  Assert.ok(exports2.obj === global2.obj);
  Assert.ok(exports2.obj === global.obj);
});

add_task(function test_ChromeUtils_import_no_shim_first() {
  // Load and cache with importESModule.

  const ns = ChromeUtils.importESModule("resource://test/esmified-4.sys.mjs");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns.obj.value, 10);

  const exports = {};
  const global = ChromeUtils.import(
    "resource://test/esmified-4.jsm", exports
  );
  Assert.equal(global.loadCount, 1);
  Assert.equal(global.obj.value, 10);
  Assert.equal(exports.loadCount, 1);
  Assert.equal(exports.obj.value, 10);
  Assert.ok(exports.obj === global.obj);
  Assert.ok(ns.obj === global.obj);

  const ns2 = ChromeUtils.importESModule("resource://test/esmified-4.sys.mjs");
  Assert.equal(ns2.loadCount, 1);
  Assert.equal(ns2.obj.value, 10);
});

add_task(function test_ChromeUtils_import_not_exported_no_shim_JSM() {
  // `exports` properties for not-ESM-ified case.

  const exports = ChromeUtils.import(
    "resource://test/not-esmified-not-exported.jsm"
  );

  Assert.equal(exports.exportedVar, "exported var");
  Assert.equal(exports.exportedFunction(), "exported function");
  Assert.equal(exports.exportedLet, "exported let");
  Assert.equal(exports.exportedConst, "exported const");
  Assert.equal(exports.notExportedVar, undefined);
  Assert.equal(exports.notExportedFunction, undefined);
  Assert.equal(exports.notExportedLet, undefined);
  Assert.equal(exports.notExportedConst, undefined);
});

add_task(function test_ChromeUtils_import_not_exported_shim() {
  // `exports` properties for shim case.

  const exports = ChromeUtils.import(
    "resource://test/esmified-not-exported.jsm"
  );

  Assert.equal(exports.exportedVar, "exported var");
  Assert.equal(exports.exportedFunction(), "exported function");
  Assert.equal(exports.exportedLet, "exported let");
  Assert.equal(exports.exportedConst, "exported const");
  Assert.equal(exports.notExportedVar, undefined);
  Assert.equal(exports.notExportedFunction, undefined);
  Assert.equal(exports.notExportedLet, undefined);
  Assert.equal(exports.notExportedConst, undefined);
});

add_task(function test_ChromeUtils_import_not_exported_no_shim_ESM() {
  // `exports` properties for ESM-ified case.

  const exports = ChromeUtils.importESModule(
    "resource://test/esmified-not-exported.sys.mjs"
  );

  Assert.equal(exports.exportedVar, "exported var");
  Assert.equal(exports.exportedFunction(), "exported function");
  Assert.equal(exports.exportedLet, "exported let");
  Assert.equal(exports.exportedConst, "exported const");
  Assert.equal(exports.notExportedVar, undefined);
  Assert.equal(exports.notExportedFunction, undefined);
  Assert.equal(exports.notExportedLet, undefined);
  Assert.equal(exports.notExportedConst, undefined);
});

function testReadProxyOps(global, expectedNames, expectedDesc) {
  expectedNames.sort();

  // enumerate
  const names = Object.keys(global).sort();
  Assert.equal(JSON.stringify(names), JSON.stringify(expectedNames),
              `enumerate`);

  // has
  for (const name of expectedNames) {
    Assert.ok(name in global, `has for ${name}`);
  }

  // getOwnPropertyDescriptor
  for (const name of expectedNames) {
    const desc = Object.getOwnPropertyDescriptor(global, name);
    Assert.equal(desc.value, global[name]);
    Assert.equal(desc.writable, expectedDesc.writable,
                 `writable for ${name}`);
    Assert.equal(desc.enumerable, expectedDesc.enumerable,
                 `enumerable for ${name}`);
    Assert.equal(desc.configurable, expectedDesc.configurable,
                 `configurable for ${name}`);
  }
}

function testWriteProxyOps(global, expectedNames) {
  // set: no-op
  for (const name of expectedNames) {
    const before = global[name];
    global[name] = -1;
    Assert.equal(global[name], before, `value after set for ${name}`);
  }

  // delete: no-op
  for (const name of expectedNames) {
    const before = global[name];
    Assert.ok(!(delete global[name]), `delete result for ${name}`);
    Assert.equal(global[name], before, `value after delete for ${name}`);
  }
}

add_task(function test_Cu_import_not_exported_no_shim_JSM() {
  // `exports` and `global` properties for not-ESM-ified case.
  // Not-exported variables should be visible in `global`.

  const exports = {};
  const global = Components.utils.import(
    "resource://test/not-esmified-not-exported.jsm",
    exports
  );

  Assert.equal(global.exportedVar, "exported var");
  Assert.equal(global.exportedFunction(), "exported function");
  Assert.equal(global.exportedLet, "exported let");
  Assert.equal(global.exportedConst, "exported const");
  Assert.equal(global.notExportedVar, "not exported var");
  Assert.equal(global.notExportedFunction(), "not exported function");
  Assert.equal(global.notExportedLet, "not exported let");
  Assert.equal(global.notExportedConst, "not exported const");

  const expectedNames = [
    "EXPORTED_SYMBOLS",
    "exportedVar",
    "exportedFunction",
    "exportedLet",
    "exportedConst",
    "notExportedVar",
    "notExportedFunction",
    "notExportedLet",
    "notExportedConst",
  ];

  testReadProxyOps(global, expectedNames, {
    writable: false,
    enumerable: true,
    configurable: false,
  });
  testWriteProxyOps(global, expectedNames);

  Assert.equal(exports.exportedVar, "exported var");
  Assert.equal(exports.exportedFunction(), "exported function");
  Assert.equal(exports.exportedLet, "exported let");
  Assert.equal(exports.exportedConst, "exported const");
  Assert.equal(exports.notExportedVar, undefined);
  Assert.equal(exports.notExportedFunction, undefined);
  Assert.equal(exports.notExportedLet, undefined);
  Assert.equal(exports.notExportedConst, undefined);
});

add_task(function test_Cu_import_not_exported_shim() {
  // `exports` and `global` properties for shim case.
  // Not-exported variables should be visible in global.

  const exports = {};
  const global = Components.utils.import(
    "resource://test/esmified-not-exported.jsm",
    exports
  );

  Assert.equal(global.exportedVar, "exported var");
  Assert.equal(global.exportedFunction(), "exported function");
  Assert.equal(global.exportedLet, "exported let");
  Assert.equal(global.exportedConst, "exported const");

  Assert.equal(global.notExportedVar, "not exported var");
  Assert.equal(global.notExportedFunction(), "not exported function");
  Assert.equal(global.notExportedLet, "not exported let");
  Assert.equal(global.notExportedConst, "not exported const");

  const expectedNames = [
    "exportedVar",
    "exportedFunction",
    "exportedLet",
    "exportedConst",
    "notExportedVar",
    "notExportedFunction",
    "notExportedLet",
    "notExportedConst",
  ];

  testReadProxyOps(global, expectedNames, {
    writable: false,
    enumerable: true,
    configurable: false,
  });
  testWriteProxyOps(global, expectedNames);

  Assert.equal(exports.exportedVar, "exported var");
  Assert.equal(exports.exportedFunction(), "exported function");
  Assert.equal(exports.exportedLet, "exported let");
  Assert.equal(exports.exportedConst, "exported const");
  Assert.equal(exports.notExportedVar, undefined);
  Assert.equal(exports.notExportedFunction, undefined);
  Assert.equal(exports.notExportedLet, undefined);
  Assert.equal(exports.notExportedConst, undefined);

  const desc = Object.getOwnPropertyDescriptor(global, "*namespace*");
  Assert.ok(!desc, `*namespace* special binding should not be exposed`);
  Assert.equal("*namespace*" in global, false,
               `*namespace* special binding should not be exposed`);
  Assert.equal(global["*namespace*"], undefined,
               `*namespace* special binding should not be exposed`);
});

add_task(function test_Cu_isModuleLoaded_shim() {
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.js"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm.js"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.sys.mjs"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.sys.mjs"), false);

  Cu.import("resource://test/esmified-5.jsm", {});

  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), true);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.js"), true);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm.js"), true);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), true);

  // This is false because Cu.isModuleLoaded does not support ESM directly
  // (bug 1768819)
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.sys.mjs"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.sys.mjs"), false);
});

add_task(function test_Cu_isModuleLoaded_no_shim() {
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.js"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm.js"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.js"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm.js"), false);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.sys.mjs"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.sys.mjs"), false);

  ChromeUtils.importESModule("resource://test/esmified-6.sys.mjs");

  // Regardless of whether the ESM is loaded by shim or not,
  // query that accesses the ESM-ified module returns the existence of
  // ESM.
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), true);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.js"), true);
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm.js"), true);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), true);

  // This is false because shim always use *.jsm.
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.js"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm.js"), false);

  // This is false because Cu.isModuleLoaded does not support ESM directly
  // (bug 1768819)
  Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.sys.mjs"), false);
  Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.sys.mjs"), false);
});