git.delta.rocks / jrsonnet / refs/commits / 7e00c7d820e8

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::{EvaluationState, LazyBinding, LazyVal, ObjMember, ObjValue, Val};6use jrsonnet_parser::Visibility;7use std::{collections::HashMap, 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 *Box::from_raw(arr) {19		Val::Arr(old) => {20			let mut new = Rc::try_unwrap(old).expect("arr with no refs");21			new.push(val.clone());22			*arr = Val::Arr(Rc::new(new));23		}24		_ => panic!("should receive array"),25	}26}2728/// # Safety29///30/// This function is safe if passed name is ok31#[no_mangle]32pub unsafe extern "C" fn jsonnet_json_object_append(33	_vm: &EvaluationState,34	obj: &mut Val,35	name: *const c_char,36	val: &Val,37) {38	match obj {39		Val::Obj(old) => {40			let mut new = HashMap::new();41			new.insert(42				CStr::from_ptr(name).to_str().unwrap().into(),43				ObjMember {44					add: false,45					visibility: Visibility::Normal,46					invoke: LazyBinding::Bound(LazyVal::new_resolved(val.clone())),47					location: None,48				},49			);50			let new_obj = ObjValue::new(Some(old.clone()), Rc::new(new));51			*obj = Val::Obj(new_obj);52		}53		_ => panic!("should receive object"),54	}55}