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

difftreelog

source

bindings/jsonnet/src/vars_tlas.rs2.7 KiBsourcehistory
1//! Manipulate external variables and top level arguments23use std::{ffi::CStr, os::raw::c_char};45use jrsonnet_evaluator::State;67/// Bind a Jsonnet external var to the given string.8///9/// Argument values are copied so memory should be managed by caller.10///11/// # Safety12///13/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings14#[no_mangle]15pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {16	let name = CStr::from_ptr(name);17	let value = CStr::from_ptr(value);1819	let any_initializer = vm.context_initializer();20	any_initializer21		.as_any()22		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()23		.expect("only stdlib context initializer supported")24		.add_ext_str(25			name.to_str().expect("name is not utf-8").into(),26			value.to_str().expect("value is not utf-8").into(),27		)28}2930/// Bind a Jsonnet external var to the given code.31///32/// Argument values are copied so memory should be managed by caller.33///34/// # Safety35///36/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings37#[no_mangle]38pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, code: *const c_char) {39	let name = CStr::from_ptr(name);40	let code = CStr::from_ptr(code);4142	let any_initializer = vm.context_initializer();43	any_initializer44		.as_any()45		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()46		.expect("only stdlib context initializer supported")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		)51		.expect("can't parse ext code")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///58/// # Safety59///60/// Caller should pass correct pointers as `name` and `value`, they need to be \0-terminated strings61#[no_mangle]62pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {63	let name = CStr::from_ptr(name);64	let value = CStr::from_ptr(value);65	vm.add_tla_str(66		name.to_str().expect("name is not utf-8").into(),67		value.to_str().expect("value is not utf-8").into(),68	)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///75/// # Safety76///77/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings78#[no_mangle]79pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, code: *const c_char) {80	let name = CStr::from_ptr(name);81	let code = CStr::from_ptr(code);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	)86	.expect("can't parse tla code")87}