Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dcore): Allow to load snapshot from file #607

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ v8_use_custom_libcxx = ["v8/use_custom_libcxx"]
include_js_files_for_snapshotting = []
unsafe_runtime_options = []
unsafe_use_unprotected_platform = []
dcore = ["clap"]

[dependencies]
anyhow.workspace = true
bincode = { workspace = true, optional = true }
bit-set.workspace = true
bit-vec.workspace = true
bytes.workspace = true
clap = { version = "4.4.18", optional = true }
cooked-waker.workspace = true
deno_core_icudata = { workspace = true, optional = true }
deno_ops.workspace = true
Expand Down
51 changes: 41 additions & 10 deletions core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use anyhow::anyhow;
use anyhow::Context;
use clap::builder::Arg;
use clap::builder::Command;
use deno_core::anyhow::Error;
use deno_core::CustomModuleEvaluationKind;
use deno_core::FastString;
Expand Down Expand Up @@ -69,19 +71,36 @@ fn text_module(
)))
}

// TODO(bartlomieju): figure out how we can incorporate snapshotting here
// static SNAPSHOT_BYTES: &[u8] = include_bytes!("../snapshot.bin");
fn build_cli() -> Command {
Command::new("dcore")
.arg(
Arg::new("snapshot")
.long("snapshot")
.help("Optional path to a snapshot file that will be loaded on startup")
.value_hint(clap::ValueHint::FilePath)
.value_parser(clap::value_parser!(String))
.require_equals(true),
)
.arg(
Arg::new("file_to_run")
.help("A relative or absolute file to a file to run")
.value_hint(clap::ValueHint::FilePath)
.value_parser(clap::value_parser!(String))
.required(true),
)
}

fn main() -> Result<(), Error> {
let args: Vec<String> = std::env::args().collect();
eprintln!(
"🛑 deno_core binary is meant for development and testing purposes."
);
if args.len() < 2 {
println!("Usage: cargo run -- <path_to_module>");
std::process::exit(1);
}
let main_url = &args[1];

let cli = build_cli();
let matches = cli.get_matches();

let main_url = matches.get_one::<String>("file_to_run").unwrap();
let load_snapshot = matches.get_one::<String>("snapshot");

println!("Run {main_url}");

// TODO(bartlomieju): figure out how we can incorporate snapshotting here
Expand All @@ -101,9 +120,21 @@ fn main() -> Result<(), Error> {
// .unwrap();
// return Ok(());

let startup_snapshot: Option<&'static [u8]> =
if let Some(snapshot_path) = load_snapshot {
let data = std::fs::read(snapshot_path).with_context(|| {
format!("Failed to load snapshot from: {}", snapshot_path)
})?;
let boxed_data = data.into_boxed_slice();
// Leak so we can obtain a static reference to the slice.
let static_data = Box::leak(boxed_data);
Some(static_data)
} else {
None
};

let mut js_runtime = JsRuntime::new(RuntimeOptions {
// TODO(bartlomieju): figure out how we can incorporate snapshotting here
// startup_snapshot: Some(deno_core::Snapshot::Static(SNAPSHOT_BYTES)),
startup_snapshot,
module_loader: Some(Rc::new(FsModuleLoader)),
custom_module_evaluation_cb: Some(Box::new(custom_module_evaluation_cb)),
..Default::default()
Expand Down
Loading