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> = 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> = 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( "", r#" if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) { throw Error("bad"); } console.log("we have console.log!!!"); "#, ) .unwrap(); } }