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

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};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(v)))28}2930/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.31#[no_mangle]32pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {33	assert!(v == 0 || v == 1, "bad boolean value");34	Box::into_raw(Box::new(Val::Bool(v == 1)))35}3637/// Make a `JsonnetJsonValue` representing `null`.38#[no_mangle]39pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {40	Box::into_raw(Box::new(Val::Null))41}4243/// Make a `JsonnetJsonValue` representing an array.44///45/// Assign elements with [`jsonnet_json_array_append`].46#[no_mangle]47pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {48	Box::into_raw(Box::new(Val::Arr(ArrValue::eager(Vec::new()))))49}5051/// Make a `JsonnetJsonValue` representing an object.52#[no_mangle]53pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {54	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))55}