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
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -13,11 +13,13 @@
 
 use jrsonnet_evaluator::{
 	error::{Error::*, Result},
-	throw, FileImportResolver, ImportResolver, State,
+	throw, FileImportResolver, ImportResolver,
 };
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
 
+use crate::VM;
+
 pub type JsonnetImportCallback = unsafe extern "C" fn(
 	ctx: *mut c_void,
 	base: *const c_char,
@@ -100,25 +102,26 @@
 /// It should be safe to call `cb` using valid values with passed `ctx`
 #[no_mangle]
 pub unsafe extern "C" fn jsonnet_import_callback(
-	vm: &State,
+	vm: &VM,
 	cb: JsonnetImportCallback,
 	ctx: *mut c_void,
 ) {
-	vm.set_import_resolver(Box::new(CallbackImportResolver {
-		cb,
-		ctx,
-		out: RefCell::new(HashMap::new()),
-	}))
+	vm.state
+		.set_import_resolver(Box::new(CallbackImportResolver {
+			cb,
+			ctx,
+			out: RefCell::new(HashMap::new()),
+		}))
 }
 
 /// # Safety
 ///
 /// `path` should be a NUL-terminated string
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, path: *const c_char) {
+pub unsafe extern "C" fn jsonnet_jpath_add(vm: &VM, path: *const c_char) {
 	let cstr = CStr::from_ptr(path);
 	let path = PathBuf::from(cstr.to_str().unwrap());
-	let any_resolver = vm.import_resolver();
+	let any_resolver = vm.state.import_resolver();
 	let resolver = any_resolver
 		.as_any()
 		.downcast_ref::<FileImportResolver>()
modifiedbindings/jsonnet/src/interop.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -5,7 +5,7 @@
 	os::raw::{c_char, c_int},
 };
 
-use jrsonnet_evaluator::{State, Val};
+use jrsonnet_evaluator::Val;
 
 use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
 
@@ -28,14 +28,14 @@
 
 /// # Safety
 #[no_mangle]
-pub unsafe extern "C" fn jrsonnet_apply_static_import_callback(vm: &State, ctx: *mut c_void) {
+pub unsafe extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {
 	jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx)
 }
 
 /// # Safety
 #[no_mangle]
 pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
-	vm: &State,
+	vm: &VM,
 	name: *const c_char,
 	ctx: *mut c_void,
 	raw_params: *const *const c_char,
@@ -44,7 +44,7 @@
 }
 
 #[no_mangle]
