diff --git a/nix-js/runtime-ts/src/string-context.ts b/nix-js/runtime-ts/src/string-context.ts index 265bd0b..ead728b 100644 --- a/nix-js/runtime-ts/src/string-context.ts +++ b/nix-js/runtime-ts/src/string-context.ts @@ -62,28 +62,6 @@ export const mergeContexts = (...contexts: NixStringContext[]): NixStringContext return result; }; -export const concatStringsWithContext = ( - strings: (string | StringWithContext)[], -): string | StringWithContext => { - const parts: string[] = []; - const contexts: NixStringContext[] = []; - - for (const s of strings) { - parts.push(getStringValue(s)); - const ctx = getStringContext(s); - if (ctx.size > 0) { - contexts.push(ctx); - } - } - - const value = parts.join(""); - if (contexts.length === 0) { - return value; - } - - return mkStringWithContext(value, mergeContexts(...contexts)); -}; - export const encodeContextElem = (elem: StringContextElem): string => { switch (elem.type) { case "opaque": diff --git a/nix-js/src/codegen.rs b/nix-js/src/codegen.rs index ea5813d..96e20be 100644 --- a/nix-js/src/codegen.rs +++ b/nix-js/src/codegen.rs @@ -39,6 +39,8 @@ impl Compile for Ir { match self { Ir::Int(int) => format!("{int}n"), // Generate BigInt literal Ir::Float(float) => float.to_string(), + Ir::Bool(bool) => bool.to_string(), + Ir::Null(_) => "null".to_string(), Ir::Str(s) => s.val.escape_quote(), Ir::Path(p) => { // Path needs runtime resolution for interpolated paths @@ -104,8 +106,8 @@ impl Compile for BinOp { Impl => format!("(!Nix.force({}) || Nix.force({}))", lhs, rhs), Con => format!("Nix.op.concat({},{})", lhs, rhs), Upd => format!("Nix.op.update({},{})", lhs, rhs), - PipeL => format!("Nix.force({})({})", rhs, lhs), - PipeR => format!("Nix.force({})({})", lhs, rhs), + PipeL => format!("Nix.call({}, {})", rhs, lhs), + PipeR => format!("Nix.call({}, {})", lhs, rhs), } } } diff --git a/nix-js/src/context.rs b/nix-js/src/context.rs index da81875..ae49f1e 100644 --- a/nix-js/src/context.rs +++ b/nix-js/src/context.rs @@ -81,7 +81,7 @@ impl Context { let ctx = guard.as_ctx(); let code = ctx.compile_code(expr)?; - self.runtime.eval(code, CtxPtr::new(&mut self.ctx)) + self.runtime.eval(format!("Nix.force({code})"), CtxPtr::new(&mut self.ctx)) } pub fn compile_code(&mut self, expr: &str) -> Result { @@ -116,9 +116,6 @@ impl Default for Ctx { global.insert(builtins_sym, builtins_expr); let free_globals = [ - "true", - "false", - "null", "abort", "baseNameOf", "break", @@ -139,6 +136,11 @@ impl Default for Ctx { "throw", "toString", ]; + let consts = [ + ("true", Ir::Bool(true)), + ("false", Ir::Bool(false)), + ("null", Ir::Null(())), + ]; for name in free_globals { let name_sym = symbols.get_or_intern(name); @@ -146,6 +148,12 @@ impl Default for Ctx { irs.push(Builtin(name_sym).to_ir()); global.insert(name_sym, id); } + for (name, value) in consts { + let name_sym = symbols.get_or_intern(name); + let id = ExprId(irs.len()); + irs.push(value); + global.insert(name_sym, id); + } Self { symbols, @@ -195,7 +203,7 @@ impl Ctx { } else { "" }; - let code = format!("({}Nix.force({}))", debug_prefix, code); + let code = format!("({}{})", debug_prefix, code); #[cfg(debug_assertions)] eprintln!("[DEBUG] generated code: {}", &code); Ok(code) diff --git a/nix-js/src/fetcher.rs b/nix-js/src/fetcher.rs index c746308..cc64dfc 100644 --- a/nix-js/src/fetcher.rs +++ b/nix-js/src/fetcher.rs @@ -122,7 +122,7 @@ pub fn op_fetch_tarball( .map_err(|e| NixError::from(e.to_string()))?; let temp_dir = tempfile::tempdir().map_err(|e| NixError::from(e.to_string()))?; - let extracted_path = archive::extract_archive(&data, &temp_dir.path().to_path_buf()) + let extracted_path = archive::extract_archive(&data, temp_dir.path()) .map_err(|e| NixError::from(e.to_string()))?; let nar_hash = diff --git a/nix-js/src/fetcher/nar.rs b/nix-js/src/fetcher/nar.rs index 19009ef..7b2bebb 100644 --- a/nix-js/src/fetcher/nar.rs +++ b/nix-js/src/fetcher/nar.rs @@ -98,6 +98,7 @@ fn write_contents(sink: &mut W, contents: &[u8]) -> io::Result<()> { } #[cfg(test)] +#[allow(clippy::unwrap_used)] mod tests { use super::*; use tempfile::TempDir; diff --git a/nix-js/src/ir.rs b/nix-js/src/ir.rs index 9e023cf..1dc9ad4 100644 --- a/nix-js/src/ir.rs +++ b/nix-js/src/ir.rs @@ -52,6 +52,8 @@ ir! { Int(i64), Float(f64), + Bool(bool), + Null(()), Str, AttrSet, List,