git.delta.rocks / jrsonnet / refs/commits / e1eaabb7a756

difftreelog

source

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