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

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, State, Val};9use jrsonnet_gcmodule::Cc;1011/// Convert the given UTF8 string to a JsonnetJsonValue.12///13/// # Safety14///15/// `v` should be a \0-terminated string16#[no_mangle]17pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, val: *const c_char) -> *mut Val {18	let val = CStr::from_ptr(val);19	let val = val.to_str().expect("string is not utf-8");20	Box::into_raw(Box::new(Val::Str(val.into())))21}2223/// Convert the given double to a JsonnetJsonValue.24#[no_mangle]25pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {26	Box::into_raw(Box::new(Val::Num(v)))27}2829/// Convert the given bool (1 or 0) to a JsonnetJsonValue.30#[no_mangle]31pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {32	assert!(v == 0 || v == 1, "bad boolean value");33	Box::into_raw(Box::new(Val::Bool(v == 1)))34}3536/// Make a JsonnetJsonValue representing null.37#[no_mangle]38pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {39	Box::into_raw(Box::new(Val::Null))40}4142/// Make a JsonnetJsonValue representing an array.43///44/// Assign elements with jsonnet_json_array_append.45#[no_mangle]46pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {47	Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))48}4950/// Make a JsonnetJsonValue representing an object.51#[no_mangle]52pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {53	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))54}