diff options
Diffstat (limited to 'runtime/js/99_main.js')
-rw-r--r-- | runtime/js/99_main.js | 46 |
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; |