difftreelog
refactor drop wasm interop from bindings
in: master
Not needed with the introducion of jrsonnet-web
2 files changed
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -30,11 +30,9 @@
crate-type = ["cdylib", "staticlib"]
[features]
-default = ["interop-common", "interop-wasm", "interop-threading"]
+default = ["interop-common", "interop-threading"]
# Export additional functions for native integration, i.e ability to set custom trace format
interop-common = []
-# Provide ability to statically override callbacks from WASM (by using imports)
-interop-wasm = []
# Provide ability to move jsonnet vm state between threads
interop-threading = []
bindings/jsonnet/src/interop.rsdiffbeforeafterboth1//! Jrsonnet specific additional binding helpers23#[cfg(feature = "interop-wasm")]4pub mod wasm {5 use std::ffi::{c_char, c_int, c_void};67 use jrsonnet_evaluator::Val;89 use crate::VM;1011 unsafe extern "C" {12 pub fn _jrsonnet_static_import_callback(13 ctx: *mut c_void,14 base: *const c_char,15 rel: *const c_char,16 found_here: *mut *const c_char,17 buf: *mut *mut c_char,18 buflen: *mut usize,19 ) -> c_int;2021 #[allow(improper_ctypes)]22 pub fn _jrsonnet_static_native_callback(23 ctx: *const c_void,24 argv: *const *const Val,25 success: *mut c_int,26 ) -> *mut Val;27 }2829 #[unsafe(no_mangle)]30 #[cfg(feature = "interop-wasm")]31 // ctx arg is passed as-is to callback32 #[allow(clippy::not_unsafe_ptr_arg_deref)]33 pub extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {34 unsafe { crate::import::jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx) }35 }3637 /// # Safety38 ///39 /// `name` and `raw_params` should be correctly initialized40 #[unsafe(no_mangle)]41 #[cfg(feature = "interop-wasm")]42 pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(43 vm: &VM,44 name: *const c_char,45 ctx: *mut c_void,46 raw_params: *const *const c_char,47 ) {48 unsafe {49 crate::native::jsonnet_native_callback(50 vm,51 name,52 _jrsonnet_static_native_callback,53 ctx,54 raw_params,55 );56 }57 }58}5960#[cfg(feature = "interop-common")]61mod common {62 use jrsonnet_evaluator::trace::{CompactFormat, HiDocFormat, JsFormat, PathResolver};6364 use crate::VM;6566 #[unsafe(no_mangle)]67 pub extern "C" fn jrsonnet_set_trace_format(vm: &mut VM, format: u8) {68 match format {69 0 => {70 vm.trace_format = Box::new(CompactFormat {71 max_trace: 20,72 resolver: PathResolver::new_cwd_fallback(),73 padding: 4,74 });75 }76 1 => vm.trace_format = Box::new(JsFormat { max_trace: 20 }),77 2 => {78 vm.trace_format = Box::new(HiDocFormat {79 resolver: PathResolver::new_cwd_fallback(),80 max_trace: 20,81 });82 }83 _ => panic!("unknown trace format"),84 }85 }86}8788#[cfg(feature = "interop-threading")]89mod threading {90 use std::{ffi::c_int, thread::ThreadId};9192 pub struct ThreadCTX {93 interner: *mut jrsonnet_interner::interop::PoolState,94 gc: *mut jrsonnet_gcmodule::interop::GcState,95 }9697 /// Golang jrsonnet bindings require Jsonnet VM to be movable.98 /// Jrsonnet uses `thread_local` in some places, thus making VM99 /// immovable by default. By using `jrsonnet_exit_thread` and100 /// `jrsonnet_reenter_thread`, you can move `thread_local` state to101 /// where it is more convinient to use it.102 ///103 /// # Safety104 ///105 /// Current thread GC will be broken after this call, need to call106 /// `jrsonet_enter_thread` before doing anything.107 #[unsafe(no_mangle)]108 pub unsafe extern "C" fn jrsonnet_exit_thread() -> *mut ThreadCTX {109 Box::into_raw(Box::new(ThreadCTX {110 interner: jrsonnet_interner::interop::exit_thread(),111 gc: unsafe { jrsonnet_gcmodule::interop::exit_thread() },112 }))113 }114115 #[unsafe(no_mangle)]116 pub extern "C" fn jrsonnet_reenter_thread(mut ctx: Box<ThreadCTX>) {117 use std::ptr::null_mut;118 assert!(119 !ctx.interner.is_null() && !ctx.gc.is_null(),120 "reused context?"121 );122 unsafe { jrsonnet_interner::interop::reenter_thread(ctx.interner) }123 unsafe { jrsonnet_gcmodule::interop::reenter_thread(ctx.gc) }124 // Just in case125 ctx.interner = null_mut();126 ctx.gc = null_mut();127 }128129 // ThreadId is compatible with u64, and there is unstable cast130 // method... But until it is stabilized, lets erase its type by131 // boxing.132 pub enum JrThreadId {}133134 #[unsafe(no_mangle)]135 pub extern "C" fn jrsonnet_thread_id() -> *mut JrThreadId {136 Box::into_raw(Box::new(std::thread::current().id())).cast()137 }138139 #[unsafe(no_mangle)]140 pub extern "C" fn jrsonnet_thread_id_compare(141 a: *const JrThreadId,142 b: *const JrThreadId,143 ) -> c_int {144 let a: &ThreadId = unsafe { *a.cast() };145 let b: &ThreadId = unsafe { *b.cast() };146 i32::from(*a == *b)147 }148149 #[unsafe(no_mangle)]150 pub unsafe extern "C" fn jrsonnet_thread_id_free(id: *mut JrThreadId) {151 let _id: Box<ThreadId> = unsafe { Box::from_raw(id.cast()) };152 }153}