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::{6 ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, ObjValue, Val,7};8use jrsonnet_parser::Visibility;9use std::{collections::HashMap, ffi::CStr, os::raw::c_char, rc::Rc};1011/// # Safety12///13/// Received arr value should be correct pointer to array allocated by make_array14#[no_mangle]15pub unsafe extern "C" fn jsonnet_json_array_append(16 _vm: &EvaluationState,17 arr: &mut Val,18 val: &Val,19) {20 match arr {21 Val::Arr(old) => {22 let mut new = Vec::new();23 for item in old.iter_lazy() {24 new.push(item);25 }26 new.push(LazyVal::new_resolved(val.clone()));27 *arr = Val::Arr(ArrValue::Lazy(Rc::new(new)));28 }29 _ => panic!("should receive array"),30 }31}3233/// # Safety34///35/// This function is safe if passed name is ok36#[no_mangle]37pub unsafe extern "C" fn jsonnet_json_object_append(38 _vm: &EvaluationState,39 obj: &mut Val,40 name: *const c_char,41 val: &Val,42) {43 match obj {44 Val::Obj(old) => {45 let mut new = HashMap::new();46 new.insert(47 CStr::from_ptr(name).to_str().unwrap().into(),48 ObjMember {49 add: false,50 visibility: Visibility::Normal,51 invoke: LazyBinding::Bound(LazyVal::new_resolved(val.clone())),52 location: None,53 },54 );55 let new_obj = ObjValue::new(Some(old.clone()), Rc::new(new));56 *obj = Val::Obj(new_obj);57 }58 _ => panic!("should receive object"),59 }60}1//! 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());