diff --git a/Cargo.lock b/Cargo.lock index 66128c37f..45ed6c2b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,12 +42,54 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anstream" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b09b5178381e0874812a9b157f7fe84982617e48f71f4e3235482775e5b540" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -237,8 +279,10 @@ version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", ] [[package]] @@ -247,6 +291,12 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "convert_case" version = "0.4.0" @@ -426,6 +476,7 @@ dependencies = [ "bit-set", "bit-vec", "bytes", + "clap", "cooked-waker", "criterion", "deno_ast", @@ -1625,6 +1676,12 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strum" version = "0.25.0" @@ -2234,6 +2291,12 @@ dependencies = [ "serde", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.7.0" diff --git a/core/Cargo.toml b/core/Cargo.toml index caff0c0cc..6a269763f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -26,6 +26,7 @@ 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 @@ -33,6 +34,7 @@ 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 diff --git a/core/main.rs b/core/main.rs index 16d0642f9..62457aeac 100644 --- a/core/main.rs +++ b/core/main.rs @@ -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; @@ -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 = std::env::args().collect(); eprintln!( "🛑 deno_core binary is meant for development and testing purposes." ); - if args.len() < 2 { - println!("Usage: cargo run -- "); - std::process::exit(1); - } - let main_url = &args[1]; + + let cli = build_cli(); + let matches = cli.get_matches(); + + let main_url = matches.get_one::("file_to_run").unwrap(); + let load_snapshot = matches.get_one::("snapshot"); + println!("Run {main_url}"); // TODO(bartlomieju): figure out how we can incorporate snapshotting here @@ -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()