difftreelog
feat object extend_with_field method
in: master
2 files changed
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth1//! Modify VM values2//! Only tested with variables, which haven't altered by code before appearing here3//! In jrsonnet every value is immutable, and this code is probally broken45use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};6use jrsonnet_parser::Visibility;7use std::{ffi::CStr, os::raw::c_char, rc::Rc};89/// # Safety10///11/// Received arr value should be correct pointer to array allocated by make_array12#[no_mangle]13pub unsafe extern "C" fn jsonnet_json_array_append(14 _vm: &EvaluationState,15 arr: &mut Val,16 val: &Val,17) {18 match arr {19 Val::Arr(old) => {20 let mut new = Vec::new();21 for item in old.iter_lazy() {22 new.push(item);23 }24 new.push(LazyVal::new_resolved(val.clone()));25 *arr = Val::Arr(ArrValue::Lazy(Rc::new(new)));26 }27 _ => panic!("should receive array"),28 }29}3031/// # Safety32///33/// This function is safe if passed name is ok34#[no_mangle]35pub unsafe extern "C" fn jsonnet_json_object_append(36 _vm: &EvaluationState,37 obj: &mut Val,38 name: *const c_char,39 val: &Val,40) {41 match obj {42 Val::Obj(old) => {43 let new_obj = old.clone().extend_with_field(44 CStr::from_ptr(name).to_str().unwrap().into(),45 ObjMember {46 add: false,47 visibility: Visibility::Normal,48 invoke: LazyBinding::Bound(LazyVal::new_resolved(val.clone())),49 location: None,50 },51 );5253 *obj = Val::Obj(new_obj);54 }55 _ => panic!("should receive object"),56 }57}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -3,7 +3,7 @@
use jrsonnet_parser::{ExprLocation, Visibility};
use rustc_hash::FxHashMap;
use std::hash::{Hash, Hasher};
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
+use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
#[derive(Debug)]
pub struct ObjMember {
@@ -169,6 +169,13 @@
pub fn get(&self, key: IStr) -> Result<Option<Val>> {
self.get_raw(key, self.0.this_obj.as_ref())
}
+
+ pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
+ let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
+ new.insert(key, value);
+ ObjValue::new(Some(self), Rc::new(new))
+ }
+
pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
let real_this = real_this.unwrap_or(self);
let cache_key = (key.clone(), real_this.clone());