From 1378df33647e2608733d88121b77ff2f839cddfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 25 Apr 2020 01:03:45 +0200 Subject: [PATCH] remove bootstrap methods from global scope after bootstrapping (#4869) --- cli/compilers/ts.rs | 2 +- cli/compilers/wasm.rs | 2 +- cli/js.rs | 4 ++-- cli/js/compiler.ts | 19 ++++++++----------- cli/js/globals.ts | 20 ++++++++++++-------- cli/js/main.ts | 18 +++++++----------- cli/js/runtime_main.ts | 3 +++ cli/js/runtime_worker.ts | 3 +++ cli/lib.rs | 2 +- cli/ops/worker_host.rs | 2 +- cli/web_worker.rs | 2 +- cli/worker.rs | 4 ++-- 12 files changed, 42 insertions(+), 39 deletions(-) diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index 0e3e5c26c2..0560ad2920 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -267,7 +267,7 @@ impl TsCompiler { startup_data::compiler_isolate_init(), worker_state, ); - worker.execute("bootstrapTsCompilerRuntime()").unwrap(); + worker.execute("bootstrap.tsCompilerRuntime()").unwrap(); worker } diff --git a/cli/compilers/wasm.rs b/cli/compilers/wasm.rs index 4e8ba78f32..ed56035843 100644 --- a/cli/compilers/wasm.rs +++ b/cli/compilers/wasm.rs @@ -67,7 +67,7 @@ impl WasmCompiler { startup_data::compiler_isolate_init(), worker_state, ); - worker.execute("bootstrapWasmCompilerRuntime()").unwrap(); + worker.execute("bootstrap.wasmCompilerRuntime()").unwrap(); worker } diff --git a/cli/js.rs b/cli/js.rs index 08392aa92a..6b4cb2eac1 100644 --- a/cli/js.rs +++ b/cli/js.rs @@ -30,7 +30,7 @@ fn cli_snapshot() { deno_core::js_check(isolate.execute( "", r#" - if (!(bootstrapMainRuntime && bootstrapWorkerRuntime)) { + if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) { throw Error("bad"); } console.log("we have console.log!!!"); @@ -49,7 +49,7 @@ fn compiler_snapshot() { deno_core::js_check(isolate.execute( "", r#" - if (!(bootstrapTsCompilerRuntime && bootstrapTsCompilerRuntime)) { + if (!(bootstrap.tsCompilerRuntime && bootstrap.wasmCompilerRuntime)) { throw Error("bad"); } console.log(`ts version: ${ts.version}`); diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 91653a8e4e..da9b62472c 100644 --- a/cli/js/compiler.ts +++ b/cli/js/compiler.ts @@ -407,16 +407,13 @@ function bootstrapWasmCompilerRuntime(): void { delete (Object.prototype as any).__proto__; Object.defineProperties(globalThis, { - bootstrapWasmCompilerRuntime: { - value: bootstrapWasmCompilerRuntime, - enumerable: false, - writable: false, - configurable: false, - }, - bootstrapTsCompilerRuntime: { - value: bootstrapTsCompilerRuntime, - enumerable: false, - writable: false, - configurable: false, + bootstrap: { + value: { + ...globalThis.bootstrap, + wasmCompilerRuntime: bootstrapWasmCompilerRuntime, + tsCompilerRuntime: bootstrapTsCompilerRuntime, + }, + configurable: true, + writable: true, }, }); diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 897e9859f4..87309a1589 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -134,12 +134,19 @@ declare global { }; var onload: ((e: Event) => void) | undefined; var onunload: ((e: Event) => void) | undefined; - var bootstrapMainRuntime: (() => void) | undefined; - // Assigned to `self` global - worker runtime and compiler - var bootstrapWorkerRuntime: - | ((name: string) => Promise | void) - | undefined; + // These methods are used to prepare different runtime + // environments. After bootrapping, this namespace + // should be removed from global scope. + var bootstrap: { + mainRuntime: (() => void) | undefined; + // Assigned to `self` global - worker runtime and compiler + workerRuntime: ((name: string) => Promise | void) | undefined; + // Assigned to `self` global - compiler + tsCompilerRuntime: (() => void) | undefined; + wasmCompilerRuntime: (() => void) | undefined; + }; + var onerror: | (( msg: string, @@ -156,9 +163,6 @@ declare global { var close: () => void; // eslint-disable-next-line @typescript-eslint/no-explicit-any var postMessage: (msg: any) => void; - // Assigned to `self` global - compiler - var bootstrapTsCompilerRuntime: (() => void) | undefined; - var bootstrapWasmCompilerRuntime: (() => void) | undefined; /* eslint-enable */ } diff --git a/cli/js/main.ts b/cli/js/main.ts index bd30d33f79..bb7a8a2dcb 100644 --- a/cli/js/main.ts +++ b/cli/js/main.ts @@ -9,16 +9,12 @@ import { bootstrapWorkerRuntime } from "./runtime_worker.ts"; delete (Object.prototype as any).__proto__; Object.defineProperties(globalThis, { - bootstrapMainRuntime: { - value: bootstrapMainRuntime, - enumerable: false, - writable: false, - configurable: false, - }, - bootstrapWorkerRuntime: { - value: bootstrapWorkerRuntime, - enumerable: false, - writable: false, - configurable: false, + bootstrap: { + value: { + mainRuntime: bootstrapMainRuntime, + workerRuntime: bootstrapWorkerRuntime, + }, + configurable: true, + writable: true, }, }); diff --git a/cli/js/runtime_main.ts b/cli/js/runtime_main.ts index 0b0b1f75f4..4c6df892cb 100644 --- a/cli/js/runtime_main.ts +++ b/cli/js/runtime_main.ts @@ -72,6 +72,9 @@ export function bootstrapMainRuntime(): void { if (hasBootstrapped) { throw new Error("Worker runtime already bootstrapped"); } + // Remove bootstrapping methods from global scope + // @ts-ignore + globalThis.bootstrap = undefined; log("bootstrapMainRuntime"); hasBootstrapped = true; Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods); diff --git a/cli/js/runtime_worker.ts b/cli/js/runtime_worker.ts index 60c845a184..6be8ff5869 100644 --- a/cli/js/runtime_worker.ts +++ b/cli/js/runtime_worker.ts @@ -126,6 +126,9 @@ export function bootstrapWorkerRuntime( if (hasBootstrapped) { throw new Error("Worker runtime already bootstrapped"); } + // Remove bootstrapping methods from global scope + // @ts-ignore + globalThis.bootstrap = undefined; log("bootstrapWorkerRuntime"); hasBootstrapped = true; Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods); diff --git a/cli/lib.rs b/cli/lib.rs index 2e52ace93c..2a13d802de 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -154,7 +154,7 @@ fn create_main_worker( t.add("stderr", Box::new(stderr)); } - worker.execute("bootstrapMainRuntime()")?; + worker.execute("bootstrap.mainRuntime()")?; Ok(worker) } diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index 460596b818..df8bfa7fd7 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -64,7 +64,7 @@ fn create_web_worker( // Instead of using name for log we use `worker-${id}` because // WebWorkers can have empty string as name. let script = format!( - "bootstrapWorkerRuntime(\"{}\", {}, \"worker-{}\")", + "bootstrap.workerRuntime(\"{}\", {}, \"worker-{}\")", name, worker.has_deno_namespace, worker_id ); worker.execute(&script)?; diff --git a/cli/web_worker.rs b/cli/web_worker.rs index fc1575c5f3..ebb3f1d866 100644 --- a/cli/web_worker.rs +++ b/cli/web_worker.rs @@ -263,7 +263,7 @@ mod tests { false, ); worker - .execute("bootstrapWorkerRuntime(\"TEST\", false)") + .execute("bootstrap.workerRuntime(\"TEST\", false)") .unwrap(); worker } diff --git a/cli/worker.rs b/cli/worker.rs index 46e7eeafff..858958ecfb 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -382,7 +382,7 @@ mod tests { startup_data::deno_isolate_init(), state.clone(), ); - worker.execute("bootstrapMainRuntime()").unwrap(); + worker.execute("bootstrap.mainRuntime()").unwrap(); let result = worker.execute_module(&module_specifier).await; if let Err(err) = result { eprintln!("execute_mod err {:?}", err); @@ -404,7 +404,7 @@ mod tests { startup_data::deno_isolate_init(), state, ); - worker.execute("bootstrapMainRuntime()").unwrap(); + worker.execute("bootstrap.mainRuntime()").unwrap(); worker }