difftreelog
feat thread_enter/thread_exit bindins
in: master
5 files changed
Cargo.tomldiffbeforeafterboth1[workspace]2members = ["crates/*", "bindings/jsonnet", "cmds/*", "tests", "xtask"]3default-members = ["cmds/jrsonnet"]4resolver = "2"56[workspace.package]7authors = ["Yaroslav Bolyukin <iam@lach.pw>"]8edition = "2021"9license = "MIT"10repository = "https://github.com/CertainLach/jrsonnet"11version = "0.5.0-pre96"1213[workspace.dependencies]14jrsonnet-evaluator = { path = "./crates/jrsonnet-evaluator", version = "0.5.0-pre96" }15jrsonnet-macros = { path = "./crates/jrsonnet-macros", version = "0.5.0-pre96" }16jrsonnet-parser = { path = "./crates/jrsonnet-parser", version = "0.5.0-pre96" }17jrsonnet-rowan-parser = { path = "./crates/jrsonnet-rowan-parser", version = "0.5.0-pre96" }18jrsonnet-interner = { path = "./crates/jrsonnet-interner", version = "0.5.0-pre96" }19jrsonnet-stdlib = { path = "./crates/jrsonnet-stdlib", version = "0.5.0-pre96" }20jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre96" }21jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre96" }2223jrsonnet-gcmodule = "0.3.6"2425# Diagnostics.26# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet27# Previous implementation was based on annotate-snippets, which I don't like for many reasons.28#29# I'm against using miette, because I want to reuse data between interpreter and annotations, yet miette30# and other libraries want to handle spans etc by itself, which is okay for compiler diagnostics, but is31# bad for interpreter, where interpreter and parser are paired much closer.32hi-doc = "0.1.1"33annotate-snippets = "0.10.1"3435# CLI36clap = "4.5"37clap_complete = "4.5"3839# Parsing, manifestification is implemented manually everywhere40# Note on serde_yaml_with_quirks: This is a fork of serde-yaml with legacy yaml 1.1 support:41# https://github.com/dtolnay/serde-yaml/pull/22542serde = "1.0.197"43serde_json = "1.0.114"44serde_yaml_with_quirks = "0.8.24"4546# Error handling47anyhow = "1.0.83"48thiserror = "1.0.60"4950# Code formatting51dprint-core = "0.65.0"5253# Stdlib hashing functions54md5 = "0.7.0"55sha1 = "0.10.6"56sha2 = "0.10.8"57sha3 = "0.10.8"5859# Source code parsing.60# Jrsonnet has two parsers for jsonnet - one is for execution, and another is for better parsing diagnostics/lints/LSP.61# First (and fast one) is based on peg, second is based on rowan.62peg = "0.8.3"63logos = "0.14.0"64ungrammar = "1.16.1"65rowan = "0.15.15"6667mimallocator = "0.1.3"68indoc = "2.0"69insta = "1.39"70tempfile = "3.10"71pathdiff = "0.2.1"72hashbrown = "0.14.5"73static_assertions = "1.1"74rustc-hash = "1.1"75num-bigint = "0.4.5"76derivative = "2.2.0"77strsim = "0.11.0"78proc-macro2 = "1.0"79quote = "1.0"80syn = "2.0"81drop_bomb = "0.1.5"82base64 = "0.22.1"83indexmap = "2.2.3"84itertools = "0.13.0"85xshell = "0.2.6"8687lsp-server = "0.7.6"88lsp-types = "0.96.0"8990regex = "1.10"91lru = "0.12.3"9293json-structural-diff = "0.1.0"9495[workspace.lints.rust]96unsafe_op_in_unsafe_fn = "deny"9798# TODO: add docs everywhere99# missing_doc = "warn"100101elided_lifetimes_in_paths = "allow"102explicit_outlives_requirements = "allow"103noop_method_call = "allow"104single_use_lifetimes = "allow"105variant_size_differences = "allow"106macro_expanded_macro_exports_accessed_by_absolute_paths = "allow"107108[workspace.lints.rustdoc]109all = "warn"110111[workspace.lints.clippy]112all = "warn"113nursery = "warn"114pedantic = "warn"115116ptr_arg = "allow"117# Too verbose118must_use_candidate = "allow"119# A lot of functions pass around errors thrown by code120missing_errors_doc = "allow"121# A lot of pointers have interior Rc122needless_pass_by_value = "allow"123# Its fine124wildcard_imports = "allow"125enum_glob_use = "allow"126module_name_repetitions = "allow"127# TODO: fix individual issues, however this works as intended almost everywhere128cast_precision_loss = "allow"129cast_possible_wrap = "allow"130cast_possible_truncation = "allow"131cast_sign_loss = "allow"132# False positives133# https://github.com/rust-lang/rust-clippy/issues/6902134use_self = "allow"135# https://github.com/rust-lang/rust-clippy/issues/8539136iter_with_drain = "allow"137type_repetition_in_bounds = "allow"138# ci is being run with nightly, but library should work on stable139missing_const_for_fn = "allow"140# too many false-positives with .expect() calls141missing_panics_doc = "allow"142# false positive for IStr type. There is an configuration option for143# such cases, but it doesn't work:144# https://github.com/rust-lang/rust-clippy/issues/9801145mutable_key_type = "allow"146# false positives147redundant_pub_crate = "allow"148# Sometimes code is fancier without that149manual_let_else = "allow"150# Something is broken about that lint, can't be allowed for151# codegenerated-stdlib block152similar_names = "allow"153154#[profile.test]155#opt-level = 1156157[profile.release]158opt-level = 3159lto = "fat"160codegen-units = 1161debug = 0162panic = "abort"163strip = true164165[profile.releasedebug]166inherits = "release"167debug = 2168panic = "unwind"169strip = false1[workspace]2members = ["crates/*", "bindings/jsonnet", "cmds/*", "tests", "xtask"]3default-members = ["cmds/jrsonnet"]4resolver = "2"56[workspace.package]7authors = ["Yaroslav Bolyukin <iam@lach.pw>"]8edition = "2021"9license = "MIT"10repository = "https://github.com/CertainLach/jrsonnet"11version = "0.5.0-pre96"1213[workspace.dependencies]14jrsonnet-evaluator = { path = "./crates/jrsonnet-evaluator", version = "0.5.0-pre96" }15jrsonnet-macros = { path = "./crates/jrsonnet-macros", version = "0.5.0-pre96" }16jrsonnet-parser = { path = "./crates/jrsonnet-parser", version = "0.5.0-pre96" }17jrsonnet-rowan-parser = { path = "./crates/jrsonnet-rowan-parser", version = "0.5.0-pre96" }18jrsonnet-interner = { path = "./crates/jrsonnet-interner", version = "0.5.0-pre96" }19jrsonnet-stdlib = { path = "./crates/jrsonnet-stdlib", version = "0.5.0-pre96" }20jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre96" }21jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre96" }22jrsonnet-gcmodule = { version = "0.3.7" }23# Diagnostics.24# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet25# Previous implementation was based on annotate-snippets, which I don't like for many reasons.26#27# I'm against using miette, because I want to reuse data between interpreter and annotations, yet miette28# and other libraries want to handle spans etc by itself, which is okay for compiler diagnostics, but is29# bad for interpreter, where interpreter and parser are paired much closer.30hi-doc = "0.1.1"31annotate-snippets = "0.10.1"3233# CLI34clap = "4.5"35clap_complete = "4.5"3637# Parsing, manifestification is implemented manually everywhere38# Note on serde_yaml_with_quirks: This is a fork of serde-yaml with legacy yaml 1.1 support:39# https://github.com/dtolnay/serde-yaml/pull/22540serde = "1.0.197"41serde_json = "1.0.114"42serde_yaml_with_quirks = "0.8.24"4344# Error handling45anyhow = "1.0.83"46thiserror = "1.0.60"4748# Code formatting49dprint-core = "0.65.0"5051# Stdlib hashing functions52md5 = "0.7.0"53sha1 = "0.10.6"54sha2 = "0.10.8"55sha3 = "0.10.8"5657# Source code parsing.58# Jrsonnet has two parsers for jsonnet - one is for execution, and another is for better parsing diagnostics/lints/LSP.59# First (and fast one) is based on peg, second is based on rowan.60peg = "0.8.3"61logos = "0.14.0"62ungrammar = "1.16.1"63rowan = "0.15.15"6465mimallocator = "0.1.3"66indoc = "2.0"67insta = "1.39"68tempfile = "3.10"69pathdiff = "0.2.1"70hashbrown = "0.14.5"71static_assertions = "1.1"72rustc-hash = "1.1"73num-bigint = "0.4.5"74derivative = "2.2.0"75strsim = "0.11.0"76proc-macro2 = "1.0"77quote = "1.0"78syn = "2.0"79drop_bomb = "0.1.5"80base64 = "0.22.1"81indexmap = "2.2.3"82itertools = "0.13.0"83xshell = "0.2.6"8485lsp-server = "0.7.6"86lsp-types = "0.96.0"8788regex = "1.10"89lru = "0.12.3"9091json-structural-diff = "0.1.0"9293[workspace.lints.rust]94unsafe_op_in_unsafe_fn = "deny"9596# TODO: add docs everywhere97# missing_doc = "warn"9899elided_lifetimes_in_paths = "allow"100explicit_outlives_requirements = "allow"101noop_method_call = "allow"102single_use_lifetimes = "allow"103variant_size_differences = "allow"104macro_expanded_macro_exports_accessed_by_absolute_paths = "allow"105106[workspace.lints.rustdoc]107all = "warn"108109[workspace.lints.clippy]110all = "warn"111nursery = "warn"112pedantic = "warn"113114ptr_arg = "allow"115# Too verbose116must_use_candidate = "allow"117# A lot of functions pass around errors thrown by code118missing_errors_doc = "allow"119# A lot of pointers have interior Rc120needless_pass_by_value = "allow"121# Its fine122wildcard_imports = "allow"123enum_glob_use = "allow"124module_name_repetitions = "allow"125# TODO: fix individual issues, however this works as intended almost everywhere126cast_precision_loss = "allow"127cast_possible_wrap = "allow"128cast_possible_truncation = "allow"129cast_sign_loss = "allow"130# False positives131# https://github.com/rust-lang/rust-clippy/issues/6902132use_self = "allow"133# https://github.com/rust-lang/rust-clippy/issues/8539134iter_with_drain = "allow"135type_repetition_in_bounds = "allow"136# ci is being run with nightly, but library should work on stable137missing_const_for_fn = "allow"138# too many false-positives with .expect() calls139missing_panics_doc = "allow"140# false positive for IStr type. There is an configuration option for141# such cases, but it doesn't work:142# https://github.com/rust-lang/rust-clippy/issues/9801143mutable_key_type = "allow"144# false positives145redundant_pub_crate = "allow"146# Sometimes code is fancier without that147manual_let_else = "allow"148# Something is broken about that lint, can't be allowed for149# codegenerated-stdlib block150similar_names = "allow"151152#[profile.test]153#opt-level = 1154155[profile.release]156opt-level = 3157lto = "fat"158codegen-units = 1159debug = 0160panic = "abort"161strip = true162163[profile.releasedebug]164inherits = "release"165debug = 2166panic = "unwind"167strip = falsebindings/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
bindings/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"
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -40,7 +40,7 @@
/// then there is a mismatch between header and compiled library.
#[no_mangle]
pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {
- b"v0.19.1\0"
+ b"v0.20.0\0"
}
unsafe fn parse_path(input: &CStr) -> Cow<Path> {
crates/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]