git.delta.rocks / jrsonnet / refs/commits / 3c2d9ee98ad3

difftreelog

feat thread_enter/thread_exit bindins

Yaroslav Bolyukin2024-06-18parent: #9070b9a.patch.diff
in: master

5 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,9 +19,7 @@
 jrsonnet-stdlib = { path = "./crates/jrsonnet-stdlib", version = "0.5.0-pre96" }
 jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre96" }
 jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre96" }
-
-jrsonnet-gcmodule = "0.3.6"
-
+jrsonnet-gcmodule = { version = "0.3.7" }
 # Diagnostics.
 # hi-doc is my library, which handles text formatting very well, but isn't polished enough yet
 # Previous implementation was based on annotate-snippets, which I don't like for many reasons.
modifiedbindings/c/libjsonnet.hdiffbeforeafterboth
--- a/bindings/c/libjsonnet.h
+++ b/bindings/c/libjsonnet.h
@@ -1,22 +1,9 @@
-/*
-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.
-*/
-
 #ifndef LIB_JSONNET_H
 #define LIB_JSONNET_H
 
 #include <stddef.h>
 
-/** \file This file is a library interface for evaluating Jsonnet.  It is written in C++ but exposes
+/** \file This file is a library interface for evaluating Jsonnet. It is written in Rust but exposes
  * a C interface for easier wrapping by other languages.  See \see jsonnet_lib_test.c for an example
  * of using the library.
  */
@@ -28,10 +15,10 @@
  *
  * If this isn't the sae as jsonnet_version() then you've got a mismatched binary / header.
  */
-#define LIB_JSONNET_VERSION "v0.16.0"
+#define LIB_JSONNET_VERSION "v0.20.0"
 
 /** Return the version string of the Jsonnet interpreter.  Conforms to semantic versioning
- * http://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between
+ * https://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between
  * header and compiled library.
  */
 const char *jsonnet_version(void);
@@ -66,10 +53,12 @@
  *     process's CWD.  This is necessary so that imports from the content of the imported file can
  *     be resolved correctly.  Allocate memory with jsonnet_realloc.  Only use when *success = 1.
  * \param success Set this byref param to 1 to indicate success and 0 for failure.
- * \returns The content of the imported file, or an error message.
+ * \param buf Set this byref param to the content of the imported file, or an error message.  Allocate memory with jsonnet_realloc.  Do not include a null terminator byte.
+ * \param buflen Set this byref param to the length of the data returned in buf.
+ * \returns 0 to indicate success and 1 for failure.  On success, the content is in *buf.  On failure, an error message is in *buf.
  */
-typedef char *JsonnetImportCallback(void *ctx, const char *base, const char *rel, char **found_here,
-									int *success);
+typedef int JsonnetImportCallback(void *ctx, const char *base, const char *rel,
+                                  char **found_here, char **buf, size_t *buflen);
 
 /** An opaque type which can only be utilized via the jsonnet_json_* family of functions.
  */
@@ -82,7 +71,7 @@
 /** If the value is a number, return 1 and store the number in out, otherwise return 0.
  */
 int jsonnet_json_extract_number(struct JsonnetVm *vm, const struct JsonnetJsonValue *v,
-								double *out);
+                                double *out);
 
 /** Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.
  */
@@ -117,7 +106,7 @@
 /** Add v to the end of the array.
  */
 void jsonnet_json_array_append(struct JsonnetVm *vm, struct JsonnetJsonValue *arr,
-							   struct JsonnetJsonValue *v);
+                               struct JsonnetJsonValue *v);
 
 /** Make a JsonnetJsonValue representing an object with the given number of fields.
  *
@@ -130,7 +119,7 @@
  * This replaces any previous binding of the field.
  */
 void jsonnet_json_object_append(struct JsonnetVm *vm, struct JsonnetJsonValue *obj, const char *f,
-								struct JsonnetJsonValue *v);
+                                struct JsonnetJsonValue *v);
 
 /** Clean up a JSON subtree.
  *
@@ -151,8 +140,8 @@
  * \returns The content of the imported file, or an error message.
  */
 typedef struct JsonnetJsonValue *JsonnetNativeCallback(void *ctx,
-													   const struct JsonnetJsonValue *const *argv,
-													   int *success);
+                                                       const struct JsonnetJsonValue *const *argv,
+                                                       int *success);
 
 /** Allocate, resize, or free a buffer.  This will abort if the memory cannot be allocated.  It will
  * only return NULL if sz was zero.
@@ -181,7 +170,7 @@
  * \param params NULL-terminated array of the names of the params.  Must be valid identifiers.
  */
 void jsonnet_native_callback(struct JsonnetVm *vm, const char *name, JsonnetNativeCallback *cb,
-							 void *ctx, const char *const *params);
+                             void *ctx, const char *const *params);
 
 /** Bind a Jsonnet external var to the given string.
  *
@@ -236,7 +225,7 @@
  * \returns Either JSON or the error message.
  */
 char *jsonnet_evaluate_snippet(struct JsonnetVm *vm, const char *filename, const char *snippet,
-							   int *error);
+                               int *error);
 
 /** Evaluate a file containing Jsonnet code, return a number of named JSON files.
  *
@@ -260,7 +249,7 @@
  * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
  */
 char *jsonnet_evaluate_snippet_multi(struct JsonnetVm *vm, const char *filename,
-									 const char *snippet, int *error);
+                                     const char *snippet, int *error);
 
 /** Evaluate a file containing Jsonnet code, return a number of JSON files.
  *
@@ -284,9 +273,46 @@
  * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
  */
 char *jsonnet_evaluate_snippet_stream(struct JsonnetVm *vm, const char *filename,
-									  const char *snippet, int *error);
+                                      const char *snippet, int *error);
 
 /** Complement of \see jsonnet_vm_make. */
 void jsonnet_destroy(struct JsonnetVm *vm);
 
