git.delta.rocks / jrsonnet / refs/commits / 5858c9313e03

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::{NumValue, ObjValue, Val};910use crate::VM;1112/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.13///14/// # Safety15///16/// `v` should be a NUL-terminated string17#[no_mangle]18pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {19	let val = unsafe { CStr::from_ptr(val) };20	let val = val.to_str().expect("string is not utf-8");21	Box::into_raw(Box::new(Val::string(val)))22}2324/// Convert the given double to a `JsonnetJsonValue`.25#[no_mangle]26pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {27	Box::into_raw(Box::new(Val::Num(28		NumValue::new(v).expect("jsonnet numbers are finite"),29	)))30}3132/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.33#[no_mangle]34pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {35	assert!(v == 0 || v == 1, "bad boolean value");36	Box::into_raw(Box::new(Val::Bool(v == 1)))37}3839/// Make a `JsonnetJsonValue` representing `null`.40#[no_mangle]41pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {42	Box::into_raw(Box::new(Val::Null))43}4445/// Make a `JsonnetJsonValue` representing an array.46///47/// Assign elements with [`jsonnet_json_array_append`].48#[no_mangle]49pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {50	Box::into_raw(Box::new(Val::arr(())))51}5253/// Make a `JsonnetJsonValue` representing an object.54#[no_mangle]55pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {56	Box::into_raw(Box::new(Val::Obj(ObjValue::empty())))57}