feat: implement assert

This commit is contained in:
2026-01-03 18:44:46 +08:00
parent 159267c70b
commit 45d777a157
4 changed files with 8 additions and 19 deletions

View File

@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2020",
"target": "ES2022",
"module": "ESNext",
"lib": ["ES2020", "DOM"],
"lib": ["ES2022", "DOM"],
"moduleResolution": "bundler",
"strict": true,
"noUnusedLocals": false,

View File

@@ -65,7 +65,11 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
Ir::Builtin(_) => "Nix.builtins".to_string(),
Ir::ConcatStrings(x) => x.compile(ctx),
Ir::HasAttr(x) => x.compile(ctx),
ir => todo!("{ir:?}"),
&Ir::Assert(Assert { assertion, expr }) => {
let assertion = ctx.get_ir(assertion).compile(ctx);
let expr = ctx.get_ir(expr).compile(ctx);
format!("({assertion})?({expr}):(()=>{{throw \"assertion failed\"}})()")
}
}
}
}

View File

@@ -50,7 +50,6 @@ ir! {
Select,
If,
Call,
With,
Assert,
ConcatStrings,
Const,
@@ -59,7 +58,6 @@ ir! {
Func,
Let,
Arg(ArgId),
PrimOp(PrimOpId),
ExprRef(ExprId),
Thunk(ExprId),
Builtin,
@@ -150,10 +148,6 @@ pub struct ExprId(pub usize);
pub type SymId = SymbolU32;
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PrimOpId(pub usize);
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ArgId(pub usize);
@@ -342,15 +336,6 @@ pub struct Call {
pub arg: ExprId,
}
/// Represents a `with` expression.
#[derive(Debug)]
pub struct With {
/// The namespace to bring into scope.
pub namespace: ExprId,
/// The expression to be evaluated within the new scope.
pub expr: ExprId,
}
/// Represents an `assert` expression.
#[derive(Debug)]
pub struct Assert {

View File

@@ -353,7 +353,7 @@ fn to_value<'a, 'b>(val: v8::Local<'a, v8::Value>, ctx: &RuntimeContext<'a, 'b>)
.collect();
Value::AttrSet(AttrSet::new(attrs))
}
_ => todo!("{}", val.type_repr()),
_ => unimplemented!("can not convert {} to NixValue", val.type_repr()),
}
}