-#endif // LIB_JSONNET_H
+/** Jrsonnet addition.
+ *
+ * In jrsonnet, vm state is bound to the thread, because interpreter
+ * also uses thread_local storage for some things (I.e GC).
+ *
+ * It makes it impossible to correctly use those bindings in golang,
+ * where developer has little control over goroutine scheduler.
+ *
+ * To make it work, jrsonnet provides methods to dump and restore thread
+ * state manually, making it possible to wire it with golang.
+ */
+struct JrThreadCTX;
+
+/** Dump current thread state, to be restored with
+ * jrsonnet_reenter_thread.
+ */
+struct JrThreadCTX *jrsonnet_exit_thread();
+/** Restore thread state, freeing JrThreadCTX.
+ */
+void jrsonnet_reenter_thread(struct JrThreadCTX *ctx);
+
+struct JrThreadId;
+
+/** Get current thread id (opaque pointer).
+ */
+struct JrThreadId* jrsonnet_thread_id();
+
+/** Compare two thread ids, it is not the same as a == b.
+ *
+ * \returns 1 if the same thread, 0 otherwise
+ */
+int jrsonnet_thread_id_compare(struct JrThreadId *a, struct JrThreadId *b);
+
+/** Free thread id value.
+ */
+void jrsonnet_thread_id_free(struct JrThreadId *id);
+
+#endif  // LIB_JSONNET_H
modifiedbindings/jsonnet/Cargo.tomldiffbeforeafterboth
--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -23,6 +23,7 @@
 jrsonnet-parser.workspace = true
 jrsonnet-stdlib.workspace = true
 jrsonnet-gcmodule.workspace = true
+jrsonnet-interner.workspace = true
 
 [lib]
 name = "jsonnet"
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
before · bindings/jsonnet/src/lib.rs
1#![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.19.1\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}
after · bindings/jsonnet/src/lib.rs
1#![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}
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -219,8 +219,45 @@
 	}
 }
 
+type PoolMap = HashMap<Inner, (), BuildHasherDefault<FxHasher>>;
+
 thread_local! {
-	static POOL: RefCell<HashMap<Inner, (), BuildHasherDefault<FxHasher>>> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));
+	static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));
+}
+
+/// Jrsonnet golang bindings require that it is possible to move jsonnet
+/// VM between OS threads, and this is not possible due to usage of
+/// `thread_local`. Instead, there is two methods added, one should be
+/// called at the end of current thread work, and one that should be
+/// used when using other thread.
+pub mod interop {
+	use std::mem;
+
+	use crate::{PoolMap, POOL};
+
+	pub enum PoolState {}
+
+	/// Dump current interned string pool, to be restored by
+	/// `reenter_thread`
+	pub fn exit_thread() -> *mut PoolState {
+		Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()
+	}
+
+	/// Reenter thread, using state dumped by `exit_thread`.
+	///
+	/// # Safety
+	///
+	/// `state` should be acquired from `exit_thread`, it is not allowed
+	/// to reuse state to reenter multiple threads.
+	pub unsafe fn reenter_thread(state: *mut PoolState) {
+		let ptr: *mut PoolMap = state.cast();
+		// SAFETY: ptr is an unique state per method safety requirements.
+		let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };
+		let ptr: PoolMap = *ptr;
+		POOL.with_borrow_mut(|pool| {
+			let _ = mem::replace(pool, ptr);
+		});
+	}
 }
 
 #[must_use]