git.delta.rocks / jrsonnet / refs/commits / 70f37833046b

difftreelog

source

bindings/jsonnet/src/lib.rs9.1 KiBsourcehistory
1#[cfg(feature = "interop")]2pub mod interop;34pub mod import;5pub mod native;6pub mod val_extract;7pub mod val_make;8pub mod val_modify;9pub mod vars_tlas;1011use std::{12	alloc::Layout,13	borrow::Cow,14	ffi::{CStr, CString, OsStr},15	os::raw::{c_char, c_double, c_int, c_uint},16	path::Path,17};1819use jrsonnet_evaluator::{20	trace::PathResolver, FileImportResolver, IStr, ManifestFormat, State, Val,21};2223/// WASM stub24#[cfg(target_arch = "wasm32")]25#[no_mangle]26pub extern "C" fn _start() {}2728/// Return the version string of the Jsonnet interpreter.29/// Conforms to [semantic versioning](http://semver.org/).30/// If this does not match `LIB_JSONNET_VERSION`31/// then there is a mismatch between header and compiled library.32#[no_mangle]33pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {34	b"v0.16.0\0"35}3637unsafe fn parse_path(input: &CStr) -> Cow<Path> {38	#[cfg(target_family = "unix")]39	{40		use std::os::unix::ffi::OsStrExt;41		let str = OsStr::from_bytes(input.to_bytes());42		Cow::Borrowed(Path::new(str))43	}44	#[cfg(target_family = "windows")]45	{46		use std::os::windows::ffi::OsStringExt;47		let str = input.to_str().expect("input is not utf8");48		let wide = str.encode_utf16().collect::<Vec<_>>();49		let wide = OsString::from_wide(&wide);50		Cow::Owned(PathBuf::new(wide))51	}52	#[cfg(not(any(target_family = "unix", target_family = "windows")))]53	{54		compile_error!("unsupported os")55	}56}5758unsafe fn unparse_path(input: &Path) -> Cow<CStr> {59	#[cfg(target_family = "unix")]60	{61		use std::os::unix::ffi::OsStrExt;62		let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");63		Cow::Owned(str)64	}65	#[cfg(not(any(target_family = "unix", target_family = "windows")))]66	{67		compile_error!("unsupported os")68	}69}7071/// Creates a new Jsonnet virtual machine.72#[no_mangle]73#[allow(clippy::box_default)]74pub extern "C" fn jsonnet_make() -> *mut State {75	let state = State::default();76	state.settings_mut().import_resolver = Box::new(FileImportResolver::default());77	state.settings_mut().context_initializer = Box::new(jrsonnet_stdlib::ContextInitializer::new(78		state.clone(),79		PathResolver::new_cwd_fallback(),80	));81	Box::into_raw(Box::new(state))82}8384/// Complement of [`jsonnet_vm_make`].85#[no_mangle]86#[allow(clippy::boxed_local)]87pub extern "C" fn jsonnet_destroy(vm: Box<State>) {88	drop(vm);89}9091/// Set the maximum stack depth.92#[no_mangle]93pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {94	vm.settings_mut().max_stack = v as usize;95}9697/// Set the number of objects required before a garbage collection cycle is allowed.98///99/// No-op for now100#[no_mangle]101pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}102103/// Run the garbage collector after this amount of growth in the number of objects104///105/// No-op for now106#[no_mangle]107pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}108109/// Expect a string as output and don't JSON encode it.110#[no_mangle]111pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {112	match v {113		1 => vm.set_manifest_format(ManifestFormat::String),114		0 => vm.set_manifest_format(ManifestFormat::Json {115			padding: 4,116			#[cfg(feature = "exp-preserve-order")]117			preserve_order: false,118		}),119		_ => panic!("incorrect output format"),120	}121}122123/// Allocate, resize, or free a buffer.  This will abort if the memory cannot be allocated. It will124/// only return NULL if sz was zero.125///126/// # Safety127///128/// `buf` should be either previosly allocated by this library, or NULL129///130/// This function is most definitely broken, but it works somehow, see TODO inside131#[no_mangle]132pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {133	if buf.is_null() {134		if sz == 0 {135			return std::ptr::null_mut();136		}137		return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());138	}139	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D140	// OR (Alternative way of fixing this TODO)141	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,142	// TODO: so it should work in normal cases. Maybe force allocator for this library?143	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();144	if sz == 0 {145		std::alloc::dealloc(buf, old_layout);146		return std::ptr::null_mut();147	}148	std::alloc::realloc(buf, old_layout, sz)149}150151/// Clean up a JSON subtree.152///153/// This is useful if you want to abort with an error mid-way through building a complex value.154#[no_mangle]155#[allow(clippy::boxed_local)]156pub extern "C" fn jsonnet_json_destroy(_vm: &State, v: Box<Val>) {157	drop(v);158}159160/// Set the number of lines of stack trace to display (0 for all of them).161#[no_mangle]162pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {163	vm.set_max_trace(v as usize)164}165166/// Evaluate a file containing Jsonnet code, return a JSON string.167///168/// The returned string should be cleaned up with jsonnet_realloc.169///170/// # Safety171///172/// `filename` should be a \0-terminated string173#[no_mangle]174pub unsafe extern "C" fn jsonnet_evaluate_file(175	vm: &State,176	filename: *const c_char,177	error: &mut c_int,178) -> *const c_char {179	let filename = parse_path(CStr::from_ptr(filename));180	match vm181		.import(&filename)182		.and_then(|v| vm.with_tla(v))183		.and_then(|v| vm.manifest(v))184	{185		Ok(v) => {186			*error = 0;187			CString::new(&*v as &str).unwrap().into_raw()188		}189		Err(e) => {190			*error = 1;191			let out = vm.stringify_err(&e);192			CString::new(&out as &str).unwrap().into_raw()193		}194	}195}196197/// Evaluate a string containing Jsonnet code, return a JSON string.198///199/// The returned string should be cleaned up with jsonnet_realloc.200///201/// # Safety202///203/// `filename`, `snippet` should be a \0-terminated strings204#[no_mangle]205pub unsafe extern "C" fn jsonnet_evaluate_snippet(206	vm: &State,207	filename: *const c_char,208	snippet: *const c_char,209	error: &mut c_int,210) -> *const c_char {211	let filename = CStr::from_ptr(filename);212	let snippet = CStr::from_ptr(snippet);213	match vm214		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())215		.and_then(|v| vm.with_tla(v))216		.and_then(|v| vm.manifest(v))217	{218		Ok(v) => {219			*error = 0;220			CString::new(&*v as &str).unwrap().into_raw()221		}222		Err(e) => {223			*error = 1;224			let out = vm.stringify_err(&e);225			CString::new(&out as &str).unwrap().into_raw()226		}227	}228}229230fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {231	let mut out = Vec::new();232	for (i, (k, v)) in multi.iter().enumerate() {233		if i != 0 {234			out.push(0);235		}236		out.extend_from_slice(k.as_bytes());237		out.push(0);238		out.extend_from_slice(v.as_bytes());239	}240	out.push(0);241	out.push(0);242	let v = out.as_ptr();243	std::mem::forget(out);244	v as *const c_char245}246247/// # Safety248#[no_mangle]249pub unsafe extern "C" fn jsonnet_evaluate_file_multi(250	vm: &State,251	filename: *const c_char,252	error: &mut c_int,253) -> *const c_char {254	let filename = parse_path(CStr::from_ptr(filename));255	match vm256		.import(&filename)257		.and_then(|v| vm.with_tla(v))258		.and_then(|v| vm.manifest_multi(v))259	{260		Ok(v) => {261			*error = 0;262			multi_to_raw(v)263		}264		Err(e) => {265			*error = 1;266			let out = vm.stringify_err(&e);267			CString::new(&out as &str).unwrap().into_raw()268		}269	}270}271272/// # Safety273#[no_mangle]274pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(275	vm: &State,276	filename: *const c_char,277	snippet: *const c_char,278	error: &mut c_int,279) -> *const c_char {280	let filename = CStr::from_ptr(filename);281	let snippet = CStr::from_ptr(snippet);282	match vm283		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())284		.and_then(|v| vm.with_tla(v))285		.and_then(|v| vm.manifest_multi(v))286	{287		Ok(v) => {288			*error = 0;289			multi_to_raw(v)290		}291		Err(e) => {292			*error = 1;293			let out = vm.stringify_err(&e);294			CString::new(&out as &str).unwrap().into_raw()295		}296	}297}298299fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {300	let mut out = Vec::new();301	for (i, v) in multi.iter().enumerate() {302		if i != 0 {303			out.push(0);304		}305		out.extend_from_slice(v.as_bytes());306	}307	out.push(0);308	out.push(0);309	let v = out.as_ptr();310	std::mem::forget(out);311	v as *const c_char312}313314/// # Safety315#[no_mangle]316pub unsafe extern "C" fn jsonnet_evaluate_file_stream(317	vm: &State,318	filename: *const c_char,319	error: &mut c_int,320) -> *const c_char {321	let filename = parse_path(CStr::from_ptr(filename));322	match vm323		.import(&filename)324		.and_then(|v| vm.with_tla(v))325		.and_then(|v| vm.manifest_stream(v))326	{327		Ok(v) => {328			*error = 0;329			stream_to_raw(v)330		}331		Err(e) => {332			*error = 1;333			let out = vm.stringify_err(&e);334			CString::new(&out as &str)335				.expect("there should be no \\0 in the error string")336				.into_raw()337		}338	}339}340341/// # Safety342#[no_mangle]343pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(344	vm: &State,345	filename: *const c_char,346	snippet: *const c_char,347	error: &mut c_int,348) -> *const c_char {349	let filename = CStr::from_ptr(filename);350	let snippet = CStr::from_ptr(snippet);351	match vm352		.evaluate_snippet(353			filename.to_str().expect("filename is not utf-8"),354			snippet.to_str().expect("snippet is not utf-8"),355		)356		.and_then(|v| vm.with_tla(v))357		.and_then(|v| vm.manifest_stream(v))358	{359		Ok(v) => {360			*error = 0;361			stream_to_raw(v)362		}363		Err(e) => {364			*error = 1;365			let out = vm.stringify_err(&e);366			CString::new(&out as &str)367				.expect("there should be no \\0 in the error string")368				.into_raw()369		}370	}371}