git.delta.rocks / jrsonnet / refs/heads / master

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 jrsonnet_evaluator::{Thunk, Val};89use crate::VM;1011/// Adds value to the end of the array `arr`.12///13/// # Safety14///15/// `arr` should be a pointer to array value allocated by `make_array`, or returned by other library call16/// `val` should be a pointer to value allocated using this library17#[unsafe(no_mangle)]18pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {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			}2526			new.push(Thunk::evaluated(val.clone()));27			*arr = Val::arr(new);28		}29		_ => panic!("should receive array"),30	}31}3233/// Adds the field to the object, bound to value.34///35/// This shadows any previous binding of the field.36///37/// # Safety38///39/// `obj` should be a pointer to object value allocated by `make_object`, or returned by other library call40/// `name` should be NUL-terminated string41#[unsafe(no_mangle)]42pub unsafe extern "C" fn jsonnet_json_object_append(43	_vm: &VM,44	obj: &mut Val,45	name: *const c_char,46	val: &Val,47) {48	match obj {49		Val::Obj(old) => old50			.extend_field(unsafe { CStr::from_ptr(name).to_str().unwrap().into() })51			.value(val.clone()),52		_ => panic!("should receive object"),53	}54}