1 add_task(function test_Cu_import_shim_first() {
2 // Load and cache with shim.
5 const global = Components.utils.import(
6 "resource://test/esmified-1.jsm",
9 Assert.equal(global.loadCount, 1);
10 Assert.equal(global.obj.value, 10);
11 Assert.equal(exports.loadCount, 1);
12 Assert.equal(exports.obj.value, 10);
13 Assert.ok(exports.obj === global.obj);
15 const ns = ChromeUtils.importESModule("resource://test/esmified-1.sys.mjs");
16 Assert.equal(ns.loadCount, 1);
17 Assert.equal(ns.obj.value, 10);
18 Assert.ok(ns.obj === global.obj);
21 const global2 = Components.utils.import(
22 "resource://test/esmified-1.jsm", exports2
24 Assert.equal(global2.loadCount, 1);
25 Assert.equal(global2.obj.value, 10);
26 Assert.equal(exports2.loadCount, 1);
27 Assert.equal(exports2.obj.value, 10);
28 Assert.ok(exports2.obj === global2.obj);
29 Assert.ok(exports2.obj === global.obj);
31 // Also test with *.js extension.
33 const global3 = Components.utils.import(
34 "resource://test/esmified-1.js", exports3
36 Assert.equal(global3.loadCount, 1);
37 Assert.equal(global3.obj.value, 10);
38 Assert.equal(exports3.loadCount, 1);
39 Assert.equal(exports3.obj.value, 10);
40 Assert.ok(exports3.obj === global3.obj);
41 Assert.ok(exports3.obj === global.obj);
43 // Also test with *.jsm.js extension.
45 const global4 = Components.utils.import(
46 "resource://test/esmified-1.js", exports4
48 Assert.equal(global4.loadCount, 1);
49 Assert.equal(global4.obj.value, 10);
50 Assert.equal(exports4.loadCount, 1);
51 Assert.equal(exports4.obj.value, 10);
52 Assert.ok(exports4.obj === global4.obj);
53 Assert.ok(exports4.obj === global.obj);
56 add_task(function test_Cu_import_no_shim_first() {
57 // Load and cache with importESModule.
59 const ns = ChromeUtils.importESModule("resource://test/esmified-2.sys.mjs");
60 Assert.equal(ns.loadCount, 1);
61 Assert.equal(ns.obj.value, 10);
64 const global = Components.utils.import(
65 "resource://test/esmified-2.jsm", exports
67 Assert.equal(global.loadCount, 1);
68 Assert.equal(global.obj.value, 10);
69 Assert.equal(exports.loadCount, 1);
70 Assert.equal(exports.obj.value, 10);
71 Assert.ok(exports.obj === global.obj);
72 Assert.ok(ns.obj === global.obj);
74 const ns2 = ChromeUtils.importESModule("resource://test/esmified-2.sys.mjs");
75 Assert.equal(ns2.loadCount, 1);
76 Assert.equal(ns2.obj.value, 10);
79 add_task(function test_ChromeUtils_import_shim_first() {
80 // Load and cache with shim.
83 const global = ChromeUtils.import(
84 "resource://test/esmified-3.jsm", exports
86 Assert.equal(global.loadCount, 1);
87 Assert.equal(global.obj.value, 10);
88 Assert.equal(exports.loadCount, 1);
89 Assert.equal(exports.obj.value, 10);
90 Assert.ok(exports.obj === global.obj);
92 const ns = ChromeUtils.importESModule("resource://test/esmified-3.sys.mjs");
93 Assert.equal(ns.loadCount, 1);
94 Assert.equal(ns.obj.value, 10);
95 Assert.ok(ns.obj === global.obj);
98 const global2 = ChromeUtils.import(
99 "resource://test/esmified-3.jsm", exports2
101 Assert.equal(global2.loadCount, 1);
102 Assert.equal(global2.obj.value, 10);
103 Assert.equal(exports2.loadCount, 1);
104 Assert.equal(exports2.obj.value, 10);
105 Assert.ok(exports2.obj === global2.obj);
106 Assert.ok(exports2.obj === global.obj);
109 add_task(function test_ChromeUtils_import_no_shim_first() {
110 // Load and cache with importESModule.
112 const ns = ChromeUtils.importESModule("resource://test/esmified-4.sys.mjs");
113 Assert.equal(ns.loadCount, 1);
114 Assert.equal(ns.obj.value, 10);
117 const global = ChromeUtils.import(
118 "resource://test/esmified-4.jsm", exports
120 Assert.equal(global.loadCount, 1);
121 Assert.equal(global.obj.value, 10);
122 Assert.equal(exports.loadCount, 1);
123 Assert.equal(exports.obj.value, 10);
124 Assert.ok(exports.obj === global.obj);
125 Assert.ok(ns.obj === global.obj);
127 const ns2 = ChromeUtils.importESModule("resource://test/esmified-4.sys.mjs");
128 Assert.equal(ns2.loadCount, 1);
129 Assert.equal(ns2.obj.value, 10);
132 add_task(function test_ChromeUtils_import_not_exported_no_shim_JSM() {
133 // `exports` properties for not-ESM-ified case.
135 const exports = ChromeUtils.import(
136 "resource://test/not-esmified-not-exported.jsm"
139 Assert.equal(exports.exportedVar, "exported var");
140 Assert.equal(exports.exportedFunction(), "exported function");
141 Assert.equal(exports.exportedLet, "exported let");
142 Assert.equal(exports.exportedConst, "exported const");
143 Assert.equal(exports.notExportedVar, undefined);
144 Assert.equal(exports.notExportedFunction, undefined);
145 Assert.equal(exports.notExportedLet, undefined);
146 Assert.equal(exports.notExportedConst, undefined);
149 add_task(function test_ChromeUtils_import_not_exported_shim() {
150 // `exports` properties for shim case.
152 const exports = ChromeUtils.import(
153 "resource://test/esmified-not-exported.jsm"
156 Assert.equal(exports.exportedVar, "exported var");
157 Assert.equal(exports.exportedFunction(), "exported function");
158 Assert.equal(exports.exportedLet, "exported let");
159 Assert.equal(exports.exportedConst, "exported const");
160 Assert.equal(exports.notExportedVar, undefined);
161 Assert.equal(exports.notExportedFunction, undefined);
162 Assert.equal(exports.notExportedLet, undefined);
163 Assert.equal(exports.notExportedConst, undefined);
166 add_task(function test_ChromeUtils_import_not_exported_no_shim_ESM() {
167 // `exports` properties for ESM-ified case.
169 const exports = ChromeUtils.importESModule(
170 "resource://test/esmified-not-exported.sys.mjs"
173 Assert.equal(exports.exportedVar, "exported var");
174 Assert.equal(exports.exportedFunction(), "exported function");
175 Assert.equal(exports.exportedLet, "exported let");
176 Assert.equal(exports.exportedConst, "exported const");
177 Assert.equal(exports.notExportedVar, undefined);
178 Assert.equal(exports.notExportedFunction, undefined);
179 Assert.equal(exports.notExportedLet, undefined);
180 Assert.equal(exports.notExportedConst, undefined);
183 function testReadProxyOps(global, expectedNames, expectedDesc) {
184 expectedNames.sort();
187 const names = Object.keys(global).sort();
188 Assert.equal(JSON.stringify(names), JSON.stringify(expectedNames),
192 for (const name of expectedNames) {
193 Assert.ok(name in global, `has for ${name}`);
196 // getOwnPropertyDescriptor
197 for (const name of expectedNames) {
198 const desc = Object.getOwnPropertyDescriptor(global, name);
199 Assert.equal(desc.value, global[name]);
200 Assert.equal(desc.writable, expectedDesc.writable,
201 `writable for ${name}`);
202 Assert.equal(desc.enumerable, expectedDesc.enumerable,
203 `enumerable for ${name}`);
204 Assert.equal(desc.configurable, expectedDesc.configurable,
205 `configurable for ${name}`);
209 function testWriteProxyOps(global, expectedNames) {
211 for (const name of expectedNames) {
212 const before = global[name];
214 Assert.equal(global[name], before, `value after set for ${name}`);
218 for (const name of expectedNames) {
219 const before = global[name];
220 Assert.ok(!(delete global[name]), `delete result for ${name}`);
221 Assert.equal(global[name], before, `value after delete for ${name}`);
225 add_task(function test_Cu_import_not_exported_no_shim_JSM() {
226 // `exports` and `global` properties for not-ESM-ified case.
227 // Not-exported variables should be visible in `global`.
230 const global = Components.utils.import(
231 "resource://test/not-esmified-not-exported.jsm",
235 Assert.equal(global.exportedVar, "exported var");
236 Assert.equal(global.exportedFunction(), "exported function");
237 Assert.equal(global.exportedLet, "exported let");
238 Assert.equal(global.exportedConst, "exported const");
239 Assert.equal(global.notExportedVar, "not exported var");
240 Assert.equal(global.notExportedFunction(), "not exported function");
241 Assert.equal(global.notExportedLet, "not exported let");
242 Assert.equal(global.notExportedConst, "not exported const");
244 const expectedNames = [
251 "notExportedFunction",
256 testReadProxyOps(global, expectedNames, {
261 testWriteProxyOps(global, expectedNames);
263 Assert.equal(exports.exportedVar, "exported var");
264 Assert.equal(exports.exportedFunction(), "exported function");
265 Assert.equal(exports.exportedLet, "exported let");
266 Assert.equal(exports.exportedConst, "exported const");
267 Assert.equal(exports.notExportedVar, undefined);
268 Assert.equal(exports.notExportedFunction, undefined);
269 Assert.equal(exports.notExportedLet, undefined);
270 Assert.equal(exports.notExportedConst, undefined);
273 add_task(function test_Cu_import_not_exported_shim() {
274 // `exports` and `global` properties for shim case.
275 // Not-exported variables should be visible in global.
278 const global = Components.utils.import(
279 "resource://test/esmified-not-exported.jsm",
283 Assert.equal(global.exportedVar, "exported var");
284 Assert.equal(global.exportedFunction(), "exported function");
285 Assert.equal(global.exportedLet, "exported let");
286 Assert.equal(global.exportedConst, "exported const");
288 Assert.equal(global.notExportedVar, "not exported var");
289 Assert.equal(global.notExportedFunction(), "not exported function");
290 Assert.equal(global.notExportedLet, "not exported let");
291 Assert.equal(global.notExportedConst, "not exported const");
293 const expectedNames = [
299 "notExportedFunction",
304 testReadProxyOps(global, expectedNames, {
309 testWriteProxyOps(global, expectedNames);
311 Assert.equal(exports.exportedVar, "exported var");
312 Assert.equal(exports.exportedFunction(), "exported function");
313 Assert.equal(exports.exportedLet, "exported let");
314 Assert.equal(exports.exportedConst, "exported const");
315 Assert.equal(exports.notExportedVar, undefined);
316 Assert.equal(exports.notExportedFunction, undefined);
317 Assert.equal(exports.notExportedLet, undefined);
318 Assert.equal(exports.notExportedConst, undefined);
320 const desc = Object.getOwnPropertyDescriptor(global, "*namespace*");
321 Assert.ok(!desc, `*namespace* special binding should not be exposed`);
322 Assert.equal("*namespace*" in global, false,
323 `*namespace* special binding should not be exposed`);
324 Assert.equal(global["*namespace*"], undefined,
325 `*namespace* special binding should not be exposed`);
328 add_task(function test_Cu_isModuleLoaded_shim() {
329 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), false);
330 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.js"), false);
331 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm.js"), false);
332 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), false);
333 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.sys.mjs"), false);
334 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.sys.mjs"), false);
336 Cu.import("resource://test/esmified-5.jsm", {});
338 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), true);
339 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.js"), true);
340 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm.js"), true);
341 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), true);
343 // This is false because Cu.isModuleLoaded does not support ESM directly
345 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.sys.mjs"), false);
346 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.sys.mjs"), false);
349 add_task(function test_Cu_isModuleLoaded_no_shim() {
350 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), false);
351 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.js"), false);
352 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm.js"), false);
353 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), false);
354 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.js"), false);
355 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm.js"), false);
356 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.sys.mjs"), false);
357 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.sys.mjs"), false);
359 ChromeUtils.importESModule("resource://test/esmified-6.sys.mjs");
361 // Regardless of whether the ESM is loaded by shim or not,
362 // query that accesses the ESM-ified module returns the existence of
364 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), true);
365 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.js"), true);
366 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm.js"), true);
367 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), true);
369 // This is false because shim always use *.jsm.
370 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.js"), false);
371 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm.js"), false);
373 // This is false because Cu.isModuleLoaded does not support ESM directly
375 Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.sys.mjs"), false);
376 Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.sys.mjs"), false);