From 19e0568f4d1ad911e3ce011eda1efbc91f0dcbe5 Mon Sep 17 00:00:00 2001 From: Yoshi Cheng-Hao Huang Date: Sun, 19 Mar 2023 20:39:02 +0000 Subject: [PATCH] Bug 1820119 - import() should throw a TypeError in a worklet script. r=jonco,yulia Differential Revision: https://phabricator.services.mozilla.com/D171563 --- dom/worklet/tests/dynamic_import.js | 7 +++ dom/worklet/tests/mochitest.ini | 2 + dom/worklet/tests/test_dynamic_import.html | 54 +++++++++++++++++++ js/loader/ModuleLoaderBase.cpp | 5 +- js/public/friend/ErrorNumbers.msg | 15 +++--- .../tests/unit/test_import_es6_modules.js | 2 +- 6 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 dom/worklet/tests/dynamic_import.js create mode 100644 dom/worklet/tests/test_dynamic_import.html diff --git a/dom/worklet/tests/dynamic_import.js b/dom/worklet/tests/dynamic_import.js new file mode 100644 index 000000000000..a5196d212fbc --- /dev/null +++ b/dom/worklet/tests/dynamic_import.js @@ -0,0 +1,7 @@ +import("./empty-worklet-script.js") + .then(() => { + console.log("Fail"); + }) + .catch(e => { + console.log(e.name + ": Success"); + }); diff --git a/dom/worklet/tests/mochitest.ini b/dom/worklet/tests/mochitest.ini index d04c248d3e1e..c42f3aad77d2 100644 --- a/dom/worklet/tests/mochitest.ini +++ b/dom/worklet/tests/mochitest.ini @@ -20,6 +20,8 @@ support-files=worklet_audioWorklet_options.js support-files=worklet_console.js [test_dump.html] support-files=worklet_dump.js +[test_dynamic_import.html] +support-files=dynamic_import.js [test_exception.html] support-files = worklet_exception.js diff --git a/dom/worklet/tests/test_dynamic_import.html b/dom/worklet/tests/test_dynamic_import.html new file mode 100644 index 000000000000..e0326b976c31 --- /dev/null +++ b/dom/worklet/tests/test_dynamic_import.html @@ -0,0 +1,54 @@ + + + + Test import() should throw a TypeError for Worklets + + + + + + + + + diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp index 63ae2948d038..712f13bf2e07 100644 --- a/js/loader/ModuleLoaderBase.cpp +++ b/js/loader/ModuleLoaderBase.cpp @@ -318,7 +318,10 @@ bool ModuleLoaderBase::HostImportModuleDynamically( aCx, uri, script, aReferencingPrivate, specifierString, aPromise); if (!request) { - JS_ReportErrorASCII(aCx, "Dynamic import not supported in this context"); + // Throws TypeError if CreateDynamicImport returns nullptr. + JS_ReportErrorNumberASCII(aCx, js::GetErrorMessage, nullptr, + JSMSG_DYNAMIC_IMPORT_NOT_SUPPORTED); + return false; } diff --git a/js/public/friend/ErrorNumbers.msg b/js/public/friend/ErrorNumbers.msg index f59b24eab689..0211b709a058 100644 --- a/js/public/friend/ErrorNumbers.msg +++ b/js/public/friend/ErrorNumbers.msg @@ -692,13 +692,14 @@ MSG_DEF(JSMSG_CANT_DELETE_SUPER, 0, JSEXN_REFERENCEERR, "invalid delete involvin MSG_DEF(JSMSG_REINIT_THIS, 0, JSEXN_REFERENCEERR, "super() called twice in derived class constructor") // Modules -MSG_DEF(JSMSG_MISSING_INDIRECT_EXPORT, 0, JSEXN_SYNTAXERR, "indirect export not found") -MSG_DEF(JSMSG_AMBIGUOUS_INDIRECT_EXPORT, 0, JSEXN_SYNTAXERR, "ambiguous indirect export") -MSG_DEF(JSMSG_MISSING_IMPORT, 0, JSEXN_SYNTAXERR, "import not found") -MSG_DEF(JSMSG_AMBIGUOUS_IMPORT, 0, JSEXN_SYNTAXERR, "ambiguous import") -MSG_DEF(JSMSG_MISSING_EXPORT, 1, JSEXN_SYNTAXERR, "local binding for export '{0}' not found") -MSG_DEF(JSMSG_BAD_MODULE_STATUS, 1, JSEXN_INTERNALERR, "module record has unexpected status: {0}") -MSG_DEF(JSMSG_DYNAMIC_IMPORT_FAILED, 0, JSEXN_TYPEERR, "error loading dynamically imported module") +MSG_DEF(JSMSG_MISSING_INDIRECT_EXPORT, 0, JSEXN_SYNTAXERR, "indirect export not found") +MSG_DEF(JSMSG_AMBIGUOUS_INDIRECT_EXPORT, 0, JSEXN_SYNTAXERR, "ambiguous indirect export") +MSG_DEF(JSMSG_MISSING_IMPORT, 0, JSEXN_SYNTAXERR, "import not found") +MSG_DEF(JSMSG_AMBIGUOUS_IMPORT, 0, JSEXN_SYNTAXERR, "ambiguous import") +MSG_DEF(JSMSG_MISSING_EXPORT, 1, JSEXN_SYNTAXERR, "local binding for export '{0}' not found") +MSG_DEF(JSMSG_BAD_MODULE_STATUS, 1, JSEXN_INTERNALERR, "module record has unexpected status: {0}") +MSG_DEF(JSMSG_DYNAMIC_IMPORT_FAILED, 0, JSEXN_TYPEERR, "error loading dynamically imported module") +MSG_DEF(JSMSG_DYNAMIC_IMPORT_NOT_SUPPORTED, 0, JSEXN_TYPEERR, "Dynamic import not supported in this context") // Import maps MSG_DEF(JSMSG_IMPORT_MAPS_PARSE_FAILED, 1, JSEXN_SYNTAXERR, "Failed to parse import map: Invalid JSON format. {0}") diff --git a/js/xpconnect/tests/unit/test_import_es6_modules.js b/js/xpconnect/tests/unit/test_import_es6_modules.js index a05d4b845231..9b5659871f25 100644 --- a/js/xpconnect/tests/unit/test_import_es6_modules.js +++ b/js/xpconnect/tests/unit/test_import_es6_modules.js @@ -112,7 +112,7 @@ add_task(async function() { ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import.js"); const e = await ns.result; checkException(e, { - type: "Error", + type: "TypeError", message: "not supported", fileName: "resource://test/es6module_dynamic_import.js", lineNumber: 5,