Bug 1820119 - import() should throw a TypeError in a worklet script. r=jonco,yulia

Differential Revision: https://phabricator.services.mozilla.com/D171563
This commit is contained in:
Yoshi Cheng-Hao Huang 2023-03-19 20:39:02 +00:00
parent 5036a4f8da
commit 19e0568f4d
6 changed files with 76 additions and 9 deletions

View file

@ -0,0 +1,7 @@
import("./empty-worklet-script.js")
.then(() => {
console.log("Fail");
})
.catch(e => {
console.log(e.name + ": Success");
});

View file

@ -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

View file

@ -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>

View file

@ -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;
}

View file

@ -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}")

View file

@ -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,