git.delta.rocks / jrsonnet / refs/commits / 1da990e65389

difftreelog

fix upgrade libjsonnet to new apis

Yaroslav Bolyukin2022-11-09parent: #3490ff6.patch.diff
in: master

8 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
1313
14use jrsonnet_evaluator::{14use jrsonnet_evaluator::{
15 error::{Error::*, Result},15 error::{Error::*, Result},
16 throw, FileImportResolver, ImportResolver, State,16 throw, FileImportResolver, ImportResolver,
17};17};
18use jrsonnet_gcmodule::Trace;18use jrsonnet_gcmodule::Trace;
19use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};19use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
20
21use crate::VM;
2022
21pub type JsonnetImportCallback = unsafe extern "C" fn(23pub type JsonnetImportCallback = unsafe extern "C" fn(
22 ctx: *mut c_void,24 ctx: *mut c_void,
100/// It should be safe to call `cb` using valid values with passed `ctx`102/// It should be safe to call `cb` using valid values with passed `ctx`
101#[no_mangle]103#[no_mangle]
102pub unsafe extern "C" fn jsonnet_import_callback(104pub unsafe extern "C" fn jsonnet_import_callback(
103 vm: &State,105 vm: &VM,
104 cb: JsonnetImportCallback,106 cb: JsonnetImportCallback,
105 ctx: *mut c_void,107 ctx: *mut c_void,
106) {108) {
107 vm.set_import_resolver(Box::new(CallbackImportResolver {109 vm.state
110 .set_import_resolver(Box::new(CallbackImportResolver {
108 cb,111 cb,
109 ctx,112 ctx,
115///118///
116/// `path` should be a NUL-terminated string119/// `path` should be a NUL-terminated string
117#[no_mangle]120#[no_mangle]
118pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, path: *const c_char) {121pub unsafe extern "C" fn jsonnet_jpath_add(vm: &VM, path: *const c_char) {
119 let cstr = CStr::from_ptr(path);122 let cstr = CStr::from_ptr(path);
120 let path = PathBuf::from(cstr.to_str().unwrap());123 let path = PathBuf::from(cstr.to_str().unwrap());
121 let any_resolver = vm.import_resolver();124 let any_resolver = vm.state.import_resolver();
122 let resolver = any_resolver125 let resolver = any_resolver
123 .as_any()126 .as_any()
124 .downcast_ref::<FileImportResolver>()127 .downcast_ref::<FileImportResolver>()
modifiedbindings/jsonnet/src/interop.rsdiffbeforeafterboth
5 os::raw::{c_char, c_int},5 os::raw::{c_char, c_int},
6};6};
77
8use jrsonnet_evaluator::{State, Val};8use jrsonnet_evaluator::Val;
99
10use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};10use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
1111
2828
29/// # Safety29/// # Safety
30#[no_mangle]30#[no_mangle]
31pub unsafe extern "C" fn jrsonnet_apply_static_import_callback(vm: &State, ctx: *mut c_void) {31pub unsafe extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {
32 jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx)32 jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx)
33}33}
3434
35/// # Safety35/// # Safety
36#[no_mangle]36#[no_mangle]
37pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(37pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
38 vm: &State,38 vm: &VM,
39 name: *const c_char,39 name: *const c_char,
40 ctx: *mut c_void,40 ctx: *mut c_void,
41 raw_params: *const *const c_char,41 raw_params: *const *const c_char,
44}44}
4545
46#[no_mangle]46#[no_mangle]
47pub extern "C" fn jrsonnet_set_trace_format(vm: &State, format: u8) {47pub extern "C" fn jrsonnet_set_trace_format(vm: &VM, format: u8) {
48 use jrsonnet_evaluator::trace::JsFormat;48 use jrsonnet_evaluator::trace::JsFormat;
49 match format {49 match format {
50 1 => vm.set_trace_format(Box::new(JsFormat)),50 1 => vm.set_trace_format(Box::new(JsFormat)),
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
17};17};
1818
19use jrsonnet_evaluator::{19use jrsonnet_evaluator::{
20 apply_tla,
21 function::TlaArg,
22 gc::GcHashMap,
23 stack::set_stack_depth_limit,
24 stdlib::manifest::{JsonFormat, ToStringFormat},
20 tb, trace::PathResolver, FileImportResolver, IStr, ManifestFormat, State, Val,25 tb, throw,
26 trace::{CompactFormat, PathResolver, TraceFormat},
27 FileImportResolver, IStr, ManifestFormat, Result, State, Val,
21};28};
2229
23/// WASM stub30/// WASM stub
63 }70 }
64}71}
72
73pub struct VM {
74 state: State,
75 manifest_format: Box<dyn ManifestFormat>,
76 trace_format: Box<dyn TraceFormat>,
77 tla_args: GcHashMap<IStr, TlaArg>,
78}
6579
66/// Creates a new Jsonnet virtual machine.80/// Creates a new Jsonnet virtual machine.
67#[no_mangle]81#[no_mangle]
68#[allow(clippy::box_default)]82#[allow(clippy::box_default)]
69pub extern "C" fn jsonnet_make() -> *mut State {83pub extern "C" fn jsonnet_make() -> *mut VM {
70 let state = State::default();84 let state = State::default();
71 state.settings_mut().import_resolver = tb!(FileImportResolver::default());85 state.settings_mut().import_resolver = tb!(FileImportResolver::default());
72 state.settings_mut().context_initializer = tb!(jrsonnet_stdlib::ContextInitializer::new(86 state.settings_mut().context_initializer = tb!(jrsonnet_stdlib::ContextInitializer::new(
73 state.clone(),87 state.clone(),
74 PathResolver::new_cwd_fallback(),88 PathResolver::new_cwd_fallback(),
75 ));89 ));
76 Box::into_raw(Box::new(state))90 Box::into_raw(Box::new(VM {
91 state,
92 manifest_format: Box::new(JsonFormat::default()),
93 trace_format: Box::new(CompactFormat::default()),
94 tla_args: GcHashMap::new(),
95 }))
77}96}
7897
79/// Complement of [`jsonnet_vm_make`].98/// Complement of [`jsonnet_vm_make`].
80#[no_mangle]99#[no_mangle]
81#[allow(clippy::boxed_local)]100#[allow(clippy::boxed_local)]
82pub extern "C" fn jsonnet_destroy(vm: Box<State>) {101pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {
83 drop(vm);102 drop(vm);
84}103}
85104
86/// Set the maximum stack depth.105/// Set the maximum stack depth.
87#[no_mangle]106#[no_mangle]
88pub extern "C" fn jsonnet_max_stack(_vm: &State, _v: c_uint) {107pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {
89 todo!()108 set_stack_depth_limit(v as usize)
90}109}
91110
92/// Set the number of objects required before a garbage collection cycle is allowed.111/// Set the number of objects required before a garbage collection cycle is allowed.
93///112///
94/// No-op for now113/// No-op for now
95#[no_mangle]114#[no_mangle]
96pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}115pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}
97116
98/// Run the garbage collector after this amount of growth in the number of objects117/// Run the garbage collector after this amount of growth in the number of objects
99///118///
100/// No-op for now119/// No-op for now
101#[no_mangle]120#[no_mangle]
102pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}121pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}
103122
104/// Expect a string as output and don't JSON encode it.123/// Expect a string as output and don't JSON encode it.
105#[no_mangle]124#[no_mangle]
106pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {125pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {
107 match v {126 vm.manifest_format = match v {
108 1 => vm.set_manifest_format(ManifestFormat::String),127 0 => Box::new(JsonFormat::default()),
109 0 => vm.set_manifest_format(ManifestFormat::Json {128 1 => Box::new(ToStringFormat),
110 padding: 4,
111 #[cfg(feature = "exp-preserve-order")]
112 preserve_order: false,
113 }),
114 _ => panic!("incorrect output format"),129 _ => panic!("incorrect output format"),
115 }130 };
116}131}
117132
118/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will133/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will
124///139///
125/// This function is most definitely broken, but it works somehow, see TODO inside140/// This function is most definitely broken, but it works somehow, see TODO inside
126#[no_mangle]141#[no_mangle]
127pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {142pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {
128 if buf.is_null() {143 if buf.is_null() {
129 if sz == 0 {144 if sz == 0 {
130 return std::ptr::null_mut();145 return std::ptr::null_mut();
148/// This is useful if you want to abort with an error mid-way through building a complex value.163/// This is useful if you want to abort with an error mid-way through building a complex value.
149#[no_mangle]164#[no_mangle]
150#[allow(clippy::boxed_local)]165#[allow(clippy::boxed_local)]
151pub extern "C" fn jsonnet_json_destroy(_vm: &State, v: Box<Val>) {166pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {
152 drop(v);167 drop(v);
153}168}
154169
155/// Set the number of lines of stack trace to display (0 for all of them).170/// Set the number of lines of stack trace to display (0 for all of them).
156#[no_mangle]171#[no_mangle]
157pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {172pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {
158 vm.set_max_trace(v as usize)173 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {
174 format.max_trace = v as usize
175 } else {
176 panic!("max_trace is not supported by current tracing format")
177 }
159}178}
160179
161/// Evaluate a file containing Jsonnet code, return a JSON string.180/// Evaluate a file containing Jsonnet code, return a JSON string.
167/// `filename` should be a NUL-terminated string186/// `filename` should be a NUL-terminated string
168#[no_mangle]187#[no_mangle]
169pub unsafe extern "C" fn jsonnet_evaluate_file(188pub unsafe extern "C" fn jsonnet_evaluate_file(
170 vm: &State,189 vm: &VM,
171 filename: *const c_char,190 filename: *const c_char,
172 error: &mut c_int,191 error: &mut c_int,
173) -> *const c_char {192) -> *const c_char {
174 let filename = parse_path(CStr::from_ptr(filename));193 let filename = parse_path(CStr::from_ptr(filename));
175 match vm194 match vm
195 .state
176 .import(&filename)196 .import(&filename)
177 .and_then(|v| vm.with_tla(v))197 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
178 .and_then(|v| vm.manifest(v))198 .and_then(|val| val.manifest(&vm.manifest_format))
179 {199 {
180 Ok(v) => {200 Ok(v) => {
181 *error = 0;201 *error = 0;
182 CString::new(&*v as &str).unwrap().into_raw()202 CString::new(&*v as &str).unwrap().into_raw()
183 }203 }
184 Err(e) => {204 Err(e) => {
185 *error = 1;205 *error = 1;
206 let mut out = String::new();
186 let out = vm.stringify_err(&e);207 vm.trace_format.write_trace(&mut out, &e).unwrap();
187 CString::new(&out as &str).unwrap().into_raw()208 CString::new(&out as &str).unwrap().into_raw()
188 }209 }
189 }210 }
198/// `filename`, `snippet` should be a NUL-terminated strings219/// `filename`, `snippet` should be a NUL-terminated strings
199#[no_mangle]220#[no_mangle]
200pub unsafe extern "C" fn jsonnet_evaluate_snippet(221pub unsafe extern "C" fn jsonnet_evaluate_snippet(
201 vm: &State,222 vm: &VM,
202 filename: *const c_char,223 filename: *const c_char,
203 snippet: *const c_char,224 snippet: *const c_char,
204 error: &mut c_int,225 error: &mut c_int,
205) -> *const c_char {226) -> *const c_char {
206 let filename = CStr::from_ptr(filename);227 let filename = CStr::from_ptr(filename);
207 let snippet = CStr::from_ptr(snippet);228 let snippet = CStr::from_ptr(snippet);
208 match vm229 match vm
230 .state
209 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())231 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
210 .and_then(|v| vm.with_tla(v))232 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
211 .and_then(|v| vm.manifest(v))233 .and_then(|val| val.manifest(&vm.manifest_format))
212 {234 {
213 Ok(v) => {235 Ok(v) => {
214 *error = 0;236 *error = 0;
215 CString::new(&*v as &str).unwrap().into_raw()237 CString::new(&*v as &str).unwrap().into_raw()
216 }238 }
217 Err(e) => {239 Err(e) => {
218 *error = 1;240 *error = 1;
241 let mut out = String::new();
219 let out = vm.stringify_err(&e);242 vm.trace_format.write_trace(&mut out, &e).unwrap();
220 CString::new(&out as &str).unwrap().into_raw()243 CString::new(&out as &str).unwrap().into_raw()
221 }244 }
222 }245 }
223}246}
247
248fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {
249 let Val::Obj(val) = val else {
250 throw!("expected object as multi output")
251 };
252 let mut out = Vec::new();
253 for (k, v) in val.iter(
254 #[cfg(feature = "exp-preserve-order")]
255 false,
256 ) {
257 out.push((k, v?.manifest(format)?.into()));
258 }
259 Ok(out)
260}
224261
225fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {262fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {
226 let mut out = Vec::new();263 let mut out = Vec::new();
242/// # Safety279/// # Safety
243#[no_mangle]280#[no_mangle]
244pub unsafe extern "C" fn jsonnet_evaluate_file_multi(281pub unsafe extern "C" fn jsonnet_evaluate_file_multi(
245 vm: &State,282 vm: &VM,
246 filename: *const c_char,283 filename: *const c_char,
247 error: &mut c_int,284 error: &mut c_int,
248) -> *const c_char {285) -> *const c_char {
249 let filename = parse_path(CStr::from_ptr(filename));286 let filename = parse_path(CStr::from_ptr(filename));
250 match vm287 match vm
288 .state
251 .import(&filename)289 .import(&filename)
252 .and_then(|v| vm.with_tla(v))290 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
253 .and_then(|v| vm.manifest_multi(v))291 .and_then(|val| val_to_multi(val, &vm.manifest_format))
254 {292 {
255 Ok(v) => {293 Ok(v) => {
256 *error = 0;294 *error = 0;
257 multi_to_raw(v)295 multi_to_raw(v)
258 }296 }
259 Err(e) => {297 Err(e) => {
260 *error = 1;298 *error = 1;
299 let mut out = String::new();
261 let out = vm.stringify_err(&e);300 vm.trace_format.write_trace(&mut out, &e).unwrap();
262 CString::new(&out as &str).unwrap().into_raw()301 CString::new(&out as &str).unwrap().into_raw()
263 }302 }
264 }303 }
267/// # Safety306/// # Safety
268#[no_mangle]307#[no_mangle]
269pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(308pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(
270 vm: &State,309 vm: &VM,
271 filename: *const c_char,310 filename: *const c_char,
272 snippet: *const c_char,311 snippet: *const c_char,
273 error: &mut c_int,312 error: &mut c_int,
274) -> *const c_char {313) -> *const c_char {
275 let filename = CStr::from_ptr(filename);314 let filename = CStr::from_ptr(filename);
276 let snippet = CStr::from_ptr(snippet);315 let snippet = CStr::from_ptr(snippet);
277 match vm316 match vm
317 .state
278 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())318 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
279 .and_then(|v| vm.with_tla(v))319 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
280 .and_then(|v| vm.manifest_multi(v))320 .and_then(|val| val_to_multi(val, &vm.manifest_format))
281 {321 {
282 Ok(v) => {322 Ok(v) => {
283 *error = 0;323 *error = 0;
284 multi_to_raw(v)324 multi_to_raw(v)
285 }325 }
286 Err(e) => {326 Err(e) => {
287 *error = 1;327 *error = 1;
328 let mut out = String::new();
288 let out = vm.stringify_err(&e);329 vm.trace_format.write_trace(&mut out, &e).unwrap();
289 CString::new(&out as &str).unwrap().into_raw()330 CString::new(&out as &str).unwrap().into_raw()
290 }331 }
291 }332 }
292}333}
334
335fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {
336 let Val::Arr(val) = val else {
337 throw!("expected array as stream output")
338 };
339 let mut out = Vec::new();
340 for item in val.iter() {
341 out.push(item?.manifest(format)?.into());
342 }
343 Ok(out)
344}
293345
294fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {346fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {
295 let mut out = Vec::new();347 let mut out = Vec::new();
309/// # Safety361/// # Safety
310#[no_mangle]362#[no_mangle]
311pub unsafe extern "C" fn jsonnet_evaluate_file_stream(363pub unsafe extern "C" fn jsonnet_evaluate_file_stream(
312 vm: &State,364 vm: &VM,
313 filename: *const c_char,365 filename: *const c_char,
314 error: &mut c_int,366 error: &mut c_int,
315) -> *const c_char {367) -> *const c_char {
316 let filename = parse_path(CStr::from_ptr(filename));368 let filename = parse_path(CStr::from_ptr(filename));
317 match vm369 match vm
370 .state
318 .import(&filename)371 .import(filename)
319 .and_then(|v| vm.with_tla(v))372 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
320 .and_then(|v| vm.manifest_stream(v))373 .and_then(|val| val_to_stream(val, &vm.manifest_format))
321 {374 {
322 Ok(v) => {375 Ok(v) => {
323 *error = 0;376 *error = 0;
324 stream_to_raw(v)377 stream_to_raw(v)
325 }378 }
326 Err(e) => {379 Err(e) => {
327 *error = 1;380 *error = 1;
381 let mut out = String::new();
328 let out = vm.stringify_err(&e);382 vm.trace_format.write_trace(&mut out, &e).unwrap();
329 CString::new(&out as &str)383 CString::new(&out as &str).unwrap().into_raw()
330 .expect("there should be no \\0 in the error string")
331 .into_raw()
332 }384 }
333 }385 }
336/// # Safety388/// # Safety
337#[no_mangle]389#[no_mangle]
338pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(390pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(
339 vm: &State,391 vm: &VM,
340 filename: *const c_char,392 filename: *const c_char,
341 snippet: *const c_char,393 snippet: *const c_char,
342 error: &mut c_int,394 error: &mut c_int,
343) -> *const c_char {395) -> *const c_char {
344 let filename = CStr::from_ptr(filename);396 let filename = CStr::from_ptr(filename);
345 let snippet = CStr::from_ptr(snippet);397 let snippet = CStr::from_ptr(snippet);
346 match vm398 match vm
399 .state
347 .evaluate_snippet(400 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
348 filename.to_str().expect("filename is not utf-8"),
349 snippet.to_str().expect("snippet is not utf-8"),
350 )
351 .and_then(|v| vm.with_tla(v))401 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
352 .and_then(|v| vm.manifest_stream(v))402 .and_then(|val| val_to_stream(val, &vm.manifest_format))
353 {403 {
354 Ok(v) => {404 Ok(v) => {
355 *error = 0;405 *error = 0;
356 stream_to_raw(v)406 stream_to_raw(v)
357 }407 }
358 Err(e) => {408 Err(e) => {
359 *error = 1;409 *error = 1;
410 let mut out = String::new();
360 let out = vm.stringify_err(&e);411 vm.trace_format.write_trace(&mut out, &e).unwrap();
361 CString::new(&out as &str)412 CString::new(&out as &str).unwrap().into_raw()
362 .expect("there should be no \\0 in the error string")
363 .into_raw()
364 }413 }
365 }414 }
modifiedbindings/jsonnet/src/native.rsdiffbeforeafterboth
9 function::builtin::{NativeCallback, NativeCallbackHandler},9 function::builtin::{NativeCallback, NativeCallbackHandler},
10 tb,10 tb,
11 typed::Typed,11 typed::Typed,
12 IStr, State, Val,12 IStr, Val,
13};13};
14use jrsonnet_gcmodule::Cc;14use jrsonnet_gcmodule::Cc;
15
16use crate::VM;
1517
16/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up18/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up
17/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon19/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon
70/// `raw_params` should point to a NULL-terminated array of NUL-terminated strings72/// `raw_params` should point to a NULL-terminated array of NUL-terminated strings
71#[no_mangle]73#[no_mangle]
72pub unsafe extern "C" fn jsonnet_native_callback(74pub unsafe extern "C" fn jsonnet_native_callback(
73 vm: &State,75 vm: &VM,
74 name: *const c_char,76 name: *const c_char,
75 cb: JsonnetNativeCallback,77 cb: JsonnetNativeCallback,
76 ctx: *const c_void,78 ctx: *const c_void,
92 raw_params = raw_params.offset(1);94 raw_params = raw_params.offset(1);
93 }95 }
9496
95 let any_resolver = vm.context_initializer();97 let any_resolver = vm.state.context_initializer();
96 any_resolver98 any_resolver
97 .as_any()99 .as_any()
98 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()100 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
5 os::raw::{c_char, c_double, c_int},5 os::raw::{c_char, c_double, c_int},
6};6};
77
8use jrsonnet_evaluator::{State, Val};8use jrsonnet_evaluator::Val;
9
10use crate::VM;
911
10/// If the value is a string, return it as UTF-8, otherwise return `NULL`.12/// If the value is a string, return it as UTF-8, otherwise return `NULL`.
11#[no_mangle]13#[no_mangle]
12pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {14pub extern "C" fn jsonnet_json_extract_string(_vm: &VM, v: &Val) -> *mut c_char {
13 match v {15 match v {
14 Val::Str(s) => CString::new(s as &str).unwrap().into_raw(),16 Val::Str(s) => CString::new(s as &str).unwrap().into_raw(),
15 _ => std::ptr::null_mut(),17 _ => std::ptr::null_mut(),
1820
19/// If the value is a number, return `1` and store the number in out, otherwise return `0`.21/// If the value is a number, return `1` and store the number in out, otherwise return `0`.
20#[no_mangle]22#[no_mangle]
21pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {23pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {
22 match v {24 match v {
23 Val::Num(n) => {25 Val::Num(n) => {
24 *out = *n;26 *out = *n;
3032
31/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.33/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.
32#[no_mangle]34#[no_mangle]
33pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {35pub extern "C" fn jsonnet_json_extract_bool(_vm: &VM, v: &Val) -> c_int {
34 match v {36 match v {
35 Val::Bool(false) => 0,37 Val::Bool(false) => 0,
36 Val::Bool(true) => 1,38 Val::Bool(true) => 1,
4042
41/// Return `1` if the value is `null`, otherwise return `0`.43/// Return `1` if the value is `null`, otherwise return `0`.
42#[no_mangle]44#[no_mangle]
43pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {45pub extern "C" fn jsonnet_json_extract_null(_vm: &VM, v: &Val) -> c_int {
44 match v {46 match v {
45 Val::Null => 1,47 Val::Null => 1,
46 _ => 0,48 _ => 0,
modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
5 os::raw::{c_char, c_double, c_int},5 os::raw::{c_char, c_double, c_int},
6};6};
77
8use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};8use jrsonnet_evaluator::{val::ArrValue, ObjValue, Val};
9use jrsonnet_gcmodule::Cc;9use jrsonnet_gcmodule::Cc;
10
11use crate::VM;
1012
11/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.13/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.
12///14///
13/// # Safety15/// # Safety
14///16///
15/// `v` should be a NUL-terminated string17/// `v` should be a NUL-terminated string
16#[no_mangle]18#[no_mangle]
17pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, val: *const c_char) -> *mut Val {19pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {
18 let val = CStr::from_ptr(val);20 let val = CStr::from_ptr(val);
19 let val = val.to_str().expect("string is not utf-8");21 let val = val.to_str().expect("string is not utf-8");
20 Box::into_raw(Box::new(Val::Str(val.into())))22 Box::into_raw(Box::new(Val::Str(val.into())))
21}23}
2224
23/// Convert the given double to a `JsonnetJsonValue`.25/// Convert the given double to a `JsonnetJsonValue`.
24#[no_mangle]26#[no_mangle]
25pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {27pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {
26 Box::into_raw(Box::new(Val::Num(v)))28 Box::into_raw(Box::new(Val::Num(v)))
27}29}
2830
29/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.31/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.
30#[no_mangle]32#[no_mangle]
31pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {33pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {
32 assert!(v == 0 || v == 1, "bad boolean value");34 assert!(v == 0 || v == 1, "bad boolean value");
33 Box::into_raw(Box::new(Val::Bool(v == 1)))35 Box::into_raw(Box::new(Val::Bool(v == 1)))
34}36}
3537
36/// Make a `JsonnetJsonValue` representing `null`.38/// Make a `JsonnetJsonValue` representing `null`.
37#[no_mangle]39#[no_mangle]
38pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {40pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {
39 Box::into_raw(Box::new(Val::Null))41 Box::into_raw(Box::new(Val::Null))
40}42}
4143
42/// Make a `JsonnetJsonValue` representing an array.44/// Make a `JsonnetJsonValue` representing an array.
43///45///
44/// Assign elements with [`jsonnet_json_array_append`].46/// Assign elements with [`jsonnet_json_array_append`].
45#[no_mangle]47#[no_mangle]
46pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {48pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {
47 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))49 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))
48}50}
4951
50/// Make a `JsonnetJsonValue` representing an object.52/// Make a `JsonnetJsonValue` representing an object.
51#[no_mangle]53#[no_mangle]
52pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {54pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {
53 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))55 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))
54}56}
5557
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
44
5use std::{ffi::CStr, os::raw::c_char};5use std::{ffi::CStr, os::raw::c_char};
66
7use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};7use jrsonnet_evaluator::{val::ArrValue, Thunk, Val};
8use jrsonnet_gcmodule::Cc;8use jrsonnet_gcmodule::Cc;
9
10use crate::VM;
911
10/// Adds value to the end of the array `arr`.12/// Adds value to the end of the array `arr`.
11///13///
14/// `arr` should be a pointer to array value allocated by make_array, or returned by other library call16/// `arr` should be a pointer to array value allocated by make_array, or returned by other library call
15/// `val` should be a pointer to value allocated using this library17/// `val` should be a pointer to value allocated using this library
16#[no_mangle]18#[no_mangle]
17pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {19pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {
18 match arr {20 match arr {
19 Val::Arr(old) => {21 Val::Arr(old) => {
20 let mut new = Vec::new();22 let mut new = Vec::new();
39/// `name` should be NUL-terminated string41/// `name` should be NUL-terminated string
40#[no_mangle]42#[no_mangle]
41pub unsafe extern "C" fn jsonnet_json_object_append(43pub unsafe extern "C" fn jsonnet_json_object_append(
42 _vm: &State,44 _vm: &VM,
43 obj: &mut Val,45 obj: &mut Val,
44 name: *const c_char,46 name: *const c_char,
45 val: &Val,47 val: &Val,
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
22
3use std::{ffi::CStr, os::raw::c_char};3use std::{ffi::CStr, os::raw::c_char};
44
5use jrsonnet_evaluator::State;5use jrsonnet_evaluator::{function::TlaArg, IStr};
6use jrsonnet_parser::{ParserSettings, Source};
7
8use crate::VM;
69
7/// Binds a Jsonnet external variable to the given string.10/// Binds a Jsonnet external variable to the given string.
8///11///
12///15///
13/// `name`, `code` should be a NUL-terminated strings16/// `name`, `code` should be a NUL-terminated strings
14#[no_mangle]17#[no_mangle]
15pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {18pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *const c_char) {
16 let name = CStr::from_ptr(name);19 let name = CStr::from_ptr(name);
17 let value = CStr::from_ptr(value);20 let value = CStr::from_ptr(value);
1821
19 let any_initializer = vm.context_initializer();22 let any_initializer = vm.state.context_initializer();
20 any_initializer23 any_initializer
21 .as_any()24 .as_any()
22 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()25 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
35///38///
36/// `name`, `code` should be a NUL-terminated strings39/// `name`, `code` should be a NUL-terminated strings
37#[no_mangle]40#[no_mangle]
38pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, code: *const c_char) {41pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *const c_char) {
39 let name = CStr::from_ptr(name);42 let name = CStr::from_ptr(name);
40 let code = CStr::from_ptr(code);43 let code = CStr::from_ptr(code);
4144
42 let any_initializer = vm.context_initializer();45 let any_initializer = vm.state.context_initializer();
43 any_initializer46 any_initializer
44 .as_any()47 .as_any()
45 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()48 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
59///62///
60/// `name`, `value` should be a NUL-terminated strings63/// `name`, `value` should be a NUL-terminated strings
61#[no_mangle]64#[no_mangle]
62pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {65pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value: *const c_char) {
63 let name = CStr::from_ptr(name);66 let name = CStr::from_ptr(name);
64 let value = CStr::from_ptr(value);67 let value = CStr::from_ptr(value);
65 vm.add_tla_str(68 vm.tla_args.insert(
66 name.to_str().expect("name is not utf-8").into(),69 name.to_str().expect("name is not utf-8").into(),
67 value.to_str().expect("value is not utf-8").into(),70 TlaArg::String(value.to_str().expect("value is not utf-8").into()),
68 )71 );
69}72}
7073
71/// Binds a top-level code argument for a top-level parameter.74/// Binds a top-level code argument for a top-level parameter.
76///79///
77/// `name`, `code` should be a NUL-terminated strings80/// `name`, `code` should be a NUL-terminated strings
78#[no_mangle]81#[no_mangle]
79pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, code: *const c_char) {82pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code: *const c_char) {
80 let name = CStr::from_ptr(name);83 let name = CStr::from_ptr(name);
81 let code = CStr::from_ptr(code);84 let code = CStr::from_ptr(code);
82 vm.add_tla_code(85
83 name.to_str().expect("name is not utf-8").into(),86 let name: IStr = name.to_str().expect("name is not utf-8").into();
84 code.to_str().expect("code is not utf-8"),87 let code: IStr = code.to_str().expect("code is not utf-8").into();
85 )88 let code = jrsonnet_parser::parse(
89 &code,
90 &ParserSettings {
91 source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
92 },
93 )
86 .expect("can't parse tla code")94 .expect("can't parse TLA code");
95
96 vm.tla_args.insert(name, TlaArg::Code(code));
87}97}
8898