git.delta.rocks / jrsonnet / refs/commits / 643b0073bda6

difftreelog

source

bindings/jsonnet/src/val_modify.rs1.5 KiBsourcehistory
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::{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}