feat: EvalState.new_value_int

(cherry picked from commit aa79e4d0db7795ab059349e6f6bc764d17afe003)
This commit is contained in:
Robert Hensing 2024-04-09 13:29:14 +02:00
parent cd5fa278eb
commit 024ef333cb

View file

@ -137,6 +137,16 @@ impl EvalState {
Ok(v)
}
pub fn new_value_int(&self, i: Int) -> Result<Value> {
let v = unsafe {
let value = self.new_value_uninitialized();
raw::init_int(self.context.ptr(), value.raw_ptr(), i);
value
};
self.context.check_err()?;
Ok(v)
}
/// Not exposed, because the caller must always explicitly handle the context or not accept one at all.
fn get_string(&self, value: &Value) -> Result<String> {
let mut r = result_string_init!();
@ -495,6 +505,21 @@ mod tests {
.unwrap();
}
#[test]
fn eval_state_new_int() {
gc_registering_current_thread(|| {
let store = Store::open("auto").unwrap();
let es = EvalState::new(store).unwrap();
let v = es.new_value_int(42).unwrap();
es.force(&v).unwrap();
let t = es.value_type(&v).unwrap();
assert!(t == ValueType::Int);
let i = es.require_int(&v).unwrap();
assert!(i == 42);
})
.unwrap();
}
#[test]
fn eval_state_value_attrset() {
gc_registering_current_thread(|| {