git.delta.rocks / jrsonnet / refs/commits / 6e67554e1625

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, NumValue},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 = unsafe { CStr::from_ptr(val) };23	let val = val.to_str().expect("string is not utf-8");24	Box::into_raw(Box::new(Val::string(val)))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(31		NumValue::new(v).expect("jsonnet numbers are finite"),32	)))33}3435/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.36#[no_mangle]37pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {38	assert!(v == 0 || v == 1, "bad boolean value");39	Box::into_raw(Box::new(Val::Bool(v == 1)))40}4142/// Make a `JsonnetJsonValue` representing `null`.43#[no_mangle]44pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {45	Box::into_raw(Box::new(Val::Null))46}4748/// Make a `JsonnetJsonValue` representing an array.49///50/// Assign elements with [`jsonnet_json_array_append`].51#[no_mangle]52pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {53	Box::into_raw(Box::new(Val::Arr(ArrValue::eager(Vec::new()))))54}5556/// Make a `JsonnetJsonValue` representing an object.57#[no_mangle]58pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {59	Box::into_raw(Box::new(Val::Obj(ObjValue::empty())))60}