From 6853633f7d4c66eeffd98a2b6184cd5caa4e9c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 6 Feb 2024 00:58:06 +0100 Subject: [PATCH] refactor: don't expose worker ops to user code (#22276) --- runtime/js/99_main.js | 20 +++++++++----------- runtime/snapshot.rs | 1 + runtime/worker.rs | 5 +++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index ad2a373cd3..d185278656 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -14,6 +14,11 @@ import { op_ppid, op_set_format_exception_callback, op_snapshot_options, + op_worker_close, + op_worker_get_type, + op_worker_post_message, + op_worker_recv_message, + op_worker_sync_fetch, } from "ext:core/ops"; const { ArrayPrototypeFilter, @@ -223,7 +228,7 @@ function workerClose() { } isClosing = true; - ops.op_worker_close(); + op_worker_close(); } function postMessage(message, transferOrOptions = {}) { @@ -252,15 +257,13 @@ function postMessage(message, transferOrOptions = {}) { } const { transfer } = options; const data = messagePort.serializeJsMessageData(message, transfer); - ops.op_worker_post_message(data); + op_worker_post_message(data); } let isClosing = false; let globalDispatchEvent; async function pollForMessages() { - const { op_worker_recv_message } = core.ensureFastOps(); - if (!globalDispatchEvent) { globalDispatchEvent = FunctionPrototypeBind( globalThis.dispatchEvent, @@ -309,7 +312,7 @@ async function pollForMessages() { let loadedMainWorkerScript = false; function importScripts(...urls) { - if (ops.op_worker_get_type() === "module") { + if (op_worker_get_type() === "module") { throw new TypeError("Can't import scripts in a module worker."); } @@ -329,7 +332,7 @@ function importScripts(...urls) { // imported scripts, so we use `loadedMainWorkerScript` to distinguish them. // TODO(andreubotella) Refactor worker creation so the main script isn't // loaded with `importScripts()`. - const scripts = ops.op_worker_sync_fetch( + const scripts = op_worker_sync_fetch( parsedUrls, !loadedMainWorkerScript, ); @@ -595,11 +598,6 @@ const NOT_IMPORTED_OPS = [ "op_test_op_sanitizer_report", "op_void_async", "op_void_sync", - "op_worker_close", - "op_worker_get_type", - "op_worker_post_message", - "op_worker_recv_message", - "op_worker_sync_fetch", "op_ws_send_pong", "op_jupyter_broadcast", "op_format_file_name", diff --git a/runtime/snapshot.rs b/runtime/snapshot.rs index 794de14d9e..b23b024ee1 100644 --- a/runtime/snapshot.rs +++ b/runtime/snapshot.rs @@ -253,6 +253,7 @@ pub fn create_runtime_snapshot( ops::tty::deno_tty::init_ops(), ops::http::deno_http_runtime::init_ops(), ops::bootstrap::deno_bootstrap::init_ops(Some(snapshot_options)), + ops::web_worker::deno_web_worker::init_ops(), ]; for extension in &mut extensions { diff --git a/runtime/worker.rs b/runtime/worker.rs index e6da93d786..449c50e10d 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -430,6 +430,11 @@ impl MainWorker { enable_testing_features, ), runtime::init_ops_and_esm(), + // NOTE(bartlomieju): this is done, just so that ops from this extension + // are available and importing them in `99_main.js` doesn't cause an + // error because they're not defined. Trying to use these ops in non-worker + // context will cause a panic. + ops::web_worker::deno_web_worker::init_ops_and_esm().disable(), ]; #[cfg(__runtime_js_sources)]