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

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 gcmodule::Cc;8use jrsonnet_evaluator::{val::ArrValue, LazyVal, State, Val};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			}21			new.push(LazyVal::new_resolved(val.clone()));22			*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));23		}24		_ => panic!("should receive array"),25	}26}2728/// # Safety29///30/// This function is safe if passed name is ok31#[no_mangle]32pub unsafe extern "C" fn jsonnet_json_object_append(33	_vm: &State,34	obj: &mut Val,35	name: *const c_char,36	val: &Val,37) {38	match obj {39		Val::Obj(old) => old40			.extend_field(CStr::from_ptr(name).to_str().unwrap().into())41			.value(val.clone()),42		_ => panic!("should receive object"),43	}44}