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

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::{val::ArrValue, ObjValue, Val};9use jrsonnet_gcmodule::Cc;1011use crate::VM;1213/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.14///15/// # Safety16///17/// `v` should be a NUL-terminated string18#[no_mangle]19pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {20	let val = CStr::from_ptr(val);21	let val = val.to_str().expect("string is not utf-8");22	Box::into_raw(Box::new(Val::Str(val.into())))23}2425/// Convert the given double to a `JsonnetJsonValue`.26#[no_mangle]27pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {28	Box::into_raw(Box::new(Val::Num(v)))29}3031/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.32#[no_mangle]33pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {34	assert!(v == 0 || v == 1, "bad boolean value");35	Box::into_raw(Box::new(Val::Bool(v == 1)))36}3738/// Make a `JsonnetJsonValue` representing `null`.39#[no_mangle]40pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {41	Box::into_raw(Box::new(Val::Null))42}4344/// Make a `JsonnetJsonValue` representing an array.45///46/// Assign elements with [`jsonnet_json_array_append`].47#[no_mangle]48pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {49	Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))50}5152/// Make a `JsonnetJsonValue` representing an object.53#[no_mangle]54pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {55	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))56}