diff options
Diffstat (limited to 'snapshots/lib.rs')
| -rw-r--r-- | snapshots/lib.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/snapshots/lib.rs b/snapshots/lib.rs new file mode 100644 index 000000000..1d0b3ceb3 --- /dev/null +++ b/snapshots/lib.rs @@ -0,0 +1,77 @@ +use deno_core::Snapshot; +use once_cell::sync::Lazy; +use std::convert::TryInto; + +pub fn tsc_snapshot() -> Snapshot { + Snapshot::Static(&*COMPILER_SNAPSHOT) +} + +static COMPILER_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new( + #[cold] + #[inline(never)] + || { + static COMPRESSED_COMPILER_SNAPSHOT: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin")); + + zstd::bulk::decompress( + &COMPRESSED_COMPILER_SNAPSHOT[4..], + u32::from_le_bytes(COMPRESSED_COMPILER_SNAPSHOT[0..4].try_into().unwrap()) + as usize, + ) + .unwrap() + .into_boxed_slice() + }, +); + +pub fn cli_snapshot() -> Snapshot { + Snapshot::Static(&*CLI_SNAPSHOT) +} + +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() + }, +); + +#[cfg(test)] +mod tests { + use deno_runtime::deno_core; + + #[test] + fn cli_snapshot() { + let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions { + startup_snapshot: Some(crate::cli_snapshot()), + ..Default::default() + }); + js_runtime + .execute_script( + "<anon>", + r#" + if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) { + throw Error("bad"); + } + console.log("we have console.log!!!"); + "#, + ) + .unwrap(); + } +} |
