git.delta.rocks / jrsonnet / refs/commits / 1d4b842070e4

difftreelog

source

bindings/jsonnet/src/lib.rs4.4 KiBsourcehistory
1pub mod import;2pub mod interop;3pub mod val_extract;4pub mod val_make;5pub mod val_modify;6pub mod vars_tlas;78use import::NativeImportResolver;9use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Val};10use std::{11	alloc::Layout,12	ffi::{CStr, CString},13	os::raw::{c_char, c_double, c_int, c_uint},14	path::PathBuf,15	rc::Rc,16};1718/// WASM stub19#[cfg(target_arch = "wasm32")]20#[no_mangle]21pub extern "C" fn _start() {}2223#[no_mangle]24pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {25	b"v0.16.0\0"26}2728#[no_mangle]29pub extern "C" fn jsonnet_make() -> *mut EvaluationState {30	let state = EvaluationState::default();31	state.with_stdlib();32	state.settings_mut().import_resolver = Box::new(NativeImportResolver::default());33	Box::into_raw(Box::new(state))34}3536/// # Safety37#[no_mangle]38#[allow(clippy::boxed_local)]39pub unsafe extern "C" fn jsonnet_destroy(vm: *mut EvaluationState) {40	Box::from_raw(vm);41}4243#[no_mangle]44pub extern "C" fn jsonnet_max_stack(vm: &EvaluationState, v: c_uint) {45	vm.settings_mut().max_stack = v as usize;46}4748// jrsonnet currently have no GC, so these functions is no-op49#[no_mangle]50pub extern "C" fn jsonnet_gc_min_objects(_vm: &EvaluationState, _v: c_uint) {}51#[no_mangle]52pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &EvaluationState, _v: c_double) {}5354#[no_mangle]55pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {56	match v {57		1 => vm.set_manifest_format(ManifestFormat::None),58		0 => vm.set_manifest_format(ManifestFormat::Json(4)),59		_ => panic!("incorrect output format"),60	}61}6263/// # Safety64///65/// This function is most definitely broken, but it works somehow, see TODO inside66#[no_mangle]67pub unsafe extern "C" fn jsonnet_realloc(68	_vm: &EvaluationState,69	buf: *mut u8,70	sz: usize,71) -> *mut u8 {72	if buf.is_null() {73		assert!(sz != 0);74		return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());75	}76	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D77	// OR (Alternative way of fixing this TODO)78	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,79	// TODO: so it should work in normal cases. Maybe force allocator for this library?80	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();81	if sz == 0 {82		std::alloc::dealloc(buf, old_layout);83		return std::ptr::null_mut();84	}85	std::alloc::realloc(buf, old_layout, sz)86}8788/// # Safety89#[no_mangle]90#[allow(clippy::boxed_local)]91pub unsafe extern "C" fn jsonnet_json_destroy(_vm: &EvaluationState, v: *mut Val) {92	Box::from_raw(v);93}9495#[no_mangle]96pub extern "C" fn jsonnet_native_callback() {97	todo!()98}99100#[no_mangle]101pub extern "C" fn jsonnet_max_trace(vm: &EvaluationState, v: c_uint) {102	vm.set_max_trace(v as usize)103}104105/// # Safety106///107/// This function is safe, if received v is a pointer to normal C string108#[no_mangle]109pub unsafe extern "C" fn jsonnet_evaluate_file(110	vm: &EvaluationState,111	filename: *const c_char,112	error: &mut c_int,113) -> *const c_char {114	vm.run_in_state(|| {115		let filename = CStr::from_ptr(filename);116		match vm117			.evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))118			.and_then(|v| vm.with_tla(v))119			.and_then(|v| vm.manifest(v))120		{121			Ok(v) => {122				*error = 0;123				CString::new(&*v as &str).unwrap().into_raw()124			}125			Err(e) => {126				*error = 1;127				let out = vm.stringify_err(&e);128				CString::new(&out as &str).unwrap().into_raw()129			}130		}131	})132}133134/// # Safety135///136/// This function is safe, if received v is a pointer to normal C string137#[no_mangle]138pub unsafe extern "C" fn jsonnet_evaluate_snippet(139	vm: &EvaluationState,140	filename: *const c_char,141	snippet: *const c_char,142	error: &mut c_int,143) -> *const c_char {144	vm.run_in_state(|| {145		let filename = CStr::from_ptr(filename);146		let snippet = CStr::from_ptr(snippet);147		match vm148			.evaluate_snippet_raw(149				Rc::new(PathBuf::from(filename.to_str().unwrap())),150				snippet.to_str().unwrap().into(),151			)152			.and_then(|v| vm.with_tla(v))153			.and_then(|v| vm.manifest(v))154		{155			Ok(v) => {156				*error = 0;157				CString::new(&*v as &str).unwrap().into_raw()158			}159			Err(e) => {160				*error = 1;161				let out = vm.stringify_err(&e);162				CString::new(&out as &str).unwrap().into_raw()163			}164		}165	})166}167168#[no_mangle]169pub extern "C" fn jsonnet_evaluate_file_multi() {170	todo!()171}172#[no_mangle]173pub extern "C" fn jsonnet_evaluate_snippet_multi() {174	todo!()175}176#[no_mangle]177pub extern "C" fn jsonnet_evaluate_file_stream() {178	todo!()179}180#[no_mangle]181pub extern "C" fn jsonnet_evaluate_snippet_stream() {182	todo!()183}