diff --git a/.gitignore b/.gitignore index b10ea74..673b44c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ flamegraph*.svg perf.data* profile.json.gz prof.json +*.cpuprofile +*.cpuprofile.gz diff --git a/nix-js/runtime-ts/src/index.ts b/nix-js/runtime-ts/src/index.ts index 1865b80..aedec08 100644 --- a/nix-js/runtime-ts/src/index.ts +++ b/nix-js/runtime-ts/src/index.ts @@ -35,7 +35,7 @@ import { ATTR_POSITIONS, IS_PATH, mkAttrs, mkAttrsWithPos, mkFunction, type NixV export type NixRuntime = typeof Nix; -const replBindings: Map = new Map; +const replBindings: Map = new Map(); export const Nix = { createThunk, diff --git a/nix-js/src/codegen.rs b/nix-js/src/codegen.rs index 46a37a9..819ef79 100644 --- a/nix-js/src/codegen.rs +++ b/nix-js/src/codegen.rs @@ -207,7 +207,6 @@ pub(crate) trait CodegenContext { fn get_current_dir(&self) -> &Path; fn get_store_dir(&self) -> &str; fn get_current_source_id(&self) -> usize; - fn get_current_source(&self) -> crate::error::Source; } impl Compile for ExprId { @@ -500,16 +499,10 @@ impl Compile for [(ExprId, ExprId)] { for &(slot, inner) in self { let inner_ir = ctx.get_ir(inner); - let inner_span = inner_ir.span(); - code!( buf, ctx; "let expr" slot.0 "=Nix.createThunk(()=>(" inner_ir ")," - "\"expr" slot.0 " " - ctx.get_current_source().get_name() ":" - usize::from(inner_span.start()) ":" - usize::from(inner_span.end()) - "\");" + "\"expr" slot.0 "\");" ); } } diff --git a/nix-js/src/context.rs b/nix-js/src/context.rs index ffdbf3a..6285895 100644 --- a/nix-js/src/context.rs +++ b/nix-js/src/context.rs @@ -363,9 +363,6 @@ impl CodegenContext for Ctx { .checked_sub(1) .expect("current_source not set") } - fn get_current_source(&self) -> crate::error::Source { - self.sources.last().expect("current_source not set").clone() - } fn get_store_dir(&self) -> &str { self.store.get_store_dir() } diff --git a/nix-js/src/main.rs b/nix-js/src/main.rs index 8337b9a..3a625e8 100644 --- a/nix-js/src/main.rs +++ b/nix-js/src/main.rs @@ -26,6 +26,12 @@ struct Cli { #[derive(Subcommand)] enum Command { + Compile { + #[clap(flatten)] + source: ExprSource, + #[arg(long)] + silent: bool + }, Eval { #[clap(flatten)] source: ExprSource, @@ -63,6 +69,30 @@ fn create_context(#[cfg(feature = "inspector")] cli: &Cli) -> Result { Ok(Context::new()?) } +fn run_compile(context: &mut Context, src: ExprSource, silent: bool) -> Result<()> { + let src = if let Some(expr) = src.expr { + Source::new_eval(expr)? + } else if let Some(file) = src.file { + Source::new_file(file)? + } else { + unreachable!() + }; + match context.compile(src) { + Ok(compiled) => { + if !silent { + println!("{compiled}"); + } + } + Err(err) => { + eprintln!("{:?}", miette::Report::new(*err)); + exit(1); + } + }; + #[cfg(feature = "inspector")] + context.wait_for_inspector_disconnect(); + Ok(()) +} + fn run_eval(context: &mut Context, src: ExprSource) -> Result<()> { let src = if let Some(expr) = src.expr { Source::new_eval(expr)? @@ -150,6 +180,7 @@ fn main() -> Result<()> { )?; match cli.command { + Command::Compile { source , silent } => run_compile(&mut context, source, silent), Command::Eval { source } => run_eval(&mut context, source), Command::Repl => run_repl(&mut context), }