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

difftreelog

source

bindings/jsonnet/src/val_make.rs1.6 KiBsourcehistory
1//! Create values in VM23use std::{4	ffi::CStr,5	os::raw::{c_char, c_double, c_int},6};78use jrsonnet_evaluator::{9	val::{ArrValue, StrValue},10	ObjValue, Val,11};1213use crate::VM;1415/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.16///17/// # Safety18///19/// `v` should be a NUL-terminated string20#[no_mangle]21pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {22	let val = CStr::from_ptr(val);23	let val = val.to_str().expect("string is not utf-8");24	Box::into_raw(Box::new(Val::Str(StrValue::Flat(val.into()))))25}2627/// Convert the given double to a `JsonnetJsonValue`.28#[no_mangle]29pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {30	Box::into_raw(Box::new(Val::Num(v)))31}3233/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.34#[no_mangle]35pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {36	assert!(v == 0 || v == 1, "bad boolean value");37	Box::into_raw(Box::new(Val::Bool(v == 1)))38}3940/// Make a `JsonnetJsonValue` representing `null`.41#[no_mangle]42pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {43	Box::into_raw(Box::new(Val::Null))44}4546/// Make a `JsonnetJsonValue` representing an array.47///48/// Assign elements with [`jsonnet_json_array_append`].49#[no_mangle]50pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {51	Box::into_raw(Box::new(Val::Arr(ArrValue::eager(Vec::new()))))52}5354/// Make a `JsonnetJsonValue` representing an object.55#[no_mangle]56pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {57	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))58}