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
40/// then there is a mismatch between header and compiled library.40/// then there is a mismatch between header and compiled library.
41#[no_mangle]41#[no_mangle]
42pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {42pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {
43 b"v0.19.1\0"43 b"v0.20.0\0"
44}44}
4545
46unsafe fn parse_path(input: &CStr) -> Cow<Path> {46unsafe fn parse_path(input: &CStr) -> Cow<Path> {
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]