Bug 1834360 - Add support for ES modules in ChromeWorker. r=dom-worker-reviewers,webidl,smaug

Differential Revision: https://phabricator.services.mozilla.com/D178696
This commit is contained in:
Andrew Sutherland 2023-05-22 22:00:03 +00:00
parent 42ef0ea1c2
commit 49e7f477b9
7 changed files with 65 additions and 23 deletions

View File

@ -44,5 +44,5 @@ enum WorkerType { "classic", "module" };
Exposed=(Window,DedicatedWorker,SharedWorker)]
interface ChromeWorker : Worker {
[Throws]
constructor(USVString scriptURL);
constructor(USVString scriptURL, optional WorkerOptions options = {});
};

View File

@ -18,7 +18,7 @@ namespace mozilla::dom {
/* static */
already_AddRefed<ChromeWorker> ChromeWorker::Constructor(
const GlobalObject& aGlobal, const nsAString& aScriptURL,
ErrorResult& aRv) {
const WorkerOptions& aOptions, ErrorResult& aRv) {
// Dump the JS stack if somebody's creating a ChromeWorker after shutdown has
// begun. See bug 1813353.
if (xpc::IsInAutomation() &&
@ -32,7 +32,7 @@ already_AddRefed<ChromeWorker> ChromeWorker::Constructor(
RefPtr<WorkerPrivate> workerPrivate = WorkerPrivate::Constructor(
cx, aScriptURL, true /* aIsChromeWorker */, WorkerKindDedicated,
RequestCredentials::Omit, WorkerType::Classic, u""_ns, VoidCString(),
RequestCredentials::Omit, aOptions.mType, aOptions.mName, VoidCString(),
nullptr /*aLoadInfo */, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;

View File

@ -13,9 +13,9 @@ namespace mozilla::dom {
class ChromeWorker final : public Worker {
public:
static already_AddRefed<ChromeWorker> Constructor(const GlobalObject& aGlobal,
const nsAString& aScriptURL,
ErrorResult& aRv);
static already_AddRefed<ChromeWorker> Constructor(
const GlobalObject& aGlobal, const nsAString& aScriptURL,
const WorkerOptions& aOptions, ErrorResult& aRv);
static bool WorkerAvailable(JSContext* aCx, JSObject* /* unused */);

View File

@ -39,7 +39,9 @@ support-files =
WorkerTest_worker.js
bug1062920_worker.js
chromeWorker_subworker.js
chromeWorker_worker_submod.sys.mjs
chromeWorker_worker.js
chromeWorker_worker.sys.mjs
dom_worker_helper.js
empty.html
fileBlobSubWorker_worker.js

View File

@ -0,0 +1,16 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// our onmessage handler lives in the module.
import _ from "./chromeWorker_worker_submod.sys.mjs";
if (!("ctypes" in self)) {
throw "No ctypes!";
}
// Go ahead and verify that the ctypes lazy getter actually works.
if (ctypes.toString() != "[object ctypes]") {
throw "Bad ctypes object: " + ctypes.toString();
}

View File

@ -0,0 +1,9 @@
onmessage = function (event) {
let worker = new ChromeWorker("chromeWorker_subworker.js");
worker.onmessage = function (msg) {
postMessage(msg.data);
};
worker.postMessage(event.data);
};
export default "go away linter";

View File

@ -4,8 +4,7 @@
http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="DOM Worker Threads Test"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="test();">
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
@ -14,23 +13,39 @@
<script type="application/javascript">
<![CDATA[
function test()
{
add_task(async function classic_worker_test() {
let worker = window.classicWorker = new ChromeWorker("chromeWorker_worker.js");
await new Promise((resolve, reject) => {
worker.onmessage = function(event) {
is(event.data, "Done!", "Got the done message!");
resolve();
};
worker.onerror = function(event) {
ok(false, "Classic Worker had an error: " + event.message);
worker.terminate();
reject();
};
worker.postMessage("go");
});
});
add_task(async function module_worker_test() {
waitForWorkerFinish();
var worker = new ChromeWorker("chromeWorker_worker.js");
worker.onmessage = function(event) {
is(event.data, "Done!", "Wrong message!");
finish();
}
worker.onerror = function(event) {
ok(false, "Worker had an error: " + event.message);
worker.terminate();
finish();
}
worker.postMessage("go");
}
let worker = window.moduleWorker = new ChromeWorker("chromeWorker_worker.sys.mjs", { type: "module" });
await new Promise((resolve, reject) => {
worker.onmessage = function(event) {
is(event.data, "Done!", "Got the done message!");
resolve();
};
worker.onerror = function(event) {
ok(false, "Module Worker had an error: " + event.message);
worker.terminate();
reject();
};
worker.postMessage("go");
});
});
]]>
</script>