Skip to content

Commit

Permalink
Bug 1820119 - import() should throw a TypeError in a worklet script. …
Browse files Browse the repository at this point in the history
…r=jonco,yulia

Differential Revision: https://phabricator.services.mozilla.com/D171563
  • Loading branch information
allstarschh committed Mar 19, 2023
1 parent 5036a4f commit 19e0568
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
7 changes: 7 additions & 0 deletions dom/worklet/tests/dynamic_import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import("./empty-worklet-script.js")
.then(() => {
console.log("Fail");
})
.catch(e => {
console.log(e.name + ": Success");
});
2 changes: 2 additions & 0 deletions dom/worklet/tests/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions dom/worklet/tests/test_dynamic_import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test import() should throw a TypeError for Worklets</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="common.js"></script>
</head>
<body>

<script type="application/javascript">
const WORKLET_SCRIPT = "dynamic_import.js";

function configureTest() {
const ConsoleAPIStorage = SpecialPowers.Cc[
"@mozilla.org/consoleAPI-storage;1"
].getService(SpecialPowers.Ci.nsIConsoleAPIStorage);

// We use console API to check if a TypeError has been thrown, as worklets
// have limitations to post the result back to the main document:
// Worklets have a different global, and they don't have postMessage() APIs,
// and static import SimpleTest.js in worklets also don't work.
function consoleListener() {
this.observe = this.observe.bind(this);
ConsoleAPIStorage.addLogEventListener(this.observe, SpecialPowers.wrap(document).nodePrincipal);
}

consoleListener.prototype = {
observe(aSubject) {
var obj = aSubject.wrappedJSObject;
info("Got console message:" + obj.arguments[0]);
is(TypeError.name + ": Success", obj.arguments[0], "import() should throw");

ConsoleAPIStorage.removeLogEventListener(this.observe);
SimpleTest.finish();
}
}

var cl = new consoleListener();

return SpecialPowers.pushPrefEnv(
{"set": [["dom.audioworklet.enabled", true],
["dom.worklet.enabled", true]]});
}

// This function is called into an iframe.
function runTestInIframe() {
var audioContext = new AudioContext();
audioContext.audioWorklet.addModule(WORKLET_SCRIPT);
}

</script>
</body>
</html>
5 changes: 4 additions & 1 deletion js/loader/ModuleLoaderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 8 additions & 7 deletions js/public/friend/ErrorNumbers.msg
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion js/xpconnect/tests/unit/test_import_es6_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 19e0568

Please sign in to comment.