File: system-modules.rst

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (383 lines) | stat: -rw-r--r-- 11,603 bytes parent folder | download | duplicates (11)
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
System Modules
==============

Gecko uses a variant of the standard ECMAScript module to implement the
browser internals.

Each system module is a per-process singleton, shared among all consumers in
the process.

Shared System Global
--------------------

The shared system global is a privileged global dedicated for the system
modules.

All system modules are imported into the shared system global (except for
modules loaded into the `DevTools distinct system global`_).

See ``mozJSModuleLoader::CreateLoaderGlobal`` in `mozJSModuleLoader.cpp <https://searchfox.org/mozilla-central/source/js/xpconnect/loader/mozJSModuleLoader.cpp>`_ for details about the global and built-in functions.

Defining a Module
-----------------

The system module is written as a subset of the standard ECMAScript module
(see `Limitations`_ below), and symbols can be exported with the standard
``export`` declarations.

The system module uses the ``.sys.mjs`` filename extension.

.. code:: JavaScript

    // Test.sys.mjs

    export const TestUtils = {
      hello() {
        console.log("hello");
      }
    };

    export function TestFunc() {
      console.log("hi");
    }

System modules can use other extensions than ``.sys.mjs``, but in that case
make sure the right ESLint rules are applied to them.

Importing a Module
------------------

Immediate Import
^^^^^^^^^^^^^^^^

Inside all privileged code, system modules can be imported with
``ChromeUtils.importESModule``.
The system module is imported synchronously, and the namespace object is
returned.

.. note::

    At the script or module top-level, if the module is not going to be
    immediately and unconditionally used, please consider using
    ``ChromeUtils.defineESModuleGetters`` below instead, in order to improve
    the browser startup performance and the window open performance.

.. code:: JavaScript

    // Privileged code.

    const { TestUtils } =
      ChromeUtils.importESModule("resource://gre/modules/Test.sys.mjs");

    TestUtils.hello();

Inside system modules, other system modules can be imported with the regular
``import`` declaration and the dynamic ``import()``.

.. code:: JavaScript

    // System module top-level scope.

    import { TestUtils } from "resource://gre/modules/Test.sys.mjs";

    TestUtils.hello();

.. code:: JavaScript

    // A function inside a system module.

    async function f() {
      const { TestUtils } = await import("resource://gre/modules/Test.sys.mjs");
      TestUtils.hello();
    }

.. note::

    The ``import`` declaration and the dynamic ``import()`` can be used only
    from system modules.
    If the system module is imported from regular modules in some random global
    with these ways, the module is imported into that global instead of
    the shared system global, and it becomes a different instance.

Lazy Import
^^^^^^^^^^^

Modules can be lazily imported with ``ChromeUtils.defineESModuleGetters``.
``ChromeUtils.defineESModuleGetters`` receives a target object, and a object
that defines a map from the exported symbol name to the module URI.
Those symbols are defined on the target object as a lazy getter.
The module is imported on the first access, and the getter is replaced with
a data property with the exported symbol's value.

.. note::

    ``ChromeUtils.defineESModuleGetters`` is applicable only to the exported
    symbol, and not to the namespace object.
    See the next section for how to define the lazy getter for the namespace
    object.

The convention for the target object's name is ``lazy``.

.. code:: JavaScript

    // Privileged code.

    const lazy = {}
    ChromeUtils.defineESModuleGetters(lazy, {
      TestUtils: "resource://gre/modules/Test.sys.mjs",
    });

    function f() {
      // Test.sys.mjs is imported on the first access.
      lazy.TestUtils.hello();
    }

In order to import multiple symbols from the same module, add the corresponding
property with the symbol name and the module URI for each.

.. code:: JavaScript

    // Privileged code.

    const lazy = {}
    ChromeUtils.defineESModuleGetters(lazy, {
      TestUtils: "resource://gre/modules/Test.sys.mjs",
      TestFunc: "resource://gre/modules/Test.sys.mjs",
    });

See `ChromeUtils.webidl <https://searchfox.org/mozilla-central/source/dom/chrome-webidl/ChromeUtils.webidl>`_ for more details.

Using the Namespace Object
--------------------------

The namespace object returned by the ``ChromeUtils.importESModule`` call
can also be directly used.

.. code:: JavaScript

    // Privileged code.

    const TestNS =
      ChromeUtils.importESModule("resource://gre/modules/Test.sys.mjs");

    TestNS.TestUtils.hello();

This is almost same as the following normal ``import`` declaration.

.. code:: JavaScript

    // System module top-level scope.

    import * as TestNS from "resource://gre/modules/Test.sys.mjs";

    TestNS.TestUtils.hello();

or the dynamic import without the destructuring assignment.

.. code:: JavaScript

    async function f() {
      const TestNS = await import("resource://gre/modules/Test.sys.mjs");
      TestNS.TestUtils.hello();
    }


``ChromeUtils.defineESModuleGetters`` does not support directly using
the namespace object.
Possible workaround is to use ``ChromeUtils.defineLazyGetter`` with
``ChromeUtils.importESModule``.

.. code:: JavaScript

    const lazy = {}
    ChromeUtils.defineLazyGetter(lazy, "TestNS", () =>
      ChromeUtils.importESModule("resource://gre/modules/Test.sys.mjs"));

    function f() {
      // Test.sys.mjs is imported on the first access.
      lazy.TestNS.TestUtils.hello();
    }


Importing from Unprivileged Testing Code
----------------------------------------

