diff --git a/.gitignore b/.gitignore index d256c94..a1a2051 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ coverage.out .*.swp /tests_path.source /jsonnet/jsonnet +*.so diff --git a/compat/c_layer.go b/compat/c_layer.go new file mode 100644 index 0000000..3fdda63 --- /dev/null +++ b/compat/c_layer.go @@ -0,0 +1,48 @@ +package main + +import "fmt" +import "github.com/google/go-jsonnet" + +// #cgo CXXFLAGS: -std=c++11 -Wall +import "C" + +//export test +func test() { + fmt.Printf("hello") +} + +//export run_jsonnet +func run_jsonnet(code *C.char) { + s := C.GoString(code) + vm := jsonnet.MakeVM() + out, err := vm.EvaluateSnippet("c", s) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(out) + } +} + +//export jsonnet_evaluate_snippet2 +func jsonnet_evaluate_snippet2(filename *C.char, code *C.char, e *C.int) *C.char { + f := C.GoString(filename) + fmt.Println(code) + s := C.GoString(code) + vm := jsonnet.MakeVM() + fmt.Println(s) + out, err := vm.EvaluateSnippet(f, s) + var result *C.char + if err != nil { + *e = 1 + result = C.CString(err.Error()) + } else { + *e = 0 + fmt.Println(out) + result = C.CString(out) + } + return result +} + +func main() { + +} diff --git a/compat/compat.h b/compat/compat.h new file mode 100644 index 0000000..0a2869b --- /dev/null +++ b/compat/compat.h @@ -0,0 +1,71 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/google/go-jsonnet/compat */ + +/* Start of preamble from import "C" comments. */ + + +#line 6 "/usr/local/google/home/sbarzowski/go/src/github.com/google/go-jsonnet/compat/c_layer.go" +#include "libjsonnet.h" +#include "blah.h" + +#line 1 "cgo-generated-wrapper" + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +typedef signed char GoInt8; +typedef unsigned char GoUint8; +typedef short GoInt16; +typedef unsigned short GoUint16; +typedef int GoInt32; +typedef unsigned int GoUint32; +typedef long long GoInt64; +typedef unsigned long long GoUint64; +typedef GoInt64 GoInt; +typedef GoUint64 GoUint; +typedef __SIZE_TYPE__ GoUintptr; +typedef float GoFloat32; +typedef double GoFloat64; +typedef float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const char *p; GoInt n; } GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +extern void test(); + +extern void run_jsonnet(char* p0); + +extern char* jsonnet_evaluate_snippet2(char* p0, char* p1, int* p2); + +extern char* jsonnet_evaluate_snippet3(struct JsonnetVm* p0, char* p1, char* p2, int* p3); + +#ifdef __cplusplus +} +#endif diff --git a/compat/compat.so b/compat/compat.so new file mode 100644 index 0000000..6276607 Binary files /dev/null and b/compat/compat.so differ diff --git a/compat/json.cpp b/compat/json.cpp new file mode 100644 index 0000000..6079c4b --- /dev/null +++ b/compat/json.cpp @@ -0,0 +1,110 @@ +extern "C" { + #include "libjsonnet.h" +} + +#include "json.h" + +#include + +const char *jsonnet_json_extract_string(JsonnetVm *vm, const struct JsonnetJsonValue *v) +{ + (void)vm; + if (v->kind != JsonnetJsonValue::STRING) + return nullptr; + return v->string.c_str(); +} + +int jsonnet_json_extract_number(struct JsonnetVm *vm, const struct JsonnetJsonValue *v, double *out) +{ + (void)vm; + if (v->kind != JsonnetJsonValue::NUMBER) + return 0; + *out = v->number; + return 1; +} + +int jsonnet_json_extract_bool(struct JsonnetVm *vm, const struct JsonnetJsonValue *v) +{ + (void)vm; + if (v->kind != JsonnetJsonValue::BOOL) + return 2; + return v->number != 0; +} + +int jsonnet_json_extract_null(struct JsonnetVm *vm, const struct JsonnetJsonValue *v) +{ + (void)vm; + return v->kind == JsonnetJsonValue::NULL_KIND; +} + +JsonnetJsonValue *jsonnet_json_make_string(JsonnetVm *vm, const char *v) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::STRING; + r->string = v; + return r; +} + +JsonnetJsonValue *jsonnet_json_make_number(struct JsonnetVm *vm, double v) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::NUMBER; + r->number = v; + return r; +} + +JsonnetJsonValue *jsonnet_json_make_bool(struct JsonnetVm *vm, int v) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::BOOL; + r->number = v != 0; + return r; +} + +JsonnetJsonValue *jsonnet_json_make_null(struct JsonnetVm *vm) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::NULL_KIND; + return r; +} + +JsonnetJsonValue *jsonnet_json_make_array(JsonnetVm *vm) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::ARRAY; + return r; +} + +void jsonnet_json_array_append(JsonnetVm *vm, JsonnetJsonValue *arr, JsonnetJsonValue *v) +{ + (void)vm; + assert(arr->kind == JsonnetJsonValue::ARRAY); + arr->elements.emplace_back(v); +} + +JsonnetJsonValue *jsonnet_json_make_object(JsonnetVm *vm) +{ + (void)vm; + JsonnetJsonValue *r = new JsonnetJsonValue(); + r->kind = JsonnetJsonValue::OBJECT; + return r; +} + +void jsonnet_json_object_append(JsonnetVm *vm, JsonnetJsonValue *obj, const char *f, + JsonnetJsonValue *v) +{ + (void)vm; + assert(obj->kind == JsonnetJsonValue::OBJECT); + obj->fields[std::string(f)] = std::unique_ptr(v); +} + +void jsonnet_json_destroy(JsonnetVm *vm, JsonnetJsonValue *v) +{ + (void)vm; + delete v; +} diff --git a/compat/json.h b/compat/json.h new file mode 100644 index 0000000..922705b --- /dev/null +++ b/compat/json.h @@ -0,0 +1,51 @@ +/* +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 JSONNET_JSON_H +#define JSONNET_JSON_H + +#include +#include +#include +#include + +#include + +struct JsonnetJsonValue { + enum Kind { + ARRAY, + BOOL, + NULL_KIND, + NUMBER, + OBJECT, + STRING, + }; + + JsonnetJsonValue() = default; + JsonnetJsonValue(JsonnetJsonValue&) = delete; + JsonnetJsonValue(JsonnetJsonValue&&) = default; + + JsonnetJsonValue(Kind kind, std::string string, double number) + : kind(kind), string(string), number(number) {} + + Kind kind; + std::string string; + double number; // Also used for bool (0.0 and 1.0) + std::vector> elements; + std::map> fields; +}; + +#endif diff --git a/compat/libgojsonnet.h b/compat/libgojsonnet.h new file mode 100644 index 0000000..b67474a --- /dev/null +++ b/compat/libgojsonnet.h @@ -0,0 +1,71 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/google/go-jsonnet/compat */ + +/* Start of preamble from import "C" comments. */ + + +#line 6 "/usr/local/google/home/sbarzowski/go/src/github.com/google/go-jsonnet/compat/c_layer.go" + +#include "libjsonnet.h" + +#line 1 "cgo-generated-wrapper" + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +typedef signed char GoInt8; +typedef unsigned char GoUint8; +typedef short GoInt16; +typedef unsigned short GoUint16; +typedef int GoInt32; +typedef unsigned int GoUint32; +typedef long long GoInt64; +typedef unsigned long long GoUint64; +typedef GoInt64 GoInt; +typedef GoUint64 GoUint; +typedef __SIZE_TYPE__ GoUintptr; +typedef float GoFloat32; +typedef double GoFloat64; +typedef float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const char *p; GoInt n; } GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +extern void test(); + +extern void run_jsonnet(char* p0); + +extern char* jsonnet_evaluate_snippet2(char* p0, char* p1, int* p2); + +extern char* jsonnet_evaluate_snippet3(struct JsonnetVm* p0, char* p1, char* p2, int* p3); + +#ifdef __cplusplus +} +#endif diff --git a/compat/libjsonnet.cpp b/compat/libjsonnet.cpp new file mode 100644 index 0000000..e5c6d6b --- /dev/null +++ b/compat/libjsonnet.cpp @@ -0,0 +1,209 @@ +#include +#include + +extern "C" { + #include "libjsonnet.h" +} + +#include "json.h" + +struct JsonnetVm { + int id; +}; + +JsonnetVm *jsonnet_make(void) { + JsonnetVm *vm = new JsonnetVm(); + vm->id = 42; // TODO(sbarzowski) real ids + return vm; +} + +inline static void not_supported() { + fputs("FATAL ERROR: Not supported by Go implementation.\n", stderr); + abort(); +} + +inline static void todo() { + fputs("TODO, NOT IMPLEMENTED YET\n", stderr); + abort(); +} + +void jsonnet_max_stack(struct JsonnetVm *vm, unsigned v) { + todo(); +} + +void jsonnet_gc_min_objects(struct JsonnetVm *vm, unsigned v) { + not_supported(); +} + +void jsonnet_gc_growth_trigger(struct JsonnetVm *vm, double v) { + not_supported(); +} + +void jsonnet_string_output(struct JsonnetVm *vm, int v) { + todo(); +} + +static void memory_panic(void) +{ + fputs("FATAL ERROR: A memory allocation error occurred.\n", stderr); + abort(); +} + +char *jsonnet_realloc(JsonnetVm *vm, char *str, size_t sz) +{ + (void)vm; + if (str == nullptr) { + if (sz == 0) + return nullptr; + auto *r = static_cast(::malloc(sz)); + if (r == nullptr) + memory_panic(); + return r; + } else { + if (sz == 0) { + ::free(str); + return nullptr; + } else { + auto *r = static_cast(::realloc(str, sz)); + if (r == nullptr) + memory_panic(); + return r; + } + } +} + +const char *jsonnet_version(void) +{ + return LIB_JSONNET_VERSION; +} + +void jsonnet_native_callback(struct JsonnetVm *vm, const char *name, JsonnetNativeCallback *cb, + void *ctx, const char *const *params) +{ + todo(); +} + +void jsonnet_ext_var(JsonnetVm *vm, const char *key, const char *val) +{ + todo(); +} + +void jsonnet_ext_code(JsonnetVm *vm, const char *key, const char *val) +{ + todo(); +} + +void jsonnet_tla_var(JsonnetVm *vm, const char *key, const char *val) +{ + todo(); +} + +void jsonnet_tla_code(JsonnetVm *vm, const char *key, const char *val) +{ + todo(); +} + +void jsonnet_fmt_debug_desugaring(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_indent(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_max_blank_lines(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_string(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_comment(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_pad_arrays(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_pad_objects(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_pretty_field_names(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_fmt_sort_imports(JsonnetVm *vm, int v) +{ + not_supported(); +} + +void jsonnet_max_trace(JsonnetVm *vm, unsigned v) +{ + todo(); +} + +void jsonnet_jpath_add(JsonnetVm *vm, const char *path_) +{ + todo(); +} + +char *jsonnet_fmt_file(JsonnetVm *vm, const char *filename, int *error) +{ + not_supported(); + return nullptr; +} + +char *jsonnet_fmt_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error) +{ + not_supported(); + return nullptr; +} + +char *jsonnet_evaluate_file(JsonnetVm *vm, const char *filename, int *error) +{ + todo(); + return nullptr; +} + +char *jsonnet_evaluate_file_multi(JsonnetVm *vm, const char *filename, int *error) +{ + todo(); + return nullptr; +} + +char *jsonnet_evaluate_file_stream(JsonnetVm *vm, const char *filename, int *error) +{ + todo(); + return nullptr; +} + +char *jsonnet_evaluate_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error) +{ + todo(); + return nullptr; +} + +char *jsonnet_evaluate_snippet_multi(JsonnetVm *vm, const char *filename, const char *snippet, + int *error) +{ + todo(); + return nullptr; +} + +char *jsonnet_evaluate_snippet_stream(JsonnetVm *vm, const char *filename, const char *snippet, + int *error) +{ + todo(); + return nullptr; +} diff --git a/compat/libjsonnet.h b/compat/libjsonnet.h new file mode 100644 index 0000000..86e55a3 --- /dev/null +++ b/compat/libjsonnet.h @@ -0,0 +1,350 @@ +/* +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 + +/** \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. + */ + +#define LIB_JSONNET_VERSION "go-jsonnet" + +/** 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 = 0. + * \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. + * + * \param ctx User pointer, given in jsonnet_native_callback. + * \param argc The number of arguments from Jsonnet code. + * \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); + +/** Indentation level when reformatting (number of spaeces). + * + * \param n Number of spaces, must be > 0. + */ +void jsonnet_fmt_indent(struct JsonnetVm *vm, int n); + +/** Indentation level when reformatting (number of spaeces). + * + * \param n Number of spaces, must be > 0. + */ +void jsonnet_fmt_max_blank_lines(struct JsonnetVm *vm, int n); + +/** Preferred style for string literals ("" or ''). + * + * \param c String style as a char ('d', 's', or 'l' (leave)). + */ +void jsonnet_fmt_string(struct JsonnetVm *vm, int c); + +/** Preferred style for line comments (# or //). + * + * \param c Comment style as a char ('h', 's', or 'l' (leave)). + */ +void jsonnet_fmt_comment(struct JsonnetVm *vm, int c); + +/** Whether to add an extra space on the inside of arrays. + */ +void jsonnet_fmt_pad_arrays(struct JsonnetVm *vm, int v); + +/** Whether to add an extra space on the inside of objects. + */ +void jsonnet_fmt_pad_objects(struct JsonnetVm *vm, int v); + +/** Use syntax sugar where possible with field names. + */ +void jsonnet_fmt_pretty_field_names(struct JsonnetVm *vm, int v); + +/** Sort top-level imports in alphabetical order + */ +void jsonnet_fmt_sort_imports(struct JsonnetVm *vm, int v); + +/** If set to 1, will reformat the Jsonnet input after desugaring. */ +void jsonnet_fmt_debug_desugaring(struct JsonnetVm *vm, int v); + +/** Reformat a file containing Jsonnet code, return a Jsonnet 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 Jsonnet code or the error message. + */ +char *jsonnet_fmt_file(struct JsonnetVm *vm, const char *filename, int *error); + +/** Reformat a string containing Jsonnet code, return a Jsonnet 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 Jsonnet code or the error message. + */ +char *jsonnet_fmt_snippet(struct JsonnetVm *vm, const char *filename, const char *snippet, + int *error); + +/** 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. */ +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 diff --git a/compat_test/compat_test.py b/compat_test/compat_test.py new file mode 100644 index 0000000..1be835e --- /dev/null +++ b/compat_test/compat_test.py @@ -0,0 +1,22 @@ +import ctypes +lib = ctypes.CDLL('../compat/libgojsonnet.so') +lib.test() +err = ctypes.c_int() +lib.jsonnet_evaluate_snippet2.argtypes = [ + ctypes.c_char_p, + ctypes.c_char_p, + ctypes.POINTER(ctypes.c_int), +] +lib.jsonnet_evaluate_snippet2.restype = ctypes.c_char_p +res = lib.jsonnet_evaluate_snippet2(b"my_file", b"2 + 2", ctypes.byref(err)) +print(repr(res)) + + +vm = lib.jsonnet_make() +print(repr(vm)) +lib.jsonnet_string_output(vm, 1) + +json_string = lib.jsonnet_json_make_string(vm, "test test") +lib.jsonnet_json_extract_string.restype = ctypes.c_char_p +string = lib.jsonnet_json_extract_string(vm, json_string) +print(repr(string))