Provide a shim for ESM-ified modules
Categories
(Core :: XPConnect, task)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox102 | --- | fixed |
People
(Reporter: arai, Assigned: arai)
References
Details
Attachments
(4 files)
There are still not-in-tree consumer of Cu.import for JSM in AutoConfig (see bug 1766114),
and to make the transition easier, it's better providing a shim that makes Cu.import(".../foo.jsm") keeps working even after foo.jsm is converted to ESM (foo.mjs), until all modules are converted.
So that people can rewrite their AutoConfig script at once.
The shim may also help the in-tree transition itself, given the transition can be done per file, not subtree.
Possible solutions are:
- (a) Once
foo.jsmis converted tofoo.mjs, addfoo.jsmthat importsfoo.mjsand export the same symbols with existing JSM semantics - (b) Modify
Cu.importinternal (somewhere aroundmozJSComponentLoader) to automatically redirect tofoo.mjswhen called withfoo.jsm, for already converted modules
| Assignee | ||
Comment 1•4 years ago
|
||
If we take (b), the change needs to be applied onto bug 1311728 patch
| Assignee | ||
Updated•4 years ago
|
| Assignee | ||
Comment 2•4 years ago
|
||
Other possible solution is:
- (c) If
foo.jsmis not found, automatically redirect tofoo.mjs, and iffoo.mjsexists, convert the call toimportModuleand continue
The following code checks the existence of the actual file, then-clause for resource:// and else-clause for chrome://.
So, if the redirect is still possible at that point, we could take this way.
This doesn't require manual registration or wrapper JS file, and the transition becomes easier.
nsresult mozJSComponentLoader::ObjectForLocation(
ComponentLoaderInfo& aInfo, nsIFile* aComponentFile,
MutableHandleObject aObject, MutableHandleScript aTableScript,
char** aLocation, bool aPropagateExceptions,
MutableHandleValue aException) {
...
if (realFile) {
AutoMemMap map;
MOZ_TRY(map.init(aComponentFile));
...
} else {
nsCString str;
MOZ_TRY_VAR(str, ReadScript(aInfo));
| Assignee | ||
Comment 3•4 years ago
|
||
For options (b) and (c), Cu.isModuleLoaded and Cu.unload may need to handle the redirected case.
| Assignee | ||
Comment 4•4 years ago
|
||
| Assignee | ||
Comment 5•4 years ago
|
||
the attached patch is for (c) way.
it automatically redirects import for JSM if ESM-ified.
the return value is a proxy that wraps ModuleEnvironmentObject, to make it read-only and hide *namespace* binding.
It adds a hacky workaround that de-optimizes the top-level var so that they're stored in ModuleEnvironmentObject, not the local slot.
This can affect the performance, but doesn't regress given global variables in JSM is also not optimized.
| Assignee | ||
Comment 6•4 years ago
|
||
Here's Cu.import return value usage in Thunderbird/SeaMonkey extensions compatible with recent versions.
All symbols are exported. so they won't be affected by the migration as long as
they're not converted to lexical binding before ESM-ified.
| module URI | symbols | binding type |
|---|---|---|
resource://gre/modules/NetUtil.jsm |
NetUtil |
global var |
resource://gre/modules/FileUtils.jsm |
FileUtils |
global var |
resource://gre/modules/Services.jsm |
Services |
global var |
resource://gre/modules/AddonManager.jsm |
AddonManager |
global var |
resource://gre/modules/Console.jsm |
console |
global var |
resource://gre/modules/FileUtils.jsm |
FileUtils |
global var |
resource://gre/modules/PluralForm.jsm |
PluralForm |
global var |
resource://gre/modules/LightweightThemeManager.jsm |
LightweightThemeManager |
global var |
resource://gre/modules/XPCOMUtils.jsm |
XPCOMUtils |
global var |
resource://gre/modules/AsyncShutdown.jsm |
AsyncShutdown |
global var |
resource://gre/modules/Console.jsm |
console |
global var |
resource://gre/modules/AppConstants.jsm |
AppConstants |
global this property |
resource://gre/modules/osfile.jsm |
OS |
global this property |
resource:///modules/MailUtils.js |
MailUtils |
global var |
resource://devtools/shared/Loader.jsm |
loader, require |
global var |
| Assignee | ||
Comment 7•4 years ago
•
|
||
bug 1768060 patch adds support for lexical variables in Cu.import return value, so, if we can go with it, converting var or this.foo = ... with lexical won't break not-in-tree code
| Assignee | ||
Comment 8•4 years ago
|
||
This is necessary for putting all global vars in ModuleEnvironmentObject,
instead of local slot, so that they're accessible in Part 4 patch.
Updated•4 years ago
|
| Assignee | ||
Comment 9•4 years ago
|
||
In order to add public API that returns module's environment, shell-only
function GetModuleEnvironment needs to be renamed.
Depends on D146033
| Assignee | ||
Comment 10•4 years ago
|
||
Depends on D146034
Updated•4 years ago
|
Comment 11•3 years ago
|
||
Comment 12•3 years ago
|
||
Backed out for causing spidermonkey failures on Modules.cpp
- Backout link
- Push with failures
- Failure Log
- Failure line: /builds/worker/checkouts/gecko/js/src/vm/Modules.cpp:237:10: error: cannot initialize return object of type 'JSObject *' with an rvalue of type 'js::ModuleEnvironmentObject *'
| Assignee | ||
Comment 13•3 years ago
|
||
there was missing include.
I'll fix it
Comment 14•3 years ago
|
||
Comment 15•3 years ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/35d2a337b15e
https://hg.mozilla.org/mozilla-central/rev/30aecbe14c23
https://hg.mozilla.org/mozilla-central/rev/5811c45ba29c
https://hg.mozilla.org/mozilla-central/rev/651fcbcabee5
Description
•