difftreelog
fix remove _start for non wasm32 targets
in: master
1 file changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth1#![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}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#[cfg(target_arch = "wasm32")]22#[no_mangle]23pub extern "C" fn _start() {}2425#[no_mangle]26pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {27 b"v0.16.0\0"28}2930#[no_mangle]31pub extern "C" fn jsonnet_make() -> *mut EvaluationState {32 let state = EvaluationState::default();33 state.with_stdlib();34 state.settings_mut().import_resolver = Box::new(NativeImportResolver::default());35 Box::into_raw(Box::new(state))36}3738/// # Safety39#[no_mangle]40#[allow(clippy::boxed_local)]41pub unsafe extern "C" fn jsonnet_destroy(vm: *mut EvaluationState) {42 Box::from_raw(vm);43}4445#[no_mangle]46pub extern "C" fn jsonnet_max_stack(vm: &EvaluationState, v: c_uint) {47 vm.settings_mut().max_stack = v as usize;48}4950// jrsonnet currently have no GC, so these functions is no-op51#[no_mangle]52pub extern "C" fn jsonnet_gc_min_objects(_vm: &EvaluationState, _v: c_uint) {}53#[no_mangle]54pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &EvaluationState, _v: c_double) {}5556#[no_mangle]57pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {58 match v {59 1 => vm.set_manifest_format(ManifestFormat::None),60 0 => vm.set_manifest_format(ManifestFormat::Json(4)),61 _ => panic!("incorrect output format"),62 }63}6465/// # Safety66///67/// This function is most definitely broken, but it works somehow, see TODO inside68#[no_mangle]69pub unsafe extern "C" fn jsonnet_realloc(70 _vm: &EvaluationState,71 buf: *mut u8,72 sz: usize,73) -> *mut u8 {74 if buf.is_null() {75 assert!(sz != 0);76 return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());77 }78 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D79 // OR (Alternative way of fixing this TODO)80 // TODO: Standard allocator uses malloc, and it doesn't uses allocation size,81 // TODO: so it should work in normal cases. Maybe force allocator for this library?82 let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();83 if sz == 0 {84 std::alloc::dealloc(buf, old_layout);85 return std::ptr::null_mut();86 }87 std::alloc::realloc(buf, old_layout, sz)88}8990/// # Safety91#[no_mangle]92#[allow(clippy::boxed_local)]93pub unsafe extern "C" fn jsonnet_json_destroy(_vm: &EvaluationState, v: *mut Val) {94 Box::from_raw(v);95}9697#[no_mangle]98pub extern "C" fn jsonnet_native_callback() {99 todo!()100}101102#[no_mangle]103pub extern "C" fn jsonnet_max_trace(vm: &EvaluationState, v: c_uint) {104 vm.set_max_trace(v as usize)105}106107/// # Safety108///109/// This function is safe, if received v is a pointer to normal C string110#[no_mangle]111pub unsafe extern "C" fn jsonnet_evaluate_file(112 vm: &EvaluationState,113 filename: *const c_char,114 error: &mut c_int,115) -> *const c_char {116 vm.run_in_state(|| {117 let filename = CStr::from_ptr(filename);118 match vm119 .evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))120 .and_then(|v| vm.with_tla(v))121 .and_then(|v| vm.manifest(v))122 {123 Ok(v) => {124 *error = 0;125 CString::new(&*v as &str).unwrap().into_raw()126 }127 Err(e) => {128 *error = 1;129 let out = vm.stringify_err(&e);130 CString::new(&out as &str).unwrap().into_raw()131 }132 }133 })134}135136/// # Safety137///138/// This function is safe, if received v is a pointer to normal C string139#[no_mangle]140pub unsafe extern "C" fn jsonnet_evaluate_snippet(141 vm: &EvaluationState,142 filename: *const c_char,143 snippet: *const c_char,144 error: &mut c_int,145) -> *const c_char {146 vm.run_in_state(|| {147 let filename = CStr::from_ptr(filename);148 let snippet = CStr::from_ptr(snippet);149 match vm150 .evaluate_snippet_raw(151 Rc::new(PathBuf::from(filename.to_str().unwrap())),152 snippet.to_str().unwrap().into(),153 )154 .and_then(|v| vm.with_tla(v))155 .and_then(|v| vm.manifest(v))156 {157 Ok(v) => {158 *error = 0;159 CString::new(&*v as &str).unwrap().into_raw()160 }161 Err(e) => {162 *error = 1;163 let out = vm.stringify_err(&e);164 CString::new(&out as &str).unwrap().into_raw()165 }166 }167 })168}169170#[no_mangle]171pub extern "C" fn jsonnet_evaluate_file_multi() {172 todo!()173}174#[no_mangle]175pub extern "C" fn jsonnet_evaluate_snippet_multi() {176 todo!()177}178#[no_mangle]179pub extern "C" fn jsonnet_evaluate_file_stream() {180 todo!()181}182#[no_mangle]183pub extern "C" fn jsonnet_evaluate_snippet_stream() {184 todo!()185}