summaryrefslogtreecommitdiff
path: root/runtime/js/99_main.js
diff options
context:
space:
mode:
authorAndreu Botella <abb@randomunok.com>2021-08-16 14:29:54 +0200
committerGitHub <noreply@github.com>2021-08-16 14:29:54 +0200
commitddbb7b83f2c483e354f425dfb70dbab494b05ea5 (patch)
treefa84f5607395773284e331fe32f2b86b59f02a5d /runtime/js/99_main.js
parentd1d2388d7f1a09fd2469b356f00b6b361269a0b7 (diff)
feat(runtime): support classic workers for internal testing (#11338)
This commit implements classic workers, but only when the `--enable-testing-features-do-not-use` flag is provided. This change is not user facing. Classic workers are used extensively in WPT tests. The classic workers do not support loading from disk, and do not support TypeScript. Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'runtime/js/99_main.js')
-rw-r--r--runtime/js/99_main.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 6d5599e71..b1f7d1473 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -8,6 +8,7 @@ delete Object.prototype.__proto__;
((window) => {
const core = Deno.core;
const {
+ ArrayPrototypeMap,
Error,
FunctionPrototypeCall,
FunctionPrototypeBind,
@@ -164,6 +165,44 @@ delete Object.prototype.__proto__;
}
}
+ let loadedMainWorkerScript = false;
+
+ function importScripts(...urls) {
+ if (core.opSync("op_worker_get_type") === "module") {
+ throw new TypeError("Can't import scripts in a module worker.");
+ }
+
+ const baseUrl = location.getLocationHref();
+ const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => {
+ try {
+ return new url.URL(scriptUrl, baseUrl ?? undefined).href;
+ } catch {
+ throw new domException.DOMException(
+ "Failed to parse URL.",
+ "SyntaxError",
+ );
+ }
+ });
+
+ // A classic worker's main script has looser MIME type checks than any
+ // imported scripts, so we use `loadedMainWorkerScript` to distinguish them.
+ // TODO(andreubotella) Refactor worker creation so the main script isn't
+ // loaded with `importScripts()`.
+ const scripts = core.opSync(
+ "op_worker_sync_fetch",
+ parsedUrls,
+ !loadedMainWorkerScript,
+ );
+ loadedMainWorkerScript = true;
+
+ for (const { url, script } of scripts) {
+ const err = core.evalContext(script, url)[1];
+ if (err !== null) {
+ throw err.thrown;
+ }
+ }
+ }
+
function opMainModule() {
return core.opSync("op_main_module");
}
@@ -597,6 +636,13 @@ delete Object.prototype.__proto__;
}
ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties);
ObjectDefineProperties(globalThis, { name: util.readOnly(name) });
+ if (runtimeOptions.enableTestingFeaturesFlag) {
+ ObjectDefineProperty(
+ globalThis,
+ "importScripts",
+ util.writable(importScripts),
+ );
+ }
ObjectSetPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
const consoleFromDeno = globalThis.console;