Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove bootstrap methods from global scope after bootstrapping #4869

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/compilers/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl TsCompiler {
startup_data::compiler_isolate_init(),
worker_state,
);
worker.execute("bootstrapTsCompilerRuntime()").unwrap();
worker.execute("bootstrap.tsCompilerRuntime()").unwrap();
worker
}

Expand Down
2 changes: 1 addition & 1 deletion cli/compilers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl WasmCompiler {
startup_data::compiler_isolate_init(),
worker_state,
);
worker.execute("bootstrapWasmCompilerRuntime()").unwrap();
worker.execute("bootstrap.wasmCompilerRuntime()").unwrap();
worker
}

Expand Down
4 changes: 2 additions & 2 deletions cli/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn cli_snapshot() {
deno_core::js_check(isolate.execute(
"<anon>",
r#"
if (!(bootstrapMainRuntime && bootstrapWorkerRuntime)) {
if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) {
throw Error("bad");
}
console.log("we have console.log!!!");
Expand All @@ -49,7 +49,7 @@ fn compiler_snapshot() {
deno_core::js_check(isolate.execute(
"<anon>",
r#"
if (!(bootstrapTsCompilerRuntime && bootstrapTsCompilerRuntime)) {
if (!(bootstrap.tsCompilerRuntime && bootstrap.wasmCompilerRuntime)) {
throw Error("bad");
}
console.log(`ts version: ${ts.version}`);
Expand Down
19 changes: 8 additions & 11 deletions cli/js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
20 changes: 12 additions & 8 deletions cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | 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> | void) | undefined;
// Assigned to `self` global - compiler
tsCompilerRuntime: (() => void) | undefined;
wasmCompilerRuntime: (() => void) | undefined;
};

var onerror:
| ((
msg: string,
Expand All @@ -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 */
}

Expand Down
18 changes: 7 additions & 11 deletions cli/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
3 changes: 3 additions & 0 deletions cli/js/runtime_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions cli/js/runtime_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn create_main_worker(
t.add("stderr", Box::new(stderr));
}

worker.execute("bootstrapMainRuntime()")?;
worker.execute("bootstrap.mainRuntime()")?;
Ok(worker)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/ops/worker_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod tests {
false,
);
worker
.execute("bootstrapWorkerRuntime(\"TEST\", false)")
.execute("bootstrap.workerRuntime(\"TEST\", false)")
.unwrap();
worker
}
Expand Down
4 changes: 2 additions & 2 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -404,7 +404,7 @@ mod tests {
startup_data::deno_isolate_init(),
state,
);
worker.execute("bootstrapMainRuntime()").unwrap();
worker.execute("bootstrap.mainRuntime()").unwrap();
worker
}

Expand Down