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

difftreelog

source

bindings/jsonnet/src/val_modify.rs1.1 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/// # 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(_vm: &State, arr: &mut Val, val: &Val) {15	match arr {16		Val::Arr(old) => {17			let mut new = Vec::new();18			for item in old.iter_lazy() {19				new.push(item);20			}2122			new.push(Thunk::evaluated(val.clone()));23			*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));24		}25		_ => panic!("should receive array"),26	}27}2829/// # Safety30///31/// This function is safe if passed name is ok32#[no_mangle]33pub unsafe extern "C" fn jsonnet_json_object_append(34	_vm: &State,35	obj: &mut Val,36	name: *const c_char,37	val: &Val,38) {39	match obj {40		Val::Obj(old) => old41			.extend_field(CStr::from_ptr(name).to_str().unwrap().into())42			.value(val.clone()),43		_ => panic!("should receive object"),44	}45}