-pub extern "C" fn jrsonnet_set_trace_format(vm: &State, format: u8) {
+pub extern "C" fn jrsonnet_set_trace_format(vm: &VM, format: u8) {
 	use jrsonnet_evaluator::trace::JsFormat;
 	match format {
 		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
--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -9,10 +9,12 @@
 	function::builtin::{NativeCallback, NativeCallbackHandler},
 	tb,
 	typed::Typed,
-	IStr, State, Val,
+	IStr, Val,
 };
 use jrsonnet_gcmodule::Cc;
 
+use crate::VM;
+
 /// 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
@@ -70,7 +72,7 @@
 /// `raw_params` should point to a NULL-terminated array of NUL-terminated strings
 #[no_mangle]
 pub unsafe extern "C" fn jsonnet_native_callback(
-	vm: &State,
+	vm: &VM,
 	name: *const c_char,
 	cb: JsonnetNativeCallback,
 	ctx: *const c_void,
@@ -92,7 +94,7 @@
 		raw_params = raw_params.offset(1);
 	}
 
-	let any_resolver = vm.context_initializer();
+	let any_resolver = vm.state.context_initializer();
 	any_resolver
 		.as_any()
 		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -5,11 +5,13 @@
 	os::raw::{c_char, c_double, c_int},
 };
 
-use jrsonnet_evaluator::{State, Val};
+use jrsonnet_evaluator::Val;
+
+use crate::VM;
 
 /// If the value is a string, return it as UTF-8, otherwise return `NULL`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {
+pub extern "C" fn jsonnet_json_extract_string(_vm: &VM, v: &Val) -> *mut c_char {
 	match v {
 		Val::Str(s) => CString::new(s as &str).unwrap().into_raw(),
 		_ => std::ptr::null_mut(),
@@ -18,7 +20,7 @@
 
 /// If the value is a number, return `1` and store the number in out, otherwise return `0`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {
+pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {
 	match v {
 		Val::Num(n) => {
 			*out = *n;
@@ -30,7 +32,7 @@
 
 /// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {
+pub extern "C" fn jsonnet_json_extract_bool(_vm: &VM, v: &Val) -> c_int {
 	match v {
 		Val::Bool(false) => 0,
 		Val::Bool(true) => 1,
@@ -40,7 +42,7 @@
 
 /// Return `1` if the value is `null`, otherwise return `0`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {
+pub extern "C" fn jsonnet_json_extract_null(_vm: &VM, v: &Val) -> c_int {
 	match v {
 		Val::Null => 1,
 		_ => 0,
modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -5,16 +5,18 @@
 	os::raw::{c_char, c_double, c_int},
 };
 
-use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};
+use jrsonnet_evaluator::{val::ArrValue, ObjValue, Val};
 use jrsonnet_gcmodule::Cc;
 
+use crate::VM;
+
 /// Convert the given `UTF-8` string to a `JsonnetJsonValue`.
 ///
 /// # Safety
 ///
 /// `v` should be a NUL-terminated string
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, val: *const c_char) -> *mut Val {
+pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {
 	let val = CStr::from_ptr(val);
 	let val = val.to_str().expect("string is not utf-8");
 	Box::into_raw(Box::new(Val::Str(val.into())))
@@ -22,20 +24,20 @@
 
 /// Convert the given double to a `JsonnetJsonValue`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {
+pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {
 	Box::into_raw(Box::new(Val::Num(v)))
 }
 
 /// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {
+pub extern "C" fn jsonnet_json_make_bool(_vm: &VM, v: c_int) -> *mut Val {
 	assert!(v == 0 || v == 1, "bad boolean value");
 	Box::into_raw(Box::new(Val::Bool(v == 1)))
 }
 
 /// Make a `JsonnetJsonValue` representing `null`.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {
+pub extern "C" fn jsonnet_json_make_null(_vm: &VM) -> *mut Val {
 	Box::into_raw(Box::new(Val::Null))
 }
 
@@ -43,12 +45,12 @@
 ///
 /// Assign elements with [`jsonnet_json_array_append`].
 #[no_mangle]
-pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {
+pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {
 	Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))
 }
 
 /// Make a `JsonnetJsonValue` representing an object.
 #[no_mangle]
-pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {
+pub extern "C" fn jsonnet_json_make_object(_vm: &VM) -> *mut Val {
 	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))
 }
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -4,9 +4,11 @@
 
 use std::{ffi::CStr, os::raw::c_char};
 
-use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};
+use jrsonnet_evaluator::{val::ArrValue, Thunk, Val};
 use jrsonnet_gcmodule::Cc;
 
+use crate::VM;
+
 /// Adds value to the end of the array `arr`.
 ///
 /// # Safety
@@ -14,7 +16,7 @@
 /// `arr` should be a pointer to array value allocated by make_array, or returned by other library call
 /// `val` should be a pointer to value allocated using this library
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {
+pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {
 	match arr {
 		Val::Arr(old) => {
 			let mut new = Vec::new();
@@ -39,7 +41,7 @@
 /// `name` should be NUL-terminated string
 #[no_mangle]
 pub unsafe extern "C" fn jsonnet_json_object_append(
-	_vm: &State,
+	_vm: &VM,
 	obj: &mut Val,
 	name: *const c_char,
 	val: &Val,
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -2,7 +2,10 @@
 
 use std::{ffi::CStr, os::raw::c_char};
 
-use jrsonnet_evaluator::State;
+use jrsonnet_evaluator::{function::TlaArg, IStr};
+use jrsonnet_parser::{ParserSettings, Source};
+
+use crate::VM;
 
 /// Binds a Jsonnet external variable to the given string.
 ///
@@ -12,11 +15,11 @@
 ///
 /// `name`, `code` should be a NUL-terminated strings
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {
+pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *const c_char) {
 	let name = CStr::from_ptr(name);
 	let value = CStr::from_ptr(value);
 
-	let any_initializer = vm.context_initializer();
+	let any_initializer = vm.state.context_initializer();
 	any_initializer
 		.as_any()
 		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
@@ -35,11 +38,11 @@
 ///
 /// `name`, `code` should be a NUL-terminated strings
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, code: *const c_char) {
+pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *const c_char) {
 	let name = CStr::from_ptr(name);
 	let code = CStr::from_ptr(code);
 
-	let any_initializer = vm.context_initializer();
+	let any_initializer = vm.state.context_initializer();
 	any_initializer
 		.as_any()
 		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
@@ -59,13 +62,13 @@
 ///
 /// `name`, `value` should be a NUL-terminated strings
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {
+pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value: *const c_char) {
 	let name = CStr::from_ptr(name);
 	let value = CStr::from_ptr(value);
-	vm.add_tla_str(
+	vm.tla_args.insert(
 		name.to_str().expect("name is not utf-8").into(),
-		value.to_str().expect("value is not utf-8").into(),
-	)
+		TlaArg::String(value.to_str().expect("value is not utf-8").into()),
+	);
 }
 
 /// Binds a top-level code argument for a top-level parameter.
@@ -76,12 +79,19 @@
 ///
 /// `name`, `code` should be a NUL-terminated strings
 #[no_mangle]
-pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, code: *const c_char) {
+pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code: *const c_char) {
 	let name = CStr::from_ptr(name);
 	let code = CStr::from_ptr(code);
-	vm.add_tla_code(
-		name.to_str().expect("name is not utf-8").into(),
-		code.to_str().expect("code is not utf-8"),
+
+	let name: IStr = name.to_str().expect("name is not utf-8").into();
+	let code: IStr = code.to_str().expect("code is not utf-8").into();
+	let code = jrsonnet_parser::parse(
+		&code,
+		&ParserSettings {
+			source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
+		},
 	)
-	.expect("can't parse tla code")
+	.expect("can't parse TLA code");
+
+	vm.tla_args.insert(name, TlaArg::Code(code));
 }