feat: error handling

This commit is contained in:
2025-08-08 19:35:51 +08:00
parent a9cfddbf5c
commit fd182b6233
12 changed files with 187 additions and 311 deletions

View File

@@ -12,6 +12,7 @@ use std::ptr::NonNull;
use hashbrown::HashMap;
use nixjit_eval::{AttrSet, EvalContext, List, Value};
use nixjit_ir::ArgIdx;
use super::JITContext;
@@ -34,7 +35,7 @@ pub extern "C" fn helper_call<Ctx: JITContext>(
/// Helper function to look up a value in the evaluation stack.
///
/// This function is called from JIT-compiled code to access values in the evaluation stack.
pub extern "C" fn helper_lookup_stack<Ctx: JITContext>(
pub extern "C" fn helper_lookup_stack<Ctx: JITContext + EvalContext>(
ctx: &Ctx,
offset: usize,
ret: &mut MaybeUninit<Value>,
@@ -45,12 +46,12 @@ pub extern "C" fn helper_lookup_stack<Ctx: JITContext>(
/// Helper function to look up a function argument.
///
/// This function is called from JIT-compiled code to access function arguments.
pub extern "C" fn helper_lookup_arg<Ctx: JITContext>(
pub extern "C" fn helper_lookup_arg<Ctx: EvalContext>(
ctx: &Ctx,
offset: usize,
idx: ArgIdx,
ret: &mut MaybeUninit<Value>,
) {
ret.write(JITContext::lookup_arg(ctx, offset).clone());
ret.write(ctx.lookup_arg(idx).clone());
}
/// Helper function to look up a variable by name.
@@ -87,7 +88,7 @@ pub extern "C" fn helper_select<Ctx: JITContext>(
let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len);
let path = unsafe { Box::from_raw(path) };
val.select(path.into_iter().map(|mut val| {
val.coerce_to_string();
val.coerce_to_string().unwrap();
Ok(val.unwrap_string())
}))
.unwrap();
@@ -107,7 +108,7 @@ pub extern "C" fn helper_select_with_default<Ctx: JITContext>(
let path = unsafe { Box::from_raw(path) };
val.select_with_default(
path.into_iter().map(|mut val| {
val.coerce_to_string();
val.coerce_to_string().unwrap();
Ok(val.unwrap_string())
}),
unsafe { default.read() },
@@ -119,7 +120,7 @@ pub extern "C" fn helper_select_with_default<Ctx: JITContext>(
///
/// This function is called from JIT-compiled code to perform equality comparisons.
pub extern "C" fn helper_eq<Ctx: JITContext>(lhs: &mut Value, rhs: &Value) {
lhs.eq(rhs);
lhs.eq(unsafe { core::ptr::read(rhs) });
}
/// Helper function to create a string value.

View File

@@ -32,13 +32,8 @@ use helpers::*;
/// A trait that provides the execution context for JIT-compiled code.
///
/// This trait extends `EvalContext` with additional methods needed
/// for JIT compilation, such as stack and argument lookups, and
/// managing `with` expression scopes.
/// for JIT compilation, such as managing `with` expression scopes directly.
pub trait JITContext: EvalContext {
/// Looks up a value in the evaluation stack by offset.
fn lookup_stack(&self, offset: usize) -> &Value;
/// Looks up a function argument by offset.
fn lookup_arg(&self, offset: usize) -> &Value;
/// Enters a `with` expression scope with the given namespace.
fn enter_with(&mut self, namespace: Rc<HashMap<String, Value>>);
/// Exits the current `with` expression scope.