From 2cd5f64695542e52b7f65bd8bd3eb2156a025a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Mar 2023 16:09:45 -0400 Subject: [PATCH] fix: Split extension registration and snapshotting (#18098) This commit partially reverts changes from https://github.com/denoland/deno/pull/18095. Turns out I made a mistake that became apparent when working on removing "RuntimeOptions::extensions_with_js" in a follow up. --- cli/build.rs | 46 ++++----- ext/napi/lib.rs | 2 +- ext/tls/lib.rs | 2 +- runtime/build.rs | 4 +- runtime/web_worker.rs | 235 +++++++++++++----------------------------- runtime/worker.rs | 226 ++++++++++++---------------------------- 6 files changed, 161 insertions(+), 354 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 6687b1e443..846224af22 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -322,40 +322,36 @@ mod ts { fn create_cli_snapshot(snapshot_path: PathBuf) { let extensions: Vec = vec![ - deno_webidl::init_esm(), - deno_console::init_esm(), - deno_url::init_ops_and_esm(), - deno_tls::init(), - deno_web::init_ops_and_esm::( + deno_webidl::init(), + deno_console::init(), + deno_url::init_ops(), + deno_tls::init_ops(), + deno_web::init_ops::( deno_web::BlobStore::default(), Default::default(), ), - deno_fetch::init_ops_and_esm::(Default::default()), - deno_cache::init_ops_and_esm::(None), - deno_websocket::init_ops_and_esm::( - "".to_owned(), - None, - None, - ), - deno_webstorage::init_ops_and_esm(None), - deno_crypto::init_ops_and_esm(None), - deno_webgpu::init_ops_and_esm(false), - deno_broadcast_channel::init_ops_and_esm( + deno_fetch::init_ops::(Default::default()), + deno_cache::init_ops::(None), + deno_websocket::init_ops::("".to_owned(), None, None), + deno_webstorage::init_ops(None), + deno_crypto::init_ops(None), + deno_webgpu::init_ops(false), + deno_broadcast_channel::init_ops( deno_broadcast_channel::InMemoryBroadcastChannel::default(), false, // No --unstable. ), - deno_io::init_ops_and_esm(Default::default()), - deno_fs::init_ops_and_esm::(false), - deno_node::init_ops_and_esm::(None), // No --unstable. - deno_node::init_polyfill_ops_and_esm(), - deno_ffi::init_ops_and_esm::(false), - deno_net::init_ops_and_esm::( + 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_napi::init::(), - deno_http::init_ops_and_esm(), - deno_flash::init_ops_and_esm::(false), // No --unstable + deno_napi::init_ops::(), + deno_http::init_ops(), + deno_flash::init_ops::(false), // No --unstable ]; let mut esm_files = include_js_files!( diff --git a/ext/napi/lib.rs b/ext/napi/lib.rs index 471ebfeabc..41004638e1 100644 --- a/ext/napi/lib.rs +++ b/ext/napi/lib.rs @@ -514,7 +514,7 @@ impl Env { } } -pub fn init() -> Extension { +pub fn init_ops() -> Extension { Extension::builder(env!("CARGO_PKG_NAME")) .ops(vec![op_napi_open::decl::

()]) .event_loop_middleware(|op_state_rc, cx| { diff --git a/ext/tls/lib.rs b/ext/tls/lib.rs index b762ac90ad..fb4e8759d9 100644 --- a/ext/tls/lib.rs +++ b/ext/tls/lib.rs @@ -36,7 +36,7 @@ use std::sync::Arc; use std::time::SystemTime; /// This extension has no runtime apis, it only exports some shared native functions. -pub fn init() -> Extension { +pub fn init_ops() -> Extension { Extension::builder(env!("CARGO_PKG_NAME")).build() } diff --git a/runtime/build.rs b/runtime/build.rs index 788174d1ba..784e46d4fc 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -254,7 +254,7 @@ mod startup_snapshot { deno_webidl::init_esm(), deno_console::init_esm(), deno_url::init_ops_and_esm(), - deno_tls::init(), + deno_tls::init_ops(), deno_web::init_ops_and_esm::( deno_web::BlobStore::default(), Default::default(), @@ -278,7 +278,7 @@ mod startup_snapshot { None, false, // No --unstable. None, ), - deno_napi::init::(), + deno_napi::init_ops::(), deno_http::init_ops_and_esm(), deno_io::init_ops_and_esm(Default::default()), deno_fs::init_ops_and_esm::(false), diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index f16ffd5a0c..d413ad11ff 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -346,168 +346,6 @@ pub struct WebWorkerOptions { pub stdio: Stdio, } -#[cfg(feature = "dont_create_runtime_snapshot")] -fn get_extensions( - options: &mut WebWorkerOptions, - unstable: bool, - main_module: ModuleSpecifier, -) -> Vec { - let create_cache = options.cache_storage_dir.take().map(|storage_dir| { - let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); - CreateCache(Arc::new(create_cache_fn)) - }); - - vec![ - // Web APIs - deno_webidl::init(), - deno_console::init(), - deno_url::init_ops(), - deno_web::init_ops::( - options.blob_store.clone(), - Some(main_module.clone()), - ), - deno_fetch::init_ops::(deno_fetch::Options { - user_agent: options.bootstrap.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), - ..Default::default() - }), - deno_cache::init_ops::(create_cache), - deno_websocket::init_ops::( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init_ops(None).disable(), - 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 that are always initialized for WebWorkers - ops::web_worker::init(), - ops::runtime::init(main_module), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.preload_module_cb.clone(), - 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(std::mem::take(&mut options.stdio)), - deno_tls::init(), - deno_net::init_ops::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - // TODO(bartlomieju): thes two should be conditional on `dont_create_runtime_snapshot` - // cargo feature and should use `init_polyfill_ops` or `init_polyfill_ops_and_esm` - // if the feature is enabled - deno_node::init_polyfill_ops(), - deno_node::init_ops::(options.npm_resolver.take()), - 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(), - ] -} - -#[cfg(not(feature = "dont_create_runtime_snapshot"))] -fn get_extensions( - options: &mut WebWorkerOptions, - unstable: bool, - main_module: ModuleSpecifier, -) -> Vec { - let create_cache = options.cache_storage_dir.take().map(|storage_dir| { - let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); - CreateCache(Arc::new(create_cache_fn)) - }); - - vec![ - // Web APIs - deno_webidl::init_esm(), - deno_console::init_esm(), - deno_url::init_ops_and_esm(), - deno_web::init_ops_and_esm::( - options.blob_store.clone(), - Some(main_module.clone()), - ), - deno_fetch::init_ops_and_esm::(deno_fetch::Options { - user_agent: options.bootstrap.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), - ..Default::default() - }), - deno_cache::init_ops_and_esm::(create_cache), - deno_websocket::init_ops_and_esm::( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init_ops_and_esm(None).disable(), - deno_broadcast_channel::init_ops_and_esm( - options.broadcast_channel.clone(), - unstable, - ), - deno_crypto::init_ops_and_esm(options.seed), - deno_webgpu::init_ops_and_esm(unstable), - // ffi - deno_ffi::init_ops_and_esm::(unstable), - // Runtime ops that are always initialized for WebWorkers - ops::web_worker::init(), - ops::runtime::init(main_module), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.preload_module_cb.clone(), - options.pre_execute_module_cb.clone(), - options.format_js_error_fn.clone(), - ), - // Extensions providing Deno.* features - ops::fs_events::init(), - deno_fs::init_ops_and_esm::(unstable), - deno_io::init_ops_and_esm(std::mem::take(&mut options.stdio)), - deno_tls::init(), - deno_net::init_ops_and_esm::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - // TODO(bartlomieju): thes two should be conditional on `dont_create_runtime_snapshot` - // cargo feature and should use `init_polyfill_ops` or `init_polyfill_ops_and_esm` - // if the feature is enabled - deno_node::init_polyfill_ops_and_esm(), - deno_node::init_ops_and_esm::( - options.npm_resolver.take(), - ), - ops::os::init_for_worker(), - ops::permissions::init(), - ops::process::init_ops(), - ops::signal::init(), - ops::tty::init(), - deno_http::init_ops_and_esm(), - deno_flash::init_ops_and_esm::(unstable), - ops::http::init(), - ] -} - impl WebWorker { pub fn bootstrap_from_options( name: String, @@ -540,10 +378,77 @@ impl WebWorker { state.put(ops::TestingFeaturesEnabled(enable_testing_features)); }) .build(); + let create_cache = options.cache_storage_dir.map(|storage_dir| { + let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); + CreateCache(Arc::new(create_cache_fn)) + }); - let mut extensions = - get_extensions(&mut options, unstable, main_module.clone()); - extensions.push(perm_ext); + let mut extensions: Vec = vec![ + // Web APIs + deno_webidl::init(), + deno_console::init(), + deno_url::init_ops(), + deno_web::init_ops::( + options.blob_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init_ops::(deno_fetch::Options { + user_agent: options.bootstrap.user_agent.clone(), + root_cert_store: options.root_cert_store.clone(), + unsafely_ignore_certificate_errors: options + .unsafely_ignore_certificate_errors + .clone(), + file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), + ..Default::default() + }), + deno_cache::init_ops::(create_cache), + deno_websocket::init_ops::( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops(None).disable(), + 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 that are always initialized for WebWorkers + ops::web_worker::init(), + ops::runtime::init(main_module.clone()), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.preload_module_cb.clone(), + 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, + ]; // Append exts extensions.extend(std::mem::take(&mut options.extensions)); diff --git a/runtime/worker.rs b/runtime/worker.rs index 3d30b95a36..ff55e27cc8 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -183,160 +183,6 @@ impl Default for WorkerOptions { } } -#[cfg(not(feature = "dont_create_runtime_snapshot"))] -fn get_extensions( - options: &mut WorkerOptions, - unstable: bool, - exit_code: ExitCode, - main_module: ModuleSpecifier, -) -> Vec { - let create_cache = options.cache_storage_dir.take().map(|storage_dir| { - let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); - CreateCache(Arc::new(create_cache_fn)) - }); - - vec![ - // Web APIs - deno_webidl::init(), - deno_console::init(), - deno_url::init_ops(), - deno_web::init_ops::( - options.blob_store.clone(), - options.bootstrap.location.clone(), - ), - deno_fetch::init_ops::(deno_fetch::Options { - user_agent: options.bootstrap.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), - ..Default::default() - }), - deno_cache::init_ops::(create_cache), - deno_websocket::init_ops::( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init_ops(options.origin_storage_dir.clone()), - 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 - ops::runtime::init(main_module), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.web_worker_preload_module_cb.clone(), - options.web_worker_pre_execute_module_cb.clone(), - options.format_js_error_fn.clone(), - ), - ops::fs_events::init(), - deno_fs::init_ops::(unstable), - deno_io::init_ops(std::mem::take(&mut options.stdio)), - deno_tls::init(), - deno_net::init_ops::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - deno_node::init_ops::(options.npm_resolver.take()), - ops::os::init(exit_code), - 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(), - deno_node::init_polyfill_ops(), - ] -} - -#[cfg(feature = "dont_create_runtime_snapshot")] -fn get_extensions( - options: &mut WorkerOptions, - unstable: bool, - exit_code: ExitCode, - main_module: ModuleSpecifier, -) -> Vec { - let create_cache = options.cache_storage_dir.take().map(|storage_dir| { - let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); - CreateCache(Arc::new(create_cache_fn)) - }); - - vec![ - // Web APIs - deno_webidl::init_esm(), - deno_console::init_esm(), - deno_url::init_ops_and_esm(), - deno_web::init_ops_and_esm::( - options.blob_store.clone(), - options.bootstrap.location.clone(), - ), - deno_fetch::init_ops_and_esm::(deno_fetch::Options { - user_agent: options.bootstrap.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), - ..Default::default() - }), - deno_cache::init_ops_and_esm::(create_cache), - deno_websocket::init_ops_and_esm::( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init_ops_and_esm(options.origin_storage_dir.clone()), - deno_broadcast_channel::init_ops_and_esm( - options.broadcast_channel.clone(), - unstable, - ), - deno_crypto::init_ops_and_esm(options.seed), - deno_webgpu::init_ops_and_esm(unstable), - // ffi - deno_ffi::init_ops_and_esm::(unstable), - // Runtime ops - ops::runtime::init(main_module), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.web_worker_preload_module_cb.clone(), - options.web_worker_pre_execute_module_cb.clone(), - options.format_js_error_fn.clone(), - ), - ops::fs_events::init(), - deno_fs::init_ops_and_esm::(unstable), - deno_io::init_ops_and_esm(std::mem::take(&mut options.stdio)), - deno_tls::init(), - deno_net::init_ops_and_esm::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - deno_node::init_ops_and_esm::( - options.npm_resolver.take(), - ), - ops::os::init(exit_code), - ops::permissions::init(), - ops::process::init_ops(), - ops::signal::init(), - ops::tty::init(), - deno_http::init_ops_and_esm(), - deno_flash::init_ops_and_esm::(unstable), - ops::http::init(), - deno_node::init_polyfill_ops_and_esm(), - ] -} - impl MainWorker { pub fn bootstrap_from_options( main_module: ModuleSpecifier, @@ -365,13 +211,73 @@ impl MainWorker { }) .build(); let exit_code = ExitCode(Arc::new(AtomicI32::new(0))); + let create_cache = options.cache_storage_dir.map(|storage_dir| { + let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); + CreateCache(Arc::new(create_cache_fn)) + }); - let mut extensions = get_extensions( - &mut options, - unstable, - exit_code.clone(), - main_module.clone(), - ); + let mut extensions = vec![ + // Web APIs + deno_webidl::init(), + deno_console::init(), + deno_url::init_ops(), + deno_web::init_ops::( + options.blob_store.clone(), + options.bootstrap.location.clone(), + ), + deno_fetch::init_ops::(deno_fetch::Options { + user_agent: options.bootstrap.user_agent.clone(), + root_cert_store: options.root_cert_store.clone(), + unsafely_ignore_certificate_errors: options + .unsafely_ignore_certificate_errors + .clone(), + file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), + ..Default::default() + }), + deno_cache::init_ops::(create_cache), + deno_websocket::init_ops::( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops(options.origin_storage_dir.clone()), + 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 + ops::runtime::init(main_module.clone()), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.web_worker_preload_module_cb.clone(), + options.web_worker_pre_execute_module_cb.clone(), + 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(), + ]; extensions.push(perm_ext);