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

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 gcmodule::Cc;6use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};7use jrsonnet_parser::Visibility;8use std::{ffi::CStr, os::raw::c_char};910/// # Safety11///12/// Received arr value should be correct pointer to array allocated by make_array13#[no_mangle]14pub unsafe extern "C" fn jsonnet_json_array_append(15	_vm: &EvaluationState,16	arr: &mut Val,17	val: &Val,18) {19	match arr {20		Val::Arr(old) => {21			let mut new = Vec::new();22			for item in old.iter_lazy() {23				new.push(item);24			}25			new.push(LazyVal::new_resolved(val.clone()));26			*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));27		}28		_ => panic!("should receive array"),29	}30}3132/// # Safety33///34/// This function is safe if passed name is ok35#[no_mangle]36pub unsafe extern "C" fn jsonnet_json_object_append(37	_vm: &EvaluationState,38	obj: &mut Val,39	name: *const c_char,40	val: &Val,41) {42	match obj {43		Val::Obj(old) => {44			let new_obj = old.clone().extend_with_field(45				CStr::from_ptr(name).to_str().unwrap().into(),46				ObjMember {47					add: false,48					visibility: Visibility::Normal,49					invoke: LazyBinding::Bound(LazyVal::new_resolved(val.clone())),50					location: None,51				},52			);5354			*obj = Val::Obj(new_obj);55		}56		_ => panic!("should receive object"),57	}58}