diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2019-08-05 04:23:41 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-08-05 07:23:41 -0400 |
commit | ddee2dff14772ade16e282ad18eda6f5054ce94e (patch) | |
tree | 473cdf935b8d0254a7ab8e0c89fd407201793ac6 /js | |
parent | aaa7a3eac4df0de9a93dc8fc4717d38212a3de5b (diff) |
Provide option to delete Deno namespace in worker (#2717)
Diffstat (limited to 'js')
-rw-r--r-- | js/compiler.ts | 2 | ||||
-rw-r--r-- | js/core.ts | 2 | ||||
-rw-r--r-- | js/globals.ts | 3 | ||||
-rw-r--r-- | js/main.ts | 7 | ||||
-rw-r--r-- | js/os.ts | 20 | ||||
-rw-r--r-- | js/workers.ts | 31 |
6 files changed, 52 insertions, 13 deletions
diff --git a/js/compiler.ts b/js/compiler.ts index 4203f753b..6bda5a1ec 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -25,7 +25,7 @@ const console = new Console(core.print); window.console = console; window.workerMain = workerMain; export default function denoMain(): void { - os.start("TS"); + os.start(true, "TS"); } const ASSETS = "$asset$"; diff --git a/js/core.ts b/js/core.ts index 03473ff3d..436ba7ccb 100644 --- a/js/core.ts +++ b/js/core.ts @@ -1,4 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { window } from "./window"; +// This allows us to access core in API even if we +// dispose window.Deno export const core = window.Deno.core as DenoCore; diff --git a/js/globals.ts b/js/globals.ts index 27cef3909..92e4662aa 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -32,7 +32,6 @@ import * as request from "./request"; // These imports are not exposed and therefore are fine to just import the // symbols required. import { core } from "./core"; -import { immutableDefine } from "./util"; // During the build process, augmentations to the variable `window` in this // file are tracked and created as part of default library that is built into @@ -71,7 +70,7 @@ window.window = window; // This is the Deno namespace, it is handled differently from other window // properties when building the runtime type library, as the whole module // is flattened into a single namespace. -immutableDefine(window, "Deno", deno); +window.Deno = deno; Object.freeze(window.Deno); // Globally available functions and object instances. diff --git a/js/main.ts b/js/main.ts index 5687a6926..769f522a6 100644 --- a/js/main.ts +++ b/js/main.ts @@ -18,8 +18,11 @@ import { setLocation } from "./location"; // builtin modules import * as deno from "./deno"; -export default function denoMain(name?: string): void { - const startResMsg = os.start(name); +export default function denoMain( + preserveDenoNamespace: boolean = true, + name?: string +): void { + const startResMsg = os.start(preserveDenoNamespace, name); setVersions(startResMsg.denoVersion()!, startResMsg.v8Version()!); @@ -112,7 +112,10 @@ function sendStart(): msg.StartRes { // This function bootstraps an environment within Deno, it is shared both by // the runtime and the compiler environments. // @internal -export function start(source?: string): msg.StartRes { +export function start( + preserveDenoNamespace = true, + source?: string +): msg.StartRes { core.setAsyncHandler(handleAsyncMsgFromRust); // First we send an empty `Start` message to let the privileged side know we @@ -124,9 +127,18 @@ export function start(source?: string): msg.StartRes { setGlobals(startResMsg.pid(), startResMsg.noColor(), startResMsg.execPath()!); - // Deno.core could ONLY be safely frozen here (not in globals.ts) - // since shared_queue.js will modify core properties. - Object.freeze(window.Deno.core); + if (preserveDenoNamespace) { + util.immutableDefine(window, "Deno", window.Deno); + // Deno.core could ONLY be safely frozen here (not in globals.ts) + // since shared_queue.js will modify core properties. + Object.freeze(window.Deno.core); + // core.sharedQueue is an object so we should also freeze it. + Object.freeze(window.Deno.core.sharedQueue); + } else { + // Remove window.Deno + delete window.Deno; + assert(window.Deno === undefined); + } return startResMsg; } diff --git a/js/workers.ts b/js/workers.ts index 1531b960b..f008ccecf 100644 --- a/js/workers.ts +++ b/js/workers.ts @@ -20,10 +20,17 @@ export function decodeMessage(dataIntArray: Uint8Array): any { return JSON.parse(dataJson); } -function createWorker(specifier: string): number { +function createWorker( + specifier: string, + includeDenoNamespace: boolean +): number { const builder = flatbuffers.createBuilder(); const specifier_ = builder.createString(specifier); - const inner = msg.CreateWorker.createCreateWorker(builder, specifier_); + const inner = msg.CreateWorker.createCreateWorker( + builder, + specifier_, + includeDenoNamespace + ); const baseRes = sendSync(builder, msg.Any.CreateWorker, inner); assert(baseRes != null); assert( @@ -149,6 +156,18 @@ export interface Worker { closed: Promise<void>; } +// TODO(kevinkassimo): Maybe implement reasonable web worker options? +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface WorkerOptions {} + +/** Extended Deno Worker initialization options. + * `noDenoNamespace` hides global `window.Deno` namespace for + * spawned worker and nested workers spawned by it (default: false). + */ +export interface DenoWorkerOptions extends WorkerOptions { + noDenoNamespace?: boolean; +} + export class WorkerImpl implements Worker { private readonly rid: number; private isClosing: boolean = false; @@ -157,8 +176,12 @@ export class WorkerImpl implements Worker { public onmessage?: (data: any) => void; public onmessageerror?: () => void; - constructor(specifier: string) { - this.rid = createWorker(specifier); + constructor(specifier: string, options?: DenoWorkerOptions) { + let includeDenoNamespace = true; + if (options && options.noDenoNamespace) { + includeDenoNamespace = false; + } + this.rid = createWorker(specifier, includeDenoNamespace); this.run(); this.isClosedPromise = hostGetWorkerClosed(this.rid); this.isClosedPromise.then( |