From c24d6a8bb39a880104fe8dce8831b9bf6ae03a6a Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sun, 22 Feb 2026 22:25:42 +0800 Subject: [PATCH] chore: merge `codegen::compile` and `codegen::compile_scoped` --- nix-js/src/codegen.rs | 64 ++++++++++--------------------------------- nix-js/src/context.rs | 6 ++-- 2 files changed, 18 insertions(+), 52 deletions(-) diff --git a/nix-js/src/codegen.rs b/nix-js/src/codegen.rs index 4948e9c..18bf873 100644 --- a/nix-js/src/codegen.rs +++ b/nix-js/src/codegen.rs @@ -26,32 +26,17 @@ macro_rules! code { }; } -pub(crate) fn compile(expr: &Ir, ctx: &impl CodegenContext) -> String { +pub(crate) fn compile(expr: &Ir, ctx: &impl CodegenContext) -> String { let mut buf = CodeBuffer::with_capacity(8192); - code!(&mut buf, ctx; "(()=>{"); - - code!(&mut buf, ctx; - "const _d=" - quoted(&ctx.get_current_dir().display().to_string()) - ",_w=null;return " - expr - "})()"); - - buf.into_string() -} - -pub(crate) fn compile_scoped(expr: &Ir, ctx: &impl CodegenContext) -> String { - let mut buf = CodeBuffer::with_capacity(8192); - - code!(&mut buf, ctx; "((_s)=>{"); - - code!(&mut buf, ctx; - "const _d=" - quoted(&ctx.get_current_dir().display().to_string()) - ",_w=null;return " - expr - "})" + code!( + &mut buf, ctx; + "((" { if SCOPED { "_s" } else { "" } } ")=>{" + "const _d=" + quoted(&ctx.get_current_dir().display().to_string()) + ",_w=null;" + "return " expr + "})" { if SCOPED { "" } else { "()" } } ); buf.into_string() @@ -244,11 +229,7 @@ impl Compile for Ir<'_> { } &Ir::Builtin(Builtin { inner: name, .. }) => { // Nix.builtins - code!(buf, ctx; - "$b.get(" - ctx.get_sym(name) - ")" - ); + code!(buf, ctx; "$b.get(" ctx.get_sym(name) ")"); } Ir::ConcatStrings(x) => x.compile(ctx, buf), Ir::HasAttr(x) => x.compile(ctx, buf), @@ -273,35 +254,19 @@ impl Compile for Ir<'_> { } Ir::CurPos(cur_pos) => { // Nix.mkPos - code!(buf, ctx; - "$mp(" - cur_pos.span - ")" - ); + code!(buf, ctx; "$mp(" cur_pos.span ")"); } &Ir::ReplBinding(ReplBinding { inner: name, .. }) => { // Nix.getReplBinding - code!(buf, ctx; - "$gb(" - ctx.get_sym(name) - ")" - ); + code!(buf, ctx; "$gb(" ctx.get_sym(name) ")"); } &Ir::ScopedImportBinding(ScopedImportBinding { inner: name, .. }) => { - code!(buf, ctx; - "_s.get(" - ctx.get_sym(name) - ")" - ); + code!(buf, ctx; "_s.get(" ctx.get_sym(name) ")"); } Ir::With(x) => x.compile(ctx, buf), &Ir::WithLookup(WithLookup { inner: name, .. }) => { // Nix.lookupWith - code!(buf, ctx; - "$l(" - ctx.get_sym(name) - ",_w)" - ); + code!(buf, ctx; "$l(" ctx.get_sym(name) ",_w)"); } } } @@ -385,6 +350,7 @@ impl Compile for UnOp<'_> { let rhs = self.rhs; match self.kind { Neg => { + // 0 - rhs code!(buf, ctx; "$os(0n," rhs ")"); } Not => { diff --git a/nix-js/src/context.rs b/nix-js/src/context.rs index 29d9398..a59a31d 100644 --- a/nix-js/src/context.rs +++ b/nix-js/src/context.rs @@ -6,7 +6,7 @@ use hashbrown::{DefaultHashBuilder, HashMap, HashSet}; use rnix::TextRange; use string_interner::DefaultStringInterner; -use crate::codegen::{CodegenContext, compile, compile_scoped}; +use crate::codegen::{CodegenContext, compile}; use crate::downgrade::*; use crate::error::{Error, Result, Source}; use crate::ir::{ @@ -354,7 +354,7 @@ impl Ctx { ) -> Result { let root = self.downgrade(source, extra_scope)?; tracing::debug!("Generating JavaScript code"); - let code = compile(root.as_ref(), self); + let code = compile::(root.as_ref(), self); tracing::debug!("Generated code: {}", &code); Ok(code) } @@ -368,7 +368,7 @@ impl Ctx { ); let root = self.downgrade(source, Some(scope))?; tracing::debug!("Generating JavaScript code for scoped import"); - let code = compile_scoped(root.as_ref(), self); + let code = compile::(root.as_ref(), self); tracing::debug!("Generated scoped code: {}", &code); Ok(code) }