From 60bf79c18410fd332b6b9b7c222e6d3d62bfe3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 14 Jun 2023 00:36:16 +0200 Subject: =?UTF-8?q?Revert=20"refactor(core):=20cleanup=20feature=20flags?= =?UTF-8?q?=20for=20js=20source=20inclusion=E2=80=A6=20(#19490)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … (#19463)" This reverts commit ceb03cfb037cf7024a5048b17b508ddda59cfa05. This is being reverted because it causes 3.5Mb increase in the binary size, due to runtime JS code being included in the binary, even though it's already snapshotted. CC @nayeemrmn --- runtime/Cargo.toml | 11 +++++- runtime/build.rs | 59 +++++++++++++++------------- runtime/examples/extension_with_esm/main.rs | 2 +- runtime/examples/extension_with_ops/main.rs | 2 +- runtime/js.rs | 7 ++++ runtime/web_worker.rs | 60 ++++++++++++++--------------- runtime/worker.rs | 58 ++++++++++++++-------------- 7 files changed, 110 insertions(+), 89 deletions(-) (limited to 'runtime') diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index e66698a01..77571b210 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -14,8 +14,15 @@ description = "Provides the deno runtime library" docsrs = [] # A feature that disables creation of startup snapshot during in the build script. dont_create_runtime_snapshot = [] -# Enable to exclude `js/99_main.js` from the generated snapshot. -exclude_js_main_from_snapshot = [] +# A feature that changes how startup snapshot is generated, that allows +# extending it in embedder crates. +snapshot_from_snapshot = [] +# A feature that disables embedding of the JavaScript source files in the binary. +# With this feature enabled, the sources must be consumed during build time, +# by creating a startup snapshot. +include_js_files_for_snapshotting = [ + "deno_core/include_js_files_for_snapshotting", +] [lib] name = "deno_runtime" diff --git a/runtime/build.rs b/runtime/build.rs index d7b4018fb..f656682a1 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -35,7 +35,7 @@ mod startup_snapshot { file_source.specifier ), }; - let code = file_source.load(); + let code = file_source.load()?; if !should_transpile { return Ok(code); @@ -264,20 +264,22 @@ mod startup_snapshot { ], ); - #[cfg(not(feature = "exclude_js_main_from_snapshot"))] + #[cfg(not(feature = "snapshot_from_snapshot"))] deno_core::extension!( runtime_main, deps = [runtime], customizer = |ext: &mut deno_core::ExtensionBuilder| { ext.esm(vec![ExtensionFileSource { specifier: "ext:runtime_main/js/99_main.js", - code: include_str!("js/99_main.js"), + code: deno_core::ExtensionFileSourceCode::IncludedInBinary( + include_str!("js/99_main.js"), + ), }]); ext.esm_entry_point("ext:runtime_main/js/99_main.js"); } ); - #[cfg(feature = "exclude_js_main_from_snapshot")] + #[cfg(feature = "snapshot_from_snapshot")] deno_core::extension!( runtime_main, deps = [runtime], @@ -292,48 +294,50 @@ mod startup_snapshot { // `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`! let fs = std::sync::Arc::new(deno_fs::RealFs); let extensions: Vec = vec![ - deno_webidl::deno_webidl::init(), - deno_console::deno_console::init(), - deno_url::deno_url::init(), - deno_web::deno_web::init::( + deno_webidl::deno_webidl::init_ops_and_esm(), + deno_console::deno_console::init_ops_and_esm(), + deno_url::deno_url::init_ops_and_esm(), + deno_web::deno_web::init_ops_and_esm::( deno_web::BlobStore::default(), Default::default(), ), - deno_fetch::deno_fetch::init::(Default::default()), - deno_cache::deno_cache::init::(None), - deno_websocket::deno_websocket::init::( + deno_fetch::deno_fetch::init_ops_and_esm::( + Default::default(), + ), + deno_cache::deno_cache::init_ops_and_esm::(None), + deno_websocket::deno_websocket::init_ops_and_esm::( "".to_owned(), None, None, ), - deno_webstorage::deno_webstorage::init(None), - deno_crypto::deno_crypto::init(None), - deno_broadcast_channel::deno_broadcast_channel::init( + deno_webstorage::deno_webstorage::init_ops_and_esm(None), + deno_crypto::deno_crypto::init_ops_and_esm(None), + deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm( deno_broadcast_channel::InMemoryBroadcastChannel::default(), false, // No --unstable. ), - deno_ffi::deno_ffi::init::(false), - deno_net::deno_net::init::( + deno_ffi::deno_ffi::init_ops_and_esm::(false), + deno_net::deno_net::init_ops_and_esm::( None, false, // No --unstable. None, ), - deno_tls::deno_tls::init(), - deno_kv::deno_kv::init( + deno_tls::deno_tls::init_ops_and_esm(), + deno_kv::deno_kv::init_ops_and_esm( deno_kv::sqlite::SqliteDbHandler::::new(None), false, // No --unstable ), - deno_napi::deno_napi::init::(), - deno_http::deno_http::init::(), - deno_io::deno_io::init(Default::default()), - deno_fs::deno_fs::init::(false, fs.clone()), - runtime::init(), + deno_napi::deno_napi::init_ops_and_esm::(), + deno_http::deno_http::init_ops_and_esm::(), + deno_io::deno_io::init_ops_and_esm(Default::default()), + deno_fs::deno_fs::init_ops_and_esm::(false, fs.clone()), + runtime::init_ops_and_esm(), // FIXME(bartlomieju): these extensions are specified last, because they // depend on `runtime`, even though it should be other way around - deno_node::deno_node::init::(None, fs), - runtime_main::init(), + deno_node::deno_node::init_ops_and_esm::(None, fs), + runtime_main::init_ops_and_esm(), ]; - create_snapshot(CreateSnapshotOptions { + let output = create_snapshot(CreateSnapshotOptions { cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"), snapshot_path, startup_snapshot: None, @@ -341,6 +345,9 @@ mod startup_snapshot { compression_cb: None, snapshot_module_load_cb: Some(Box::new(transpile_ts_for_snapshotting)), }); + for path in output.files_loaded_during_snapshot { + println!("cargo:rerun-if-changed={}", path.display()); + } } } diff --git a/runtime/examples/extension_with_esm/main.rs b/runtime/examples/extension_with_esm/main.rs index e2b7fccd1..6b21460a3 100644 --- a/runtime/examples/extension_with_esm/main.rs +++ b/runtime/examples/extension_with_esm/main.rs @@ -26,7 +26,7 @@ async fn main() -> Result<(), AnyError> { PermissionsContainer::allow_all(), WorkerOptions { module_loader: Rc::new(FsModuleLoader), - extensions: vec![hello_runtime::init()], + extensions: vec![hello_runtime::init_ops_and_esm()], ..Default::default() }, ); diff --git a/runtime/examples/extension_with_ops/main.rs b/runtime/examples/extension_with_ops/main.rs index 53b6ca4fc..1feb4ba27 100644 --- a/runtime/examples/extension_with_ops/main.rs +++ b/runtime/examples/extension_with_ops/main.rs @@ -28,7 +28,7 @@ async fn main() -> Result<(), AnyError> { PermissionsContainer::allow_all(), WorkerOptions { module_loader: Rc::new(FsModuleLoader), - extensions: vec![hello_runtime::init()], + extensions: vec![hello_runtime::init_ops()], ..Default::default() }, ); diff --git a/runtime/js.rs b/runtime/js.rs index 6aaa32f5f..def2724ce 100644 --- a/runtime/js.rs +++ b/runtime/js.rs @@ -13,3 +13,10 @@ pub fn deno_isolate_init() -> Snapshot { debug!("Deno isolate init with snapshots."); Snapshot::Static(RUNTIME_SNAPSHOT) } + +#[cfg(not(feature = "include_js_files_for_snapshotting"))] +pub static SOURCE_CODE_FOR_99_MAIN_JS: &str = include_str!("js/99_main.js"); + +#[cfg(feature = "include_js_files_for_snapshotting")] +pub static PATH_FOR_99_MAIN_JS: &str = + concat!(env!("CARGO_MANIFEST_DIR"), "/js/99_main.js"); diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 796e23c3c..36f9718b5 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -399,14 +399,14 @@ impl WebWorker { // `runtime/build.rs`, `runtime/worker.rs` and `cli/build.rs`! let mut extensions: Vec = vec![ // Web APIs - deno_webidl::deno_webidl::init(), - deno_console::deno_console::init(), - deno_url::deno_url::init(), - deno_web::deno_web::init::( + deno_webidl::deno_webidl::init_ops(), + deno_console::deno_console::init_ops(), + deno_url::deno_url::init_ops(), + deno_web::deno_web::init_ops::( options.blob_store.clone(), Some(main_module.clone()), ), - deno_fetch::deno_fetch::init::( + deno_fetch::deno_fetch::init_ops::( deno_fetch::Options { user_agent: options.bootstrap.user_agent.clone(), root_cert_store_provider: options.root_cert_store_provider.clone(), @@ -417,57 +417,57 @@ impl WebWorker { ..Default::default() }, ), - deno_cache::deno_cache::init::(create_cache), - deno_websocket::deno_websocket::init::( + deno_cache::deno_cache::init_ops::(create_cache), + deno_websocket::deno_websocket::init_ops::( options.bootstrap.user_agent.clone(), options.root_cert_store_provider.clone(), options.unsafely_ignore_certificate_errors.clone(), ), - deno_webstorage::deno_webstorage::init(None).disable(), - deno_crypto::deno_crypto::init(options.seed), - deno_broadcast_channel::deno_broadcast_channel::init( + deno_webstorage::deno_webstorage::init_ops(None).disable(), + deno_crypto::deno_crypto::init_ops(options.seed), + deno_broadcast_channel::deno_broadcast_channel::init_ops( options.broadcast_channel.clone(), unstable, ), - deno_ffi::deno_ffi::init::(unstable), - deno_net::deno_net::init::( + deno_ffi::deno_ffi::init_ops::(unstable), + deno_net::deno_net::init_ops::( options.root_cert_store_provider.clone(), unstable, options.unsafely_ignore_certificate_errors.clone(), ), - deno_tls::deno_tls::init(), - deno_kv::deno_kv::init( + deno_tls::deno_tls::init_ops(), + deno_kv::deno_kv::init_ops( SqliteDbHandler::::new(None), unstable, ), - deno_napi::deno_napi::init::(), - deno_http::deno_http::init::(), - deno_io::deno_io::init(Some(options.stdio)), - deno_fs::deno_fs::init::( + deno_napi::deno_napi::init_ops::(), + deno_http::deno_http::init_ops::(), + deno_io::deno_io::init_ops(Some(options.stdio)), + deno_fs::deno_fs::init_ops::( unstable, options.fs.clone(), ), - deno_node::deno_node::init::( + deno_node::deno_node::init_ops::( options.npm_resolver, options.fs, ), // Runtime ops that are always initialized for WebWorkers - ops::web_worker::deno_web_worker::init(), - ops::runtime::deno_runtime::init(main_module.clone()), - ops::worker_host::deno_worker_host::init( + ops::web_worker::deno_web_worker::init_ops(), + ops::runtime::deno_runtime::init_ops(main_module.clone()), + ops::worker_host::deno_worker_host::init_ops( options.create_web_worker_cb.clone(), options.preload_module_cb.clone(), options.pre_execute_module_cb.clone(), options.format_js_error_fn.clone(), ), - ops::fs_events::deno_fs_events::init(), - ops::os::deno_os_worker::init(), - ops::permissions::deno_permissions::init(), - ops::process::deno_process::init(), - ops::signal::deno_signal::init(), - ops::tty::deno_tty::init(), - ops::http::deno_http_runtime::init(), - deno_permissions_web_worker::init( + ops::fs_events::deno_fs_events::init_ops(), + ops::os::deno_os_worker::init_ops(), + ops::permissions::deno_permissions::init_ops(), + ops::process::deno_process::init_ops(), + ops::signal::deno_signal::init_ops(), + ops::tty::deno_tty::init_ops(), + ops::http::deno_http_runtime::init_ops(), + deno_permissions_web_worker::init_ops( permissions, unstable, enable_testing_features, diff --git a/runtime/worker.rs b/runtime/worker.rs index 4b78c1a94..10375818d 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -235,14 +235,14 @@ impl MainWorker { // `runtime/build.rs`, `runtime/web_worker.rs` and `cli/build.rs`! let mut extensions = vec![ // Web APIs - deno_webidl::deno_webidl::init(), - deno_console::deno_console::init(), - deno_url::deno_url::init(), - deno_web::deno_web::init::( + deno_webidl::deno_webidl::init_ops(), + deno_console::deno_console::init_ops(), + deno_url::deno_url::init_ops(), + deno_web::deno_web::init_ops::( options.blob_store.clone(), options.bootstrap.location.clone(), ), - deno_fetch::deno_fetch::init::( + deno_fetch::deno_fetch::init_ops::( deno_fetch::Options { user_agent: options.bootstrap.user_agent.clone(), root_cert_store_provider: options.root_cert_store_provider.clone(), @@ -253,60 +253,60 @@ impl MainWorker { ..Default::default() }, ), - deno_cache::deno_cache::init::(create_cache), - deno_websocket::deno_websocket::init::( + deno_cache::deno_cache::init_ops::(create_cache), + deno_websocket::deno_websocket::init_ops::( options.bootstrap.user_agent.clone(), options.root_cert_store_provider.clone(), options.unsafely_ignore_certificate_errors.clone(), ), - deno_webstorage::deno_webstorage::init( + deno_webstorage::deno_webstorage::init_ops( options.origin_storage_dir.clone(), ), - deno_crypto::deno_crypto::init(options.seed), - deno_broadcast_channel::deno_broadcast_channel::init( + deno_crypto::deno_crypto::init_ops(options.seed), + deno_broadcast_channel::deno_broadcast_channel::init_ops( options.broadcast_channel.clone(), unstable, ), - deno_ffi::deno_ffi::init::(unstable), - deno_net::deno_net::init::( + deno_ffi::deno_ffi::init_ops::(unstable), + deno_net::deno_net::init_ops::( options.root_cert_store_provider.clone(), unstable, options.unsafely_ignore_certificate_errors.clone(), ), - deno_tls::deno_tls::init(), - deno_kv::deno_kv::init( + deno_tls::deno_tls::init_ops(), + deno_kv::deno_kv::init_ops( SqliteDbHandler::::new( options.origin_storage_dir.clone(), ), unstable, ), - deno_napi::deno_napi::init::(), - deno_http::deno_http::init::(), - deno_io::deno_io::init(Some(options.stdio)), - deno_fs::deno_fs::init::( + deno_napi::deno_napi::init_ops::(), + deno_http::deno_http::init_ops::(), + deno_io::deno_io::init_ops(Some(options.stdio)), + deno_fs::deno_fs::init_ops::( unstable, options.fs.clone(), ), - deno_node::deno_node::init::( + deno_node::deno_node::init_ops::( options.npm_resolver, options.fs, ), // Ops from this crate - ops::runtime::deno_runtime::init(main_module.clone()), - ops::worker_host::deno_worker_host::init( + ops::runtime::deno_runtime::init_ops(main_module.clone()), + ops::worker_host::deno_worker_host::init_ops( 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::deno_fs_events::init(), - ops::os::deno_os::init(exit_code.clone()), - ops::permissions::deno_permissions::init(), - ops::process::deno_process::init(), - ops::signal::deno_signal::init(), - ops::tty::deno_tty::init(), - ops::http::deno_http_runtime::init(), - deno_permissions_worker::init( + ops::fs_events::deno_fs_events::init_ops(), + ops::os::deno_os::init_ops(exit_code.clone()), + ops::permissions::deno_permissions::init_ops(), + ops::process::deno_process::init_ops(), + ops::signal::deno_signal::init_ops(), + ops::tty::deno_tty::init_ops(), + ops::http::deno_http_runtime::init_ops(), + deno_permissions_worker::init_ops( permissions, unstable, enable_testing_features, -- cgit v1.2.3