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": { "compilerOptions": {
"target": "ES2020", "target": "ES2022",
"module": "ESNext", "module": "ESNext",
"lib": ["ES2020", "DOM"], "lib": ["ES2022", "DOM"],
"moduleResolution": "bundler", "moduleResolution": "bundler",
"strict": true, "strict": true,
"noUnusedLocals": false, "noUnusedLocals": false,

View File

@@ -65,7 +65,11 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
Ir::Builtin(_) => "Nix.builtins".to_string(), Ir::Builtin(_) => "Nix.builtins".to_string(),
Ir::ConcatStrings(x) => x.compile(ctx), Ir::ConcatStrings(x) => x.compile(ctx),
Ir::HasAttr(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, Select,
If, If,
Call, Call,
With,
Assert, Assert,
ConcatStrings, ConcatStrings,
Const, Const,
@@ -59,7 +58,6 @@ ir! {
Func, Func,
Let, Let,
Arg(ArgId), Arg(ArgId),
PrimOp(PrimOpId),
ExprRef(ExprId), ExprRef(ExprId),
Thunk(ExprId), Thunk(ExprId),
Builtin, Builtin,
@@ -150,10 +148,6 @@ pub struct ExprId(pub usize);
pub type SymId = SymbolU32; pub type SymId = SymbolU32;
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PrimOpId(pub usize);
#[repr(transparent)] #[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ArgId(pub usize); pub struct ArgId(pub usize);
@@ -342,15 +336,6 @@ pub struct Call {
pub arg: ExprId, 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. /// Represents an `assert` expression.
#[derive(Debug)] #[derive(Debug)]
pub struct Assert { 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(); .collect();
Value::AttrSet(AttrSet::new(attrs)) Value::AttrSet(AttrSet::new(attrs))
} }
_ => todo!("{}", val.type_repr()), _ => unimplemented!("can not convert {} to NixValue", val.type_repr()),
} }
} }