summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-07-05 00:12:41 +0200
committerGitHub <noreply@github.com>2022-07-05 00:12:41 +0200
commita919a5dd1167b79f267cbed7312b3a7d296d429f (patch)
tree73b7b1a8b5daef3ab7ad9c07423c4907a23a0269 /runtime
parent06934db883e9ae64c7603f98051676cbcf90994f (diff)
Revert "refactor(snapshots): to their own crate (#14794)" (#15076)
This reverts commit fd5a12d7e25dc53238e2bbcffe970e646c1035f3.
Diffstat (limited to 'runtime')
-rw-r--r--runtime/Cargo.toml19
-rw-r--r--runtime/build.rs222
-rw-r--r--runtime/examples/hello_runtime.rs1
-rw-r--r--runtime/js.rs92
-rw-r--r--runtime/web_worker.rs5
-rw-r--r--runtime/worker.rs6
6 files changed, 289 insertions, 56 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index 0f12281dd..fe50f9f1c 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -21,6 +21,25 @@ path = "lib.rs"
name = "hello_runtime"
path = "examples/hello_runtime.rs"
+[build-dependencies]
+deno_broadcast_channel = { version = "0.53.0", path = "../ext/broadcast_channel" }
+deno_console = { version = "0.59.0", path = "../ext/console" }
+deno_core = { version = "0.141.0", path = "../core" }
+deno_crypto = { version = "0.73.0", path = "../ext/crypto" }
+deno_fetch = { version = "0.82.0", path = "../ext/fetch" }
+deno_ffi = { version = "0.46.0", path = "../ext/ffi" }
+deno_http = { version = "0.53.0", path = "../ext/http" }
+deno_net = { version = "0.51.0", path = "../ext/net" }
+deno_tls = { version = "0.46.0", path = "../ext/tls" }
+deno_url = { version = "0.59.0", path = "../ext/url" }
+deno_web = { version = "0.90.0", path = "../ext/web" }
+deno_webgpu = { version = "0.60.0", path = "../ext/webgpu" }
+deno_webidl = { version = "0.59.0", path = "../ext/webidl" }
+deno_websocket = { version = "0.64.0", path = "../ext/websocket" }
+deno_webstorage = { version = "0.54.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 0cb113af1..eea7a3602 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -1,17 +1,219 @@
// 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() {
- // Skip building from docs.rs.
- if std::env::var_os("DOCS_RS").is_some() {
+ // 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();
return;
}
- println!(
- "cargo:rustc-env=TARGET={}",
- std::env::var("TARGET").unwrap()
- );
- println!(
- "cargo:rustc-env=PROFILE={}",
- std::env::var("PROFILE").unwrap()
- );
+ #[cfg(not(feature = "docsrs"))]
+ not_docs::build_snapshot(runtime_snapshot_path)
}
diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs
index 19f462d4f..07e42f0ff 100644
--- a/runtime/examples/hello_runtime.rs
+++ b/runtime/examples/hello_runtime.rs
@@ -58,7 +58,6 @@ 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 a18dc3bd3..cdd479858 100644
--- a/runtime/js.rs
+++ b/runtime/js.rs
@@ -1,38 +1,58 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
-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()
+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();
+ }
}
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index 19e344ee7..ba2c016cc 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -335,7 +335,6 @@ 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 {
@@ -428,8 +427,6 @@ impl WebWorker {
ops::tty::init(),
deno_http::init(),
ops::http::init(),
- // Runtime JS
- js::init(),
// Permissions ext (worker specific state)
perm_ext,
];
@@ -439,7 +436,7 @@ impl WebWorker {
let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()),
- startup_snapshot: options.startup_snapshot.take(),
+ startup_snapshot: Some(js::deno_isolate_init()),
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 f9274babe..5100f42da 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -81,7 +81,6 @@ 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 {
@@ -169,8 +168,6 @@ impl MainWorker {
ops::tty::init(),
deno_http::init(),
ops::http::init(),
- // Runtime JS
- js::init(),
// Permissions ext (worker specific state)
perm_ext,
];
@@ -178,7 +175,7 @@ impl MainWorker {
let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()),
- startup_snapshot: options.startup_snapshot.take(),
+ startup_snapshot: Some(js::deno_isolate_init()),
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(),
@@ -432,7 +429,6 @@ 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)