git.delta.rocks / jrsonnet / refs/commits / 7af406eaa740

difftreelog

source

bindings/jsonnet/src/vars_tlas.rs2.8 KiBsourcehistory
1//! Manipulate external variables and top level arguments23use std::{ffi::CStr, os::raw::c_char};45use jrsonnet_evaluator::{IStr, tla::TlaArg};67use crate::VM;89/// Binds a Jsonnet external variable to the given string.10///11/// Argument values are copied so memory should be managed by the caller.12///13/// # Safety14///15/// `name`, `code` should be a NUL-terminated strings16#[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}3132/// Binds a Jsonnet external variable to the given code.33///34/// Argument values are copied so memory should be managed by the caller.35///36/// # Safety37///38/// `name`, `code` should be a NUL-terminated strings39#[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}5556/// Binds a top-level string argument for a top-level parameter.57///58/// Argument values are copied so memory should be managed by the caller.59///60/// # Safety61///62/// `name`, `value` should be a NUL-terminated strings63#[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}7273/// Binds a top-level code argument for a top-level parameter.74///75/// Argument values are copied so memory should be managed by the caller.76///77/// # Safety78///79/// `name`, `code` should be a NUL-terminated strings80#[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}