difftreelog
refactor move state to global
in: master
9 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth1#![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 rc::Rc,21};2223use jrsonnet_evaluator::{24 apply_tla, bail,25 function::TlaArg,26 gc::WithCapacityExt as _,27 manifest::{JsonFormat, ManifestFormat, ToStringFormat},28 rustc_hash::FxHashMap,29 stack::set_stack_depth_limit,30 trace::{CompactFormat, PathResolver, TraceFormat},31 AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,32};33use jrsonnet_gcmodule::Acyclic;34use jrsonnet_parser::SourcePath;35use jrsonnet_stdlib::ContextInitializer;3637/// WASM stub38#[cfg(target_arch = "wasm32")]39#[no_mangle]40pub extern "C" fn _start() {}4142/// Return the version string of the Jsonnet interpreter.43/// Conforms to [semantic versioning](http://semver.org/).44/// If this does not match `LIB_JSONNET_VERSION`45/// then there is a mismatch between header and compiled library.46#[no_mangle]47pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {48 b"v0.20.0\0"49}5051unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {52 #[cfg(target_family = "unix")]53 {54 use std::os::unix::ffi::OsStrExt;55 let str = OsStr::from_bytes(input.to_bytes());56 Cow::Borrowed(Path::new(str))57 }58 #[cfg(not(target_family = "unix"))]59 {60 let string = input.to_str().expect("bad utf-8");61 Cow::Borrowed(string.as_ref())62 }63}6465unsafe fn unparse_path(input: &Path) -> CString {66 #[cfg(target_family = "unix")]67 {68 use std::os::unix::ffi::OsStrExt;69 let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");70 str71 }72 #[cfg(not(target_family = "unix"))]73 {74 let str = input.as_os_str().to_str().expect("bad utf-8");75 let cstr = CString::new(str).expect("input has NUL inside");76 cstr77 }78}7980#[derive(Acyclic)]81struct VMImportResolver {82 inner: RefCell<Rc<dyn ImportResolver>>,83}84impl VMImportResolver {85 fn new(value: impl ImportResolver) -> Self {86 Self {87 inner: RefCell::new(Rc::new(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: &dyn AsPathLike) -> Result<SourcePath> {97 self.inner.borrow().resolve_from(from, path)98 }99100 fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {101 self.inner.borrow().resolve_from_default(path)102 }103}104105pub struct VM {106 state: State,107 manifest_format: Box<dyn ManifestFormat>,108 trace_format: Box<dyn TraceFormat>,109 tla_args: FxHashMap<IStr, TlaArg>,110}111impl VM {112 fn replace_import_resolver(&self, resolver: impl ImportResolver) {113 *(self.state.import_resolver() as &dyn Any)114 .downcast_ref::<VMImportResolver>()115 .expect("valid resolver ty")116 .inner117 .borrow_mut() = Rc::new(resolver);118 }119 fn add_jpath(&self, path: PathBuf) {120 let ir = self.state.import_resolver();121 let vmi = (ir as &dyn Any)122 .downcast_ref::<VMImportResolver>()123 .expect("valid resolver ty");124 let vmi = &mut *vmi.inner.borrow_mut();125 (vmi as &mut dyn Any)126 .downcast_mut::<FileImportResolver>()127 .expect("jpaths are not compatible with callback imports!")128 .add_jpath(path);129 }130}131132/// Creates a new Jsonnet virtual machine.133#[no_mangle]134#[allow(clippy::box_default)]135pub extern "C" fn jsonnet_make() -> *mut VM {136 let mut state = State::builder();137 state138 .import_resolver(VMImportResolver::new(FileImportResolver::default()))139 .context_initializer(ContextInitializer::new(PathResolver::new_cwd_fallback()));140 let state = state.build();141 Box::into_raw(Box::new(VM {142 state,143 manifest_format: Box::new(JsonFormat::default()),144 trace_format: Box::new(CompactFormat::default()),145 tla_args: FxHashMap::new(),146 }))147}148149/// Complement of [`jsonnet_vm_make`].150#[no_mangle]151#[allow(clippy::boxed_local)]152pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {153 drop(vm);154}155156/// Set the maximum stack depth.157#[no_mangle]158pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {159 set_stack_depth_limit(v as usize);160}161162/// Set the number of objects required before a garbage collection cycle is allowed.163///164/// No-op for now165#[no_mangle]166pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}167168/// Run the garbage collector after this amount of growth in the number of objects169///170/// No-op for now171#[no_mangle]172pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}173174/// Expect a string as output and don't JSON encode it.175#[no_mangle]176pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {177 vm.manifest_format = match v {178 0 => Box::new(JsonFormat::default()),179 1 => Box::new(ToStringFormat),180 _ => panic!("incorrect output format"),181 };182}183184/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will185/// only return NULL if sz was zero.186///187/// # Safety188///189/// `buf` should be either previosly allocated by this library, or NULL190///191/// This function is most definitely broken, but it works somehow, see TODO inside192#[no_mangle]193pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {194 if buf.is_null() {195 if sz == 0 {196 return std::ptr::null_mut();197 }198 return unsafe {199 std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())200 };201 }202 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D203 // OR (Alternative way of fixing this TODO)204 // TODO: Standard allocator uses malloc, and it doesn't uses allocation size,205 // TODO: so it should work in normal cases. Maybe force allocator for this library?206 let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();207 if sz == 0 {208 unsafe { std::alloc::dealloc(buf, old_layout) };209 return std::ptr::null_mut();210 }211 unsafe { std::alloc::realloc(buf, old_layout, sz) }212}213214/// Clean up a JSON subtree.215///216/// This is useful if you want to abort with an error mid-way through building a complex value.217#[no_mangle]218#[allow(clippy::boxed_local)]219pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {220 drop(v);221}222223/// Set the number of lines of stack trace to display (0 for all of them).224#[no_mangle]225pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {226 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {227 format.max_trace = v as usize;228 } else {229 panic!("max_trace is not supported by current tracing format")230 }231}232233/// Evaluate a file containing Jsonnet code, return a JSON string.234///235/// The returned string should be cleaned up with `jsonnet_realloc`.236///237/// # Safety238///239/// `filename` should be a NUL-terminated string240#[no_mangle]241pub unsafe extern "C" fn jsonnet_evaluate_file(242 vm: &VM,243 filename: *const c_char,244 error: &mut c_int,245) -> *const c_char {246 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };247 match vm248 .state249 .import(filename)250 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))251 .and_then(|val| val.manifest(&vm.manifest_format))252 {253 Ok(v) => {254 *error = 0;255 CString::new(&*v as &str).unwrap().into_raw()256 }257 Err(e) => {258 *error = 1;259 let mut out = String::new();260 vm.trace_format.write_trace(&mut out, &e).unwrap();261 CString::new(&out as &str).unwrap().into_raw()262 }263 }264}265266/// Evaluate a string containing Jsonnet code, return a JSON string.267///268/// The returned string should be cleaned up with `jsonnet_realloc`.269///270/// # Safety271///272/// `filename`, `snippet` should be a NUL-terminated strings273#[no_mangle]274pub unsafe extern "C" fn jsonnet_evaluate_snippet(275 vm: &VM,276 filename: *const c_char,277 snippet: *const c_char,278 error: &mut c_int,279) -> *const c_char {280 let filename = unsafe { CStr::from_ptr(filename) };281 let snippet = unsafe { CStr::from_ptr(snippet) };282 match vm283 .state284 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())285 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))286 .and_then(|val| val.manifest(&vm.manifest_format))287 {288 Ok(v) => {289 *error = 0;290 CString::new(&*v as &str).unwrap().into_raw()291 }292 Err(e) => {293 *error = 1;294 let mut out = String::new();295 vm.trace_format.write_trace(&mut out, &e).unwrap();296 CString::new(&out as &str).unwrap().into_raw()297 }298 }299}300301fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {302 let Val::Obj(val) = val else {303 bail!("expected object as multi output")304 };305 let mut out = Vec::new();306 for (k, v) in val.iter(307 #[cfg(feature = "exp-preserve-order")]308 false,309 ) {310 out.push((k, v?.manifest(format)?.into()));311 }312 Ok(out)313}314315fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {316 let mut out = Vec::new();317 for (i, (k, v)) in multi.iter().enumerate() {318 if i != 0 {319 out.push(0);320 }321 out.extend_from_slice(k.as_bytes());322 out.push(0);323 out.extend_from_slice(v.as_bytes());324 }325 out.push(0);326 out.push(0);327 let v = out.as_ptr();328 std::mem::forget(out);329 v.cast::<c_char>()330}331332/// # Safety333#[no_mangle]334pub unsafe extern "C" fn jsonnet_evaluate_file_multi(335 vm: &VM,336 filename: *const c_char,337 error: &mut c_int,338) -> *const c_char {339 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };340 match vm341 .state342 .import(filename)343 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))344 .and_then(|val| val_to_multi(val, &vm.manifest_format))345 {346 Ok(v) => {347 *error = 0;348 multi_to_raw(v)349 }350 Err(e) => {351 *error = 1;352 let mut out = String::new();353 vm.trace_format.write_trace(&mut out, &e).unwrap();354 CString::new(&out as &str).unwrap().into_raw()355 }356 }357}358359/// # Safety360#[no_mangle]361pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(362 vm: &VM,363 filename: *const c_char,364 snippet: *const c_char,365 error: &mut c_int,366) -> *const c_char {367 let filename = unsafe { CStr::from_ptr(filename) };368 let snippet = unsafe { CStr::from_ptr(snippet) };369 match vm370 .state371 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())372 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))373 .and_then(|val| val_to_multi(val, &vm.manifest_format))374 {375 Ok(v) => {376 *error = 0;377 multi_to_raw(v)378 }379 Err(e) => {380 *error = 1;381 let mut out = String::new();382 vm.trace_format.write_trace(&mut out, &e).unwrap();383 CString::new(&out as &str).unwrap().into_raw()384 }385 }386}387388fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {389 let Val::Arr(val) = val else {390 bail!("expected array as stream output")391 };392 let mut out = Vec::new();393 for item in val.iter() {394 out.push(item?.manifest(format)?.into());395 }396 Ok(out)397}398399fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {400 let mut out = Vec::new();401 for (i, v) in multi.iter().enumerate() {402 if i != 0 {403 out.push(0);404 }405 out.extend_from_slice(v.as_bytes());406 }407 out.push(0);408 out.push(0);409 let v = out.as_ptr();410 std::mem::forget(out);411 v.cast::<c_char>()412}413414/// # Safety415#[no_mangle]416pub unsafe extern "C" fn jsonnet_evaluate_file_stream(417 vm: &VM,418 filename: *const c_char,419 error: &mut c_int,420) -> *const c_char {421 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };422 match vm423 .state424 .import(filename)425 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))426 .and_then(|val| val_to_stream(val, &vm.manifest_format))427 {428 Ok(v) => {429 *error = 0;430 stream_to_raw(v)431 }432 Err(e) => {433 *error = 1;434 let mut out = String::new();435 vm.trace_format.write_trace(&mut out, &e).unwrap();436 CString::new(&out as &str).unwrap().into_raw()437 }438 }439}440441/// # Safety442#[no_mangle]443pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(444 vm: &VM,445 filename: *const c_char,446 snippet: *const c_char,447 error: &mut c_int,448) -> *const c_char {449 let filename = unsafe { CStr::from_ptr(filename) };450 let snippet = unsafe { CStr::from_ptr(snippet) };451 match vm452 .state453 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())454 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))455 .and_then(|val| val_to_stream(val, &vm.manifest_format))456 {457 Ok(v) => {458 *error = 0;459 stream_to_raw(v)460 }461 Err(e) => {462 *error = 1;463 let mut out = String::new();464 vm.trace_format.write_trace(&mut out, &e).unwrap();465 CString::new(&out as &str).unwrap().into_raw()466 }467 }468}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 rc::Rc,21};2223use jrsonnet_evaluator::{24 apply_tla, bail,25 function::TlaArg,26 gc::WithCapacityExt as _,27 manifest::{JsonFormat, ManifestFormat, ToStringFormat},28 rustc_hash::FxHashMap,29 stack::set_stack_depth_limit,30 trace::{CompactFormat, PathResolver, TraceFormat},31 AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,32};33use jrsonnet_gcmodule::Acyclic;34use jrsonnet_parser::SourcePath;35use jrsonnet_stdlib::ContextInitializer;3637/// WASM stub38#[cfg(target_arch = "wasm32")]39#[no_mangle]40pub extern "C" fn _start() {}4142/// Return the version string of the Jsonnet interpreter.43/// Conforms to [semantic versioning](http://semver.org/).44/// If this does not match `LIB_JSONNET_VERSION`45/// then there is a mismatch between header and compiled library.46#[no_mangle]47pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {48 b"v0.20.0\0"49}5051unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {52 #[cfg(target_family = "unix")]53 {54 use std::os::unix::ffi::OsStrExt;55 let str = OsStr::from_bytes(input.to_bytes());56 Cow::Borrowed(Path::new(str))57 }58 #[cfg(not(target_family = "unix"))]59 {60 let string = input.to_str().expect("bad utf-8");61 Cow::Borrowed(string.as_ref())62 }63}6465unsafe fn unparse_path(input: &Path) -> CString {66 #[cfg(target_family = "unix")]67 {68 use std::os::unix::ffi::OsStrExt;69 let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");70 str71 }72 #[cfg(not(target_family = "unix"))]73 {74 let str = input.as_os_str().to_str().expect("bad utf-8");75 let cstr = CString::new(str).expect("input has NUL inside");76 cstr77 }78}7980#[derive(Acyclic)]81struct VMImportResolver {82 inner: RefCell<Rc<dyn ImportResolver>>,83}84impl VMImportResolver {85 fn new(value: impl ImportResolver) -> Self {86 Self {87 inner: RefCell::new(Rc::new(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: &dyn AsPathLike) -> Result<SourcePath> {97 self.inner.borrow().resolve_from(from, path)98 }99100 fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {101 self.inner.borrow().resolve_from_default(path)102 }103}104105pub struct VM {106 state: State,107 manifest_format: Box<dyn ManifestFormat>,108 trace_format: Box<dyn TraceFormat>,109 tla_args: FxHashMap<IStr, TlaArg>,110}111impl VM {112 fn replace_import_resolver(&self, resolver: impl ImportResolver) {113 *(self.state.import_resolver() as &dyn Any)114 .downcast_ref::<VMImportResolver>()115 .expect("valid resolver ty")116 .inner117 .borrow_mut() = Rc::new(resolver);118 }119 fn add_jpath(&self, path: PathBuf) {120 let ir = self.state.import_resolver();121 let vmi = (ir as &dyn Any)122 .downcast_ref::<VMImportResolver>()123 .expect("valid resolver ty");124 let vmi = &mut *vmi.inner.borrow_mut();125 (vmi as &mut dyn Any)126 .downcast_mut::<FileImportResolver>()127 .expect("jpaths are not compatible with callback imports!")128 .add_jpath(path);129 }130}131132/// Creates a new Jsonnet virtual machine.133#[no_mangle]134#[allow(clippy::box_default)]135pub extern "C" fn jsonnet_make() -> *mut VM {136 let mut state = State::builder();137 state138 .import_resolver(VMImportResolver::new(FileImportResolver::default()))139 .context_initializer(ContextInitializer::new(PathResolver::new_cwd_fallback()));140 let state = state.build();141 Box::into_raw(Box::new(VM {142 state,143 manifest_format: Box::new(JsonFormat::default()),144 trace_format: Box::new(CompactFormat::default()),145 tla_args: FxHashMap::new(),146 }))147}148149/// Complement of [`jsonnet_vm_make`].150#[no_mangle]151#[allow(clippy::boxed_local)]152pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {153 drop(vm);154}155156/// Set the maximum stack depth.157#[no_mangle]158pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {159 set_stack_depth_limit(v as usize);160}161162/// Set the number of objects required before a garbage collection cycle is allowed.163///164/// No-op for now165#[no_mangle]166pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}167168/// Run the garbage collector after this amount of growth in the number of objects169///170/// No-op for now171#[no_mangle]172pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}173174/// Expect a string as output and don't JSON encode it.175#[no_mangle]176pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {177 vm.manifest_format = match v {178 0 => Box::new(JsonFormat::default()),179 1 => Box::new(ToStringFormat),180 _ => panic!("incorrect output format"),181 };182}183184/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will185/// only return NULL if sz was zero.186///187/// # Safety188///189/// `buf` should be either previosly allocated by this library, or NULL190///191/// This function is most definitely broken, but it works somehow, see TODO inside192#[no_mangle]193pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {194 if buf.is_null() {195 if sz == 0 {196 return std::ptr::null_mut();197 }198 return unsafe {199 std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())200 };201 }202 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D203 // OR (Alternative way of fixing this TODO)204 // TODO: Standard allocator uses malloc, and it doesn't uses allocation size,205 // TODO: so it should work in normal cases. Maybe force allocator for this library?206 let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();207 if sz == 0 {208 unsafe { std::alloc::dealloc(buf, old_layout) };209 return std::ptr::null_mut();210 }211 unsafe { std::alloc::realloc(buf, old_layout, sz) }212}213214/// Clean up a JSON subtree.215///216/// This is useful if you want to abort with an error mid-way through building a complex value.217#[no_mangle]218#[allow(clippy::boxed_local)]219pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {220 drop(v);221}222223/// Set the number of lines of stack trace to display (0 for all of them).224#[no_mangle]225pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {226 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {227 format.max_trace = v as usize;228 } else {229 panic!("max_trace is not supported by current tracing format")230 }231}232233/// Evaluate a file containing Jsonnet code, return a JSON string.234///235/// The returned string should be cleaned up with `jsonnet_realloc`.236///237/// # Safety238///239/// `filename` should be a NUL-terminated string240#[no_mangle]241pub unsafe extern "C" fn jsonnet_evaluate_file(242 vm: &VM,243 filename: *const c_char,244 error: &mut c_int,245) -> *const c_char {246 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };247 match vm248 .state249 .import(filename)250 .and_then(|val| apply_tla(&vm.tla_args, val))251 .and_then(|val| val.manifest(&vm.manifest_format))252 {253 Ok(v) => {254 *error = 0;255 CString::new(&*v as &str).unwrap().into_raw()256 }257 Err(e) => {258 *error = 1;259 let mut out = String::new();260 vm.trace_format.write_trace(&mut out, &e).unwrap();261 CString::new(&out as &str).unwrap().into_raw()262 }263 }264}265266/// Evaluate a string containing Jsonnet code, return a JSON string.267///268/// The returned string should be cleaned up with `jsonnet_realloc`.269///270/// # Safety271///272/// `filename`, `snippet` should be a NUL-terminated strings273#[no_mangle]274pub unsafe extern "C" fn jsonnet_evaluate_snippet(275 vm: &VM,276 filename: *const c_char,277 snippet: *const c_char,278 error: &mut c_int,279) -> *const c_char {280 let filename = unsafe { CStr::from_ptr(filename) };281 let snippet = unsafe { CStr::from_ptr(snippet) };282 match vm283 .state284 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())285 .and_then(|val| apply_tla(&vm.tla_args, val))286 .and_then(|val| val.manifest(&vm.manifest_format))287 {288 Ok(v) => {289 *error = 0;290 CString::new(&*v as &str).unwrap().into_raw()291 }292 Err(e) => {293 *error = 1;294 let mut out = String::new();295 vm.trace_format.write_trace(&mut out, &e).unwrap();296 CString::new(&out as &str).unwrap().into_raw()297 }298 }299}300301fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {302 let Val::Obj(val) = val else {303 bail!("expected object as multi output")304 };305 let mut out = Vec::new();306 for (k, v) in val.iter(307 #[cfg(feature = "exp-preserve-order")]308 false,309 ) {310 out.push((k, v?.manifest(format)?.into()));311 }312 Ok(out)313}314315fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {316 let mut out = Vec::new();317 for (i, (k, v)) in multi.iter().enumerate() {318 if i != 0 {319 out.push(0);320 }321 out.extend_from_slice(k.as_bytes());322 out.push(0);323 out.extend_from_slice(v.as_bytes());324 }325 out.push(0);326 out.push(0);327 let v = out.as_ptr();328 std::mem::forget(out);329 v.cast::<c_char>()330}331332/// # Safety333#[no_mangle]334pub unsafe extern "C" fn jsonnet_evaluate_file_multi(335 vm: &VM,336 filename: *const c_char,337 error: &mut c_int,338) -> *const c_char {339 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };340 match vm341 .state342 .import(filename)343 .and_then(|val| apply_tla(&vm.tla_args, val))344 .and_then(|val| val_to_multi(val, &vm.manifest_format))345 {346 Ok(v) => {347 *error = 0;348 multi_to_raw(v)349 }350 Err(e) => {351 *error = 1;352 let mut out = String::new();353 vm.trace_format.write_trace(&mut out, &e).unwrap();354 CString::new(&out as &str).unwrap().into_raw()355 }356 }357}358359/// # Safety360#[no_mangle]361pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(362 vm: &VM,363 filename: *const c_char,364 snippet: *const c_char,365 error: &mut c_int,366) -> *const c_char {367 let filename = unsafe { CStr::from_ptr(filename) };368 let snippet = unsafe { CStr::from_ptr(snippet) };369 match vm370 .state371 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())372 .and_then(|val| apply_tla(&vm.tla_args, val))373 .and_then(|val| val_to_multi(val, &vm.manifest_format))374 {375 Ok(v) => {376 *error = 0;377 multi_to_raw(v)378 }379 Err(e) => {380 *error = 1;381 let mut out = String::new();382 vm.trace_format.write_trace(&mut out, &e).unwrap();383 CString::new(&out as &str).unwrap().into_raw()384 }385 }386}387388fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {389 let Val::Arr(val) = val else {390 bail!("expected array as stream output")391 };392 let mut out = Vec::new();393 for item in val.iter() {394 out.push(item?.manifest(format)?.into());395 }396 Ok(out)397}398399fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {400 let mut out = Vec::new();401 for (i, v) in multi.iter().enumerate() {402 if i != 0 {403 out.push(0);404 }405 out.extend_from_slice(v.as_bytes());406 }407 out.push(0);408 out.push(0);409 let v = out.as_ptr();410 std::mem::forget(out);411 v.cast::<c_char>()412}413414/// # Safety415#[no_mangle]416pub unsafe extern "C" fn jsonnet_evaluate_file_stream(417 vm: &VM,418 filename: *const c_char,419 error: &mut c_int,420) -> *const c_char {421 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };422 match vm423 .state424 .import(filename)425 .and_then(|val| apply_tla(&vm.tla_args, val))426 .and_then(|val| val_to_stream(val, &vm.manifest_format))427 {428 Ok(v) => {429 *error = 0;430 stream_to_raw(v)431 }432 Err(e) => {433 *error = 1;434 let mut out = String::new();435 vm.trace_format.write_trace(&mut out, &e).unwrap();436 CString::new(&out as &str).unwrap().into_raw()437 }438 }439}440441/// # Safety442#[no_mangle]443pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(444 vm: &VM,445 filename: *const c_char,446 snippet: *const c_char,447 error: &mut c_int,448) -> *const c_char {449 let filename = unsafe { CStr::from_ptr(filename) };450 let snippet = unsafe { CStr::from_ptr(snippet) };451 match vm452 .state453 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())454 .and_then(|val| apply_tla(&vm.tla_args, val))455 .and_then(|val| val_to_stream(val, &vm.manifest_format))456 {457 Ok(v) => {458 *error = 0;459 stream_to_raw(v)460 }461 Err(e) => {462 *error = 1;463 let mut out = String::new();464 vm.trace_format.write_trace(&mut out, &e).unwrap();465 CString::new(&out as &str).unwrap().into_raw()466 }467 }468}cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -173,6 +173,7 @@
let mut s = State::builder();
s.import_resolver(import_resolver).context_initializer(std);
let s = s.build();
+ let _s = s.enter();
let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
let val = if opts.input.exec {
@@ -192,7 +193,7 @@
unused_mut,
clippy::redundant_clone,
)]
- let mut val = apply_tla(s.clone(), &tla, val)?;
+ let mut val = apply_tla(&tla, val)?;
#[cfg(feature = "exp-apply")]
for apply in opts.input.exp_apply {
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -6,12 +6,11 @@
use crate::{
error::ErrorKind::*, gc::WithCapacityExt as _, map::LayeredHashMap, ObjValue, Pending, Result,
- State, Thunk, Val,
+ Thunk, Val,
};
#[derive(Trace)]
struct ContextInternals {
- state: Option<State>,
dollar: Option<ObjValue>,
sup: Option<ObjValue>,
this: Option<ObjValue>,
@@ -33,13 +32,6 @@
Pending::new()
}
- pub fn state(&self) -> &State {
- self.0
- .state
- .as_ref()
- .expect("used state from dummy context")
- }
-
pub fn dollar(&self) -> Option<&ObjValue> {
self.0.dollar.as_ref()
}
@@ -112,7 +104,6 @@
ctx.bindings.clone().extend(new_bindings)
};
Self(Cc::new(ContextInternals {
- state: ctx.state.clone(),
dollar,
sup,
this,
@@ -128,34 +119,22 @@
}
pub struct ContextBuilder {
- state: Option<State>,
bindings: FxHashMap<IStr, Thunk<Val>>,
extend: Option<Context>,
}
impl ContextBuilder {
- /// # Panics
- /// Panics aren't directly caused by this function, but if state from resulting context is used
- pub fn dangerous_empty_state() -> Self {
- Self {
- state: None,
- bindings: FxHashMap::new(),
- extend: None,
- }
+ pub fn new() -> Self {
+ Self::with_capacity(0)
}
- pub fn new(state: State) -> Self {
- Self::with_capacity(state, 0)
- }
- pub fn with_capacity(state: State, capacity: usize) -> Self {
+ pub fn with_capacity(capacity: usize) -> Self {
Self {
- state: Some(state),
bindings: FxHashMap::with_capacity(capacity),
extend: None,
}
}
pub fn extend(parent: Context) -> Self {
Self {
- state: parent.0.state.clone(),
bindings: FxHashMap::new(),
extend: Some(parent),
}
@@ -173,7 +152,6 @@
parent.extend(self.bindings, None, None, None)
} else {
Context(Cc::new(ContextInternals {
- state: self.state,
bindings: LayeredHashMap::new(self.bindings),
dollar: None,
sup: None,
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,7 @@
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
-use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
+use crate::{evaluate, typed::Typed, with_state, Context, Result, Thunk, Val};
/// Marker for arguments, which can be evaluated with context set to None
pub trait OptionalContext {}
@@ -47,28 +47,59 @@
ImportStr(String),
InlineCode(String),
}
-impl ArgLike for TlaArg {
- fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
+impl TlaArg {
+ pub fn evaluate_tailstrict(&self) -> Result<Val> {
match self {
+ Self::String(s) => Ok(Val::string(s.clone())),
+ Self::Val(val) => Ok(val.clone()),
+ Self::Lazy(lazy) => Ok(lazy.evaluate()?),
+ Self::Import(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ s.import_resolved(resolved)
+ }),
+ Self::ImportStr(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ s.import_resolved_str(resolved).map(Val::string)
+ }),
+ Self::InlineCode(p) => with_state(|s| {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ s.import_resolved(resolved)
+ }),
+ }
+ }
+ pub fn evaluate(&self) -> Result<Thunk<Val>> {
+ match self {
Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
Self::Lazy(lazy) => Ok(lazy.clone()),
- Self::Import(p) => {
- let resolved = ctx.state().resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
- }
- Self::ImportStr(p) => {
- let resolved = ctx.state().resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || ctx
- .state()
+ Self::Import(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || s.import_resolved(resolved)))
+ }),
+ Self::ImportStr(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || s
.import_resolved_str(resolved)
.map(Val::string)))
- }
- Self::InlineCode(p) => {
+ }),
+ Self::InlineCode(p) => with_state(|s| {
let resolved =
SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
- Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
- }
+ Ok(Thunk!(move || s.import_resolved(resolved)))
+ }),
+ }
+ }
+}
+
+// TODO: Is this implementation really required, as there is no Context to use?
+// Maybe something a bit stricter is possible to add, especially with precompiled calls?
+impl ArgLike for TlaArg {
+ fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+ if tailstrict {
+ self.evaluate_tailstrict().map(Thunk::evaluated)
+ } else {
+ self.evaluate()
}
}
}
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -207,7 +207,7 @@
tailstrict: bool,
) -> Result<Val> {
self.evaluate(
- ContextBuilder::dangerous_empty_state().build(),
+ ContextBuilder::new().build(),
CallLocation::native(),
args,
tailstrict,
crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -1,21 +1,24 @@
use jrsonnet_interner::IStr;
use jrsonnet_parser::Source;
+use rustc_hash::FxHashMap;
use crate::{
- function::{ArgsLike, CallLocation},
- in_description_frame, Result, State, Val,
+ function::{CallLocation, TlaArg},
+ in_description_frame, with_state, Result, Val,
};
-pub fn apply_tla<A: ArgsLike>(s: State, args: &A, val: Val) -> Result<Val> {
+pub fn apply_tla(args: &FxHashMap<IStr, TlaArg>, val: Val) -> Result<Val> {
Ok(if let Val::Func(func) = val {
in_description_frame(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
- s.create_default_context(Source::new_virtual(
- "<top-level-arg>".into(),
- IStr::empty(),
- )),
+ with_state(|s| {
+ s.create_default_context(Source::new_virtual(
+ "<top-level-arg>".into(),
+ IStr::empty(),
+ ))
+ }),
CallLocation::native(),
args,
false,
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -335,11 +335,6 @@
pub path_resolver: PathResolver,
}
-fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
- let source_name = format!("<extvar:{name}>");
- Source::new_virtual(source_name.into(), code.into())
-}
-
#[derive(Trace, Clone)]
pub struct ContextInitializer {
/// std without applied thisFile overlay
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -3,15 +3,15 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- function::{builtin, ArgLike, CallLocation, FuncVal},
+ function::{builtin, CallLocation, FuncVal},
manifest::JsonFormat,
typed::{Either2, Either4},
val::{equals, ArrValue},
- Context, Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
+ Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
};
use jrsonnet_gcmodule::Cc;
-use crate::{extvar_source, Settings};
+use crate::Settings;
#[builtin]
pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {
@@ -50,16 +50,14 @@
#[builtin(fields(
settings: Cc<RefCell<Settings>>,
))]
-pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {
- let ctx = ctx.state().create_default_context(extvar_source(&x, ""));
+pub fn builtin_ext_var(this: &builtin_ext_var, x: IStr) -> Result<Val> {
this.settings
.borrow()
.ext_vars
.get(&x)
.cloned()
.ok_or_else(|| UndefinedExternalVariable(x))?
- .evaluate_arg(ctx, true)?
- .evaluate()
+ .evaluate_tailstrict()
}
#[builtin(fields(
tests/tests/builtin.rsdiffbeforeafterboth--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -19,7 +19,7 @@
fn basic_function() -> Result<()> {
let a: a = a {};
let v = u32::from_untyped(a.call(
- ContextBuilder::dangerous_empty_state().build(),
+ ContextBuilder::new().build(),
CallLocation::native(),
&(),
)?)?;