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.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,5 +1,4 @@
use std::{
- env::current_dir,
fs::{create_dir_all, File},
io::{Read, Write},
};
@@ -140,7 +139,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(¤t_dir().expect("cwd"), &input)?
+ s.import(&input)?
};
let val = s.with_tla(val)?;
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.rsdiffbeforeafterboth1use std::{2 borrow::Cow,3 cell::{Ref, RefCell, RefMut},4 collections::HashMap,5 rc::Rc,6};78use jrsonnet_evaluator::{9 error::{Error::*, Result},10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11 gc::{GcHashMap, TraceBox},12 tb, throw_runtime,13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::{equals, ArrValue},15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44 let mut builder = ObjValueBuilder::new();4546 let expr = expr::stdlib_expr();47 let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48 .expect("stdlib.jsonnet should have no errors")49 .as_obj()50 .expect("stdlib.jsonnet should evaluate to object");5152 builder.with_super(eval);5354 for (name, builtin) in [55 ("length".into(), builtin_length::INST),56 // Types57 ("type".into(), builtin_type::INST),58 ("isString".into(), builtin_is_string::INST),59 ("isNumber".into(), builtin_is_number::INST),60 ("isBoolean".into(), builtin_is_boolean::INST),61 ("isObject".into(), builtin_is_object::INST),62 ("isArray".into(), builtin_is_array::INST),63 ("isFunction".into(), builtin_is_function::INST),64 // Arrays65 ("makeArray".into(), builtin_make_array::INST),66 ("slice".into(), builtin_slice::INST),67 ("map".into(), builtin_map::INST),68 ("flatMap".into(), builtin_flatmap::INST),69 ("filter".into(), builtin_filter::INST),70 ("foldl".into(), builtin_foldl::INST),71 ("foldr".into(), builtin_foldr::INST),72 ("range".into(), builtin_range::INST),73 ("join".into(), builtin_join::INST),74 ("reverse".into(), builtin_reverse::INST),75 ("any".into(), builtin_any::INST),76 ("all".into(), builtin_all::INST),77 ("member".into(), builtin_member::INST),78 ("count".into(), builtin_count::INST),79 // Math80 ("modulo".into(), builtin_modulo::INST),81 ("floor".into(), builtin_floor::INST),82 ("ceil".into(), builtin_ceil::INST),83 ("log".into(), builtin_log::INST),84 ("pow".into(), builtin_pow::INST),85 ("sqrt".into(), builtin_sqrt::INST),86 ("sin".into(), builtin_sin::INST),87 ("cos".into(), builtin_cos::INST),88 ("tan".into(), builtin_tan::INST),89 ("asin".into(), builtin_asin::INST),90 ("acos".into(), builtin_acos::INST),91 ("atan".into(), builtin_atan::INST),92 ("exp".into(), builtin_exp::INST),93 ("mantissa".into(), builtin_mantissa::INST),94 ("exponent".into(), builtin_exponent::INST),95 // Operator96 ("mod".into(), builtin_mod::INST),97 ("primitiveEquals".into(), builtin_primitive_equals::INST),98 ("equals".into(), builtin_equals::INST),99 ("format".into(), builtin_format::INST),100 // Sort101 ("sort".into(), builtin_sort::INST),102 // Hash103 ("md5".into(), builtin_md5::INST),104 // Encoding105 ("encodeUTF8".into(), builtin_encode_utf8::INST),106 ("decodeUTF8".into(), builtin_decode_utf8::INST),107 ("base64".into(), builtin_base64::INST),108 ("base64Decode".into(), builtin_base64_decode::INST),109 (110 "base64DecodeBytes".into(),111 builtin_base64_decode_bytes::INST,112 ),113 // Objects114 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),115 ("objectHasEx".into(), builtin_object_has_ex::INST),116 // Manifest117 ("escapeStringJson".into(), builtin_escape_string_json::INST),118 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120 // Parsing121 ("parseJson".into(), builtin_parse_json::INST),122 ("parseYaml".into(), builtin_parse_yaml::INST),123 // Misc124 ("codepoint".into(), builtin_codepoint::INST),125 ("substr".into(), builtin_substr::INST),126 ("char".into(), builtin_char::INST),127 ("strReplace".into(), builtin_str_replace::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),131 ("findSubstr".into(), builtin_find_substr::INST),132 ("startsWith".into(), builtin_starts_with::INST),133 ("endsWith".into(), builtin_ends_with::INST),134 ]135 .iter()136 .cloned()137 {138 builder139 .member(name)140 .hide()141 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))142 .expect("no conflict");143 }144145 builder146 .member("extVar".into())147 .hide()148 .value(149 s.clone(),150 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {151 settings: settings.clone()152 })))),153 )154 .expect("no conflict");155 builder156 .member("native".into())157 .hide()158 .value(159 s.clone(),160 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {161 settings: settings.clone()162 })))),163 )164 .expect("no conflict");165 builder166 .member("trace".into())167 .hide()168 .value(169 s.clone(),170 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),171 )172 .expect("no conflict");173174 builder175 .member("id".into())176 .hide()177 .value(s, Val::Func(FuncVal::Id))178 .expect("no conflict");179180 builder.build()181}182183pub trait TracePrinter {184 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);185}186187pub struct StdTracePrinter;188impl TracePrinter for StdTracePrinter {189 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {190 eprint!("TRACE:");191 if let Some(loc) = loc.0 {192 let locs = loc.0.map_source_locations(&[loc.1]);193 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);194 }195 eprintln!(" {}", value);196 }197}198199pub struct Settings {200 /// Used for `std.extVar`201 pub ext_vars: HashMap<IStr, TlaArg>,202 /// Used for `std.native`203 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,204 /// Helper to add globals without implementing custom ContextInitializer205 pub globals: GcHashMap<IStr, Thunk<Val>>,206 /// Used for `std.trace`207 pub trace_printer: Box<dyn TracePrinter>,208}209210impl Default for Settings {211 fn default() -> Self {212 Self {213 ext_vars: Default::default(),214 ext_natives: Default::default(),215 globals: Default::default(),216 trace_printer: Box::new(StdTracePrinter),217 }218 }219}220221pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {222 let source_name = format!("<extvar:{}>", name);223 Source::new_virtual(Cow::Owned(source_name), code.into())224}225226pub struct ContextInitializer {227 // When we don't need to support legacy-this-file, we can reuse same context for all files228 #[cfg(not(feature = "legacy-this-file"))]229 context: Context,230 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it231 #[cfg(feature = "legacy-this-file")]232 stdlib_obj: ObjValue,233 settings: Rc<RefCell<Settings>>,234}235impl ContextInitializer {236 pub fn new(s: State) -> Self {237 let settings = Rc::new(RefCell::new(Settings::default()));238 Self {239 #[cfg(not(feature = "legacy-this-file"))]240 context: {241 let mut context = ContextBuilder::with_capacity(1);242 context.bind(243 "std".into(),244 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),245 );246 context.build()247 },248 #[cfg(feature = "legacy-this-file")]249 stdlib_obj: stdlib_uncached(s, settings.clone()),250 settings,251 }252 }253 pub fn settings(&self) -> Ref<Settings> {254 self.settings.borrow()255 }256 pub fn settings_mut(&self) -> RefMut<Settings> {257 self.settings.borrow_mut()258 }259 pub fn add_ext_var(&self, name: IStr, value: Val) {260 self.settings_mut()261 .ext_vars262 .insert(name, TlaArg::Val(value));263 }264 pub fn add_ext_str(&self, name: IStr, value: IStr) {265 self.settings_mut()266 .ext_vars267 .insert(name, TlaArg::String(value));268 }269 pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {270 let code = code.into();271 let source = extvar_source(name, code.clone());272 let parsed = jrsonnet_parser::parse(273 &code,274 &jrsonnet_parser::ParserSettings {275 file_name: source.clone(),276 },277 )278 .map_err(|e| ImportSyntaxError {279 path: source,280 error: Box::new(e),281 })?;282 // self.data_mut().volatile_files.insert(source_name, code);283 self.settings_mut()284 .ext_vars285 .insert(name.into(), TlaArg::Code(parsed));286 Ok(())287 }288 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {289 self.settings_mut().ext_natives.insert(name, cb);290 }291}292impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {293 #[cfg(not(feature = "legacy-this-file"))]294 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {295 let out = self.context.clone();296 let globals = &self.settings().globals;297 if globals.is_empty() {298 return out;299 }300301 let mut out = ContextBuilder::extend(out);302 for (k, v) in globals.iter() {303 out.bind(k.clone(), v.clone());304 }305 out.build()306 }307 #[cfg(feature = "legacy-this-file")]308 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {309 let mut builder = ObjValueBuilder::new();310 builder.with_super(self.stdlib_obj.clone());311 builder312 .member("thisFile".into())313 .hide()314 .value(315 s,316 Val::Str(317 source318 .path()319 .map(|p| p.display().to_string())320 .unwrap_or_else(String::new)321 .into(),322 ),323 )324 .expect("this object builder is empty");325 let stdlib_with_this_file = builder.build();326327 let mut context = ContextBuilder::with_capacity(1);328 context.bind(329 "std".into(),330 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),331 );332 for (k, v) in &self.settings().globals {333 context.bind(k.clone(), v.clone())334 }335 context.build()336 }337 unsafe fn as_any(&self) -> &dyn std::any::Any {338 self339 }340}341342#[builtin]343fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {344 use Either4::*;345 Ok(match x {346 A(x) => x.chars().count(),347 B(x) => x.len(),348 C(x) => x.len(),349 D(f) => f.params_len(),350 })351}352353#[builtin]354const fn builtin_codepoint(str: char) -> Result<u32> {355 Ok(str as u32)356}357358#[builtin]359fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {360 Ok(str.chars().skip(from as usize).take(len as usize).collect())361}362363#[builtin(fields(364 settings: Rc<RefCell<Settings>>,365))]366fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {367 let ctx = s.create_default_context(extvar_source(&x, ""));368 Ok(Any(this369 .settings370 .borrow()371 .ext_vars372 .get(&x)373 .cloned()374 .ok_or(UndefinedExternalVariable(x))?375 .evaluate_arg(s.clone(), ctx, true)?376 .evaluate(s)?))377}378379#[builtin(fields(380 settings: Rc<RefCell<Settings>>,381))]382fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {383 Ok(Any(this384 .settings385 .borrow()386 .ext_natives387 .get(&name)388 .cloned()389 .map_or(Val::Null, |v| {390 Val::Func(FuncVal::Builtin(v.clone()))391 })))392}393394#[builtin]395fn builtin_char(n: u32) -> Result<char> {396 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)397}398399#[builtin(fields(400 settings: Rc<RefCell<Settings>>,401))]402fn builtin_trace(403 this: &builtin_trace,404 s: State,405 loc: CallLocation,406 str: IStr,407 rest: Thunk<Val>,408) -> Result<Any> {409 this.settings410 .borrow()411 .trace_printer412 .print_trace(s.clone(), loc, str);413 Ok(Any(rest.evaluate(s)?))414}415416#[builtin]417fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {418 Ok(str.replace(&from as &str, &to as &str))419}420421#[builtin]422fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {423 use Either2::*;424 Ok(VecVal(Cc::new(match maxsplits {425 A(n) => str426 .splitn(n + 1, &c as &str)427 .map(|s| Val::Str(s.into()))428 .collect(),429 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),430 })))431}432433#[builtin]434fn builtin_ascii_upper(str: IStr) -> Result<String> {435 Ok(str.to_ascii_uppercase())436}437438#[builtin]439fn builtin_ascii_lower(str: IStr) -> Result<String> {440 Ok(str.to_ascii_lowercase())441}442443#[builtin]444fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {445 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {446 return Ok(ArrValue::empty());447 }448449 let str = str.as_str();450 let pat = pat.as_bytes();451 let strb = str.as_bytes();452453 let max_pos = str.len() - pat.len();454455 let mut out: Vec<Val> = Vec::new();456 for (ch_idx, (i, _)) in str457 .char_indices()458 .take_while(|(i, _)| i <= &max_pos)459 .enumerate()460 {461 if &strb[i..i + pat.len()] == pat {462 out.push(Val::Num(ch_idx as f64))463 }464 }465 Ok(out.into())466}467468#[allow(clippy::comparison_chain)]469#[builtin]470fn builtin_starts_with(471 s: State,472 a: Either![IStr, ArrValue],473 b: Either![IStr, ArrValue],474) -> Result<bool> {475 Ok(match (a, b) {476 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),477 (Either2::B(a), Either2::B(b)) => {478 if b.len() > a.len() {479 return Ok(false);480 } else if b.len() == a.len() {481 return equals(s, &Val::Arr(a), &Val::Arr(b));482 } else {483 for (a, b) in a484 .slice(None, Some(b.len()), None)485 .iter(s.clone())486 .zip(b.iter(s.clone()))487 {488 let a = a?;489 let b = b?;490 if !equals(s.clone(), &a, &b)? {491 return Ok(false);492 }493 }494 true495 }496 }497 _ => throw_runtime!("both arguments should be of the same type"),498 })499}500501#[allow(clippy::comparison_chain)]502#[builtin]503fn builtin_ends_with(504 s: State,505 a: Either![IStr, ArrValue],506 b: Either![IStr, ArrValue],507) -> Result<bool> {508 Ok(match (a, b) {509 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),510 (Either2::B(a), Either2::B(b)) => {511 if b.len() > a.len() {512 return Ok(false);513 } else if b.len() == a.len() {514 return equals(s, &Val::Arr(a), &Val::Arr(b));515 } else {516 let a_len = a.len();517 for (a, b) in a518 .slice(Some(a_len - b.len()), None, None)519 .iter(s.clone())520 .zip(b.iter(s.clone()))521 {522 let a = a?;523 let b = b?;524 if !equals(s.clone(), &a, &b)? {525 return Ok(false);526 }527 }528 true529 }530 }531 _ => throw_runtime!("both arguments should be of the same type"),532 })533}534535pub trait StateExt {536 /// This method was previously implemented in jrsonnet-evaluator itself537 fn with_stdlib(&self);538 fn add_global(&self, name: IStr, value: Thunk<Val>);539}540541impl StateExt for State {542 fn with_stdlib(&self) {543 let initializer = ContextInitializer::new(self.clone());544 self.settings_mut().context_initializer = Box::new(initializer)545 }546 fn add_global(&self, name: IStr, value: Thunk<Val>) {547 // Safety:548 unsafe { self.settings().context_initializer.as_any() }549 .downcast_ref::<ContextInitializer>()550 .expect("not standard context initializer")551 .settings_mut()552 .globals553 .insert(name, value);554 }555}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()),