diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/Cargo.toml | 11 | ||||
-rw-r--r-- | runtime/build.rs | 33 | ||||
-rw-r--r-- | runtime/js.rs | 18 | ||||
-rw-r--r-- | runtime/web_worker.rs | 15 | ||||
-rw-r--r-- | runtime/worker.rs | 15 |
5 files changed, 61 insertions, 31 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 2d93f9dff..b2ef0637a 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -12,8 +12,17 @@ description = "Provides the deno runtime library" [features] # "fake" feature that allows to generate docs on docs.rs docsrs = [] -# feature that modifies the snapshot to allow extending it +# A feature that disables creation of startup snapshot during in the build script. +dont_create_runtime_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 a813d63cb..4678eab99 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -1,24 +1,25 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use deno_core::include_js_files; use std::env; use std::path::PathBuf; -// This is a shim that allows to generate documentation on docs.rs -mod not_docs { +#[cfg(all( + not(feature = "docsrs"), + not(feature = "dont_create_runtime_snapshot") +))] +mod startup_snapshot { use std::path::Path; use super::*; - use deno_cache::SqliteBackedCache; - use deno_core::snapshot_util::*; - use deno_core::Extension; - use deno_ast::MediaType; use deno_ast::ParseParams; use deno_ast::SourceTextInfo; + use deno_cache::SqliteBackedCache; use deno_core::error::AnyError; + use deno_core::include_js_files; + use deno_core::snapshot_util::*; + use deno_core::Extension; use deno_core::ExtensionFileSource; - use deno_core::ExtensionFileSourceCode; fn transpile_ts_for_snapshotting( file_source: &ExtensionFileSource, @@ -34,16 +35,15 @@ mod not_docs { file_source.specifier ), }; - - let ExtensionFileSourceCode::IncludedInBinary(code) = file_source.code; + let code = file_source.code.load()?; if !should_transpile { - return Ok(code.to_string()); + return Ok(code); } let parsed = deno_ast::parse_module(ParseParams { specifier: file_source.specifier.to_string(), - text_info: SourceTextInfo::from_string(code.to_string()), + text_info: SourceTextInfo::from_string(code), media_type, capture_tokens: false, scope_analysis: false, @@ -281,6 +281,7 @@ mod not_docs { #[cfg(not(feature = "snapshot_from_snapshot"))] { + use deno_core::ExtensionFileSourceCode; maybe_additional_extension = Some( Extension::builder("runtime_main") .dependencies(vec!["runtime"]) @@ -314,9 +315,13 @@ fn main() { // doesn't actually compile on docs.rs if env::var_os("DOCS_RS").is_some() { let snapshot_slice = &[]; + #[allow(clippy::needless_borrow)] std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap(); } - #[cfg(not(feature = "docsrs"))] - not_docs::build_snapshot(runtime_snapshot_path) + #[cfg(all( + not(feature = "docsrs"), + not(feature = "dont_create_runtime_snapshot") + ))] + startup_snapshot::build_snapshot(runtime_snapshot_path) } diff --git a/runtime/js.rs b/runtime/js.rs index 82e58a09c..57b1e7be4 100644 --- a/runtime/js.rs +++ b/runtime/js.rs @@ -1,16 +1,21 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +#[cfg(not(feature = "dont_create_runtime_snapshot"))] use deno_core::Snapshot; +#[cfg(not(feature = "dont_create_runtime_snapshot"))] use log::debug; +#[cfg(not(feature = "dont_create_runtime_snapshot"))] use once_cell::sync::Lazy; +#[cfg(not(feature = "dont_create_runtime_snapshot"))] +static COMPRESSED_RUNTIME_SNAPSHOT: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin")); + +#[cfg(not(feature = "dont_create_runtime_snapshot"))] pub static RUNTIME_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new( #[allow(clippy::uninit_vec)] #[cold] #[inline(never)] || { - static COMPRESSED_RUNTIME_SNAPSHOT: &[u8] = - include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin")); - let size = u32::from_le_bytes(COMPRESSED_RUNTIME_SNAPSHOT[0..4].try_into().unwrap()) as usize; @@ -29,10 +34,15 @@ pub static RUNTIME_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new( }, ); +#[cfg(not(feature = "dont_create_runtime_snapshot"))] pub fn deno_isolate_init() -> Snapshot { debug!("Deno isolate init with snapshots."); Snapshot::Static(&RUNTIME_SNAPSHOT) } -#[cfg(feature = "snapshot_from_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 df75d79c1..bcf999b3b 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -1,7 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use crate::colors; use crate::inspector_server::InspectorServer; -use crate::js; use crate::ops; use crate::ops::io::Stdio; use crate::permissions::PermissionsContainer; @@ -453,13 +452,17 @@ impl WebWorker { // Append exts extensions.extend(std::mem::take(&mut options.extensions)); + #[cfg(not(feature = "dont_create_runtime_snapshot"))] + let startup_snapshot = options + .startup_snapshot + .unwrap_or_else(crate::js::deno_isolate_init); + #[cfg(feature = "dont_create_runtime_snapshot")] + let startup_snapshot = options.startup_snapshot + .expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature."); + let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), - startup_snapshot: Some( - options - .startup_snapshot - .unwrap_or_else(js::deno_isolate_init), - ), + startup_snapshot: Some(startup_snapshot), source_map_getter: options.source_map_getter, get_error_class_fn: options.get_error_class_fn, shared_array_buffer_store: options.shared_array_buffer_store.clone(), diff --git a/runtime/worker.rs b/runtime/worker.rs index 141b5a650..d1998cd88 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -35,7 +35,6 @@ use deno_web::BlobStore; use log::debug; use crate::inspector_server::InspectorServer; -use crate::js; use crate::ops; use crate::ops::io::Stdio; use crate::permissions::PermissionsContainer; @@ -282,13 +281,17 @@ impl MainWorker { ]; extensions.extend(std::mem::take(&mut options.extensions)); + #[cfg(not(feature = "dont_create_runtime_snapshot"))] + let startup_snapshot = options + .startup_snapshot + .unwrap_or_else(crate::js::deno_isolate_init); + #[cfg(feature = "dont_create_runtime_snapshot")] + let startup_snapshot = options.startup_snapshot + .expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature."); + let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), - startup_snapshot: Some( - options - .startup_snapshot - .unwrap_or_else(js::deno_isolate_init), - ), + startup_snapshot: Some(startup_snapshot), source_map_getter: options.source_map_getter, get_error_class_fn: options.get_error_class_fn, shared_array_buffer_store: options.shared_array_buffer_store.clone(), |