difftreelog
refactor(bindings) extra interop methods
in: master
5 files changed
bindings/c/libjsonnet_test_file.cdiffbeforeafterboth--- a/bindings/c/libjsonnet_test_file.c
+++ b/bindings/c/libjsonnet_test_file.c
@@ -1,16 +1,3 @@
-/*
-Copyright 2015 Google Inc. All rights reserved.
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
#include <stdlib.h>
#include <stdio.h>
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -27,11 +27,17 @@
[lib]
name = "jsonnet"
-crate-type = ["cdylib"]
+crate-type = ["cdylib", "staticlib"]
[features]
+default = ["interop-common", "interop-wasm", "interop-threading"]
# Export additional functions for native integration, i.e ability to set custom trace format
-interop = []
+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 = []
+
experimental = ["exp-preserve-order", "exp-destruct"]
exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -15,7 +15,7 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- FileImportResolver, ImportResolver,
+ ImportResolver,
};
use jrsonnet_gcmodule::Trace;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
@@ -106,6 +106,10 @@
fn as_any(&self) -> &dyn Any {
self
}
+
+ fn as_any_mut(&mut self) -> &mut dyn Any {
+ self
+ }
}
/// # Safety
@@ -117,7 +121,7 @@
cb: JsonnetImportCallback,
ctx: *mut c_void,
) {
- vm.state.set_import_resolver(CallbackImportResolver {
+ vm.replace_import_resolver(CallbackImportResolver {
cb,
ctx,
out: RefCell::new(HashMap::new()),
@@ -131,10 +135,5 @@
pub unsafe extern "C" fn jsonnet_jpath_add(vm: &VM, path: *const c_char) {
let cstr = unsafe { CStr::from_ptr(path) };
let path = PathBuf::from(cstr.to_str().unwrap());
- let any_resolver = vm.state.import_resolver();
- let resolver = any_resolver
- .as_any()
- .downcast_ref::<FileImportResolver>()
- .expect("jpaths are not compatible with callback imports!");
- resolver.add_jpath(path);
+ vm.add_jpath(path);
}
bindings/jsonnet/src/interop.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -1,53 +1,156 @@
//! Jrsonnet specific additional binding helpers
-use std::{
- ffi::c_void,
- os::raw::{c_char, c_int},
-};
+use crate::VM;
+
+#[cfg(feature = "interop-wasm")]
+pub mod wasm {
+ use std::ffi::{c_char, c_int, c_void};
+
+ use jrsonnet_evaluator::Val;
+
+ use crate::VM;
+
+ extern "C" {
+
+ pub fn _jrsonnet_static_import_callback(
+ ctx: *mut c_void,
+ base: *const c_char,
+ rel: *const c_char,
+ found_here: *mut *const c_char,
+ buf: *mut *mut c_char,
+ buflen: *mut usize,
+ ) -> c_int;
-use jrsonnet_evaluator::Val;
+ #[allow(improper_ctypes)]
+ pub fn _jrsonnet_static_native_callback(
+ ctx: *const c_void,
+ argv: *const *const Val,
+ success: *mut c_int,
+ ) -> *mut Val;
+ }
-use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
+ #[no_mangle]
+ #[cfg(feature = "interop-wasm")]
+ // ctx arg is passed as-is to callback
+ #[allow(clippy::not_unsafe_ptr_arg_deref)]
+ pub extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {
+ unsafe { crate::import::jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx) }
+ }
-extern "C" {
- pub fn _jrsonnet_static_import_callback(
+ /// # Safety
+ ///
+ /// `name` and `raw_params` should be correctly initialized
+ #[no_mangle]
+ #[cfg(feature = "interop-wasm")]
+ pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
+ vm: &VM,
+ name: *const c_char,
ctx: *mut c_void,
- base: *const c_char,
- rel: *const c_char,
- found_here: *mut *const c_char,
- success: &mut c_int,
- ) -> *const c_char;
-
- #[allow(improper_ctypes)]
- pub fn _jrsonnet_static_native_callback(
- ctx: *const c_void,
- argv: *const *const Val,
- success: *mut c_int,
- ) -> *mut Val;
+ raw_params: *const *const c_char,
+ ) {
+ unsafe {
+ crate::native::jsonnet_native_callback(
+ vm,
+ name,
+ _jrsonnet_static_native_callback,
+ ctx,
+ raw_params,
+ );
+ }
+ }
}
-/// # Safety
-#[no_mangle]
-pub unsafe extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {
- jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx)
-}
+#[cfg(feature = "interop-common")]
+mod common {
+ use jrsonnet_evaluator::trace::{CompactFormat, ExplainingFormat, JsFormat, PathResolver};
-/// # Safety
-#[no_mangle]
-pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
- vm: &VM,
- name: *const c_char,
- ctx: *mut c_void,
- raw_params: *const *const c_char,
-) {
- jsonnet_native_callback(vm, name, _jrsonnet_static_native_callback, ctx, raw_params)
+ use crate::VM;
+
+ #[no_mangle]
+ pub extern "C" fn jrsonnet_set_trace_format(vm: &mut VM, format: u8) {
+ match format {
+ 0 => {
+ vm.trace_format = Box::new(CompactFormat {
+ max_trace: 20,
+ resolver: PathResolver::new_cwd_fallback(),
+ padding: 4,
+ });
+ }
+ 1 => vm.trace_format = Box::new(JsFormat { max_trace: 20 }),
+ 2 => {
+ vm.trace_format = Box::new(ExplainingFormat {
+ resolver: PathResolver::new_cwd_fallback(),
+ max_trace: 20,
+ });
+ }
+ _ => panic!("unknown trace format"),
+ }
+ }
}
-#[no_mangle]
-pub extern "C" fn jrsonnet_set_trace_format(vm: &VM, format: u8) {
- use jrsonnet_evaluator::trace::JsFormat;
- match format {
- 1 => vm.set_trace_format(Box::new(JsFormat)),
- _ => panic!("unknown trace format"),
+#[cfg(feature = "interop-threading")]
+mod threading {
+ use std::{ffi::c_int, thread::ThreadId};
+
+ pub struct ThreadCTX {
+ interner: *mut jrsonnet_interner::interop::PoolState,
+ gc: *mut jrsonnet_gcmodule::interop::GcState,
+ }
+
+ /// Golang jrsonnet bindings require Jsonnet VM to be movable.
+ /// Jrsonnet uses `thread_local` in some places, thus making VM
+ /// immovable by default. By using `jrsonnet_exit_thread` and
+ /// `jrsonnet_reenter_thread`, you can move `thread_local` state to
+ /// where it is more convinient to use it.
+ ///
+ /// # Safety
+ ///
+ /// Current thread GC will be broken after this call, need to call
+ /// `jrsonet_enter_thread` before doing anything.
+ #[no_mangle]
+ pub unsafe extern "C" fn jrsonnet_exit_thread() -> *mut ThreadCTX {
+ Box::into_raw(Box::new(ThreadCTX {
+ interner: jrsonnet_interner::interop::exit_thread(),
+ gc: unsafe { jrsonnet_gcmodule::interop::exit_thread() },
+ }))
+ }
+
+ #[no_mangle]
+ pub extern "C" fn jrsonnet_reenter_thread(mut ctx: Box<ThreadCTX>) {
+ use std::ptr::null_mut;
+ assert!(
+ !ctx.interner.is_null() && !ctx.gc.is_null(),
+ "reused context?"
+ );
+ unsafe { jrsonnet_interner::interop::reenter_thread(ctx.interner) }
+ unsafe { jrsonnet_gcmodule::interop::reenter_thread(ctx.gc) }
+ // Just in case
+ ctx.interner = null_mut();
+ ctx.gc = null_mut();
+ }
+
+ // ThreadId is compatible with u64, and there is unstable cast
+ // method... But until it is stabilized, lets erase its type by
+ // boxing.
+ pub enum JrThreadId {}
+
+ #[no_mangle]
+ pub extern "C" fn jrsonnet_thread_id() -> *mut JrThreadId {
+ Box::into_raw(Box::new(std::thread::current().id())).cast()
+ }
+
+ #[no_mangle]
+ pub extern "C" fn jrsonnet_thread_id_compare(
+ a: *const JrThreadId,
+ b: *const JrThreadId,
+ ) -> c_int {
+ let a: &ThreadId = unsafe { *a.cast() };
+ let b: &ThreadId = unsafe { *b.cast() };
+ i32::from(*a == *b)
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn jrsonnet_thread_id_free(id: *mut JrThreadId) {
+ let _id: Box<ThreadId> = unsafe { Box::from_raw(id.cast()) };
}
}
bindings/jsonnet/src/lib.rsdiffbeforeafterboth1#![allow(clippy::box_default)]23#[cfg(feature = "interop")]4pub mod interop;56pub mod import;7pub mod native;8pub mod val_extract;9pub mod val_make;10pub mod val_modify;11pub mod vars_tlas;1213use std::{14 alloc::Layout,15 borrow::Cow,16 ffi::{CStr, CString, OsStr},17 os::raw::{c_char, c_double, c_int, c_uint},18 path::Path,19};2021use jrsonnet_evaluator::{22 apply_tla, bail,23 function::TlaArg,24 gc::GcHashMap,25 manifest::{JsonFormat, ManifestFormat, ToStringFormat},26 stack::set_stack_depth_limit,27 tb,28 trace::{CompactFormat, PathResolver, TraceFormat},29 FileImportResolver, IStr, Result, State, Val,30};3132/// WASM stub33#[cfg(target_arch = "wasm32")]34#[no_mangle]35pub extern "C" fn _start() {}3637/// Return the version string of the Jsonnet interpreter.38/// Conforms to [semantic versioning](http://semver.org/).39/// If this does not match `LIB_JSONNET_VERSION`40/// then there is a mismatch between header and compiled library.41#[no_mangle]42pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {43 b"v0.20.0\0"44}4546unsafe fn parse_path(input: &CStr) -> Cow<Path> {47 #[cfg(target_family = "unix")]48 {49 use std::os::unix::ffi::OsStrExt;50 let str = OsStr::from_bytes(input.to_bytes());51 Cow::Borrowed(Path::new(str))52 }53 #[cfg(not(target_family = "unix"))]54 {55 let string = input.to_str().expect("bad utf-8");56 Cow::Borrowed(string.as_ref())57 }58}5960unsafe fn unparse_path(input: &Path) -> Cow<CStr> {61 #[cfg(target_family = "unix")]62 {63 use std::os::unix::ffi::OsStrExt;64 let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");65 Cow::Owned(str)66 }67 #[cfg(not(target_family = "unix"))]68 {69 let str = input.as_os_str().to_str().expect("bad utf-8");70 let cstr = CString::new(str).expect("input has NUL inside");71 Cow::Owned(cstr)72 }73}7475pub struct VM {76 state: State,77 manifest_format: Box<dyn ManifestFormat>,78 trace_format: Box<dyn TraceFormat>,79 tla_args: GcHashMap<IStr, TlaArg>,80}8182/// Creates a new Jsonnet virtual machine.83#[no_mangle]84#[allow(clippy::box_default)]85pub extern "C" fn jsonnet_make() -> *mut VM {86 let state = State::default();87 state.settings_mut().import_resolver = tb!(FileImportResolver::default());88 state.settings_mut().context_initializer = tb!(jrsonnet_stdlib::ContextInitializer::new(89 PathResolver::new_cwd_fallback(),90 ));91 Box::into_raw(Box::new(VM {92 state,93 manifest_format: Box::new(JsonFormat::default()),94 trace_format: Box::new(CompactFormat::default()),95 tla_args: GcHashMap::new(),96 }))97}9899/// Complement of [`jsonnet_vm_make`].100#[no_mangle]101#[allow(clippy::boxed_local)]102pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {103 drop(vm);104}105106/// Set the maximum stack depth.107#[no_mangle]108pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {109 set_stack_depth_limit(v as usize);110}111112/// Set the number of objects required before a garbage collection cycle is allowed.113///114/// No-op for now115#[no_mangle]116pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}117118/// Run the garbage collector after this amount of growth in the number of objects119///120/// No-op for now121#[no_mangle]122pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}123124/// Expect a string as output and don't JSON encode it.125#[no_mangle]126pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {127 vm.manifest_format = match v {128 0 => Box::new(JsonFormat::default()),129 1 => Box::new(ToStringFormat),130 _ => panic!("incorrect output format"),131 };132}133134/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will135/// only return NULL if sz was zero.136///137/// # Safety138///139/// `buf` should be either previosly allocated by this library, or NULL140///141/// This function is most definitely broken, but it works somehow, see TODO inside142#[no_mangle]143pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {144 if buf.is_null() {145 if sz == 0 {146 return std::ptr::null_mut();147 }148 return unsafe {149 std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())150 };151 }152 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D153 // OR (Alternative way of fixing this TODO)154 // TODO: Standard allocator uses malloc, and it doesn't uses allocation size,155 // TODO: so it should work in normal cases. Maybe force allocator for this library?156 let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();157 if sz == 0 {158 unsafe { std::alloc::dealloc(buf, old_layout) };159 return std::ptr::null_mut();160 }161 unsafe { std::alloc::realloc(buf, old_layout, sz) }162}163164/// Clean up a JSON subtree.165///166/// This is useful if you want to abort with an error mid-way through building a complex value.167#[no_mangle]168#[allow(clippy::boxed_local)]169pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {170 drop(v);171}172173/// Set the number of lines of stack trace to display (0 for all of them).174#[no_mangle]175pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {176 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {177 format.max_trace = v as usize;178 } else {179 panic!("max_trace is not supported by current tracing format")180 }181}182183/// Evaluate a file containing Jsonnet code, return a JSON string.184///185/// The returned string should be cleaned up with `jsonnet_realloc`.186///187/// # Safety188///189/// `filename` should be a NUL-terminated string190#[no_mangle]191pub unsafe extern "C" fn jsonnet_evaluate_file(192 vm: &VM,193 filename: *const c_char,194 error: &mut c_int,195) -> *const c_char {196 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };197 match vm198 .state199 .import(filename)200 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))201 .and_then(|val| val.manifest(&vm.manifest_format))202 {203 Ok(v) => {204 *error = 0;205 CString::new(&*v as &str).unwrap().into_raw()206 }207 Err(e) => {208 *error = 1;209 let mut out = String::new();210 vm.trace_format.write_trace(&mut out, &e).unwrap();211 CString::new(&out as &str).unwrap().into_raw()212 }213 }214}215216/// Evaluate a string containing Jsonnet code, return a JSON string.217///218/// The returned string should be cleaned up with `jsonnet_realloc`.219///220/// # Safety221///222/// `filename`, `snippet` should be a NUL-terminated strings223#[no_mangle]224pub unsafe extern "C" fn jsonnet_evaluate_snippet(225 vm: &VM,226 filename: *const c_char,227 snippet: *const c_char,228 error: &mut c_int,229) -> *const c_char {230 let filename = unsafe { CStr::from_ptr(filename) };231 let snippet = unsafe { CStr::from_ptr(snippet) };232 match vm233 .state234 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())235 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))236 .and_then(|val| val.manifest(&vm.manifest_format))237 {238 Ok(v) => {239 *error = 0;240 CString::new(&*v as &str).unwrap().into_raw()241 }242 Err(e) => {243 *error = 1;244 let mut out = String::new();245 vm.trace_format.write_trace(&mut out, &e).unwrap();246 CString::new(&out as &str).unwrap().into_raw()247 }248 }249}250251fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {252 let Val::Obj(val) = val else {253 bail!("expected object as multi output")254 };255 let mut out = Vec::new();256 for (k, v) in val.iter(257 #[cfg(feature = "exp-preserve-order")]258 false,259 ) {260 out.push((k, v?.manifest(format)?.into()));261 }262 Ok(out)263}264265fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {266 let mut out = Vec::new();267 for (i, (k, v)) in multi.iter().enumerate() {268 if i != 0 {269 out.push(0);270 }271 out.extend_from_slice(k.as_bytes());272 out.push(0);273 out.extend_from_slice(v.as_bytes());274 }275 out.push(0);276 out.push(0);277 let v = out.as_ptr();278 std::mem::forget(out);279 v.cast::<c_char>()280}281282/// # Safety283#[no_mangle]284pub unsafe extern "C" fn jsonnet_evaluate_file_multi(285 vm: &VM,286 filename: *const c_char,287 error: &mut c_int,288) -> *const c_char {289 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };290 match vm291 .state292 .import(filename)293 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))294 .and_then(|val| val_to_multi(val, &vm.manifest_format))295 {296 Ok(v) => {297 *error = 0;298 multi_to_raw(v)299 }300 Err(e) => {301 *error = 1;302 let mut out = String::new();303 vm.trace_format.write_trace(&mut out, &e).unwrap();304 CString::new(&out as &str).unwrap().into_raw()305 }306 }307}308309/// # Safety310#[no_mangle]311pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(312 vm: &VM,313 filename: *const c_char,314 snippet: *const c_char,315 error: &mut c_int,316) -> *const c_char {317 let filename = unsafe { CStr::from_ptr(filename) };318 let snippet = unsafe { CStr::from_ptr(snippet) };319 match vm320 .state321 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())322 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))323 .and_then(|val| val_to_multi(val, &vm.manifest_format))324 {325 Ok(v) => {326 *error = 0;327 multi_to_raw(v)328 }329 Err(e) => {330 *error = 1;331 let mut out = String::new();332 vm.trace_format.write_trace(&mut out, &e).unwrap();333 CString::new(&out as &str).unwrap().into_raw()334 }335 }336}337338fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {339 let Val::Arr(val) = val else {340 bail!("expected array as stream output")341 };342 let mut out = Vec::new();343 for item in val.iter() {344 out.push(item?.manifest(format)?.into());345 }346 Ok(out)347}348349fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {350 let mut out = Vec::new();351 for (i, v) in multi.iter().enumerate() {352 if i != 0 {353 out.push(0);354 }355 out.extend_from_slice(v.as_bytes());356 }357 out.push(0);358 out.push(0);359 let v = out.as_ptr();360 std::mem::forget(out);361 v.cast::<c_char>()362}363364/// # Safety365#[no_mangle]366pub unsafe extern "C" fn jsonnet_evaluate_file_stream(367 vm: &VM,368 filename: *const c_char,369 error: &mut c_int,370) -> *const c_char {371 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };372 match vm373 .state374 .import(filename)375 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))376 .and_then(|val| val_to_stream(val, &vm.manifest_format))377 {378 Ok(v) => {379 *error = 0;380 stream_to_raw(v)381 }382 Err(e) => {383 *error = 1;384 let mut out = String::new();385 vm.trace_format.write_trace(&mut out, &e).unwrap();386 CString::new(&out as &str).unwrap().into_raw()387 }388 }389}390391/// # Safety392#[no_mangle]393pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(394 vm: &VM,395 filename: *const c_char,396 snippet: *const c_char,397 error: &mut c_int,398) -> *const c_char {399 let filename = unsafe { CStr::from_ptr(filename) };400 let snippet = unsafe { CStr::from_ptr(snippet) };401 match vm402 .state403 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())404 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))405 .and_then(|val| val_to_stream(val, &vm.manifest_format))406 {407 Ok(v) => {408 *error = 0;409 stream_to_raw(v)410 }411 Err(e) => {412 *error = 1;413 let mut out = String::new();414 vm.trace_format.write_trace(&mut out, &e).unwrap();415 CString::new(&out as &str).unwrap().into_raw()416 }417 }418}