git.delta.rocks / jrsonnet / refs/commits / 80f37a416bf7

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 std::{ffi::CStr, os::raw::c_char};67use gcmodule::Cc;8use jrsonnet_evaluator::{val::ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};9use jrsonnet_parser::Visibility;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(Cc::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 new_obj = old.clone().extend_with_field(46				CStr::from_ptr(name).to_str().unwrap().into(),47				ObjMember {48					add: false,49					visibility: Visibility::Normal,50					invoke: LazyBinding::Bound(LazyVal::new_resolved(val.clone())),51					location: None,52				},53			);5455			*obj = Val::Obj(new_obj);56		}57		_ => panic!("should receive object"),58	}59}