difftreelog
refactor(treewide) custom path support
in: master
15 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth4 any::Any,4 any::Any,5 cell::RefCell,5 cell::RefCell,6 collections::HashMap,6 collections::HashMap,7 env::current_dir,7 ffi::{c_void, CStr, CString},8 ffi::{c_void, CStr, CString},8 fs::File,9 io::Read,10 os::raw::{c_char, c_int},9 os::raw::{c_char, c_int},11 path::{Path, PathBuf},10 path::PathBuf,12 ptr::null_mut,11 ptr::null_mut,13};12};141315use jrsonnet_evaluator::{14use jrsonnet_evaluator::{16 error::{Error::*, Result},15 error::{Error::*, Result},17 throw, ImportResolver, State,16 throw, FileImportResolver, ImportResolver, State,18};17};19use jrsonnet_parser::SourcePath;18use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};201921pub type JsonnetImportCallback = unsafe extern "C" fn(20pub type JsonnetImportCallback = unsafe extern "C" fn(22 ctx: *mut c_void,21 ctx: *mut c_void,33 out: RefCell<HashMap<SourcePath, Vec<u8>>>,32 out: RefCell<HashMap<SourcePath, Vec<u8>>>,34}33}35impl ImportResolver for CallbackImportResolver {34impl ImportResolver for CallbackImportResolver {36 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {35 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {37 let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();36 let base = if let Some(p) = from.downcast_ref::<SourceFile>() {37 let mut o = p.path().to_owned();38 o.pop();39 o40 } else if let Some(d) = from.downcast_ref::<SourceDirectory>() {41 d.path().to_owned()42 } else if from.is_default() {43 current_dir().map_err(|e| ImportIo(e.to_string()))?44 } else {45 unreachable!("can't resolve this path");46 };47 let base = unsafe { crate::unparse_path(&base) };38 let rel = CString::new(path).unwrap().into_raw();48 let rel = CString::new(path).unwrap();39 let found_here: *mut c_char = null_mut();49 let found_here: *mut c_char = null_mut();40 let mut success: i32 = 0;50 let mut success: i32 = 0;41 let result_ptr = unsafe {51 let result_ptr = unsafe {42 (self.cb)(52 (self.cb)(43 self.ctx,53 self.ctx,44 base,54 base.as_ptr(),45 rel,55 rel.as_ptr(),46 &mut (found_here as *const _),56 &mut (found_here as *const _),47 &mut success,57 &mut success,48 )58 )49 };59 };50 // Release memory occipied by arguments passed51 unsafe {52 let _ = CString::from_raw(base);53 let _ = CString::from_raw(rel);54 }55 let result_raw = unsafe { CStr::from_ptr(result_ptr) };60 let result_raw = unsafe { CStr::from_ptr(result_ptr) };56 let result_str = result_raw.to_str().unwrap();61 let result_str = result_raw.to_str().unwrap();57 assert!(success == 0 || success == 1);62 assert!(success == 0 || success == 1);62 }67 }636864 let found_here_raw = unsafe { CStr::from_ptr(found_here) };69 let found_here_raw = unsafe { CStr::from_ptr(found_here) };65 let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));70 let found_here_buf = SourcePath::new(SourceFile::new(PathBuf::from(71 found_here_raw.to_str().unwrap(),72 )));66 unsafe {73 unsafe {67 let _ = CString::from_raw(found_here);74 let _ = CString::from_raw(found_here);68 }75 }79 Ok(self.out.borrow().get(resolved).unwrap().clone())86 Ok(self.out.borrow().get(resolved).unwrap().clone())80 }87 }818882 unsafe fn as_any(&self) -> &dyn Any {89 fn as_any(&self) -> &dyn Any {83 self90 self84 }91 }85}92}869387/// # Safety94/// # Safety95///96/// Caller should pass correct callback function88#[no_mangle]97#[no_mangle]89pub unsafe extern "C" fn jsonnet_import_callback(98pub unsafe extern "C" fn jsonnet_import_callback(90 vm: &State,99 vm: &State,98 }))107 }))99}108}100101/// Standard FS import resolver102#[derive(Default)]103pub struct NativeImportResolver {104 library_paths: RefCell<Vec<PathBuf>>,105}106impl NativeImportResolver {107 fn add_jpath(&self, path: PathBuf) {108 self.library_paths.borrow_mut().push(path);109 }110}111impl ImportResolver for NativeImportResolver {112 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {113 let mut new_path = from.to_owned();114 new_path.push(path);115 if new_path.exists() {116 Ok(SourcePath::Path(new_path))117 } else {118 for library_path in self.library_paths.borrow().iter() {119 let mut cloned = library_path.clone();120 cloned.push(path);121 if cloned.exists() {122 return Ok(SourcePath::Path(cloned));123 }124 }125 throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))126 }127 }128 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {129 let path = match id {130 SourcePath::Path(path) => path,131 _ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),132 };133 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;134 let mut out = Vec::new();135 file.read_to_end(&mut out)136 .map_err(|e| ImportIo(e.to_string()))?;137 Ok(out)138 }139 unsafe fn as_any(&self) -> &dyn Any {140 self141 }142}143109144/// # Safety110/// # Safety145///111///146/// This function is safe, if received v is a pointer to normal C string112/// Caller should pass correct path: it should contain correct utf-8, and be \0-terminated147#[no_mangle]113#[no_mangle]148pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {114pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {149 let cstr = CStr::from_ptr(v);115 let cstr = CStr::from_ptr(v);150 let path = PathBuf::from(cstr.to_str().unwrap());116 let path = PathBuf::from(cstr.to_str().unwrap());151 let any_resolver = vm.import_resolver();117 let any_resolver = vm.import_resolver();152 let resolver = any_resolver118 let resolver = any_resolver153 .as_any()119 .as_any()154 .downcast_ref::<NativeImportResolver>()120 .downcast_ref::<FileImportResolver>()155 .expect("jpaths are not compatible with callback imports!");121 .expect("jpaths are not compatible with callback imports!");156 resolver.add_jpath(path);122 resolver.add_jpath(path);157}123}cmds/jrsonnet/Cargo.tomldiffbeforeafterboth19]19]20# Destructuring of locals20# Destructuring of locals21exp-destruct = ["jrsonnet-evaluator/exp-destruct"]21exp-destruct = ["jrsonnet-evaluator/exp-destruct"]22# std.thisFile support23legacy-this-file = ["jrsonnet-cli/legacy-this-file"]222423[dependencies]25[dependencies]24jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2" }26jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2" }cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use std::{1use std::{2 env::current_dir,3 fs::{create_dir_all, File},2 fs::{create_dir_all, File},4 io::{Read, Write},3 io::{Read, Write},5};4};140 let input_str = std::str::from_utf8(&input)?;139 let input_str = std::str::from_utf8(&input)?;141 s.evaluate_snippet("<stdin>".to_owned(), input_str)?140 s.evaluate_snippet("<stdin>".to_owned(), input_str)?142 } else {141 } else {143 s.import(¤t_dir().expect("cwd"), &input)?142 s.import(&input)?144 };143 };145144146 let val = s.with_tla(val)?;145 let val = s.with_tla(val)?;crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth15 "jrsonnet-evaluator/exp-serde-preserve-order",15 "jrsonnet-evaluator/exp-serde-preserve-order",16 "jrsonnet-stdlib/exp-serde-preserve-order",16 "jrsonnet-stdlib/exp-serde-preserve-order",17]17]18legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]181919[dependencies]20[dependencies]20jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [21jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth51 library_paths.extend(env::split_paths(path.as_os_str()));51 library_paths.extend(env::split_paths(path.as_os_str()));52 }52 }535354 s.set_import_resolver(Box::new(FileImportResolver { library_paths }));54 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));555556 s.set_max_stack(self.max_stack);56 s.set_max_stack(self.max_stack);57 Ok(())57 Ok(())crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth1use std::{fs::read_to_string, str::FromStr};1use std::{fs::read_to_string, str::FromStr};223use clap::Parser;3use clap::Parser;4use jrsonnet_evaluator::{error::Result, State};4use jrsonnet_evaluator::{error::Result, trace::PathResolver, State};556use crate::ConfigureState;6use crate::ConfigureState;77111 return Ok(());111 return Ok(());112 }112 }113 let ctx = jrsonnet_stdlib::ContextInitializer::new(s.clone());113 let ctx =114 jrsonnet_stdlib::ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback());114 for ext in self.ext_str.iter() {115 for ext in self.ext_str.iter() {115 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());116 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());116 }117 }crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth42}42}43impl ConfigureState for TraceOpts {43impl ConfigureState for TraceOpts {44 fn configure(&self, s: &State) -> Result<()> {44 fn configure(&self, s: &State) -> Result<()> {45 let resolver = if let Ok(dir) = std::env::current_dir() {45 let resolver = PathResolver::new_cwd_fallback();46 PathResolver::Relative(dir)47 } else {48 PathResolver::Absolute49 };50 match self46 match self51 .trace_format47 .trace_format52 .as_ref()48 .as_ref()crates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth84 unsafe { Self::new_raw(str.as_bytes(), true) }84 unsafe { Self::new_raw(str.as_bytes(), true) }85 }85 }868687 // `slice::from_raw_parts` is not yet stabilized88 #[allow(clippy::missing_const_for_fn)]87 pub const fn as_slice(&self) -> &[u8] {89 pub fn as_slice(&self) -> &[u8] {88 let header = Self::header(self);90 let header = Self::header(self);89 // SAFETY: data is not null, and it is correctly initialized91 // SAFETY: data is not null, and it is correctly initialized90 let size = unsafe { (*header).size };92 let size = unsafe { (*header).size };99101100 /// # Safety102 /// # Safety101 /// Data should be checked to be utf8 via [`check_utf8`] first103 /// Data should be checked to be utf8 via [`check_utf8`] first102 pub const unsafe fn as_str_unchecked(&self) -> &str {104 pub unsafe fn as_str_unchecked(&self) -> &str {103 // SAFETY: data is checked105 // SAFETY: data is checked104 unsafe { str::from_utf8_unchecked(self.as_slice()) }106 unsafe { str::from_utf8_unchecked(self.as_slice()) }105 }107 }crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth133 }133 }134134135 #[must_use]135 #[must_use]136 pub const fn as_slice(&self) -> &[u8] {136 pub fn as_slice(&self) -> &[u8] {137 self.0.as_slice()137 self.0.as_slice()138 }138 }139}139}crates/jrsonnet-stdlib/build.rsdiffbeforeafterboth1use std::{borrow::Cow, env, fs::File, io::Write, path::Path};1use std::{env, fs::File, io::Write, path::Path};223use jrsonnet_parser::{parse, ParserSettings, Source};3use jrsonnet_parser::{parse, ParserSettings, Source};4use structdump::CodegenResult;4use structdump::CodegenResult;8 include_str!("./src/std.jsonnet"),8 include_str!("./src/std.jsonnet"),9 &ParserSettings {9 &ParserSettings {10 file_name: Source::new_virtual(10 file_name: Source::new_virtual(11 Cow::Borrowed("<std>"),11 "<std>".into(),12 include_str!("./src/std.jsonnet").into(),12 include_str!("./src/std.jsonnet").into(),13 ),13 ),14 },14 },crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth1use jrsonnet_parser::LocExpr;1use jrsonnet_parser::LocExpr;223mod structdump_import {3mod structdump_import {4 pub(super) use std::{borrow::Cow, option::Option, rc::Rc, vec};4 pub(super) use std::{option::Option, rc::Rc, vec};556 pub(super) use jrsonnet_parser::*;6 pub(super) use jrsonnet_parser::*;7}7}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth1use std::{1use std::{2 borrow::Cow,3 cell::{Ref, RefCell, RefMut},2 cell::{Ref, RefCell, RefMut},4 collections::HashMap,3 collections::HashMap,5 rc::Rc,4 rc::Rc,10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},9 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11 gc::{GcHashMap, TraceBox},10 gc::{GcHashMap, TraceBox},12 tb, throw_runtime,11 tb, throw_runtime,12 trace::PathResolver,13 typed::{Any, Either, Either2, Either4, VecVal, M1},13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::{equals, ArrValue},14 val::{equals, ArrValue},15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,184 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);184 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);185}185}186186187pub struct StdTracePrinter;187pub struct StdTracePrinter {188 resolver: PathResolver,189}190impl StdTracePrinter {191 pub fn new(resolver: PathResolver) -> Self {192 Self { resolver }193 }194}188impl TracePrinter for StdTracePrinter {195impl TracePrinter for StdTracePrinter {189 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {196 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {190 eprint!("TRACE:");197 eprint!("TRACE:");191 if let Some(loc) = loc.0 {198 if let Some(loc) = loc.0 {192 let locs = loc.0.map_source_locations(&[loc.1]);199 let locs = loc.0.map_source_locations(&[loc.1]);193 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);200 eprint!(201 " {}:{}",202 match loc.0.source_path().path() {203 Some(p) => self.resolver.resolve(p),204 None => loc.0.source_path().to_string(),205 },206 locs[0].line207 );194 }208 }195 eprintln!(" {}", value);209 eprintln!(" {}", value);196 }210 }197}211}198212199pub struct Settings {213pub struct Settings {200 /// Used for `std.extVar`214 /// Used for `std.extVar`201 pub ext_vars: HashMap<IStr, TlaArg>,215 pub ext_vars: HashMap<IStr, TlaArg>,202 /// Used for `std.native`216 /// Used for `std.native`205 pub globals: GcHashMap<IStr, Thunk<Val>>,219 pub globals: GcHashMap<IStr, Thunk<Val>>,206 /// Used for `std.trace`220 /// Used for `std.trace`207 pub trace_printer: Box<dyn TracePrinter>,221 pub trace_printer: Box<dyn TracePrinter>,208}222 /// Used for `std.thisFile`209210impl Default for Settings {211 fn default() -> Self {212 Self {213 ext_vars: Default::default(),214 ext_natives: Default::default(),223 pub path_resolver: PathResolver,215 globals: Default::default(),224}216 trace_printer: Box::new(StdTracePrinter),217 }218 }219}220225221pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {226pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {222 let source_name = format!("<extvar:{}>", name);227 let source_name = format!("<extvar:{}>", name);223 Source::new_virtual(Cow::Owned(source_name), code.into())228 Source::new_virtual(source_name.into(), code.into())224}229}225230226pub struct ContextInitializer {231pub struct ContextInitializer {233 settings: Rc<RefCell<Settings>>,238 settings: Rc<RefCell<Settings>>,234}239}235impl ContextInitializer {240impl ContextInitializer {236 pub fn new(s: State) -> Self {241 pub fn new(s: State, resolver: PathResolver) -> Self {237 let settings = Rc::new(RefCell::new(Settings::default()));242 let settings = Settings {243 ext_vars: Default::default(),244 ext_natives: Default::default(),245 globals: Default::default(),246 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),247 path_resolver: resolver,248 };249 let settings = Rc::new(RefCell::new(settings));238 Self {250 Self {239 #[cfg(not(feature = "legacy-this-file"))]251 #[cfg(not(feature = "legacy-this-file"))]240 context: {252 context: {313 .hide()325 .hide()314 .value(326 .value(315 s,327 s,316 Val::Str(328 Val::Str(match source.source_path().path() {317 source318 .path()329 Some(p) => self.settings().path_resolver.resolve(p).into(),319 .map(|p| p.display().to_string())320 .unwrap_or_else(String::new)321 .into(),330 None => source.source_path().to_string().into(),322 ),331 }),323 )332 )324 .expect("this object builder is empty");333 .expect("this object builder is empty");325 let stdlib_with_this_file = builder.build();334 let stdlib_with_this_file = builder.build();329 "std".into(),338 "std".into(),330 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),339 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),331 );340 );332 for (k, v) in &self.settings().globals {341 for (k, v) in self.settings().globals.iter() {333 context.bind(k.clone(), v.clone())342 context.bind(k.clone(), v.clone());334 }343 }335 context.build()344 context.build()336 }345 }337 unsafe fn as_any(&self) -> &dyn std::any::Any {346 fn as_any(&self) -> &dyn std::any::Any {338 self347 self339 }348 }340}349}540549541impl StateExt for State {550impl StateExt for State {542 fn with_stdlib(&self) {551 fn with_stdlib(&self) {543 let initializer = ContextInitializer::new(self.clone());552 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());544 self.settings_mut().context_initializer = Box::new(initializer)553 self.settings_mut().context_initializer = Box::new(initializer)545 }554 }546 fn add_global(&self, name: IStr, value: Thunk<Val>) {555 fn add_global(&self, name: IStr, value: Thunk<Val>) {547 // Safety:548 unsafe { self.settings().context_initializer.as_any() }556 self.settings()557 .context_initializer558 .as_any()549 .downcast_ref::<ContextInitializer>()559 .downcast_ref::<ContextInitializer>()crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth2 local std = self,2 local std = self,3 local id = std.id,3 local id = std.id,445 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support. This will slow down stdlib caching a bit, though',5 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',667 toString(a):: '' + a,7 toString(a):: '' + a,88tests/tests/golden.rsdiffbeforeafterboth21 common::with_test(&s);21 common::with_test(&s);22 s.set_import_resolver(Box::new(FileImportResolver::default()));22 s.set_import_resolver(Box::new(FileImportResolver::default()));232324 let v = match s.import(root, &file.display().to_string()) {24 let v = match s.import(file) {25 Ok(v) => v,25 Ok(v) => v,26 Err(e) => return s.stringify_err(&e),26 Err(e) => return s.stringify_err(&e),27 };27 };tests/tests/suite.rsdiffbeforeafterboth21 common::with_test(&s);21 common::with_test(&s);22 s.set_import_resolver(Box::new(FileImportResolver::default()));22 s.set_import_resolver(Box::new(FileImportResolver::default()));232324 match s.import(root, &file.display().to_string()) {24 match s.import(file) {25 Ok(Val::Bool(true)) => {}25 Ok(Val::Bool(true)) => {}26 Ok(Val::Bool(false)) => panic!("test {} returned false", file.display()),26 Ok(Val::Bool(false)) => panic!("test {} returned false", file.display()),27 Ok(_) => panic!("test {} returned wrong type as result", file.display()),27 Ok(_) => panic!("test {} returned wrong type as result", file.display()),