diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/Cargo.toml | 19 | ||||
-rw-r--r-- | runtime/build.rs | 222 | ||||
-rw-r--r-- | runtime/examples/hello_runtime.rs | 1 | ||||
-rw-r--r-- | runtime/js.rs | 92 | ||||
-rw-r--r-- | runtime/web_worker.rs | 5 | ||||
-rw-r--r-- | runtime/worker.rs | 6 |
6 files changed, 56 insertions, 289 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 7cb7b7820..652b2342d 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -21,25 +21,6 @@ path = "lib.rs" name = "hello_runtime" path = "examples/hello_runtime.rs" -[build-dependencies] -deno_broadcast_channel = { version = "0.52.0", path = "../ext/broadcast_channel" } -deno_console = { version = "0.58.0", path = "../ext/console" } -deno_core = { version = "0.140.0", path = "../core" } -deno_crypto = { version = "0.72.0", path = "../ext/crypto" } -deno_fetch = { version = "0.81.0", path = "../ext/fetch" } -deno_ffi = { version = "0.45.0", path = "../ext/ffi" } -deno_http = { version = "0.52.0", path = "../ext/http" } -deno_net = { version = "0.50.0", path = "../ext/net" } -deno_tls = { version = "0.45.0", path = "../ext/tls" } -deno_url = { version = "0.58.0", path = "../ext/url" } -deno_web = { version = "0.89.0", path = "../ext/web" } -deno_webgpu = { version = "0.59.0", path = "../ext/webgpu" } -deno_webidl = { version = "0.58.0", path = "../ext/webidl" } -deno_websocket = { version = "0.63.0", path = "../ext/websocket" } -deno_webstorage = { version = "0.53.0", path = "../ext/webstorage" } - -lzzzz = '1.0' - [target.'cfg(windows)'.build-dependencies] winres = "0.1.11" winapi = "0.3.9" diff --git a/runtime/build.rs b/runtime/build.rs index eea7a3602..0cb113af1 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -1,219 +1,17 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use std::env; -use std::path::Path; -use std::path::PathBuf; - -// This is a shim that allows to generate documentation on docs.rs -#[cfg(not(feature = "docsrs"))] -mod not_docs { - use super::*; - use deno_core::Extension; - use deno_core::JsRuntime; - use deno_core::RuntimeOptions; - - // TODO(bartlomieju): this module contains a lot of duplicated - // logic with `cli/build.rs`, factor out to `deno_core`. - fn create_snapshot( - mut js_runtime: JsRuntime, - snapshot_path: &Path, - files: Vec<PathBuf>, - ) { - // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the - // workspace root. - let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); - for file in files { - println!("cargo:rerun-if-changed={}", file.display()); - let display_path = file.strip_prefix(display_root).unwrap(); - let display_path_str = display_path.display().to_string(); - js_runtime - .execute_script( - &("deno:".to_string() + &display_path_str.replace('\\', "/")), - &std::fs::read_to_string(&file).unwrap(), - ) - .unwrap(); - } - - let snapshot = js_runtime.snapshot(); - let snapshot_slice: &[u8] = &*snapshot; - println!("Snapshot size: {}", snapshot_slice.len()); - - let compressed_snapshot_with_size = { - let mut vec = vec![]; - - vec.extend_from_slice( - &u32::try_from(snapshot.len()) - .expect("snapshot larger than 4gb") - .to_le_bytes(), - ); - - lzzzz::lz4_hc::compress_to_vec( - snapshot_slice, - &mut vec, - lzzzz::lz4_hc::CLEVEL_MAX, - ) - .expect("snapshot compression failed"); - - vec - }; - - println!( - "Snapshot compressed size: {}", - compressed_snapshot_with_size.len() - ); - - std::fs::write(&snapshot_path, compressed_snapshot_with_size).unwrap(); - println!("Snapshot written to: {} ", snapshot_path.display()); - } - - struct Permissions; - - impl deno_fetch::FetchPermissions for Permissions { - fn check_net_url( - &mut self, - _url: &deno_core::url::Url, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - - fn check_read( - &mut self, - _p: &Path, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - } - - impl deno_websocket::WebSocketPermissions for Permissions { - fn check_net_url( - &mut self, - _url: &deno_core::url::Url, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - } - - impl deno_web::TimersPermission for Permissions { - fn allow_hrtime(&mut self) -> bool { - unreachable!("snapshotting!") - } - - fn check_unstable( - &self, - _state: &deno_core::OpState, - _api_name: &'static str, - ) { - unreachable!("snapshotting!") - } - } - - impl deno_ffi::FfiPermissions for Permissions { - fn check( - &mut self, - _path: Option<&Path>, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - } - - impl deno_net::NetPermissions for Permissions { - fn check_net<T: AsRef<str>>( - &mut self, - _host: &(T, Option<u16>), - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - - fn check_read( - &mut self, - _p: &Path, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - - fn check_write( - &mut self, - _p: &Path, - ) -> Result<(), deno_core::error::AnyError> { - unreachable!("snapshotting!") - } - } - - fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) { - let extensions: Vec<Extension> = vec![ - deno_webidl::init(), - deno_console::init(), - deno_url::init(), - deno_tls::init(), - deno_web::init::<Permissions>( - deno_web::BlobStore::default(), - Default::default(), - ), - deno_fetch::init::<Permissions>(Default::default()), - deno_websocket::init::<Permissions>("".to_owned(), None, None), - deno_webstorage::init(None), - deno_crypto::init(None), - deno_webgpu::init(false), - deno_broadcast_channel::init( - deno_broadcast_channel::InMemoryBroadcastChannel::default(), - false, // No --unstable. - ), - deno_ffi::init::<Permissions>(false), - deno_net::init::<Permissions>( - None, false, // No --unstable. - None, - ), - deno_http::init(), - ]; - - let js_runtime = JsRuntime::new(RuntimeOptions { - will_snapshot: true, - extensions, - ..Default::default() - }); - create_snapshot(js_runtime, snapshot_path, files); - } - - fn get_js_files(d: &str) -> Vec<PathBuf> { - let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); - let mut js_files = std::fs::read_dir(d) - .unwrap() - .map(|dir_entry| { - let file = dir_entry.unwrap(); - manifest_dir.join(file.path()) - }) - .filter(|path| path.extension().unwrap_or_default() == "js") - .collect::<Vec<PathBuf>>(); - js_files.sort(); - js_files - } - - pub fn build_snapshot(runtime_snapshot_path: PathBuf) { - let js_files = get_js_files("js"); - create_runtime_snapshot(&runtime_snapshot_path, js_files); - } -} - fn main() { - // To debug snapshot issues uncomment: - // op_fetch_asset::trace_serializer(); - - println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap()); - println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap()); - let o = PathBuf::from(env::var_os("OUT_DIR").unwrap()); - - // Main snapshot - let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin"); - - // If we're building on docs.rs we just create - // and empty snapshot file and return, because `rusty_v8` - // doesn't actually compile on docs.rs - if env::var_os("DOCS_RS").is_some() { - let snapshot_slice = &[]; - std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap(); + // Skip building from docs.rs. + if std::env::var_os("DOCS_RS").is_some() { return; } - #[cfg(not(feature = "docsrs"))] - not_docs::build_snapshot(runtime_snapshot_path) + println!( + "cargo:rustc-env=TARGET={}", + std::env::var("TARGET").unwrap() + ); + println!( + "cargo:rustc-env=PROFILE={}", + std::env::var("PROFILE").unwrap() + ); } diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index 07e42f0ff..19f462d4f 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -58,6 +58,7 @@ async fn main() -> Result<(), AnyError> { shared_array_buffer_store: None, compiled_wasm_module_store: None, stdio: Default::default(), + startup_snapshot: None, }; let js_path = diff --git a/runtime/js.rs b/runtime/js.rs index cdd479858..a18dc3bd3 100644 --- a/runtime/js.rs +++ b/runtime/js.rs @@ -1,58 +1,38 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use deno_core::Snapshot; -use log::debug; -use once_cell::sync::Lazy; - -pub static CLI_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new( - #[allow(clippy::uninit_vec)] - #[cold] - #[inline(never)] - || { - static COMPRESSED_CLI_SNAPSHOT: &[u8] = - include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin")); - - let size = - u32::from_le_bytes(COMPRESSED_CLI_SNAPSHOT[0..4].try_into().unwrap()) - as usize; - let mut vec = Vec::with_capacity(size); - - // SAFETY: vec is allocated with exact snapshot size (+ alignment) - // SAFETY: non zeroed bytes are overwritten with decompressed snapshot - unsafe { - vec.set_len(size); - } - - lzzzz::lz4::decompress(&COMPRESSED_CLI_SNAPSHOT[4..], &mut vec).unwrap(); - - vec.into_boxed_slice() - }, -); - -pub fn deno_isolate_init() -> Snapshot { - debug!("Deno isolate init with snapshots."); - Snapshot::Static(&*CLI_SNAPSHOT) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn cli_snapshot() { - let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions { - startup_snapshot: Some(deno_isolate_init()), - ..Default::default() - }); - js_runtime - .execute_script( - "<anon>", - r#" - if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) { - throw Error("bad"); - } - console.log("we have console.log!!!"); - "#, - ) - .unwrap(); - } +use deno_core::include_js_files; +use deno_core::Extension; + +pub fn init() -> Extension { + Extension::builder() + .js(include_js_files!( + prefix "deno:runtime", + // Generated with: + // bash -c "cd runtime && ls js/*.js | sort" + "js/01_build.js", + "js/01_errors.js", + "js/01_version.js", + "js/01_web_util.js", + "js/06_util.js", + "js/10_permissions.js", + "js/11_workers.js", + "js/12_io.js", + "js/13_buffer.js", + "js/30_fs.js", + "js/30_os.js", + "js/40_diagnostics.js", + "js/40_files.js", + "js/40_fs_events.js", + "js/40_http.js", + "js/40_process.js", + "js/40_read_file.js", + "js/40_signals.js", + "js/40_spawn.js", + "js/40_testing.js", + "js/40_tty.js", + "js/40_write_file.js", + "js/41_prompt.js", + "js/90_deno_ns.js", + "js/99_main.js", + )) + .build() } diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index ba2c016cc..19e344ee7 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -335,6 +335,7 @@ pub struct WebWorkerOptions { pub shared_array_buffer_store: Option<SharedArrayBufferStore>, pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>, pub stdio: Stdio, + pub startup_snapshot: Option<deno_core::Snapshot>, } impl WebWorker { @@ -427,6 +428,8 @@ impl WebWorker { ops::tty::init(), deno_http::init(), ops::http::init(), + // Runtime JS + js::init(), // Permissions ext (worker specific state) perm_ext, ]; @@ -436,7 +439,7 @@ impl WebWorker { let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), - startup_snapshot: Some(js::deno_isolate_init()), + startup_snapshot: options.startup_snapshot.take(), 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 e46b5c75f..acb50dc30 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -81,6 +81,7 @@ pub struct WorkerOptions { pub shared_array_buffer_store: Option<SharedArrayBufferStore>, pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>, pub stdio: Stdio, + pub startup_snapshot: Option<deno_core::Snapshot>, } impl MainWorker { @@ -168,6 +169,8 @@ impl MainWorker { ops::tty::init(), deno_http::init(), ops::http::init(), + // Runtime JS + js::init(), // Permissions ext (worker specific state) perm_ext, ]; @@ -175,7 +178,7 @@ impl MainWorker { let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), - startup_snapshot: Some(js::deno_isolate_init()), + startup_snapshot: options.startup_snapshot.take(), 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(), @@ -411,6 +414,7 @@ mod tests { shared_array_buffer_store: None, compiled_wasm_module_store: None, stdio: Default::default(), + startup_snapshot: None, }; MainWorker::bootstrap_from_options(main_module, permissions, options) |