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: remove bootstrap methods from global scope after bootstrapping (#4869) --- 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 +++ 5 files changed, 33 insertions(+), 30 deletions(-) (limited to 'cli/js') diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 91653a8e4..da9b62472 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 897e9859f..87309a158 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 bd30d33f7..bb7a8a2dc 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 0b0b1f75f..4c6df892c 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 60c845a18..6be8ff586 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); -- cgit v1.2.3