git.delta.rocks / jrsonnet / refs/commits / 097494ede5f1

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 std::{ffi::CStr, os::raw::c_char};67use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};8use jrsonnet_gcmodule::Cc;910/// Adds value to the end of the array `arr`.11///12/// # Safety13///14/// `arr` should be correct pointer to array value allocated by make_array, or returned by other library call15/// `val` should be correct pointer to value allocated using this library16#[no_mangle]17pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {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			}2425			new.push(Thunk::evaluated(val.clone()));26			*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));27		}28		_ => panic!("should receive array"),29	}30}3132/// Adds the field to the object, bound to value.33///34/// This shadows any previous binding of the field.35///36/// # Safety37///38/// `obj` should be a valid pointer to object value allocated by `make_object`, or returned by other library call39/// `name` should be \0-terminated string40#[no_mangle]41pub unsafe extern "C" fn jsonnet_json_object_append(42	_vm: &State,43	obj: &mut Val,44	name: *const c_char,45	val: &Val,46) {47	match obj {48		Val::Obj(old) => old49			.extend_field(CStr::from_ptr(name).to_str().unwrap().into())50			.value(val.clone()),51		_ => panic!("should receive object"),52	}53}