difftreelog
refactor(treewide) custom path support
in: master
15 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -4,19 +4,18 @@
any::Any,
cell::RefCell,
collections::HashMap,
+ env::current_dir,
ffi::{c_void, CStr, CString},
- fs::File,
- io::Read,
os::raw::{c_char, c_int},
- path::{Path, PathBuf},
+ path::PathBuf,
ptr::null_mut,
};
use jrsonnet_evaluator::{
error::{Error::*, Result},
- throw, ImportResolver, State,
+ throw, FileImportResolver, ImportResolver, State,
};
-use jrsonnet_parser::SourcePath;
+use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
pub type JsonnetImportCallback = unsafe extern "C" fn(
ctx: *mut c_void,
@@ -33,25 +32,31 @@
out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
- let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
- let rel = CString::new(path).unwrap().into_raw();
+ fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ let base = if let Some(p) = from.downcast_ref::<SourceFile>() {
+ let mut o = p.path().to_owned();
+ o.pop();
+ o
+ } else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
+ d.path().to_owned()
+ } else if from.is_default() {
+ current_dir().map_err(|e| ImportIo(e.to_string()))?
+ } else {
+ unreachable!("can't resolve this path");
+ };
+ let base = unsafe { crate::unparse_path(&base) };
+ let rel = CString::new(path).unwrap();
let found_here: *mut c_char = null_mut();
let mut success: i32 = 0;
let result_ptr = unsafe {
(self.cb)(
self.ctx,
- base,
- rel,
+ base.as_ptr(),
+ rel.as_ptr(),
&mut (found_here as *const _),
&mut success,
)
};
- // Release memory occipied by arguments passed
- unsafe {
- let _ = CString::from_raw(base);
- let _ = CString::from_raw(rel);
- }
let result_raw = unsafe { CStr::from_ptr(result_ptr) };
let result_str = result_raw.to_str().unwrap();
assert!(success == 0 || success == 1);
@@ -62,7 +67,9 @@
}
let found_here_raw = unsafe { CStr::from_ptr(found_here) };
- let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));
+ let found_here_buf = SourcePath::new(SourceFile::new(PathBuf::from(
+ found_here_raw.to_str().unwrap(),
+ )));
unsafe {
let _ = CString::from_raw(found_here);
}
@@ -79,12 +86,14 @@
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
- unsafe fn as_any(&self) -> &dyn Any {
+ fn as_any(&self) -> &dyn Any {
self
}
}
/// # Safety
+///
+/// Caller should pass correct callback function
#[no_mangle]
pub unsafe extern "C" fn jsonnet_import_callback(
vm: &State,
@@ -96,54 +105,11 @@
ctx,
out: RefCell::new(HashMap::new()),
}))
-}
-
-/// Standard FS import resolver
-#[derive(Default)]
-pub struct NativeImportResolver {
- library_paths: RefCell<Vec<PathBuf>>,
-}
-impl NativeImportResolver {
- fn add_jpath(&self, path: PathBuf) {
- self.library_paths.borrow_mut().push(path);
- }
}
-impl ImportResolver for NativeImportResolver {
- fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
- let mut new_path = from.to_owned();
- new_path.push(path);
- if new_path.exists() {
- Ok(SourcePath::Path(new_path))
- } else {
- for library_path in self.library_paths.borrow().iter() {
- let mut cloned = library_path.clone();
- cloned.push(path);
- if cloned.exists() {
- return Ok(SourcePath::Path(cloned));
- }
- }
- throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
- }
- }
- fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
- let path = match id {
- SourcePath::Path(path) => path,
- _ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),
- };
- let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
- let mut out = Vec::new();
- file.read_to_end(&mut out)
- .map_err(|e| ImportIo(e.to_string()))?;
- Ok(out)
- }
- unsafe fn as_any(&self) -> &dyn Any {
- self
- }
-}
/// # Safety
///
-/// This function is safe, if received v is a pointer to normal C string
+/// Caller should pass correct path: it should contain correct utf-8, and be \0-terminated
#[no_mangle]
pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {
let cstr = CStr::from_ptr(v);
@@ -151,7 +117,7 @@
let any_resolver = vm.import_resolver();
let resolver = any_resolver
.as_any()
- .downcast_ref::<NativeImportResolver>()
+ .downcast_ref::<FileImportResolver>()
.expect("jpaths are not compatible with callback imports!");
resolver.add_jpath(path);
}
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -19,6 +19,8 @@
]
# Destructuring of locals
exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
+# std.thisFile support
+legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2" }
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use std::{2 env::current_dir,3 fs::{create_dir_all, File},4 io::{Read, Write},5};67use clap::{AppSettings, IntoApp, Parser};8use clap_complete::Shell;9use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, ManifestOpts, OutputOpts};10use jrsonnet_evaluator::{error::LocError, State};1112#[cfg(feature = "mimalloc")]13#[global_allocator]14static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1516#[derive(Parser)]17enum SubOpts {18 /// Generate completions for specified shell19 Generate {20 /// Target shell name21 shell: Shell,22 },23}2425#[derive(Parser)]26#[clap(next_help_heading = "DEBUG")]27struct DebugOpts {28 /// Required OS stack size.29 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.30 #[clap(long, name = "size")]31 pub os_stack: Option<usize>,32}3334#[derive(Parser)]35#[clap(next_help_heading = "INPUT")]36struct InputOpts {37 /// Treat input as code, evaluate them instead of reading file38 #[clap(long, short = 'e')]39 pub exec: bool,4041 /// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself42 pub input: Option<String>,43}4445#[derive(Parser)]46#[clap(47 global_setting = AppSettings::DeriveDisplayOrder,48 args_conflicts_with_subcommands = true,49)]50struct Opts {51 #[clap(subcommand)]52 sub: Option<SubOpts>,5354 #[clap(flatten)]55 input: InputOpts,56 #[clap(flatten)]57 general: GeneralOpts,58 #[clap(flatten)]59 manifest: ManifestOpts,60 #[clap(flatten)]61 output: OutputOpts,62 #[clap(flatten)]63 debug: DebugOpts,64 #[clap(flatten)]65 gc: GcOpts,66}6768fn main() {69 let opts: Opts = Opts::parse();7071 if let Some(sub) = opts.sub {72 match sub {73 SubOpts::Generate { shell } => {74 use clap_complete::generate;75 let app = &mut Opts::command();76 let buf = &mut std::io::stdout();77 generate(shell, app, "jrsonnet", buf);78 std::process::exit(0)79 }80 }81 }8283 let success = if let Some(size) = opts.debug.os_stack {84 std::thread::Builder::new()85 .stack_size(size * 1024 * 1024)86 .spawn(|| main_catch(opts))87 .expect("new thread spawned")88 .join()89 .expect("thread finished successfully")90 } else {91 main_catch(opts)92 };93 if !success {94 std::process::exit(1);95 }96}9798#[derive(thiserror::Error, Debug)]99enum Error {100 // Handled differently101 #[error("evaluation error")]102 Evaluation(jrsonnet_evaluator::error::LocError),103 #[error("io error")]104 Io(#[from] std::io::Error),105 #[error("input is not utf8 encoded")]106 Utf8(#[from] std::str::Utf8Error),107 #[error("missing input argument")]108 MissingInputArgument,109}110impl From<LocError> for Error {111 fn from(e: LocError) -> Self {112 Self::Evaluation(e)113 }114}115116fn main_catch(opts: Opts) -> bool {117 let _printer = opts.gc.stats_printer();118 let s = State::default();119 if let Err(e) = main_real(&s, opts) {120 if let Error::Evaluation(e) = e {121 eprintln!("{}", s.stringify_err(&e));122 } else {123 eprintln!("{}", e);124 }125 return false;126 }127 true128}129130fn main_real(s: &State, opts: Opts) -> Result<(), Error> {131 opts.general.configure(s)?;132 opts.manifest.configure(s)?;133134 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;135 let val = if opts.input.exec {136 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?137 } else if input == "-" {138 let mut input = Vec::new();139 std::io::stdin().read_to_end(&mut input)?;140 let input_str = std::str::from_utf8(&input)?;141 s.evaluate_snippet("<stdin>".to_owned(), input_str)?142 } else {143 s.import(¤t_dir().expect("cwd"), &input)?144 };145146 let val = s.with_tla(val)?;147148 if let Some(multi) = opts.output.multi {149 if opts.output.create_output_dirs {150 let mut dir = multi.clone();151 dir.pop();152 create_dir_all(dir)?;153 }154 for (file, data) in s.manifest_multi(val)?.iter() {155 let mut path = multi.clone();156 path.push(file as &str);157 if opts.output.create_output_dirs {158 let mut dir = path.clone();159 dir.pop();160 create_dir_all(dir)?;161 }162 println!("{}", path.to_str().expect("path"));163 let mut file = File::create(path)?;164 writeln!(file, "{}", data)?;165 }166 } else if let Some(path) = opts.output.output_file {167 if opts.output.create_output_dirs {168 let mut dir = path.clone();169 dir.pop();170 create_dir_all(dir)?;171 }172 let mut file = File::create(path)?;173 writeln!(file, "{}", s.manifest(val)?)?;174 } else {175 let output = s.manifest(val)?;176 if !output.is_empty() {177 println!("{}", output);178 }179 }180181 Ok(())182}1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{AppSettings, IntoApp, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, ManifestOpts, OutputOpts};9use jrsonnet_evaluator::{error::LocError, State};1011#[cfg(feature = "mimalloc")]12#[global_allocator]13static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1415#[derive(Parser)]16enum SubOpts {17 /// Generate completions for specified shell18 Generate {19 /// Target shell name20 shell: Shell,21 },22}2324#[derive(Parser)]25#[clap(next_help_heading = "DEBUG")]26struct DebugOpts {27 /// Required OS stack size.28 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.29 #[clap(long, name = "size")]30 pub os_stack: Option<usize>,31}3233#[derive(Parser)]34#[clap(next_help_heading = "INPUT")]35struct InputOpts {36 /// Treat input as code, evaluate them instead of reading file37 #[clap(long, short = 'e')]38 pub exec: bool,3940 /// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself41 pub input: Option<String>,42}4344#[derive(Parser)]45#[clap(46 global_setting = AppSettings::DeriveDisplayOrder,47 args_conflicts_with_subcommands = true,48)]49struct Opts {50 #[clap(subcommand)]51 sub: Option<SubOpts>,5253 #[clap(flatten)]54 input: InputOpts,55 #[clap(flatten)]56 general: GeneralOpts,57 #[clap(flatten)]58 manifest: ManifestOpts,59 #[clap(flatten)]60 output: OutputOpts,61 #[clap(flatten)]62 debug: DebugOpts,63 #[clap(flatten)]64 gc: GcOpts,65}6667fn main() {68 let opts: Opts = Opts::parse();6970 if let Some(sub) = opts.sub {71 match sub {72 SubOpts::Generate { shell } => {73 use clap_complete::generate;74 let app = &mut Opts::command();75 let buf = &mut std::io::stdout();76 generate(shell, app, "jrsonnet", buf);77 std::process::exit(0)78 }79 }80 }8182 let success = if let Some(size) = opts.debug.os_stack {83 std::thread::Builder::new()84 .stack_size(size * 1024 * 1024)85 .spawn(|| main_catch(opts))86 .expect("new thread spawned")87 .join()88 .expect("thread finished successfully")89 } else {90 main_catch(opts)91 };92 if !success {93 std::process::exit(1);94 }95}9697#[derive(thiserror::Error, Debug)]98enum Error {99 // Handled differently100 #[error("evaluation error")]101 Evaluation(jrsonnet_evaluator::error::LocError),102 #[error("io error")]103 Io(#[from] std::io::Error),104 #[error("input is not utf8 encoded")]105 Utf8(#[from] std::str::Utf8Error),106 #[error("missing input argument")]107 MissingInputArgument,108}109impl From<LocError> for Error {110 fn from(e: LocError) -> Self {111 Self::Evaluation(e)112 }113}114115fn main_catch(opts: Opts) -> bool {116 let _printer = opts.gc.stats_printer();117 let s = State::default();118 if let Err(e) = main_real(&s, opts) {119 if let Error::Evaluation(e) = e {120 eprintln!("{}", s.stringify_err(&e));121 } else {122 eprintln!("{}", e);123 }124 return false;125 }126 true127}128129fn main_real(s: &State, opts: Opts) -> Result<(), Error> {130 opts.general.configure(s)?;131 opts.manifest.configure(s)?;132133 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;134 let val = if opts.input.exec {135 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?136 } else if input == "-" {137 let mut input = Vec::new();138 std::io::stdin().read_to_end(&mut input)?;139 let input_str = std::str::from_utf8(&input)?;140 s.evaluate_snippet("<stdin>".to_owned(), input_str)?141 } else {142 s.import(&input)?143 };144145 let val = s.with_tla(val)?;146147 if let Some(multi) = opts.output.multi {148 if opts.output.create_output_dirs {149 let mut dir = multi.clone();150 dir.pop();151 create_dir_all(dir)?;152 }153 for (file, data) in s.manifest_multi(val)?.iter() {154 let mut path = multi.clone();155 path.push(file as &str);156 if opts.output.create_output_dirs {157 let mut dir = path.clone();158 dir.pop();159 create_dir_all(dir)?;160 }161 println!("{}", path.to_str().expect("path"));162 let mut file = File::create(path)?;163 writeln!(file, "{}", data)?;164 }165 } else if let Some(path) = opts.output.output_file {166 if opts.output.create_output_dirs {167 let mut dir = path.clone();168 dir.pop();169 create_dir_all(dir)?;170 }171 let mut file = File::create(path)?;172 writeln!(file, "{}", s.manifest(val)?)?;173 } else {174 let output = s.manifest(val)?;175 if !output.is_empty() {176 println!("{}", output);177 }178 }179180 Ok(())181}crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -15,6 +15,7 @@
"jrsonnet-evaluator/exp-serde-preserve-order",
"jrsonnet-stdlib/exp-serde-preserve-order",
]
+legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -51,7 +51,7 @@
library_paths.extend(env::split_paths(path.as_os_str()));
}
- s.set_import_resolver(Box::new(FileImportResolver { library_paths }));
+ s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
s.set_max_stack(self.max_stack);
Ok(())
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,7 @@
use std::{fs::read_to_string, str::FromStr};
use clap::Parser;
-use jrsonnet_evaluator::{error::Result, State};
+use jrsonnet_evaluator::{error::Result, trace::PathResolver, State};
use crate::ConfigureState;
@@ -110,7 +110,8 @@
if self.no_stdlib {
return Ok(());
}
- let ctx = jrsonnet_stdlib::ContextInitializer::new(s.clone());
+ let ctx =
+ jrsonnet_stdlib::ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback());
for ext in self.ext_str.iter() {
ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
}
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -42,11 +42,7 @@
}
impl ConfigureState for TraceOpts {
fn configure(&self, s: &State) -> Result<()> {
- let resolver = if let Ok(dir) = std::env::current_dir() {
- PathResolver::Relative(dir)
- } else {
- PathResolver::Absolute
- };
+ let resolver = PathResolver::new_cwd_fallback();
match self
.trace_format
.as_ref()
crates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -84,7 +84,9 @@
unsafe { Self::new_raw(str.as_bytes(), true) }
}
- pub const fn as_slice(&self) -> &[u8] {
+ // `slice::from_raw_parts` is not yet stabilized
+ #[allow(clippy::missing_const_for_fn)]
+ pub fn as_slice(&self) -> &[u8] {
let header = Self::header(self);
// SAFETY: data is not null, and it is correctly initialized
let size = unsafe { (*header).size };
@@ -99,7 +101,7 @@
/// # Safety
/// Data should be checked to be utf8 via [`check_utf8`] first
- pub const unsafe fn as_str_unchecked(&self) -> &str {
+ pub unsafe fn as_str_unchecked(&self) -> &str {
// SAFETY: data is checked
unsafe { str::from_utf8_unchecked(self.as_slice()) }
}
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -133,7 +133,7 @@
}
#[must_use]
- pub const fn as_slice(&self) -> &[u8] {
+ pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
}
crates/jrsonnet-stdlib/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -1,4 +1,4 @@
-use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
+use std::{env, fs::File, io::Write, path::Path};
use jrsonnet_parser::{parse, ParserSettings, Source};
use structdump::CodegenResult;
@@ -8,7 +8,7 @@
include_str!("./src/std.jsonnet"),
&ParserSettings {
file_name: Source::new_virtual(
- Cow::Borrowed("<std>"),
+ "<std>".into(),
include_str!("./src/std.jsonnet").into(),
),
},
crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -1,7 +1,7 @@
use jrsonnet_parser::LocExpr;
mod structdump_import {
- pub(super) use std::{borrow::Cow, option::Option, rc::Rc, vec};
+ pub(super) use std::{option::Option, rc::Rc, vec};
pub(super) use jrsonnet_parser::*;
}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -1,5 +1,4 @@
use std::{
- borrow::Cow,
cell::{Ref, RefCell, RefMut},
collections::HashMap,
rc::Rc,
@@ -10,6 +9,7 @@
function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},
gc::{GcHashMap, TraceBox},
tb, throw_runtime,
+ trace::PathResolver,
typed::{Any, Either, Either2, Either4, VecVal, M1},
val::{equals, ArrValue},
Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
@@ -184,13 +184,27 @@
fn print_trace(&self, s: State, loc: CallLocation, value: IStr);
}
-pub struct StdTracePrinter;
+pub struct StdTracePrinter {
+ resolver: PathResolver,
+}
+impl StdTracePrinter {
+ pub fn new(resolver: PathResolver) -> Self {
+ Self { resolver }
+ }
+}
impl TracePrinter for StdTracePrinter {
fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {
eprint!("TRACE:");
if let Some(loc) = loc.0 {
let locs = loc.0.map_source_locations(&[loc.1]);
- eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
+ eprint!(
+ " {}:{}",
+ match loc.0.source_path().path() {
+ Some(p) => self.resolver.resolve(p),
+ None => loc.0.source_path().to_string(),
+ },
+ locs[0].line
+ );
}
eprintln!(" {}", value);
}
@@ -205,22 +219,13 @@
pub globals: GcHashMap<IStr, Thunk<Val>>,
/// Used for `std.trace`
pub trace_printer: Box<dyn TracePrinter>,
-}
-
-impl Default for Settings {
- fn default() -> Self {
- Self {
- ext_vars: Default::default(),
- ext_natives: Default::default(),
- globals: Default::default(),
- trace_printer: Box::new(StdTracePrinter),
- }
- }
+ /// Used for `std.thisFile`
+ pub path_resolver: PathResolver,
}
pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
let source_name = format!("<extvar:{}>", name);
- Source::new_virtual(Cow::Owned(source_name), code.into())
+ Source::new_virtual(source_name.into(), code.into())
}
pub struct ContextInitializer {
@@ -233,8 +238,15 @@
settings: Rc<RefCell<Settings>>,
}
impl ContextInitializer {
- pub fn new(s: State) -> Self {
- let settings = Rc::new(RefCell::new(Settings::default()));
+ pub fn new(s: State, resolver: PathResolver) -> Self {
+ let settings = Settings {
+ ext_vars: Default::default(),
+ ext_natives: Default::default(),
+ globals: Default::default(),
+ trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
+ path_resolver: resolver,
+ };
+ let settings = Rc::new(RefCell::new(settings));
Self {
#[cfg(not(feature = "legacy-this-file"))]
context: {
@@ -313,13 +325,10 @@
.hide()
.value(
s,
- Val::Str(
- source
- .path()
- .map(|p| p.display().to_string())
- .unwrap_or_else(String::new)
- .into(),
- ),
+ Val::Str(match source.source_path().path() {
+ Some(p) => self.settings().path_resolver.resolve(p).into(),
+ None => source.source_path().to_string().into(),
+ }),
)
.expect("this object builder is empty");
let stdlib_with_this_file = builder.build();
@@ -329,12 +338,12 @@
"std".into(),
Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
);
- for (k, v) in &self.settings().globals {
- context.bind(k.clone(), v.clone())
+ for (k, v) in self.settings().globals.iter() {
+ context.bind(k.clone(), v.clone());
}
context.build()
}
- unsafe fn as_any(&self) -> &dyn std::any::Any {
+ fn as_any(&self) -> &dyn std::any::Any {
self
}
}
@@ -540,12 +549,13 @@
impl StateExt for State {
fn with_stdlib(&self) {
- let initializer = ContextInitializer::new(self.clone());
+ let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
self.settings_mut().context_initializer = Box::new(initializer)
}
fn add_global(&self, name: IStr, value: Thunk<Val>) {
- // Safety:
- unsafe { self.settings().context_initializer.as_any() }
+ self.settings()
+ .context_initializer
+ .as_any()
.downcast_ref::<ContextInitializer>()
.expect("not standard context initializer")
.settings_mut()
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -2,7 +2,7 @@
local std = self,
local id = std.id,
- 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',
+ 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',
toString(a):: '' + a,
tests/tests/golden.rsdiffbeforeafterboth--- a/tests/tests/golden.rs
+++ b/tests/tests/golden.rs
@@ -21,7 +21,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- let v = match s.import(root, &file.display().to_string()) {
+ let v = match s.import(file) {
Ok(v) => v,
Err(e) => return s.stringify_err(&e),
};
tests/tests/suite.rsdiffbeforeafterboth--- a/tests/tests/suite.rs
+++ b/tests/tests/suite.rs
@@ -21,7 +21,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- match s.import(root, &file.display().to_string()) {
+ match s.import(file) {
Ok(Val::Bool(true)) => {}
Ok(Val::Bool(false)) => panic!("test {} returned false", file.display()),
Ok(_) => panic!("test {} returned wrong type as result", file.display()),