remove bootstrap methods from global scope after bootstrapping (#4869)

This commit is contained in:
Bartek Iwańczuk 2020-04-25 01:03:45 +02:00 committed by GitHub
parent 912a57f6a2
commit 1378df3364
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 42 additions and 39 deletions

View file

@ -267,7 +267,7 @@ impl TsCompiler {
startup_data::compiler_isolate_init(), startup_data::compiler_isolate_init(),
worker_state, worker_state,
); );
worker.execute("bootstrapTsCompilerRuntime()").unwrap(); worker.execute("bootstrap.tsCompilerRuntime()").unwrap();
worker worker
} }

View file

@ -67,7 +67,7 @@ impl WasmCompiler {
startup_data::compiler_isolate_init(), startup_data::compiler_isolate_init(),
worker_state, worker_state,
); );
worker.execute("bootstrapWasmCompilerRuntime()").unwrap(); worker.execute("bootstrap.wasmCompilerRuntime()").unwrap();
worker worker
} }

View file

@ -30,7 +30,7 @@ fn cli_snapshot() {
deno_core::js_check(isolate.execute( deno_core::js_check(isolate.execute(
"<anon>", "<anon>",
r#" r#"
if (!(bootstrapMainRuntime && bootstrapWorkerRuntime)) { if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) {
throw Error("bad"); throw Error("bad");
} }
console.log("we have console.log!!!"); console.log("we have console.log!!!");
@ -49,7 +49,7 @@ fn compiler_snapshot() {
deno_core::js_check(isolate.execute( deno_core::js_check(isolate.execute(
"<anon>", "<anon>",
r#" r#"
if (!(bootstrapTsCompilerRuntime && bootstrapTsCompilerRuntime)) { if (!(bootstrap.tsCompilerRuntime && bootstrap.wasmCompilerRuntime)) {
throw Error("bad"); throw Error("bad");
} }
console.log(`ts version: ${ts.version}`); console.log(`ts version: ${ts.version}`);

View file

@ -407,16 +407,13 @@ function bootstrapWasmCompilerRuntime(): void {
delete (Object.prototype as any).__proto__; delete (Object.prototype as any).__proto__;
Object.defineProperties(globalThis, { Object.defineProperties(globalThis, {
bootstrapWasmCompilerRuntime: { bootstrap: {
value: bootstrapWasmCompilerRuntime, value: {
enumerable: false, ...globalThis.bootstrap,
writable: false, wasmCompilerRuntime: bootstrapWasmCompilerRuntime,
configurable: false, tsCompilerRuntime: bootstrapTsCompilerRuntime,
}, },
bootstrapTsCompilerRuntime: { configurable: true,
value: bootstrapTsCompilerRuntime, writable: true,
enumerable: false,
writable: false,
configurable: false,
}, },
}); });

View file

@ -134,12 +134,19 @@ declare global {
}; };
var onload: ((e: Event) => void) | undefined; var onload: ((e: Event) => void) | undefined;
var onunload: ((e: Event) => void) | undefined; var onunload: ((e: Event) => void) | undefined;
var bootstrapMainRuntime: (() => void) | undefined;
// Assigned to `self` global - worker runtime and compiler // These methods are used to prepare different runtime
var bootstrapWorkerRuntime: // environments. After bootrapping, this namespace
| ((name: string) => Promise<void> | void) // should be removed from global scope.
| undefined; var bootstrap: {
mainRuntime: (() => void) | undefined;
// Assigned to `self` global - worker runtime and compiler
workerRuntime: ((name: string) => Promise<void> | void) | undefined;
// Assigned to `self` global - compiler
tsCompilerRuntime: (() => void) | undefined;
wasmCompilerRuntime: (() => void) | undefined;
};
var onerror: var onerror:
| (( | ((
msg: string, msg: string,
@ -156,9 +163,6 @@ declare global {
var close: () => void; var close: () => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
var postMessage: (msg: any) => void; var postMessage: (msg: any) => void;
// Assigned to `self` global - compiler
var bootstrapTsCompilerRuntime: (() => void) | undefined;
var bootstrapWasmCompilerRuntime: (() => void) | undefined;
/* eslint-enable */ /* eslint-enable */
} }

View file

@ -9,16 +9,12 @@ import { bootstrapWorkerRuntime } from "./runtime_worker.ts";
delete (Object.prototype as any).__proto__; delete (Object.prototype as any).__proto__;
Object.defineProperties(globalThis, { Object.defineProperties(globalThis, {
bootstrapMainRuntime: { bootstrap: {
value: bootstrapMainRuntime, value: {
enumerable: false, mainRuntime: bootstrapMainRuntime,
writable: false, workerRuntime: bootstrapWorkerRuntime,
configurable: false, },
}, configurable: true,
bootstrapWorkerRuntime: { writable: true,
value: bootstrapWorkerRuntime,
enumerable: false,
writable: false,
configurable: false,
}, },
}); });

View file

@ -72,6 +72,9 @@ export function bootstrapMainRuntime(): void {
if (hasBootstrapped) { if (hasBootstrapped) {
throw new Error("Worker runtime already bootstrapped"); throw new Error("Worker runtime already bootstrapped");
} }
// Remove bootstrapping methods from global scope
// @ts-ignore
globalThis.bootstrap = undefined;
log("bootstrapMainRuntime"); log("bootstrapMainRuntime");
hasBootstrapped = true; hasBootstrapped = true;
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods); Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);

View file

@ -126,6 +126,9 @@ export function bootstrapWorkerRuntime(
if (hasBootstrapped) { if (hasBootstrapped) {
throw new Error("Worker runtime already bootstrapped"); throw new Error("Worker runtime already bootstrapped");
} }
// Remove bootstrapping methods from global scope
// @ts-ignore
globalThis.bootstrap = undefined;
log("bootstrapWorkerRuntime"); log("bootstrapWorkerRuntime");
hasBootstrapped = true; hasBootstrapped = true;
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods); Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);

View file

@ -154,7 +154,7 @@ fn create_main_worker(
t.add("stderr", Box::new(stderr)); t.add("stderr", Box::new(stderr));
} }
worker.execute("bootstrapMainRuntime()")?; worker.execute("bootstrap.mainRuntime()")?;
Ok(worker) Ok(worker)
} }

View file

@ -64,7 +64,7 @@ fn create_web_worker(
// Instead of using name for log we use `worker-${id}` because // Instead of using name for log we use `worker-${id}` because
// WebWorkers can have empty string as name. // WebWorkers can have empty string as name.
let script = format!( let script = format!(
"bootstrapWorkerRuntime(\"{}\", {}, \"worker-{}\")", "bootstrap.workerRuntime(\"{}\", {}, \"worker-{}\")",
name, worker.has_deno_namespace, worker_id name, worker.has_deno_namespace, worker_id
); );
worker.execute(&script)?; worker.execute(&script)?;

View file

@ -263,7 +263,7 @@ mod tests {
false, false,
); );
worker worker
.execute("bootstrapWorkerRuntime(\"TEST\", false)") .execute("bootstrap.workerRuntime(\"TEST\", false)")
.unwrap(); .unwrap();
worker worker
} }

View file

@ -382,7 +382,7 @@ mod tests {
startup_data::deno_isolate_init(), startup_data::deno_isolate_init(),
state.clone(), state.clone(),
); );
worker.execute("bootstrapMainRuntime()").unwrap(); worker.execute("bootstrap.mainRuntime()").unwrap();
let result = worker.execute_module(&module_specifier).await; let result = worker.execute_module(&module_specifier).await;
if let Err(err) = result { if let Err(err) = result {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
@ -404,7 +404,7 @@ mod tests {
startup_data::deno_isolate_init(), startup_data::deno_isolate_init(),
state, state,
); );
worker.execute("bootstrapMainRuntime()").unwrap(); worker.execute("bootstrap.mainRuntime()").unwrap();
worker worker
} }