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

difftreelog

source

bindings/jsonnet/src/vars_tlas.rs3.0 KiBsourcehistory
1//! Manipulate external variables and top level arguments23use std::{ffi::CStr, os::raw::c_char};45use jrsonnet_evaluator::{function::TlaArg, IStr};6use jrsonnet_parser::{ParserSettings, Source};78use crate::VM;910/// Binds a Jsonnet external variable to the given string.11///12/// Argument values are copied so memory should be managed by the caller.13///14/// # Safety15///16/// `name`, `code` should be a NUL-terminated strings17#[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}3233/// Binds a Jsonnet external variable to the given code.34///35/// Argument values are copied so memory should be managed by the caller.36///37/// # Safety38///39/// `name`, `code` should be a NUL-terminated strings40#[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}5657/// Binds a top-level string argument for a top-level parameter.58///59/// Argument values are copied so memory should be managed by the caller.60///61/// # Safety62///63/// `name`, `value` should be a NUL-terminated strings64#[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}7374/// Binds a top-level code argument for a top-level parameter.75///76/// Argument values are copied so memory should be managed by the caller.77///78/// # Safety79///80/// `name`, `code` should be a NUL-terminated strings81#[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}