difftreelog
feat thread_enter/thread_exit bindins
in: master
5 files changed
Cargo.tomldiffbeforeafterboth20jrsonnet-cli = { path = "./crates/jrsonnet-cli", 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" }21jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre96" }2223jrsonnet-gcmodule = "0.3.6"22jrsonnet-gcmodule = { version = "0.3.7" }2425# Diagnostics.23# Diagnostics.26# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet24# hi-doc is my library, which handles text formatting very well, but isn't polished enough yetbindings/c/libjsonnet.hdiffbeforeafterboth1/*2Copyright 2015 Google Inc. All rights reserved.3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/1314#ifndef LIB_JSONNET_H1#ifndef LIB_JSONNET_H15#define LIB_JSONNET_H2#define LIB_JSONNET_H16317#include <stddef.h>4#include <stddef.h>18519/** \file This file is a library interface for evaluating Jsonnet. It is written in C++ but exposes6/** \file This file is a library interface for evaluating Jsonnet. It is written in Rust but exposes20 * a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example7 * a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example21 * of using the library.8 * of using the library.22 */9 */231024/** The version string of th Jsonnet interpreter.11/** The version string of th Jsonnet interpreter.25 *12 *28 *15 *29 * If this isn't the sae as jsonnet_version() then you've got a mismatched binary / header.16 * If this isn't the sae as jsonnet_version() then you've got a mismatched binary / header.30 */17 */31#define LIB_JSONNET_VERSION "v0.16.0"18#define LIB_JSONNET_VERSION "v0.20.0"321933/** Return the version string of the Jsonnet interpreter. Conforms to semantic versioning20/** Return the version string of the Jsonnet interpreter. Conforms to semantic versioning34 * http://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between21 * https://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between35 * header and compiled library.22 * header and compiled library.36 */23 */37const char *jsonnet_version(void);24const char *jsonnet_version(void);382539/** Jsonnet virtual machine context. */26/** Jsonnet virtual machine context. */54/** Expect a string as output and don't JSON encode it. */41/** Expect a string as output and don't JSON encode it. */55void jsonnet_string_output(struct JsonnetVm *vm, int v);42void jsonnet_string_output(struct JsonnetVm *vm, int v);564357/** Callback used to load imports.44/** Callback used to load imports.58 *45 *59 * The returned char* should be allocated with jsonnet_realloc. It will be cleaned up by46 * The returned char* should be allocated with jsonnet_realloc. It will be cleaned up by60 * libjsonnet when no-longer needed.47 * libjsonnet when no-longer needed.61 *48 *62 * \param ctx User pointer, given in jsonnet_import_callback.49 * \param ctx User pointer, given in jsonnet_import_callback.63 * \param base The directory containing the code that did the import.50 * \param base The directory containing the code that did the import.64 * \param rel The path imported by the code.51 * \param rel The path imported by the code.65 * \param found_here Set this byref param to path to the file, absolute or relative to the52 * \param found_here Set this byref param to path to the file, absolute or relative to the66 * process's CWD. This is necessary so that imports from the content of the imported file can53 * process's CWD. This is necessary so that imports from the content of the imported file can67 * be resolved correctly. Allocate memory with jsonnet_realloc. Only use when *success = 1.54 * be resolved correctly. Allocate memory with jsonnet_realloc. Only use when *success = 1.68 * \param success Set this byref param to 1 to indicate success and 0 for failure.55 * \param success Set this byref param to 1 to indicate success and 0 for failure.69 * \returns The content of the imported file, or an error message.56 * \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.57 * \param buflen Set this byref param to the length of the data returned in buf.58 * \returns 0 to indicate success and 1 for failure. On success, the content is in *buf. On failure, an error message is in *buf.70 */59 */71typedef char *JsonnetImportCallback(void *ctx, const char *base, const char *rel, char **found_here,60typedef int JsonnetImportCallback(void *ctx, const char *base, const char *rel,72 int *success);61 char **found_here, char **buf, size_t *buflen);736274/** An opaque type which can only be utilized via the jsonnet_json_* family of functions.63/** An opaque type which can only be utilized via the jsonnet_json_* family of functions.75 */64 */289/** Complement of \see jsonnet_vm_make. */278/** Complement of \see jsonnet_vm_make. */290void jsonnet_destroy(struct JsonnetVm *vm);279void jsonnet_destroy(struct JsonnetVm *vm);280281/** Jrsonnet addition.282 *283 * In jrsonnet, vm state is bound to the thread, because interpreter284 * also uses thread_local storage for some things (I.e GC).285 *286 * It makes it impossible to correctly use those bindings in golang,287 * where developer has little control over goroutine scheduler.288 *289 * To make it work, jrsonnet provides methods to dump and restore thread290 * state manually, making it possible to wire it with golang.291 */292struct JrThreadCTX;293294/** Dump current thread state, to be restored with295 * jrsonnet_reenter_thread.296 */297struct JrThreadCTX *jrsonnet_exit_thread();298/** Restore thread state, freeing JrThreadCTX.299 */300void jrsonnet_reenter_thread(struct JrThreadCTX *ctx);301302struct JrThreadId;303304/** Get current thread id (opaque pointer).305 */306struct JrThreadId* jrsonnet_thread_id();307308/** Compare two thread ids, it is not the same as a == b.309 *310 * \returns 1 if the same thread, 0 otherwise311 */312int jrsonnet_thread_id_compare(struct JrThreadId *a, struct JrThreadId *b);313314/** Free thread id value.315 */316void jrsonnet_thread_id_free(struct JrThreadId *id);291317292#endif // LIB_JSONNET_H318#endif // LIB_JSONNET_H293319bindings/jsonnet/Cargo.tomldiffbeforeafterboth23jrsonnet-parser.workspace = true23jrsonnet-parser.workspace = true24jrsonnet-stdlib.workspace = true24jrsonnet-stdlib.workspace = true25jrsonnet-gcmodule.workspace = true25jrsonnet-gcmodule.workspace = true26jrsonnet-interner.workspace = true262727[lib]28[lib]28name = "jsonnet"29name = "jsonnet"bindings/jsonnet/src/lib.rsdiffbeforeafterboth40/// 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}454546unsafe fn parse_path(input: &CStr) -> Cow<Path> {46unsafe fn parse_path(input: &CStr) -> Cow<Path> {crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth219 }219 }220}220}221222type PoolMap = HashMap<Inner, (), BuildHasherDefault<FxHasher>>;221223222thread_local! {224thread_local! {223 static POOL: RefCell<HashMap<Inner, (), BuildHasherDefault<FxHasher>>> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));225 static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));224}226}227228/// Jrsonnet golang bindings require that it is possible to move jsonnet229/// VM between OS threads, and this is not possible due to usage of230/// `thread_local`. Instead, there is two methods added, one should be231/// called at the end of current thread work, and one that should be232/// used when using other thread.233pub mod interop {234 use std::mem;235236 use crate::{PoolMap, POOL};237238 pub enum PoolState {}239240 /// Dump current interned string pool, to be restored by241 /// `reenter_thread`242 pub fn exit_thread() -> *mut PoolState {243 Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()244 }245246 /// Reenter thread, using state dumped by `exit_thread`.247 ///248 /// # Safety249 ///250 /// `state` should be acquired from `exit_thread`, it is not allowed251 /// to reuse state to reenter multiple threads.252 pub unsafe fn reenter_thread(state: *mut PoolState) {253 let ptr: *mut PoolMap = state.cast();254 // SAFETY: ptr is an unique state per method safety requirements.255 let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };256 let ptr: PoolMap = *ptr;257 POOL.with_borrow_mut(|pool| {258 let _ = mem::replace(pool, ptr);259 });260 }261}225262226#[must_use]263#[must_use]227pub fn intern_bytes(bytes: &[u8]) -> IBytes {264pub fn intern_bytes(bytes: &[u8]) -> IBytes {