git.delta.rocks / jrsonnet / refs/commits / 83c939e4fa0b

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, Thunk, Val};8use jrsonnet_gcmodule::Cc;910use crate::VM;1112/// Adds value to the end of the array `arr`.13///14/// # Safety15///16/// `arr` should be a pointer to array value allocated by make_array, or returned by other library call17/// `val` should be a pointer to value allocated using this library18#[no_mangle]19pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {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			}2627			new.push(Thunk::evaluated(val.clone()));28			*arr = Val::Arr(ArrValue::lazy(Cc::new(new)));29		}30		_ => panic!("should receive array"),31	}32}3334/// Adds the field to the object, bound to value.35///36/// This shadows any previous binding of the field.37///38/// # Safety39///40/// `obj` should be a pointer to object value allocated by `make_object`, or returned by other library call41/// `name` should be NUL-terminated string42#[no_mangle]43pub unsafe extern "C" fn jsonnet_json_object_append(44	_vm: &VM,45	obj: &mut Val,46	name: *const c_char,47	val: &Val,48) {49	match obj {50		Val::Obj(old) => old51			.extend_field(CStr::from_ptr(name).to_str().unwrap().into())52			.value(val.clone()),53		_ => panic!("should receive object"),54	}55}