difftreelog
doc(libjsonnet): copy official documentation to doccomments
in: master
7 files changed
bindings/jsonnet/Cargo.tomldiffbeforeafterboth1[package]1[package]2name = "jsonnet"2name = "libjsonnet"3description = "Rust implementation of libjsonnet.so"3description = "Rust implementation of libjsonnet.so"4version = "0.4.2"4version = "0.4.2"5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]14jrsonnet-gcmodule = { version = "0.3.4" }14jrsonnet-gcmodule = { version = "0.3.4" }151516[lib]16[lib]17name = "jsonnet"17crate-type = ["cdylib"]18crate-type = ["cdylib"]181919[features]20[features]21# Export additional functions for native integration, i.e ability to set custom trace format20interop = []22interop = []21experimental = ["exp-preserve-order", "exp-destruct"]23experimental = ["exp-preserve-order", "exp-destruct"]22exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]24exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]bindings/jsonnet/src/lib.rsdiffbeforeafterboth101011use std::{11use std::{12 alloc::Layout,12 alloc::Layout,13 env,13 borrow::Cow,14 ffi::{CStr, CString},14 ffi::{CStr, CString, OsStr},15 os::raw::{c_char, c_double, c_int, c_uint},15 os::raw::{c_char, c_double, c_int, c_uint},16 path::Path,16};17};171818use import::NativeImportResolver;19use jrsonnet_evaluator::{IStr, ManifestFormat, State, Val};19use jrsonnet_evaluator::{20 trace::PathResolver, FileImportResolver, IStr, ManifestFormat, State, Val,21};202221/// WASM stub23/// WASM stub22#[cfg(target_arch = "wasm32")]24#[cfg(target_arch = "wasm32")]23#[no_mangle]25#[no_mangle]24pub extern "C" fn _start() {}26pub extern "C" fn _start() {}252728/// Return the version string of the Jsonnet interpreter. Conforms to semantic versioning29/// http://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between30/// header and compiled library.26#[no_mangle]31#[no_mangle]27pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {32pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {28 b"v0.16.0\0"33 b"v0.16.0\0"29}34}303536unsafe fn parse_path(input: &CStr) -> Cow<Path> {37 #[cfg(target_family = "unix")]38 {39 use std::os::unix::ffi::OsStrExt;40 let str = OsStr::from_bytes(input.to_bytes());41 Cow::Borrowed(Path::new(str))42 }43 #[cfg(target_family = "windows")]44 {45 use std::os::windows::ffi::OsStringExt;46 let str = input.to_str().expect("input is not utf8");47 let wide = str.encode_utf16().collect::<Vec<_>>();48 let wide = OsString::from_wide(&wide);49 Cow::Owned(PathBuf::new(wide))50 }51 #[cfg(not(any(target_family = "unix", target_family = "windows")))]52 {53 compile_error!("unsupported os")54 }55}5657unsafe fn unparse_path(input: &Path) -> Cow<CStr> {58 #[cfg(target_family = "unix")]59 {60 use std::os::unix::ffi::OsStrExt;61 let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");62 Cow::Owned(str)63 }64 #[cfg(not(any(target_family = "unix", target_family = "windows")))]65 {66 compile_error!("unsupported os")67 }68}6970/// Create a new Jsonnet virtual machine.31#[no_mangle]71#[no_mangle]32pub extern "C" fn jsonnet_make() -> *mut State {72pub extern "C" fn jsonnet_make() -> *mut State {33 let state = State::default();73 let state = State::default();34 state.settings_mut().import_resolver = Box::new(NativeImportResolver::default());74 state.settings_mut().import_resolver = Box::new(FileImportResolver::default());35 state.settings_mut().context_initializer =75 state.settings_mut().context_initializer = Box::new(jrsonnet_stdlib::ContextInitializer::new(36 Box::new(jrsonnet_stdlib::ContextInitializer::new(state.clone()));76 state.clone(),77 PathResolver::new_cwd_fallback(),78 ));37 Box::into_raw(Box::new(state))79 Box::into_raw(Box::new(state))38}80}398140/// # Safety82/// Complement of `jsonnet_vm_make`41#[no_mangle]83#[no_mangle]42#[allow(clippy::boxed_local)]84#[allow(clippy::boxed_local)]43pub unsafe extern "C" fn jsonnet_destroy(vm: *mut State) {85pub extern "C" fn jsonnet_destroy(vm: Box<State>) {44 drop(Box::from_raw(vm));86 drop(vm);45}87}468889/// Set the maximum stack depth.47#[no_mangle]90#[no_mangle]48pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {91pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {49 vm.settings_mut().max_stack = v as usize;92 vm.settings_mut().max_stack = v as usize;50}93}519452// jrsonnet currently have no GC, so these functions is no-op95/// Set the number of objects required before a garbage collection cycle is allowed.96///97/// No-op for now53#[no_mangle]98#[no_mangle]54pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}99pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}100101/// Run the garbage collector after this amount of growth in the number of objects102///103/// No-op for now55#[no_mangle]104#[no_mangle]56pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}105pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}57106107/// Expect a string as output and don't JSON encode it.58#[no_mangle]108#[no_mangle]59pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {109pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {60 match v {110 match v {68 }118 }69}119}70120121/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will122/// only return NULL if sz was zero.123///71/// # Safety124/// # Safety125///126/// `buf` should be either previosly allocated by this library, or NULL72///127///73/// This function is most definitely broken, but it works somehow, see TODO inside128/// This function is most definitely broken, but it works somehow, see TODO inside74#[no_mangle]129#[no_mangle]75pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {130pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {76 if buf.is_null() {131 if buf.is_null() {77 assert!(sz != 0);132 if sz == 0 {133 return std::ptr::null_mut();134 }78 return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());135 return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());79 }136 }80 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D137 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D89 std::alloc::realloc(buf, old_layout, sz)146 std::alloc::realloc(buf, old_layout, sz)90}147}91148149/// Clean up a JSON subtree.92/// # Safety150///151/// This is useful if you want to abort with an error mid-way through building a complex value.93#[no_mangle]152#[no_mangle]94#[allow(clippy::boxed_local)]153#[allow(clippy::boxed_local)]95pub unsafe extern "C" fn jsonnet_json_destroy(_vm: &State, v: *mut Val) {154pub extern "C" fn jsonnet_json_destroy(_vm: &State, v: Box<Val>) {96 drop(Box::from_raw(v));155 drop(v);97}156}98157158/// Set the number of lines of stack trace to display (0 for all of them).99#[no_mangle]159#[no_mangle]100pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {160pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {101 vm.set_max_trace(v as usize)161 vm.set_max_trace(v as usize)102}162}103163164/// Evaluate a file containing Jsonnet code, return a JSON string.165///166/// The returned string should be cleaned up with jsonnet_realloc.167///104/// # Safety168/// # Safety105///169///106/// This function is safe, if received v is a pointer to normal C string170/// `filename` should be a \0-terminated string107#[no_mangle]171#[no_mangle]108pub unsafe extern "C" fn jsonnet_evaluate_file(172pub unsafe extern "C" fn jsonnet_evaluate_file(109 vm: &State,173 vm: &State,110 filename: *const c_char,174 filename: *const c_char,111 error: &mut c_int,175 error: &mut c_int,112) -> *const c_char {176) -> *const c_char {113 let filename = CStr::from_ptr(filename);177 let filename = parse_path(CStr::from_ptr(filename));114 match vm178 match vm115 .import(179 .import(&filename)116 &env::current_dir().expect("cwd"),117 filename.to_str().unwrap(),118 )119 .and_then(|v| vm.with_tla(v))180 .and_then(|v| vm.with_tla(v))120 .and_then(|v| vm.manifest(v))181 .and_then(|v| vm.manifest(v))131 }192 }132}193}133194195/// Evaluate a string containing Jsonnet code, return a JSON string.196///197/// The returned string should be cleaned up with jsonnet_realloc.198///134/// # Safety199/// # Safety135///200///136/// This function is safe, if received v is a pointer to normal C string201/// `filename`, `snippet` should be a \0-terminated strings137#[no_mangle]202#[no_mangle]138pub unsafe extern "C" fn jsonnet_evaluate_snippet(203pub unsafe extern "C" fn jsonnet_evaluate_snippet(139 vm: &State,204 vm: &State,144 let filename = CStr::from_ptr(filename);209 let filename = CStr::from_ptr(filename);145 let snippet = CStr::from_ptr(snippet);210 let snippet = CStr::from_ptr(snippet);146 match vm211 match vm147 .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())212 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())148 .and_then(|v| vm.with_tla(v))213 .and_then(|v| vm.with_tla(v))149 .and_then(|v| vm.manifest(v))214 .and_then(|v| vm.manifest(v))150 {215 {184 filename: *const c_char,249 filename: *const c_char,185 error: &mut c_int,250 error: &mut c_int,186) -> *const c_char {251) -> *const c_char {187 let filename = CStr::from_ptr(filename);252 let filename = parse_path(CStr::from_ptr(filename));188 match vm253 match vm189 .import(254 .import(&filename)190 &env::current_dir().expect("cwd"),191 filename.to_str().unwrap(),192 )193 .and_then(|v| vm.with_tla(v))255 .and_then(|v| vm.with_tla(v))194 .and_then(|v| vm.manifest_multi(v))256 .and_then(|v| vm.manifest_multi(v))216 let filename = CStr::from_ptr(filename);278 let filename = CStr::from_ptr(filename);217 let snippet = CStr::from_ptr(snippet);279 let snippet = CStr::from_ptr(snippet);218 match vm280 match vm219 .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())281 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())220 .and_then(|v| vm.with_tla(v))282 .and_then(|v| vm.with_tla(v))221 .and_then(|v| vm.manifest_multi(v))283 .and_then(|v| vm.manifest_multi(v))222 {284 {254 filename: *const c_char,316 filename: *const c_char,255 error: &mut c_int,317 error: &mut c_int,256) -> *const c_char {318) -> *const c_char {257 let filename = CStr::from_ptr(filename);319 let filename = parse_path(CStr::from_ptr(filename));258 match vm320 match vm259 .import(321 .import(&filename)260 &env::current_dir().expect("cwd"),261 filename.to_str().unwrap(),262 )263 .and_then(|v| vm.with_tla(v))322 .and_then(|v| vm.with_tla(v))264 .and_then(|v| vm.manifest_stream(v))323 .and_then(|v| vm.manifest_stream(v))270 Err(e) => {329 Err(e) => {271 *error = 1;330 *error = 1;272 let out = vm.stringify_err(&e);331 let out = vm.stringify_err(&e);273 CString::new(&out as &str).unwrap().into_raw()332 CString::new(&out as &str)333 .expect("there should be no \\0 in the error string")334 .into_raw()274 }335 }275 }336 }286 let filename = CStr::from_ptr(filename);347 let filename = CStr::from_ptr(filename);287 let snippet = CStr::from_ptr(snippet);348 let snippet = CStr::from_ptr(snippet);288 match vm349 match vm289 .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())350 .evaluate_snippet(351 filename.to_str().expect("filename is not utf-8"),352 snippet.to_str().expect("snippet is not utf-8"),353 )290 .and_then(|v| vm.with_tla(v))354 .and_then(|v| vm.with_tla(v))291 .and_then(|v| vm.manifest_stream(v))355 .and_then(|v| vm.manifest_stream(v))297 Err(e) => {361 Err(e) => {298 *error = 1;362 *error = 1;299 let out = vm.stringify_err(&e);363 let out = vm.stringify_err(&e);300 CString::new(&out as &str).unwrap().into_raw()364 CString::new(&out as &str)365 .expect("there should be no \\0 in the error string")366 .into_raw()301 }367 }302 }368 }bindings/jsonnet/src/native.rsdiffbeforeafterboth12};12};13use jrsonnet_gcmodule::Cc;13use jrsonnet_gcmodule::Cc;141415/// The returned JsonnetJsonValue* should be allocated with jsonnet_realloc. It will be cleaned up16/// along with the objects rooted at argv by libjsonnet when no-longer needed. Return a string upon17/// failure, which will appear in Jsonnet as an error. The argv pointer is an array whose size18/// matches the array of parameters supplied when the native callback was originally registered.19///20/// - `ctx` User pointer, given in jsonnet_native_callback.21/// - `argv` Array of arguments from Jsonnet code.22/// - `param` success Set this byref param to 1 to indicate success and 0 for failure.23/// Returns the content of the imported file, or an error message.15type JsonnetNativeCallback = unsafe extern "C" fn(24type JsonnetNativeCallback = unsafe extern "C" fn(16 ctx: *const c_void,25 ctx: *const c_void,17 argv: *const *const Val,26 argv: *const *const Val,44 if success == 1 {53 if success == 1 {45 Ok(v)54 Ok(v)46 } else {55 } else {47 let e = IStr::from_untyped(v, s).expect("error msg");56 let e = IStr::from_untyped(v, s).expect("error msg should be a string");48 Err(Error::RuntimeError(e).into())57 Err(Error::RuntimeError(e).into())49 }58 }50 }59 }51}60}526162/// Callback to provide native extensions to Jsonnet.63///53/// # Safety64/// # Safety65///66/// `vm` should be a vm allocated by `jsonnet_make`67/// `cb` should be a correct function pointer68/// `raw_params` should point to a NULL-terminated string array69/// `name`, `raw_params` elements should be a \0-terminated strings54#[no_mangle]70#[no_mangle]55pub unsafe extern "C" fn jsonnet_native_callback(71pub unsafe extern "C" fn jsonnet_native_callback(56 vm: &State,72 vm: &State,59 ctx: *const c_void,75 ctx: *const c_void,60 mut raw_params: *const *const c_char,76 mut raw_params: *const *const c_char,61) {77) {62 let name = CStr::from_ptr(name).to_str().expect("utf8 name").into();78 let name = CStr::from_ptr(name)79 .to_str()80 .expect("name is not utf-8")81 .into();63 let mut params = Vec::new();82 let mut params = Vec::new();64 loop {83 loop {65 if (*raw_params).is_null() {84 if (*raw_params).is_null() {66 break;85 break;67 }86 }68 let param = CStr::from_ptr(*raw_params).to_str().expect("not utf8");87 let param = CStr::from_ptr(*raw_params)88 .to_str()89 .expect("param name is not utf-8");69 params.push(BuiltinParam {90 params.push(BuiltinParam {70 name: param.into(),91 name: param.into(),71 has_default: false,92 has_default: false,bindings/jsonnet/src/val_extract.rsdiffbeforeafterboth778use jrsonnet_evaluator::{State, Val};8use jrsonnet_evaluator::{State, Val};9910/// If the value is a string, return it as UTF8 otherwise return NULL.10#[no_mangle]11#[no_mangle]11pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {12pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {12 match v {13 match v {15 }16 }16}17}1819/// If the value is a number, return 1 and store the number in out, otherwise return 0.17#[no_mangle]20#[no_mangle]18pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {21pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {19 match v {22 match v {25 }28 }26}29}3031/// Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.27#[no_mangle]32#[no_mangle]28pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {33pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {29 match v {34 match v {33 }38 }34}39}4041/// Return 1 if the value is null, else 0.35#[no_mangle]42#[no_mangle]36pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {43pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {37 match v {44 match v {bindings/jsonnet/src/val_make.rsdiffbeforeafterboth8use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};8use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};9use jrsonnet_gcmodule::Cc;9use jrsonnet_gcmodule::Cc;101011/// Convert the given UTF8 string to a JsonnetJsonValue.12///11/// # Safety13/// # Safety12///14///13/// This function is safe, if received v is a pointer to normal C string15/// `v` should be a \0-terminated string14#[no_mangle]16#[no_mangle]15pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, v: *const c_char) -> *mut Val {17pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, val: *const c_char) -> *mut Val {16 let cstr = CStr::from_ptr(v);18 let val = CStr::from_ptr(val);17 let str = cstr.to_str().unwrap();19 let val = val.to_str().expect("string is not utf-8");18 Box::into_raw(Box::new(Val::Str(str.into())))20 Box::into_raw(Box::new(Val::Str(val.into())))19}21}202223/// Convert the given double to a JsonnetJsonValue.21#[no_mangle]24#[no_mangle]22pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {25pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {23 Box::into_raw(Box::new(Val::Num(v)))26 Box::into_raw(Box::new(Val::Num(v)))24}27}252829/// Convert the given bool (1 or 0) to a JsonnetJsonValue.26#[no_mangle]30#[no_mangle]27pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {31pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {28 assert!(v == 0 || v == 1);32 assert!(v == 0 || v == 1, "bad boolean value");29 Box::into_raw(Box::new(Val::Bool(v == 1)))33 Box::into_raw(Box::new(Val::Bool(v == 1)))30}34}313536/// Make a JsonnetJsonValue representing null.32#[no_mangle]37#[no_mangle]33pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {38pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {34 Box::into_raw(Box::new(Val::Null))39 Box::into_raw(Box::new(Val::Null))35}40}364142/// Make a JsonnetJsonValue representing an array.43///44/// Assign elements with jsonnet_json_array_append.37#[no_mangle]45#[no_mangle]38pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {46pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {39 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))47 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))40}48}414950/// Make a JsonnetJsonValue representing an object.42#[no_mangle]51#[no_mangle]43pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {52pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {44 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))53 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth7use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};7use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};8use jrsonnet_gcmodule::Cc;8use jrsonnet_gcmodule::Cc;9910/// Add value to the end of the array arr11///10/// # Safety12/// # Safety11///13///14/// `arr` should be correct pointer to array value allocated by make_array, or returned by other library call12/// Received arr value should be correct pointer to array allocated by make_array15/// `val` should be correct pointer to value allocated using this library13#[no_mangle]16#[no_mangle]14pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {17pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {15 match arr {18 match arr {26 }29 }27}30}283132/// Add the field to the object, bound to value.33///34/// This shadows any previous binding of the field.35///29/// # Safety36/// # Safety30///37///31/// This function is safe if passed name is ok38/// `obj` should be pointer to object value allocated by make_object, or returned by other library call39/// `name` should be \0-terminated string32#[no_mangle]40#[no_mangle]33pub unsafe extern "C" fn jsonnet_json_object_append(41pub unsafe extern "C" fn jsonnet_json_object_append(34 _vm: &State,42 _vm: &State,bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth445use jrsonnet_evaluator::State;5use jrsonnet_evaluator::State;667/// Bind a Jsonnet external var to the given string.8///9/// Argument values are copied so memory should be managed by caller.10///7/// # Safety11/// # Safety12///13/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings8#[no_mangle]14#[no_mangle]9pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {15pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {10 let name = CStr::from_ptr(name);16 let name = CStr::from_ptr(name);11 let value = CStr::from_ptr(value);17 let value = CStr::from_ptr(value);121813 let any_resolver = vm.context_initializer();19 let any_initializer = vm.context_initializer();14 any_resolver20 any_initializer15 .as_any()21 .as_any()16 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()22 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()17 .expect("only stdlib context initializer supported")23 .expect("only stdlib context initializer supported")18 .add_ext_str(24 .add_ext_str(19 name.to_str().unwrap().into(),25 name.to_str().expect("name is not utf-8").into(),20 value.to_str().unwrap().into(),26 value.to_str().expect("value is not utf-8").into(),21 )27 )22}28}232930/// Bind a Jsonnet external var to the given code.31///32/// Argument values are copied so memory should be managed by caller.33///24/// # Safety34/// # Safety35///36/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings25#[no_mangle]37#[no_mangle]26pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, value: *const c_char) {38pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, code: *const c_char) {27 let name = CStr::from_ptr(name);39 let name = CStr::from_ptr(name);28 let value = CStr::from_ptr(value);40 let code = CStr::from_ptr(code);294130 let any_resolver = vm.context_initializer();42 let any_initializer = vm.context_initializer();31 any_resolver43 any_initializer32 .as_any()44 .as_any()33 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()45 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()34 .expect("only stdlib context initializer supported")46 .expect("only stdlib context initializer supported")35 .add_ext_code(name.to_str().unwrap(), value.to_str().unwrap())47 .add_ext_code(48 name.to_str().expect("name is not utf-8"),49 code.to_str().expect("code is not utf-8"),50 )36 .unwrap()51 .expect("can't parse ext code")37}52}5354/// Bind a string top-level argument for a top-level parameter.55///56/// Argument values are copied so memory should be managed by caller.57///38/// # Safety58/// # Safety59///60/// Caller should pass correct pointers as `name` and `value`, they need to be \0-terminated strings39#[no_mangle]61#[no_mangle]40pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {62pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {41 let name = CStr::from_ptr(name);63 let name = CStr::from_ptr(name);42 let value = CStr::from_ptr(value);64 let value = CStr::from_ptr(value);43 vm.add_tla_str(65 vm.add_tla_str(44 name.to_str().unwrap().into(),66 name.to_str().expect("name is not utf-8").into(),45 value.to_str().unwrap().into(),67 value.to_str().expect("value is not utf-8").into(),46 )68 )47}69}7071/// Bind a code top-level argument for a top-level parameter.72///73/// Argument values are copied so memory should be managed by caller.74///48/// # Safety75/// # Safety76///77/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings49#[no_mangle]78#[no_mangle]50pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, value: *const c_char) {79pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, code: *const c_char) {51 let name = CStr::from_ptr(name);80 let name = CStr::from_ptr(name);52 let value = CStr::from_ptr(value);81 let code = CStr::from_ptr(code);53 vm.add_tla_code(name.to_str().unwrap().into(), value.to_str().unwrap())82 vm.add_tla_code(83 name.to_str().expect("name is not utf-8").into(),84 code.to_str().expect("code is not utf-8"),85 )54 .unwrap()86 .expect("can't parse tla code")55}87}5688