123use std::{ffi::CStr, os::raw::c_char};45use jrsonnet_evaluator::{function::TlaArg, IStr};6use jrsonnet_parser::{ParserSettings, Source};78use crate::VM;91011121314151617#[no_mangle]18pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *const c_char) {19 let name = unsafe { CStr::from_ptr(name) };20 let value = unsafe { CStr::from_ptr(value) };2122 let any_initializer = vm.state.context_initializer();23 any_initializer24 .as_any()25 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()26 .expect("only stdlib context initializer supported")27 .add_ext_str(28 name.to_str().expect("name is not utf-8").into(),29 value.to_str().expect("value is not utf-8").into(),30 )31}323334353637383940#[no_mangle]41pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *const c_char) {42 let name = unsafe { CStr::from_ptr(name) };43 let code = unsafe { CStr::from_ptr(code) };4445 let any_initializer = vm.state.context_initializer();46 any_initializer47 .as_any()48 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()49 .expect("only stdlib context initializer supported")50 .add_ext_code(51 name.to_str().expect("name is not utf-8"),52 code.to_str().expect("code is not utf-8"),53 )54 .expect("can't parse ext code")55}565758596061626364#[no_mangle]65pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value: *const c_char) {66 let name = unsafe { CStr::from_ptr(name) };67 let value = unsafe { CStr::from_ptr(value) };68 vm.tla_args.insert(69 name.to_str().expect("name is not utf-8").into(),70 TlaArg::String(value.to_str().expect("value is not utf-8").into()),71 );72}737475767778798081#[no_mangle]82pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code: *const c_char) {83 let name = unsafe { CStr::from_ptr(name) };84 let code = unsafe { CStr::from_ptr(code) };8586 let name: IStr = name.to_str().expect("name is not utf-8").into();87 let code: IStr = code.to_str().expect("code is not utf-8").into();88 let code = jrsonnet_parser::parse(89 &code,90 &ParserSettings {91 source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),92 },93 )94 .expect("can't parse TLA code");9596 vm.tla_args.insert(name, TlaArg::Code(code));97}