implement string context

This commit is contained in:
2026-05-17 17:02:49 +08:00
parent 9a17990d5e
commit d98e389606
11 changed files with 698 additions and 224 deletions
+12 -2
View File
@@ -4,8 +4,7 @@ use fix_error::Source;
use hashbrown::HashSet;
use crate::{
AttrSet, Closure, ExtraScope, List, NixString, NixType, Null, Path, PrimOp, PrimOpApp,
StaticValue, StrictValue, Thunk, ThunkState, Value,
AttrSet, Closure, ExtraScope, List, NixString, NixType, Null, Path, PrimOp, PrimOpApp, StaticValue, StrictValue, StringContext, Thunk, ThunkState, Value
};
pub trait VmContext {
@@ -36,6 +35,9 @@ pub trait VmRuntimeCtxExt: VmRuntimeCtx {
&'a mut self,
val: StrictValue<'gc>,
) -> std::result::Result<StringId, NixType>;
/// Returns the string context attached to `val`, or `&[]` if `val` is
/// either a non-string or a string without context.
fn get_string_context<'gc>(&self, val: StrictValue<'gc>) -> &'gc StringContext;
fn convert_value(&self, val: Value) -> fix_common::Value;
}
@@ -73,6 +75,14 @@ impl<T: VmRuntimeCtx> VmRuntimeCtxExt for T {
}
}
fn get_string_context<'gc>(&self, val: StrictValue<'gc>) -> &'gc StringContext {
if let Some(ns) = val.as_gc::<NixString>() {
ns.as_ref().context()
} else {
StringContext::empty()
}
}
fn convert_value(&self, val: Value) -> fix_common::Value {
self.convert_value_with_seen(val, &mut HashSet::new())
}