From 29c9a5c90d88eba5da88ea41e681e7dd7ec170b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 16 Mar 2023 13:36:53 -0400 Subject: [PATCH] refactor: reorder op initialization (#18228) To be able to preserve "Deno.core.ops" we need to ensure that ops are registered in the same order in various places, otherwise we will get mismatch in external references ordering. Prerequisite for https://github.com/denoland/deno/pull/18080 --- cli/build.rs | 12 +++++++----- runtime/build.rs | 4 +++- runtime/web_worker.rs | 38 +++++++++++++++++++------------------- runtime/worker.rs | 35 ++++++++++++++++++----------------- 4 files changed, 47 insertions(+), 42 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index c3fa9fb48e..fdf57809a4 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -308,11 +308,12 @@ mod ts { } fn create_cli_snapshot(snapshot_path: PathBuf) { + // NOTE(bartlomieju): ordering is important here, keep it in sync with + // `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/build.rs`! let mut extensions: Vec = vec![ deno_webidl::init(), deno_console::init(), deno_url::init_ops(), - deno_tls::init_ops(), deno_web::init_ops::( deno_web::BlobStore::default(), Default::default(), @@ -327,18 +328,19 @@ fn create_cli_snapshot(snapshot_path: PathBuf) { deno_broadcast_channel::InMemoryBroadcastChannel::default(), false, // No --unstable. ), - deno_io::init_ops(Default::default()), - deno_fs::init_ops::(false), - deno_node::init_ops::(None), // No --unstable. - deno_node::init_polyfill_ops(), deno_ffi::init_ops::(false), deno_net::init_ops::( None, false, // No --unstable. None, ), + deno_tls::init_ops(), deno_napi::init_ops::(), deno_http::init_ops(), + deno_io::init_ops(Default::default()), + deno_fs::init_ops::(false), deno_flash::init_ops::(false), // No --unstable + deno_node::init_ops::(None), // No --unstable. + deno_node::init_polyfill_ops(), ]; let mut esm_files = include_js_files!( diff --git a/runtime/build.rs b/runtime/build.rs index 3392f5c13b..978fdda510 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -250,11 +250,12 @@ mod startup_snapshot { )) .build(); + // NOTE(bartlomieju): ordering is important here, keep it in sync with + // `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`! let mut extensions: Vec = vec![ deno_webidl::init_esm(), deno_console::init_esm(), deno_url::init_ops_and_esm(), - deno_tls::init_ops(), deno_web::init_ops_and_esm::( deno_web::BlobStore::default(), Default::default(), @@ -278,6 +279,7 @@ mod startup_snapshot { None, false, // No --unstable. None, ), + deno_tls::init_ops(), deno_napi::init_ops::(), deno_http::init_ops_and_esm(), deno_io::init_ops_and_esm(Default::default()), diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index d413ad11ff..5874d2aac9 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -383,6 +383,8 @@ impl WebWorker { CreateCache(Arc::new(create_cache_fn)) }); + // NOTE(bartlomieju): ordering is important here, keep it in sync with + // `runtime/build.rs`, `runtime/worker.rs` and `cli/build.rs`! let mut extensions: Vec = vec![ // Web APIs deno_webidl::init(), @@ -408,14 +410,26 @@ impl WebWorker { options.unsafely_ignore_certificate_errors.clone(), ), deno_webstorage::init_ops(None).disable(), + deno_crypto::init_ops(options.seed), + deno_webgpu::init_ops(unstable), deno_broadcast_channel::init_ops( options.broadcast_channel.clone(), unstable, ), - deno_crypto::init_ops(options.seed), - deno_webgpu::init_ops(unstable), - // ffi deno_ffi::init_ops::(unstable), + deno_net::init_ops::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_tls::init_ops(), + deno_napi::init_ops::(), + deno_http::init_ops(), + deno_io::init_ops(options.stdio), + deno_fs::init_ops::(unstable), + deno_flash::init_ops::(unstable), + deno_node::init_ops::(options.npm_resolver), + deno_node::init_polyfill_ops(), // Runtime ops that are always initialized for WebWorkers ops::web_worker::init(), ops::runtime::init(main_module.clone()), @@ -425,31 +439,17 @@ impl WebWorker { options.pre_execute_module_cb.clone(), options.format_js_error_fn.clone(), ), - // Extensions providing Deno.* features ops::fs_events::init(), - deno_fs::init_ops::(unstable), - deno_io::init_ops(options.stdio), - deno_tls::init_ops(), - deno_net::init_ops::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init_ops::(), - deno_node::init_polyfill_ops(), - deno_node::init_ops::(options.npm_resolver), ops::os::init_for_worker(), ops::permissions::init(), ops::process::init_ops(), ops::signal::init(), ops::tty::init(), - deno_http::init_ops(), - deno_flash::init_ops::(unstable), ops::http::init(), - // Permissions ext (worker specific state) - perm_ext, ]; + extensions.push(perm_ext); + // Append exts extensions.extend(std::mem::take(&mut options.extensions)); diff --git a/runtime/worker.rs b/runtime/worker.rs index 4bf7b00e8d..f5289ca359 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -203,6 +203,8 @@ impl MainWorker { CreateCache(Arc::new(create_cache_fn)) }); + // NOTE(bartlomieju): ordering is important here, keep it in sync with + // `runtime/build.rs`, `runtime/web_worker.rs` and `cli/build.rs`! let mut extensions = vec![ // Web APIs deno_webidl::init(), @@ -228,15 +230,27 @@ impl MainWorker { options.unsafely_ignore_certificate_errors.clone(), ), deno_webstorage::init_ops(options.origin_storage_dir.clone()), + deno_crypto::init_ops(options.seed), + deno_webgpu::init_ops(unstable), deno_broadcast_channel::init_ops( options.broadcast_channel.clone(), unstable, ), - deno_crypto::init_ops(options.seed), - deno_webgpu::init_ops(unstable), - // ffi deno_ffi::init_ops::(unstable), - // Runtime ops + deno_net::init_ops::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_tls::init_ops(), + deno_napi::init_ops::(), + deno_http::init_ops(), + deno_io::init_ops(options.stdio), + deno_fs::init_ops::(unstable), + deno_flash::init_ops::(unstable), + deno_node::init_ops::(options.npm_resolver), + deno_node::init_polyfill_ops(), + // Ops from this crate ops::runtime::init(main_module.clone()), ops::worker_host::init( options.create_web_worker_cb.clone(), @@ -245,24 +259,11 @@ impl MainWorker { options.format_js_error_fn.clone(), ), ops::fs_events::init(), - deno_fs::init_ops::(unstable), - deno_io::init_ops(options.stdio), - deno_tls::init_ops(), - deno_net::init_ops::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init_ops::(), - deno_node::init_ops::(options.npm_resolver), - deno_node::init_polyfill_ops(), ops::os::init(exit_code.clone()), ops::permissions::init(), ops::process::init_ops(), ops::signal::init(), ops::tty::init(), - deno_http::init_ops(), - deno_flash::init_ops::(unstable), ops::http::init(), ];