git.delta.rocks / jrsonnet / refs/commits / c6e9f5b704f3

difftreelog

refactor(bindings) extra interop methods

Yaroslav Bolyukin2024-05-28parent: #7b38a7f.patch.diff
in: master

5 files changed

modifiedbindings/c/libjsonnet_test_file.cdiffbeforeafterboth
--- a/bindings/c/libjsonnet_test_file.c
+++ b/bindings/c/libjsonnet_test_file.c
@@ -1,16 +1,3 @@
-/*
-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>
 
modifiedbindings/jsonnet/Cargo.tomldiffbeforeafterboth
--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -27,11 +27,17 @@
 
 [lib]
 name = "jsonnet"
-crate-type = ["cdylib"]
+crate-type = ["cdylib", "staticlib"]
 
 [features]
+default = ["interop-common", "interop-wasm", "interop-threading"]
 # Export additional functions for native integration, i.e ability to set custom trace format
-interop = []
+interop-common = []
+# Provide ability to statically override callbacks from WASM (by using imports)
+interop-wasm = []
+# Provide ability to move jsonnet vm state between threads
+interop-threading = []
+
 experimental = ["exp-preserve-order", "exp-destruct"]
 exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
 exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -15,7 +15,7 @@
 use jrsonnet_evaluator::{
 	bail,
 	error::{ErrorKind::*, Result},
-	FileImportResolver, ImportResolver,
+	ImportResolver,
 };
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
@@ -106,6 +106,10 @@
 	fn as_any(&self) -> &dyn Any {
 		self
 	}
+
+	fn as_any_mut(&mut self) -> &mut dyn Any {
+		self
+	}
 }
 
 /// # Safety
@@ -117,7 +121,7 @@
 	cb: JsonnetImportCallback,
 	ctx: *mut c_void,
 ) {
-	vm.state.set_import_resolver(CallbackImportResolver {
+	vm.replace_import_resolver(CallbackImportResolver {
 		cb,
 		ctx,
 		out: RefCell::new(HashMap::new()),
@@ -131,10 +135,5 @@
 pub unsafe extern "C" fn jsonnet_jpath_add(vm: &VM, path: *const c_char) {
 	let cstr = unsafe { CStr::from_ptr(path) };
 	let path = PathBuf::from(cstr.to_str().unwrap());
-	let any_resolver = vm.state.import_resolver();
-	let resolver = any_resolver
-		.as_any()
-		.downcast_ref::<FileImportResolver>()
-		.expect("jpaths are not compatible with callback imports!");
-	resolver.add_jpath(path);
+	vm.add_jpath(path);
 }
modifiedbindings/jsonnet/src/interop.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -1,53 +1,156 @@
 //! Jrsonnet specific additional binding helpers
 
-use std::{
-	ffi::c_void,
-	os::raw::{c_char, c_int},
-};
+use crate::VM;
+
+#[cfg(feature = "interop-wasm")]
+pub mod wasm {
+	use std::ffi::{c_char, c_int, c_void};
+
+	use jrsonnet_evaluator::Val;
+
+	use crate::VM;
+
+	extern "C" {
+
+		pub fn _jrsonnet_static_import_callback(
+			ctx: *mut c_void,
+			base: *const c_char,
+			rel: *const c_char,
+			found_here: *mut *const c_char,
+			buf: *mut *mut c_char,
+			buflen: *mut usize,
+		) -> c_int;
 
-use jrsonnet_evaluator::Val;
+		#[allow(improper_ctypes)]
+		pub fn _jrsonnet_static_native_callback(
+			ctx: *const c_void,
+			argv: *const *const Val,
+			success: *mut c_int,
+		) -> *mut Val;
+	}
 
-use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
+	#[no_mangle]
+	#[cfg(feature = "interop-wasm")]
+	// ctx arg is passed as-is to callback
+	#[allow(clippy::not_unsafe_ptr_arg_deref)]
+	pub extern "C" fn jrsonnet_apply_static_import_callback(vm: &VM, ctx: *mut c_void) {
+		unsafe { crate::import::jsonnet_import_callback(vm, _jrsonnet_static_import_callback, ctx) }
+	}
 
-extern "C" {
-	pub fn _jrsonnet_static_import_callback(
+	/// # Safety
+	///
+	/// `name` and `raw_params` should be correctly initialized
+	#[no_mangle]
+	#[cfg(feature = "interop-wasm")]
+	pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
+		vm: &VM,
+		name: *const c_char,
 		ctx: *mut c_void,
-		base: *const c_char,
-		rel: *const c_char,
-		found_here: *mut *const c_char,
-		success: &mut c_int,
-	) -> *const c_char;
-
-	#[allow(improper_ctypes)]
-	pub fn _jrsonnet_static_native_callback(
-		ctx: *const c_void,
-		argv: *const *const Val,
-		success: *mut c_int,
-	) -> *mut Val;
+		raw_params: *const *const c_char,
+	) {
+		unsafe {
+			crate::native::jsonnet_native_callback(
+				vm,
+				name,
+				_jrsonnet_static_native_callback,
+				ctx,
+				raw_params,
+			);
+		}
+	}
 }
 
-/// # Safety
-#[no_mangle]
-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)
-}
+#[cfg(feature = "interop-common")]
+mod common {
+	use jrsonnet_evaluator::trace::{CompactFormat, ExplainingFormat, JsFormat, PathResolver};
 
-/// # Safety
-#[no_mangle]
-pub unsafe extern "C" fn jrsonnet_apply_static_native_callback(
-	vm: &VM,
-	name: *const c_char,
-	ctx: *mut c_void,
-	raw_params: *const *const c_char,
-) {
-	jsonnet_native_callback(vm, name, _jrsonnet_static_native_callback, ctx, raw_params)
+	use crate::VM;
+
+	#[no_mangle]
+	pub extern "C" fn jrsonnet_set_trace_format(vm: &mut VM, format: u8) {
+		match format {
+			0 => {
+				vm.trace_format = Box::new(CompactFormat {
+					max_trace: 20,
+					resolver: PathResolver::new_cwd_fallback(),
+					padding: 4,
+				});
+			}
+			1 => vm.trace_format = Box::new(JsFormat { max_trace: 20 }),
+			2 => {
+				vm.trace_format = Box::new(ExplainingFormat {
+					resolver: PathResolver::new_cwd_fallback(),
+					max_trace: 20,
+				});
+			}
+			_ => panic!("unknown trace format"),
+		}
+	}
 }
 
-#[no_mangle]
-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)),
-		_ => panic!("unknown trace format"),
+#[cfg(feature = "interop-threading")]
+mod threading {
+	use std::{ffi::c_int, thread::ThreadId};
+
+	pub struct ThreadCTX {
+		interner: *mut jrsonnet_interner::interop::PoolState,
+		gc: *mut jrsonnet_gcmodule::interop::GcState,
+	}
+
+	/// Golang jrsonnet bindings require Jsonnet VM to be movable.
+	/// Jrsonnet uses `thread_local` in some places, thus making VM
+	/// immovable by default. By using `jrsonnet_exit_thread` and
+	/// `jrsonnet_reenter_thread`, you can move `thread_local` state to
+	/// where it is more convinient to use it.
+	///
+	/// # Safety
+	///
+	/// Current thread GC will be broken after this call, need to call
+	/// `jrsonet_enter_thread` before doing anything.
+	#[no_mangle]
+	pub unsafe extern "C" fn jrsonnet_exit_thread() -> *mut ThreadCTX {
+		Box::into_raw(Box::new(ThreadCTX {
+			interner: jrsonnet_interner::interop::exit_thread(),
+			gc: unsafe { jrsonnet_gcmodule::interop::exit_thread() },
+		}))
+	}
+
+	#[no_mangle]
+	pub extern "C" fn jrsonnet_reenter_thread(mut ctx: Box<ThreadCTX>) {
+		use std::ptr::null_mut;
+		assert!(
+			!ctx.interner.is_null() && !ctx.gc.is_null(),
+			"reused context?"
+		);
+		unsafe { jrsonnet_interner::interop::reenter_thread(ctx.interner) }
+		unsafe { jrsonnet_gcmodule::interop::reenter_thread(ctx.gc) }
+		// Just in case
+		ctx.interner = null_mut();
+		ctx.gc = null_mut();
+	}
+
+	// ThreadId is compatible with u64, and there is unstable cast
+	// method... But until it is stabilized, lets erase its type by
+	// boxing.
+	pub enum JrThreadId {}
+
+	#[no_mangle]
+	pub extern "C" fn jrsonnet_thread_id() -> *mut JrThreadId {
+		Box::into_raw(Box::new(std::thread::current().id())).cast()
+	}
+
+	#[no_mangle]
+	pub extern "C" fn jrsonnet_thread_id_compare(
+		a: *const JrThreadId,
+		b: *const JrThreadId,
+	) -> c_int {
+		let a: &ThreadId = unsafe { *a.cast() };
+		let b: &ThreadId = unsafe { *b.cast() };
+		i32::from(*a == *b)
+	}
+
+	#[no_mangle]
+	pub unsafe extern "C" fn jrsonnet_thread_id_free(id: *mut JrThreadId) {
+		let _id: Box<ThreadId> = unsafe { Box::from_raw(id.cast()) };
 	}
 }
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
after · bindings/jsonnet/src/lib.rs
1#![allow(clippy::box_default)]23pub mod interop;45pub mod import;6pub mod native;7pub mod val_extract;8pub mod val_make;9pub mod val_modify;10pub mod vars_tlas;1112use std::{13	alloc::Layout,14	any::Any,15	borrow::Cow,16	cell::RefCell,17	ffi::{CStr, CString, OsStr},18	os::raw::{c_char, c_double, c_int, c_uint},19	path::{Path, PathBuf},20};2122use jrsonnet_evaluator::{23	apply_tla, bail,24	function::TlaArg,25	gc::{GcHashMap, TraceBox},26	manifest::{JsonFormat, ManifestFormat, ToStringFormat},27	stack::set_stack_depth_limit,28	tb,29	trace::{CompactFormat, PathResolver, TraceFormat},30	FileImportResolver, IStr, ImportResolver, Result, State, Val,31};32use jrsonnet_gcmodule::Trace;33use jrsonnet_parser::SourcePath;34use jrsonnet_stdlib::ContextInitializer;3536/// WASM stub37#[cfg(target_arch = "wasm32")]38#[no_mangle]39pub extern "C" fn _start() {}4041/// Return the version string of the Jsonnet interpreter.42/// Conforms to [semantic versioning](http://semver.org/).43/// If this does not match `LIB_JSONNET_VERSION`44/// then there is a mismatch between header and compiled library.45#[no_mangle]46pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {47	b"v0.20.0\0"48}4950unsafe fn parse_path(input: &CStr) -> Cow<Path> {51	#[cfg(target_family = "unix")]52	{53		use std::os::unix::ffi::OsStrExt;54		let str = OsStr::from_bytes(input.to_bytes());55		Cow::Borrowed(Path::new(str))56	}57	#[cfg(not(target_family = "unix"))]58	{59		let string = input.to_str().expect("bad utf-8");60		Cow::Borrowed(string.as_ref())61	}62}6364unsafe fn unparse_path(input: &Path) -> Cow<CStr> {65	#[cfg(target_family = "unix")]66	{67		use std::os::unix::ffi::OsStrExt;68		let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");69		Cow::Owned(str)70	}71	#[cfg(not(target_family = "unix"))]72	{73		let str = input.as_os_str().to_str().expect("bad utf-8");74		let cstr = CString::new(str).expect("input has NUL inside");75		Cow::Owned(cstr)76	}77}7879#[derive(Trace)]80struct VMImportResolver {81	#[trace(tracking(force))]82	inner: RefCell<TraceBox<dyn ImportResolver>>,83}84impl VMImportResolver {85	fn new(value: impl ImportResolver) -> Self {86		Self {87			inner: RefCell::new(tb!(value)),88		}89	}90}91impl ImportResolver for VMImportResolver {92	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {93		self.inner.borrow().load_file_contents(resolved)94	}9596	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {97		self.inner.borrow().resolve_from(from, path)98	}99100	fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {101		self.inner.borrow().resolve_from_default(path)102	}103104	fn resolve(&self, path: &Path) -> Result<SourcePath> {105		self.inner.borrow().resolve(path)106	}107108	fn as_any(&self) -> &dyn Any {109		self110	}111	fn as_any_mut(&mut self) -> &mut dyn Any {112		self113	}114}115116pub struct VM {117	state: State,118	manifest_format: Box<dyn ManifestFormat>,119	trace_format: Box<dyn TraceFormat>,120	tla_args: GcHashMap<IStr, TlaArg>,121}122impl VM {123	fn replace_import_resolver(&self, resolver: impl ImportResolver) {124		*self125			.state126			.import_resolver()127			.as_any()128			.downcast_ref::<VMImportResolver>()129			.expect("valid resolver ty")130			.inner131			.borrow_mut() = tb!(resolver);132	}133	fn add_jpath(&self, path: PathBuf) {134		self.state135			.import_resolver()136			.as_any()137			.downcast_ref::<VMImportResolver>()138			.expect("valid resolver ty")139			.inner140			.borrow_mut()141			.as_any_mut()142			.downcast_mut::<FileImportResolver>()143			.expect("jpaths are not compatible with callback imports!")144			.add_jpath(path);145	}146}147148/// Creates a new Jsonnet virtual machine.149#[no_mangle]150#[allow(clippy::box_default)]151pub extern "C" fn jsonnet_make() -> *mut VM {152	let mut state = State::builder();153	state154		.import_resolver(VMImportResolver::new(FileImportResolver::default()))155		.context_initializer(ContextInitializer::new(PathResolver::new_cwd_fallback()));156	let state = state.build();157	Box::into_raw(Box::new(VM {158		state,159		manifest_format: Box::new(JsonFormat::default()),160		trace_format: Box::new(CompactFormat::default()),161		tla_args: GcHashMap::new(),162	}))163}164165/// Complement of [`jsonnet_vm_make`].166#[no_mangle]167#[allow(clippy::boxed_local)]168pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {169	drop(vm);170}171172/// Set the maximum stack depth.173#[no_mangle]174pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {175	set_stack_depth_limit(v as usize);176}177178/// Set the number of objects required before a garbage collection cycle is allowed.179///180/// No-op for now181#[no_mangle]182pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}183184/// Run the garbage collector after this amount of growth in the number of objects185///186/// No-op for now187#[no_mangle]188pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}189190/// Expect a string as output and don't JSON encode it.191#[no_mangle]192pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {193	vm.manifest_format = match v {194		0 => Box::new(JsonFormat::default()),195		1 => Box::new(ToStringFormat),196		_ => panic!("incorrect output format"),197	};198}199200/// Allocate, resize, or free a buffer.  This will abort if the memory cannot be allocated. It will201/// only return NULL if sz was zero.202///203/// # Safety204///205/// `buf` should be either previosly allocated by this library, or NULL206///207/// This function is most definitely broken, but it works somehow, see TODO inside208#[no_mangle]209pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {210	if buf.is_null() {211		if sz == 0 {212			return std::ptr::null_mut();213		}214		return unsafe {215			std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())216		};217	}218	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D219	// OR (Alternative way of fixing this TODO)220	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,221	// TODO: so it should work in normal cases. Maybe force allocator for this library?222	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();223	if sz == 0 {224		unsafe { std::alloc::dealloc(buf, old_layout) };225		return std::ptr::null_mut();226	}227	unsafe { std::alloc::realloc(buf, old_layout, sz) }228}229230/// Clean up a JSON subtree.231///232/// This is useful if you want to abort with an error mid-way through building a complex value.233#[no_mangle]234#[allow(clippy::boxed_local)]235pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {236	drop(v);237}238239/// Set the number of lines of stack trace to display (0 for all of them).240#[no_mangle]241pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {242	if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {243		format.max_trace = v as usize;244	} else {245		panic!("max_trace is not supported by current tracing format")246	}247}248249/// Evaluate a file containing Jsonnet code, return a JSON string.250///251/// The returned string should be cleaned up with `jsonnet_realloc`.252///253/// # Safety254///255/// `filename` should be a NUL-terminated string256#[no_mangle]257pub unsafe extern "C" fn jsonnet_evaluate_file(258	vm: &VM,259	filename: *const c_char,260	error: &mut c_int,261) -> *const c_char {262	let filename = unsafe { parse_path(CStr::from_ptr(filename)) };263	match vm264		.state265		.import(filename)266		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))267		.and_then(|val| val.manifest(&vm.manifest_format))268	{269		Ok(v) => {270			*error = 0;271			CString::new(&*v as &str).unwrap().into_raw()272		}273		Err(e) => {274			*error = 1;275			let mut out = String::new();276			vm.trace_format.write_trace(&mut out, &e).unwrap();277			CString::new(&out as &str).unwrap().into_raw()278		}279	}280}281282/// Evaluate a string containing Jsonnet code, return a JSON string.283///284/// The returned string should be cleaned up with `jsonnet_realloc`.285///286/// # Safety287///288/// `filename`, `snippet` should be a NUL-terminated strings289#[no_mangle]290pub unsafe extern "C" fn jsonnet_evaluate_snippet(291	vm: &VM,292	filename: *const c_char,293	snippet: *const c_char,294	error: &mut c_int,295) -> *const c_char {296	let filename = unsafe { CStr::from_ptr(filename) };297	let snippet = unsafe { CStr::from_ptr(snippet) };298	match vm299		.state300		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())301		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))302		.and_then(|val| val.manifest(&vm.manifest_format))303	{304		Ok(v) => {305			*error = 0;306			CString::new(&*v as &str).unwrap().into_raw()307		}308		Err(e) => {309			*error = 1;310			let mut out = String::new();311			vm.trace_format.write_trace(&mut out, &e).unwrap();312			CString::new(&out as &str).unwrap().into_raw()313		}314	}315}316317fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {318	let Val::Obj(val) = val else {319		bail!("expected object as multi output")320	};321	let mut out = Vec::new();322	for (k, v) in val.iter(323		#[cfg(feature = "exp-preserve-order")]324		false,325	) {326		out.push((k, v?.manifest(format)?.into()));327	}328	Ok(out)329}330331fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {332	let mut out = Vec::new();333	for (i, (k, v)) in multi.iter().enumerate() {334		if i != 0 {335			out.push(0);336		}337		out.extend_from_slice(k.as_bytes());338		out.push(0);339		out.extend_from_slice(v.as_bytes());340	}341	out.push(0);342	out.push(0);343	let v = out.as_ptr();344	std::mem::forget(out);345	v.cast::<c_char>()346}347348/// # Safety349#[no_mangle]350pub unsafe extern "C" fn jsonnet_evaluate_file_multi(351	vm: &VM,352	filename: *const c_char,353	error: &mut c_int,354) -> *const c_char {355	let filename = unsafe { parse_path(CStr::from_ptr(filename)) };356	match vm357		.state358		.import(filename)359		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))360		.and_then(|val| val_to_multi(val, &vm.manifest_format))361	{362		Ok(v) => {363			*error = 0;364			multi_to_raw(v)365		}366		Err(e) => {367			*error = 1;368			let mut out = String::new();369			vm.trace_format.write_trace(&mut out, &e).unwrap();370			CString::new(&out as &str).unwrap().into_raw()371		}372	}373}374375/// # Safety376#[no_mangle]377pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(378	vm: &VM,379	filename: *const c_char,380	snippet: *const c_char,381	error: &mut c_int,382) -> *const c_char {383	let filename = unsafe { CStr::from_ptr(filename) };384	let snippet = unsafe { CStr::from_ptr(snippet) };385	match vm386		.state387		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())388		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))389		.and_then(|val| val_to_multi(val, &vm.manifest_format))390	{391		Ok(v) => {392			*error = 0;393			multi_to_raw(v)394		}395		Err(e) => {396			*error = 1;397			let mut out = String::new();398			vm.trace_format.write_trace(&mut out, &e).unwrap();399			CString::new(&out as &str).unwrap().into_raw()400		}401	}402}403404fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {405	let Val::Arr(val) = val else {406		bail!("expected array as stream output")407	};408	let mut out = Vec::new();409	for item in val.iter() {410		out.push(item?.manifest(format)?.into());411	}412	Ok(out)413}414415fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {416	let mut out = Vec::new();417	for (i, v) in multi.iter().enumerate() {418		if i != 0 {419			out.push(0);420		}421		out.extend_from_slice(v.as_bytes());422	}423	out.push(0);424	out.push(0);425	let v = out.as_ptr();426	std::mem::forget(out);427	v.cast::<c_char>()428}429430/// # Safety431#[no_mangle]432pub unsafe extern "C" fn jsonnet_evaluate_file_stream(433	vm: &VM,434	filename: *const c_char,435	error: &mut c_int,436) -> *const c_char {437	let filename = unsafe { parse_path(CStr::from_ptr(filename)) };438	match vm439		.state440		.import(filename)441		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))442		.and_then(|val| val_to_stream(val, &vm.manifest_format))443	{444		Ok(v) => {445			*error = 0;446			stream_to_raw(v)447		}448		Err(e) => {449			*error = 1;450			let mut out = String::new();451			vm.trace_format.write_trace(&mut out, &e).unwrap();452			CString::new(&out as &str).unwrap().into_raw()453		}454	}455}456457/// # Safety458#[no_mangle]459pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(460	vm: &VM,461	filename: *const c_char,462	snippet: *const c_char,463	error: &mut c_int,464) -> *const c_char {465	let filename = unsafe { CStr::from_ptr(filename) };466	let snippet = unsafe { CStr::from_ptr(snippet) };467	match vm468		.state469		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())470		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))471		.and_then(|val| val_to_stream(val, &vm.manifest_format))472	{473		Ok(v) => {474			*error = 0;475			stream_to_raw(v)476		}477		Err(e) => {478			*error = 1;479			let mut out = String::new();480			vm.trace_format.write_trace(&mut out, &e).unwrap();481			CString::new(&out as &str).unwrap().into_raw()482		}483	}484}