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

difftreelog

source

bindings/jsonnet/src/lib.rs4.5 KiBsourcehistory
1#![feature(custom_inner_attributes)]23pub mod import;4pub mod interop;5pub mod val_extract;6pub mod val_make;7pub mod val_modify;8pub mod vars_tlas;910use import::NativeImportResolver;11use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Val};12use std::{13	alloc::Layout,14	ffi::{CStr, CString},15	os::raw::{c_char, c_double, c_int, c_uint},16	path::PathBuf,17	rc::Rc,18};1920/// WASM stub21#[no_mangle]22pub extern "C" fn _start() {}2324#[no_mangle]25pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {26	b"v0.16.0\0"27}2829#[no_mangle]30pub extern "C" fn jsonnet_make() -> *mut EvaluationState {31	let state = EvaluationState::default();32	state.with_stdlib();33	state.settings_mut().import_resolver = Box::new(NativeImportResolver::default());34	Box::into_raw(Box::new(state))35}3637/// # Safety38#[no_mangle]39#[allow(clippy::boxed_local)]40pub unsafe extern "C" fn jsonnet_destroy(vm: *mut EvaluationState) {41	Box::from_raw(vm);42}4344#[no_mangle]45pub extern "C" fn jsonnet_max_stack(vm: &EvaluationState, v: c_uint) {46	vm.settings_mut().max_stack = v as usize;47}4849// jrsonnet currently have no GC, so these functions is no-op50#[no_mangle]51pub extern "C" fn jsonnet_gc_min_objects(_vm: &EvaluationState, _v: c_uint) {}52#[no_mangle]53pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &EvaluationState, _v: c_double) {}5455#[no_mangle]56pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {57	match v {58		1 => vm.set_manifest_format(ManifestFormat::None),59		0 => vm.set_manifest_format(ManifestFormat::Json(4)),60		_ => panic!("incorrect output format"),61	}62}6364/// # Safety65///66/// This function is most definitely broken, but it works somehow, see TODO inside67#[no_mangle]68pub unsafe extern "C" fn jsonnet_realloc(69	_vm: &EvaluationState,70	buf: *mut u8,71	sz: usize,72) -> *mut u8 {73	if buf.is_null() {74		assert!(sz != 0);75		return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());76	}77	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D78	// OR (Alternative way of fixing this TODO)79	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,80	// TODO: so it should work in normal cases. Maybe force allocator for this library?81	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();82	if sz == 0 {83		std::alloc::dealloc(buf, old_layout);84		return std::ptr::null_mut();85	}86	std::alloc::realloc(buf, old_layout, sz)87}8889/// # Safety90#[no_mangle]91#[allow(clippy::boxed_local)]92pub unsafe extern "C" fn jsonnet_json_destroy(_vm: &EvaluationState, v: *mut Val) {93	Box::from_raw(v);94}9596#[no_mangle]97pub extern "C" fn jsonnet_native_callback() {98	todo!()99}100101#[no_mangle]102pub extern "C" fn jsonnet_max_trace(vm: &EvaluationState, v: c_uint) {103	vm.set_max_trace(v as usize)104}105106/// # Safety107///108/// This function is safe, if received v is a pointer to normal C string109#[no_mangle]110pub unsafe extern "C" fn jsonnet_evaluate_file(111	vm: &EvaluationState,112	filename: *const c_char,113	error: &mut c_int,114) -> *const c_char {115	vm.run_in_state(|| {116		let filename = CStr::from_ptr(filename);117		match vm118			.evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))119			.and_then(|v| vm.with_tla(v))120			.and_then(|v| vm.manifest(v))121		{122			Ok(v) => {123				*error = 0;124				CString::new(&*v as &str).unwrap().into_raw()125			}126			Err(e) => {127				*error = 1;128				let out = vm.stringify_err(&e);129				CString::new(&out as &str).unwrap().into_raw()130			}131		}132	})133}134135/// # Safety136///137/// This function is safe, if received v is a pointer to normal C string138#[no_mangle]139pub unsafe extern "C" fn jsonnet_evaluate_snippet(140	vm: &EvaluationState,141	filename: *const c_char,142	snippet: *const c_char,143	error: &mut c_int,144) -> *const c_char {145	vm.run_in_state(|| {146		let filename = CStr::from_ptr(filename);147		let snippet = CStr::from_ptr(snippet);148		match vm149			.evaluate_snippet_raw(150				Rc::new(PathBuf::from(filename.to_str().unwrap())),151				snippet.to_str().unwrap().into(),152			)153			.and_then(|v| vm.with_tla(v))154			.and_then(|v| vm.manifest(v))155		{156			Ok(v) => {157				*error = 0;158				CString::new(&*v as &str).unwrap().into_raw()159			}160			Err(e) => {161				*error = 1;162				let out = vm.stringify_err(&e);163				CString::new(&out as &str).unwrap().into_raw()164			}165		}166	})167}168169#[no_mangle]170pub extern "C" fn jsonnet_evaluate_file_multi() {171	todo!()172}173#[no_mangle]174pub extern "C" fn jsonnet_evaluate_snippet_multi() {175	todo!()176}177#[no_mangle]178pub extern "C" fn jsonnet_evaluate_file_stream() {179	todo!()180}181#[no_mangle]182pub extern "C" fn jsonnet_evaluate_snippet_stream() {183	todo!()184}