In unprivileged testing code such as mochitest plain,
``ChromeUtils.importESModule`` is available as
``SpecialPowers.ChromeUtils.importESModule``.

.. code:: JavaScript

    // Mochitest-plain testcase.

    const { TestUtils } =
      SpecialPowers.ChromeUtils.importESModule(
        "resource://gre/modules/Test.sys.mjs"
      );

Importing from C++ Code
-----------------------

C++ code can import ES modules with ``do_ImportESModule`` function.
The exported object should follow the specified XPCOM interface.

.. code:: c++

    nsCOMPtr<nsIUtils> utils = do_ImportESModule(
      "resource://gre/modules/Test.sys.mjs", "Utils");

See `nsImportModule.h <https://searchfox.org/mozilla-central/source/js/xpconnect/loader/nsImportModule.h>`_ for more details.

Lifetime
--------

The shared system global has the almost same lifetime as the process, and the
system modules are never unloaded until the end of the shared system global's
lifetime.

If a module need to be dynamically updated with the same URI, for example with
privileged extensions getting updated, they can add query string to distinguish
different versions.

Lifetime of the Global Variables
--------------------------------

Unlike the classic scripts, the ECMAScript's module's global variables are not
properties of any objects.

If the all strong references to the document goes away, the objects held by
the module global variables are ready to be GCed.  This means, the module global
variables don't have the same lifetime as the module itself.

In privileged scripts, there can be multiple usage of weak-references and
similar things, such as XPCOM ``nsISupportsWeakReference``, or
the window-less ``browser`` element and its content document.

If those objects needs to be kept alive longer, for example, if they need to
have the same lifetime as the module itself, there should be another strong
reference to them.

Possible options for those objects are the following:

  * Export the variable that holds the object
  * Store the object into the exported object's property
  * Close over the variable from the function that's reachable from the exported objects
  * Do not use weak reference

Utility Functions
-----------------

``Cu.isESmoduleLoaded`` is a function to query whether the module is already
imported to the shared system global.

.. code:: JavaScript

    if (Cu.isESmoduleLoaded("resource://gre/modules/Test.sys.mjs")) {
      // ...
    }

``Cu.loadedESModules`` returns a list of URLs of the already-imported modules.
This is only for startup testing purpose, and this shouldn't be used in
the production code.

.. code:: JavaScript

    for (const uri of Cu.loadedESModules) {
      // ...
    }

If ``browser.startup.record`` preference is set to ``true`` at the point of
importing modules, ``Cu.getModuleImportStack`` returns the call stack of the
module import.
This is only for the debugging purpose.

.. code:: JavaScript

    Services.prefs.setBoolPref("browser.startup.record", true);

    const { TestUtils } =
      ChromeUtils.importESModule("resource://gre/modules/Test.sys.mjs");

    console.log(
      Cu.getModuleImportStack("resource://gre/modules/Test.sys.mjs"));

See `xpccomponents.idl <https://searchfox.org/mozilla-central/source/js/xpconnect/idl/xpccomponents.idl>`_ for more details.

Limitations
-----------

Top-level ``await`` is not supported in the system module, due to the
requirement for synchronous loading.

DevTools Distinct System Global
-------------------------------

DevTools-related system modules can be imported into a separate dedicate global,
which is used when debugging the browser.

The target global can be controlled by the ``global`` property of the 2nd
parameter of ``ChromeUtils.importESModule``, or the 3rd parameter of
``ChromeUtils.defineESModuleGetters``.

The ``global`` property defaults to ``"shared"``, which is the shared system
global.
Passing ``"devtools"`` imports the module in the DevTools distinct system
global.

.. code:: JavaScript

    const { TestUtils } =
      ChromeUtils.importESModule("resource://gre/modules/Test.sys.mjs", {
        global: "devtools",
      });

    TestUtils.hello();

.. code:: JavaScript

    const lazy = {}
    ChromeUtils.defineESModuleGetters(lazy, {
      TestUtils: "resource://gre/modules/Test.sys.mjs",
    }, {
      global: "devtools",
    });

If the system module file is shared between both cases, ``"contextual"`` can be
used.  The module is imported into the DevTools distinct system global if the
current global is the DevTools distinct system global.  Otherwise the module
is imported into the shared system global.

See ``ImportESModuleTargetGlobal`` in `ChromeUtils.webidl <https://searchfox.org/mozilla-central/source/dom/chrome-webidl/ChromeUtils.webidl>`_ for more details.

Integration with JSActors
-------------------------

:ref:`JSActors <JSActors>` are implemented with system modules.

See the :ref:`JSActors <JSActors>` document for more details.

Integration with XPCOM Components
---------------------------------

:ref:`XPCOM Components <Defining XPCOM Components>` can be implemented with
system modules, by passing ``esModule`` option.

See the :ref:`XPCOM Components <Defining XPCOM Components>` document for more
details.

Importing into Current Global
-----------------------------

``ChromeUtils.importESModule`` can be used also for importing modules into
the current global, by passing ``{ global: "current" }`` option.
In this case the imported module is not a system module.

See the :ref:`JS Loader APIs <JS Loader APIs>` document for more details.

JSM
---

Prior to the ECMAScript-module-based system modules, Firefox codebase had been
using a Mozilla-specific module system called JSM.

The details around the migration is described in `the migration document <https://docs.google.com/document/d/1cpzIK-BdP7u6RJSar-Z955GV--2Rj8V4x2vl34m36Go/edit?usp=sharing>`_.