diff --git a/nix-js/runtime-ts/src/index.ts b/nix-js/runtime-ts/src/index.ts index 2f33f36..a574937 100644 --- a/nix-js/runtime-ts/src/index.ts +++ b/nix-js/runtime-ts/src/index.ts @@ -24,6 +24,7 @@ import { builtins, PRIMOP_METADATA } from "./builtins"; import { coerceToString, StringCoercionMode } from "./builtins/conversion"; import { HAS_CONTEXT } from "./string-context"; import { IS_PATH } from "./types"; +import { forceBool } from "./type-assert"; export type NixRuntime = typeof Nix; @@ -33,6 +34,7 @@ export type NixRuntime = typeof Nix; export const Nix = { createThunk, force, + forceBool, isThunk, IS_THUNK, HAS_CONTEXT, diff --git a/nix-js/src/codegen.rs b/nix-js/src/codegen.rs index 8012f9a..62b5bd0 100644 --- a/nix-js/src/codegen.rs +++ b/nix-js/src/codegen.rs @@ -98,11 +98,11 @@ impl Compile for Ir { if std::env::var("NIX_JS_STACK_TRACE").is_ok() { let cond_span = encode_span(ctx.get_ir(cond).span(), ctx); format!( - "(Nix.withContext(\"while evaluating a branch condition\",{},()=>({})))?({}):({})", + "(Nix.withContext(\"while evaluating a branch condition\",{},()=>Nix.forceBool({})))?({}):({})", cond_span, cond_code, consq, alter ) } else { - format!("({cond_code})?({consq}):({alter})") + format!("Nix.forceBool({cond_code})?({consq}):({alter})") } } Ir::BinOp(x) => x.compile(ctx), @@ -196,9 +196,9 @@ impl Compile for BinOp { Leq => with_ctx("<=", format!("Nix.op.lte({},{})", lhs, rhs)), Geq => with_ctx(">=", format!("Nix.op.gte({},{})", lhs, rhs)), // Short-circuit operators: use JavaScript native && and || - And => with_ctx("&&", format!("Nix.force({})&&Nix.force({})", lhs, rhs)), - Or => with_ctx("||", format!("Nix.force({})||Nix.force({})", lhs, rhs)), - Impl => with_ctx("->", format!("(!Nix.force({})||Nix.force({}))", lhs, rhs)), + And => with_ctx("&&", format!("Nix.forceBool({})&&Nix.forceBool({})", lhs, rhs)), + Or => with_ctx("||", format!("Nix.forceBool({})||Nix.forceBool({})", lhs, rhs)), + Impl => with_ctx("->", format!("(!Nix.forceBool({})||Nix.forceBool({}))", lhs, rhs)), Con => with_ctx("++", format!("Nix.op.concat({},{})", lhs, rhs)), Upd => with_ctx("//", format!("Nix.op.update({},{})", lhs, rhs)), PipeL => format!("Nix.call({},{})", rhs, lhs),