123use std::{ffi::CStr, os::raw::c_char};45use jrsonnet_evaluator::{IStr, tla::TlaArg};67use crate::VM;8910111213141516#[unsafe(no_mangle)]17pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *const c_char) {18 let name = unsafe { CStr::from_ptr(name) };19 let value = unsafe { CStr::from_ptr(value) };2021 let any_initializer = vm.state.context_initializer();22 any_initializer23 .as_any()24 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()25 .expect("only stdlib context initializer supported")26 .add_ext_str(27 name.to_str().expect("name is not utf-8").into(),28 value.to_str().expect("value is not utf-8").into(),29 );30}313233343536373839#[unsafe(no_mangle)]40pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *const c_char) {41 let name = unsafe { CStr::from_ptr(name) };42 let code = unsafe { CStr::from_ptr(code) };4344 let any_initializer = vm.state.context_initializer();45 any_initializer46 .as_any()47 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()48 .expect("only stdlib context initializer supported")49 .add_ext_code(50 name.to_str().expect("name is not utf-8"),51 code.to_str().expect("code is not utf-8"),52 )53 .expect("can't parse ext code");54}555657585960616263#[unsafe(no_mangle)]64pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value: *const c_char) {65 let name = unsafe { CStr::from_ptr(name) };66 let value = unsafe { CStr::from_ptr(value) };67 vm.tla_args.insert(68 name.to_str().expect("name is not utf-8").into(),69 TlaArg::String(value.to_str().expect("value is not utf-8").into()),70 );71}727374757677787980#[unsafe(no_mangle)]81pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code: *const c_char) {82 let name = unsafe { CStr::from_ptr(name) };83 let code = unsafe { CStr::from_ptr(code) };8485 let name: IStr = name.to_str().expect("name is not utf-8").into();86 let code: String = code.to_str().expect("code is not utf-8").to_owned();8788 vm.tla_args.insert(name, TlaArg::InlineCode(code));89}