difftreelog
feat(bindings) c bindings
in: master
10 files changed
Cargo.lockdiffbeforeafterboth42 packageslockfile v1
Might be heavy and slow!
annotate-snippets
0.8.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumd78ea013094e5ea606b1c05fe35f1dd7ea1eb1ea259908d040b25bd5ec677ee5used byautocfg
1.0.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumf8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9dused bybincode
1.3.1crates.io↘ 2↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumf30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896ddepends onused bybitflags
1.2.1crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumcf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693used bybyteorder
1.3.4crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472deused bycc
1.0.54crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311used byclap
3.0.0-beta.1crates.io↘ 8↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum860643c53f980f0d38a5e25dfab6c3c93b2cb3aa1fe192643d17a293c6c41936depends onused byclap_derive
3.0.0-beta.1crates.io↘ 5↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumfb51c9e75b94452505acd21d929323f5a5c6c4735a852adbd39ef5fb1b014f30used byclosure
0.3.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumd6173fd61b610d15a7566dd7b7620775627441c4ab9dac8906e17cb93a24b782used byfs_extra
1.1.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674used byheck
0.3.1crates.io↘ 1↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205depends onused byindexmap
1.4.0crates.io↘ 1↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumc398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afedepends onjemalloc-sys
0.3.2crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45depends onused byjemallocator
0.3.2crates.io↘ 2↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69depends onused byjrsonnet
1.0.0workspace↘ 5↖ 0jrsonnet-evaluator
1.0.0workspace↘ 8↖ 1jrsonnet-parser
1.0.0workspace↘ 6↖ 2jrsonnet-stdlib
1.0.0workspace↘ 0↖ 2lazy_static
1.4.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksume2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646used bylibc
0.2.71crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49md5
0.7.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771used byos_str_bytes
2.3.1crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum06de47b848347d8c4c94219ad8ecd35eb90231704b067e67e6ae2e36ee023510used bypeg
0.6.2crates.io↘ 2↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum9075875c14bb21f25f11cad4b6ad2e4dd443b8fb83900b2fbdd6ebd744b82e97depends onused bypeg-macros
0.6.2crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumc24c165fd39e995246140cc78df55c56c6733ba87e6658cb3e197b8856c62852used bypeg-runtime
0.6.2crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum0c1a2897e69d986c7986747ebad425cf03746ec5e3e09bb3b2600f91301ba864used byproc-macro-error
0.4.12crates.io↘ 5↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7used byproc-macro-error-attr
0.4.12crates.io↘ 5↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69deused byproc-macro2
1.0.18crates.io↘ 1↖ 9sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumbeae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fadepends onquote
1.0.7crates.io↘ 1↖ 8sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumaa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37depends onserde
1.0.114crates.io↘ 1↖ 3sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3depends onserde_derive
1.0.114crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8edepends onused bystructdump
0.1.2crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum2e16ec33a0342fdb67d13913b4ffae6527ebccfa04b5d7da174bdc7a31db29b8structdump-derive
0.1.2crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum06c337fdc077e02ccbfcc62af0090564a4af342975c3b7be09705efab90c1888depends onused bysyn
1.0.33crates.io↘ 3↖ 6sourceregistry+https://github.com/rust-lang/crates.io-indexchecksume8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cdsyn-mid
0.5.0crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338adepends onused bytextwrap
0.11.0crates.io↘ 1↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumd326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060depends onused byunescape
0.1.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumccb97dac3243214f8d8507998906ca3e2e0b900bf9bf4870477f125b82e68f6eused byunicode-segmentation
1.6.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksume83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0used byunicode-width
0.1.7crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumcaaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479unicode-xid
0.2.0crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097cused byvec_map
0.8.2crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumf1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191used byversion_check
0.9.2crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumb5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,6 +3,7 @@
"crates/jrsonnet-parser",
"crates/jrsonnet-evaluator",
"crates/jrsonnet-stdlib",
+ "bindings/jsonnet",
"cmds/jrsonnet"
]
@@ -12,6 +13,3 @@
codegen-units = 1
debug = 0
panic = "abort"
-
-[profile.test]
-opt-level = 1
README.mddiffbeforeafterboth--- a/README.md
+++ b/README.md
@@ -19,7 +19,11 @@
## Bindings
-Currently no language bindings are implemented
+C bindings (libjsonnet.so) are WIP
+
+Something is implemented, but mostly unusable for now
+
+See them in `./bindings/jsonnet/`
## Benchmark
bindings/Makefilediffbeforeafterboth--- /dev/null
+++ b/bindings/Makefile
@@ -0,0 +1,11 @@
+../target/release/libjsonnet.so:
+ cargo build --release -p jsonnet
+
+libjsonnet_test_file: libjsonnet_test_file.c ../target/release/libjsonnet.so
+ gcc -L../target/release/ -ljsonnet libjsonnet_test_file.c -o libjsonnet_test_file
+
+.PHONY: test
+test: libjsonnet_test_file ../target/release/libjsonnet.so
+ export LD_LIBRARY_PATH=../target/release/
+ ldd libjsonnet_test_file
+ ./libjsonnet_test_file test.jsonnet
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/bindings/jsonnet/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "jsonnet"
+version = "0.1.0"
+authors = ["Лач <iam@lach.pw>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "1.0.0" }
+libc = "0.2.71"
+
+[lib]
+crate-type = ["cdylib"]
bindings/jsonnet/README.mddiffbeforeafterboth--- /dev/null
+++ b/bindings/jsonnet/README.md
@@ -0,0 +1 @@
+# libjsonnet.so implemented in Rust
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/bindings/jsonnet/src/lib.rs
@@ -0,0 +1,226 @@
+use jrsonnet_evaluator::{EvaluationState, ObjValue, Val};
+use libc::{c_char, c_double, c_int, c_uint};
+use std::{
+ ffi::{CStr, CString},
+ path::PathBuf,
+ rc::Rc,
+};
+
+#[no_mangle]
+pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {
+ b"v0.16.0\0"
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_make() -> Box<EvaluationState> {
+ Box::new(EvaluationState::default())
+}
+
+// TODO
+#[no_mangle]
+pub extern "C" fn jsonnet_max_stack(_vm: &EvaluationState, _v: c_uint) {}
+
+// jrsonnet currently have no GC, so these functions is no-op
+#[no_mangle]
+pub extern "C" fn jsonnet_gc_min_objects(_vm: &EvaluationState, _v: c_uint) {}
+#[no_mangle]
+pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &EvaluationState, _v: c_double) {}
+
+// TODO
+#[no_mangle]
+pub extern "C" fn jsonnet_string_output(_vm: &EvaluationState, _v: c_int) {}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_extract_string(_vm: &EvaluationState, v: &Val) -> *mut c_char {
+ match v.unwrap_if_lazy().unwrap() {
+ Val::Str(s) => CString::new(&*s as &str).unwrap().into_raw(),
+ _ => std::ptr::null_mut(),
+ }
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_json_extract_number(
+ _vm: &EvaluationState,
+ v: &Val,
+ out: &mut c_double,
+) -> c_int {
+ match v.unwrap_if_lazy().unwrap() {
+ Val::Num(n) => {
+ *out = n;
+ 1
+ }
+ _ => 0,
+ }
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_json_extract_bool(_vm: &EvaluationState, v: &Val) -> c_int {
+ match v.unwrap_if_lazy().unwrap() {
+ Val::Bool(false) => 0,
+ Val::Bool(true) => 1,
+ _ => 2,
+ }
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_json_extract_null(_vm: &EvaluationState, v: &Val) -> c_int {
+ match v.unwrap_if_lazy().unwrap() {
+ Val::Null => 1,
+ _ => 0,
+ }
+}
+
+/// # Safety
+///
+/// This function is safe, if received v is a pointer to normal C string
+#[no_mangle]
+pub unsafe extern "C" fn jsonnet_json_make_string(
+ _vm: &EvaluationState,
+ v: *const c_char,
+) -> Box<Val> {
+ let cstr = CStr::from_ptr(v);
+ let str = cstr.to_str().unwrap();
+ Box::new(Val::Str(str.into()))
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_make_number(_vm: &EvaluationState, v: c_double) -> Box<Val> {
+ Box::new(Val::Num(v))
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_make_bool(_vm: &EvaluationState, v: c_int) -> Box<Val> {
+ assert!(v == 0 || v == 1);
+ Box::new(Val::Bool(v == 1))
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_make_null(_vm: &EvaluationState) -> Box<Val> {
+ Box::new(Val::Null)
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_make_array(_vm: &EvaluationState) -> Box<Val> {
+ Box::new(Val::Arr(Rc::new(Vec::new())))
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_array_append(_vm: &EvaluationState, arr: &mut Val, val: &Val) {
+ match arr {
+ Val::Arr(old) => {
+ let mut new = Vec::new();
+ new.extend(old.iter().cloned());
+ new.push(val.clone());
+ *arr = Val::Arr(Rc::new(new));
+ }
+ _ => panic!("should receive array"),
+ }
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_make_object(_vm: &EvaluationState) -> Box<Val> {
+ Box::new(Val::Obj(ObjValue::new_empty()))
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_json_object_append(
+ _vm: &EvaluationState,
+ _obj: &mut Val,
+ _name: *const c_char,
+ _val: &Val,
+) {
+ todo!()
+}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_realloc(_vm: &EvaluationState, _buf: *const u8, _sz: usize) -> *const u8 {
+ todo!()
+}
+
+#[no_mangle]
+#[allow(clippy::boxed_local)]
+pub extern "C" fn jsonnet_json_destroy(_vm: &EvaluationState, _v: Box<Val>) {}
+
+#[no_mangle]
+pub extern "C" fn jsonnet_import_callback() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_native_callback() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_ext_var() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_ext_code() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_tla_var() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_tla_code() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_max_trace() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_jpath_add() {
+ todo!()
+}
+
+/// # Safety
+///
+/// This function is safe, if received v is a pointer to normal C string
+#[no_mangle]
+pub unsafe extern "C" fn jsonnet_evaluate_file(
+ vm: &EvaluationState,
+ filename: *const c_char,
+ error: &mut c_int,
+) -> *const c_char {
+ vm.run_in_state(|| {
+ use std::fmt::Write;
+ let filename = CStr::from_ptr(filename);
+ match vm.evaluate_file_to_json(&PathBuf::from(filename.to_str().unwrap())) {
+ Ok(v) => {
+ *error = 0;
+ CString::new(&*v as &str).unwrap().into_raw()
+ }
+ Err(e) => {
+ *error = 1;
+ let mut out = String::new();
+ writeln!(out, "{:?}", e.0).unwrap();
+ for i in (e.1).0.iter() {
+ writeln!(out, "{:?}", i).unwrap();
+ }
+ CString::new(&out as &str).unwrap().into_raw()
+ }
+ }
+ })
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_evaluate_snippet() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_evaluate_file_multi() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_evaluate_snippet_multi() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_evaluate_file_stream() {
+ todo!()
+}
+#[no_mangle]
+pub extern "C" fn jsonnet_evaluate_snippet_stream() {
+ todo!()
+}
+
+#[no_mangle]
+#[allow(clippy::boxed_local)]
+pub extern "C" fn jsonnet_destroy(_vm: Box<EvaluationState>) {}
bindings/libjsonnet.hdiffbeforeafterboth--- /dev/null
+++ b/bindings/libjsonnet.h
@@ -0,0 +1,292 @@
+/*
+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
+ * a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example
+ * of using the library.
+ */
+
+/** The version string of th Jsonnet interpreter.
+ *
+ * This is currently grepped out of this file by setup.py, Makefile, and CMakeLists.txt so be aware
+ * of that when making changes.
+ *
+ * If this isn't the sae as jsonnet_version() then you've got a mismatched binary / header.
+ */
+#define LIB_JSONNET_VERSION "v0.16.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
+ * header and compiled library.
+ */
+const char *jsonnet_version(void);
+
+/** Jsonnet virtual machine context. */
+struct JsonnetVm;
+
+/** Create a new Jsonnet virtual machine. */
+struct JsonnetVm *jsonnet_make(void);
+
+/** Set the maximum stack depth. */
+void jsonnet_max_stack(struct JsonnetVm *vm, unsigned v);
+
+/** Set the number of objects required before a garbage collection cycle is allowed. */
+void jsonnet_gc_min_objects(struct JsonnetVm *vm, unsigned v);
+
+/** Run the garbage collector after this amount of growth in the number of objects. */
+void jsonnet_gc_growth_trigger(struct JsonnetVm *vm, double v);
+
+/** Expect a string as output and don't JSON encode it. */
+void jsonnet_string_output(struct JsonnetVm *vm, int v);
+
+/** Callback used to load imports.
+ *
+ * The returned char* should be allocated with jsonnet_realloc. It will be cleaned up by
+ * libjsonnet when no-longer needed.
+ *
+ * \param ctx User pointer, given in jsonnet_import_callback.
+ * \param base The directory containing the code that did the import.
+ * \param rel The path imported by the code.
+ * \param found_here Set this byref param to path to the file, absolute or relative to the
+ * 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.
+ */
+typedef char *JsonnetImportCallback(void *ctx, const char *base, const char *rel, char **found_here,
+ int *success);
+
+/** An opaque type which can only be utilized via the jsonnet_json_* family of functions.
+ */
+struct JsonnetJsonValue;
+
+/** If the value is a string, return it as UTF8 otherwise return NULL.
+ */
+const char *jsonnet_json_extract_string(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
+
+/** 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);
+
+/** Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.
+ */
+int jsonnet_json_extract_bool(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
+
+/** Return 1 if the value is null, else 0.
+ */
+int jsonnet_json_extract_null(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
+
+/** Convert the given UTF8 string to a JsonnetJsonValue.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_string(struct JsonnetVm *vm, const char *v);
+
+/** Convert the given double to a JsonnetJsonValue.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_number(struct JsonnetVm *vm, double v);
+
+/** Convert the given bool (1 or 0) to a JsonnetJsonValue.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_bool(struct JsonnetVm *vm, int v);
+
+/** Make a JsonnetJsonValue representing null.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_null(struct JsonnetVm *vm);
+
+/** Make a JsonnetJsonValue representing an array.
+ *
+ * Assign elements with jsonnet_json_array_append.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_array(struct JsonnetVm *vm);
+
+/** Add v to the end of the array.
+ */
+void jsonnet_json_array_append(struct JsonnetVm *vm, struct JsonnetJsonValue *arr,
+ struct JsonnetJsonValue *v);
+
+/** Make a JsonnetJsonValue representing an object with the given number of fields.
+ *
+ * Every index of the array must have a unique value assigned with jsonnet_json_array_element.
+ */
+struct JsonnetJsonValue *jsonnet_json_make_object(struct JsonnetVm *vm);
+
+/** Add the field f to the object, bound to v.
+ *
+ * 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);
+
+/** Clean up a JSON subtree.
+ *
+ * This is useful if you want to abort with an error mid-way through building a complex value.
+ */
+void jsonnet_json_destroy(struct JsonnetVm *vm, struct JsonnetJsonValue *v);
+
+/** Callback to provide native extensions to Jsonnet.
+ *
+ * The returned JsonnetJsonValue* should be allocated with jsonnet_realloc. It will be cleaned up
+ * along with the objects rooted at argv by libjsonnet when no-longer needed. Return a string upon
+ * failure, which will appear in Jsonnet as an error. The argv pointer is an array whose size
+ * matches the array of parameters supplied when the native callback was originally registered.
+ *
+ * \param ctx User pointer, given in jsonnet_native_callback.
+ * \param argv Array of arguments from Jsonnet code.
+ * \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.
+ */
+typedef struct JsonnetJsonValue *JsonnetNativeCallback(void *ctx,
+ 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.
+ *
+ * \param buf If NULL, allocate a new buffer. If an previously allocated buffer, resize it.
+ * \param sz The size of the buffer to return. If zero, frees the buffer.
+ * \returns The new buffer.
+ */
+char *jsonnet_realloc(struct JsonnetVm *vm, char *buf, size_t sz);
+
+/** Override the callback used to locate imports.
+ */
+void jsonnet_import_callback(struct JsonnetVm *vm, JsonnetImportCallback *cb, void *ctx);
+
+/** Register a native extension.
+ *
+ * This will appear in Jsonnet as a function type and can be accessed from std.nativeExt("foo").
+ *
+ * DO NOT register native callbacks with side-effects! Jsonnet is a lazy functional language and
+ * will call your function when you least expect it, more times than you expect, or not at all.
+ *
+ * \param vm The vm.
+ * \param name The name of the function as visible to Jsonnet code, e.g. "foo".
+ * \param cb The PURE function that implements the behavior you want.
+ * \param ctx User pointer, stash non-global state you need here.
+ * \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);
+
+/** Bind a Jsonnet external var to the given string.
+ *
+ * Argument values are copied so memory should be managed by caller.
+ */
+void jsonnet_ext_var(struct JsonnetVm *vm, const char *key, const char *val);
+
+/** Bind a Jsonnet external var to the given code.
+ *
+ * Argument values are copied so memory should be managed by caller.
+ */
+void jsonnet_ext_code(struct JsonnetVm *vm, const char *key, const char *val);
+
+/** Bind a string top-level argument for a top-level parameter.
+ *
+ * Argument values are copied so memory should be managed by caller.
+ */
+void jsonnet_tla_var(struct JsonnetVm *vm, const char *key, const char *val);
+
+/** Bind a code top-level argument for a top-level parameter.
+ *
+ * Argument values are copied so memory should be managed by caller.
+ */
+void jsonnet_tla_code(struct JsonnetVm *vm, const char *key, const char *val);
+
+/** Set the number of lines of stack trace to display (0 for all of them). */
+void jsonnet_max_trace(struct JsonnetVm *vm, unsigned v);
+
+/** Add to the default import callback's library search path.
+ *
+ * The search order is last to first, so more recently appended paths take precedence.
+ */
+void jsonnet_jpath_add(struct JsonnetVm *vm, const char *v);
+
+/** Evaluate a file containing Jsonnet code, return a JSON string.
+ *
+ * The returned string should be cleaned up with jsonnet_realloc.
+ *
+ * \param filename Path to a file containing Jsonnet code.
+ * \param error Return by reference whether or not there was an error.
+ * \returns Either JSON or the error message.
+ */
+char *jsonnet_evaluate_file(struct JsonnetVm *vm, const char *filename, int *error);
+
+/** Evaluate a string containing Jsonnet code, return a JSON string.
+ *
+ * The returned string should be cleaned up with jsonnet_realloc.
+ *
+ * \param filename Path to a file (used in error messages).
+ * \param snippet Jsonnet code to execute.
+ * \param error Return by reference whether or not there was an error.
+ * \returns Either JSON or the error message.
+ */
+char *jsonnet_evaluate_snippet(struct JsonnetVm *vm, const char *filename, const char *snippet,
+ int *error);
+
+/** Evaluate a file containing Jsonnet code, return a number of named JSON files.
+ *
+ * The returned character buffer contains an even number of strings, the filename and JSON for each
+ * JSON file interleaved. It should be cleaned up with jsonnet_realloc.
+ *
+ * \param filename Path to a file containing Jsonnet code.
+ * \param error Return by reference whether or not there was an error.
+ * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
+ */
+char *jsonnet_evaluate_file_multi(struct JsonnetVm *vm, const char *filename, int *error);
+
+/** Evaluate a string containing Jsonnet code, return a number of named JSON files.
+ *
+ * The returned character buffer contains an even number of strings, the filename and JSON for each
+ * JSON file interleaved. It should be cleaned up with jsonnet_realloc.
+ *
+ * \param filename Path to a file containing Jsonnet code.
+ * \param snippet Jsonnet code to execute.
+ * \param error Return by reference whether or not there was an error.
+ * \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);
+
+/** Evaluate a file containing Jsonnet code, return a number of JSON files.
+ *
+ * The returned character buffer contains several strings. It should be cleaned up with
+ * jsonnet_realloc.
+ *
+ * \param filename Path to a file containing Jsonnet code.
+ * \param error Return by reference whether or not there was an error.
+ * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
+ */
+char *jsonnet_evaluate_file_stream(struct JsonnetVm *vm, const char *filename, int *error);
+
+/** Evaluate a string containing Jsonnet code, return a number of JSON files.
+ *
+ * The returned character buffer contains several strings. It should be cleaned up with
+ * jsonnet_realloc.
+ *
+ * \param filename Path to a file containing Jsonnet code.
+ * \param snippet Jsonnet code to execute.
+ * \param error Return by reference whether or not there was an error.
+ * \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);
+
+/** Complement of \see jsonnet_vm_make. */
+void jsonnet_destroy(struct JsonnetVm *vm);
+
+#endif // LIB_JSONNET_H
bindings/libjsonnet_test_file.cdiffbeforeafterboth--- /dev/null
+++ b/bindings/libjsonnet_test_file.c
@@ -0,0 +1,40 @@
+/*
+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>
+
+#include "libjsonnet.h"
+
+int main(int argc, const char **argv)
+{
+ int error;
+ char *output;
+ struct JsonnetVm *vm;
+ if (argc != 2) {
+ fprintf(stderr, "libjsonnet_test_file <file>\n");
+ return EXIT_FAILURE;
+ }
+ vm = jsonnet_make();
+ output = jsonnet_evaluate_file(vm, argv[1], &error);
+ if (error) {
+ fprintf(stderr, "%s", output);
+ jsonnet_realloc(vm, output, 0);
+ jsonnet_destroy(vm);
+ return EXIT_FAILURE;
+ }
+ printf("%s", output);
+ jsonnet_realloc(vm, output, 0);
+ jsonnet_destroy(vm);
+ return EXIT_SUCCESS;
+}
bindings/test.jsonnetdiffbeforeafterboth--- /dev/null
+++ b/bindings/test.jsonnet
@@ -0,0 +1 @@
+2 + 2