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

difftreelog

Merge branch 'master' into gc-v2

Yaroslav Bolyukin2021-06-14parents: #a501dd9 #ff2926d.patch.diff
in: master

17 files changed

modifiedCargo.lockdiffbeforeafterboth
242dependencies = [242dependencies = [
243 "gc",243 "gc",
244 "jrsonnet-evaluator",244 "jrsonnet-evaluator",
245 "jrsonnet-interner",
246 "jrsonnet-parser",245 "jrsonnet-parser",
247]246]
248247
modifiedbindings/jsonnet/Cargo.tomldiffbeforeafterboth
8publish = false8publish = false
99
10[dependencies]10[dependencies]
11jrsonnet-interner = { path = "../../crates/jrsonnet-interner", version = "0.3.8" }
12jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.8" }11jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.8" }
13jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.8" }12jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.8" }
14gc = { version = "0.4.1", features = ["derive"] }13gc = { version = "0.4.1", features = ["derive"] }
modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
22
3use jrsonnet_evaluator::{3use jrsonnet_evaluator::{
4 error::{Error::*, Result},4 error::{Error::*, Result},
5 throw, EvaluationState, ImportResolver,5 throw, EvaluationState, IStr, ImportResolver,
6};6};
7use jrsonnet_interner::IStr;
8use std::{7use std::{
9 any::Any,8 any::Any,
10 cell::RefCell,9 cell::RefCell,
13 fs::File,12 fs::File,
14 io::Read,13 io::Read,
15 os::raw::{c_char, c_int},14 os::raw::{c_char, c_int},
16 path::PathBuf,15 path::{Path, PathBuf},
17 ptr::null_mut,16 ptr::null_mut,
18 rc::Rc,17 rc::Rc,
19};18};
34 out: RefCell<HashMap<PathBuf, IStr>>,33 out: RefCell<HashMap<PathBuf, IStr>>,
35}34}
36impl ImportResolver for CallbackImportResolver {35impl ImportResolver for CallbackImportResolver {
37 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {36 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
38 let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();37 let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
39 let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw();38 let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw();
40 let found_here: *mut c_char = null_mut();39 let found_here: *mut c_char = null_mut();
74 unsafe { CString::from_raw(result_ptr) };73 unsafe { CString::from_raw(result_ptr) };
75 }74 }
7675
77 Ok(Rc::new(found_here_buf))76 Ok(found_here_buf.into())
78 }77 }
79 fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {78 fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
80 Ok(self.out.borrow().get(resolved).unwrap().clone())79 Ok(self.out.borrow().get(resolved).unwrap().clone())
81 }80 }
82 unsafe fn as_any(&self) -> &dyn Any {81 unsafe fn as_any(&self) -> &dyn Any {
109 }108 }
110}109}
111impl ImportResolver for NativeImportResolver {110impl ImportResolver for NativeImportResolver {
112 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {111 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
113 let mut new_path = from.clone();112 let mut new_path = from.to_owned();
114 new_path.push(path);113 new_path.push(path);
115 if new_path.exists() {114 if new_path.exists() {
116 Ok(Rc::new(new_path))115 Ok(new_path.into())
117 } else {116 } else {
118 for library_path in self.library_paths.borrow().iter() {117 for library_path in self.library_paths.borrow().iter() {
119 let mut cloned = library_path.clone();118 let mut cloned = library_path.clone();
120 cloned.push(path);119 cloned.push(path);
121 if cloned.exists() {120 if cloned.exists() {
122 return Ok(Rc::new(cloned));121 return Ok(cloned.into());
123 }122 }
124 }123 }
125 throw!(ImportFileNotFound(from.clone(), path.clone()))124 throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
126 }125 }
127 }126 }
128 fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {127 fn load_file_contents(&self, id: &Path) -> Result<IStr> {
129 let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;128 let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
130 let mut out = String::new();129 let mut out = String::new();
131 file.read_to_string(&mut out)130 file.read_to_string(&mut out)
132 .map_err(|_e| ImportBadFileUtf8(id.clone()))?;131 .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
133 Ok(out.into())132 Ok(out.into())
134 }133 }
135 unsafe fn as_any(&self) -> &dyn Any {134 unsafe fn as_any(&self) -> &dyn Any {
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
9pub mod vars_tlas;9pub mod vars_tlas;
1010
11use import::NativeImportResolver;11use import::NativeImportResolver;
12use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Val};12use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
13use jrsonnet_interner::IStr;
14use std::{13use std::{
15 alloc::Layout,14 alloc::Layout,
16 ffi::{CStr, CString},15 ffi::{CStr, CString},
17 os::raw::{c_char, c_double, c_int, c_uint},16 os::raw::{c_char, c_double, c_int, c_uint},
18 path::PathBuf,17 path::PathBuf,
19 rc::Rc,
20};18};
2119
22/// WASM stub20/// WASM stub
145 let snippet = CStr::from_ptr(snippet);143 let snippet = CStr::from_ptr(snippet);
146 match vm144 match vm
147 .evaluate_snippet_raw(145 .evaluate_snippet_raw(
148 Rc::new(PathBuf::from(filename.to_str().unwrap())),146 PathBuf::from(filename.to_str().unwrap()).into(),
149 snippet.to_str().unwrap().into(),147 snippet.to_str().unwrap().into(),
150 )148 )
151 .and_then(|v| vm.with_tla(v))149 .and_then(|v| vm.with_tla(v))
221 let snippet = CStr::from_ptr(snippet);219 let snippet = CStr::from_ptr(snippet);
222 match vm220 match vm
223 .evaluate_snippet_raw(221 .evaluate_snippet_raw(
224 Rc::new(PathBuf::from(filename.to_str().unwrap())),222 PathBuf::from(filename.to_str().unwrap()).into(),
225 snippet.to_str().unwrap().into(),223 snippet.to_str().unwrap().into(),
226 )224 )
227 .and_then(|v| vm.with_tla(v))225 .and_then(|v| vm.with_tla(v))
295 let snippet = CStr::from_ptr(snippet);293 let snippet = CStr::from_ptr(snippet);
296 match vm294 match vm
297 .evaluate_snippet_raw(295 .evaluate_snippet_raw(
298 Rc::new(PathBuf::from(filename.to_str().unwrap())),296 PathBuf::from(filename.to_str().unwrap()).into(),
299 snippet.to_str().unwrap().into(),297 snippet.to_str().unwrap().into(),
300 )298 )
301 .and_then(|v| vm.with_tla(v))299 .and_then(|v| vm.with_tla(v))
modifiedbindings/jsonnet/src/native.rsdiffbeforeafterboth
8use std::{8use std::{
9 ffi::{c_void, CStr},9 ffi::{c_void, CStr},
10 os::raw::{c_char, c_int},10 os::raw::{c_char, c_int},
11 path::PathBuf,11 path::Path,
12 rc::Rc,12 rc::Rc,
13};13};
1414
27 unsafe_empty_trace!();27 unsafe_empty_trace!();
28}28}
29impl NativeCallbackHandler for JsonnetNativeCallbackHandler {29impl NativeCallbackHandler for JsonnetNativeCallbackHandler {
30 fn call(&self, _from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val, LocError> {30 fn call(&self, _from: Option<Rc<Path>>, args: &[Val]) -> Result<Val, LocError> {
31 let mut n_args = Vec::new();31 let mut n_args = Vec::new();
32 for a in args {32 for a in args {
33 n_args.push(Some(Box::new(a.clone())));33 n_args.push(Some(Box::new(a.clone())));
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
6 io::Read,6 io::Read,
7 io::Write,7 io::Write,
8 path::PathBuf,8 path::PathBuf,
9 rc::Rc,
10 str::FromStr,9 str::FromStr,
11};10};
1211
134 let val = if opts.input.exec {133 let val = if opts.input.exec {
135 state.set_manifest_format(ManifestFormat::ToString);134 state.set_manifest_format(ManifestFormat::ToString);
136 state.evaluate_snippet_raw(135 state.evaluate_snippet_raw(
137 Rc::new(PathBuf::from("args")),136 PathBuf::from("args").into(),
138 (&opts.input.input as &str).into(),137 (&opts.input.input as &str).into(),
139 )?138 )?
140 } else if opts.input.input == "-" {139 } else if opts.input.input == "-" {
141 let mut input = Vec::new();140 let mut input = Vec::new();
142 std::io::stdin().read_to_end(&mut input)?;141 std::io::stdin().read_to_end(&mut input)?;
143 let input_str = std::str::from_utf8(&input)?.into();142 let input_str = std::str::from_utf8(&input)?.into();
144 state.evaluate_snippet_raw(Rc::new(PathBuf::from("<stdin>")), input_str)?143 state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?
145 } else {144 } else {
146 state.evaluate_file_raw(&PathBuf::from(opts.input.input))?145 state.evaluate_file_raw(&PathBuf::from(opts.input.input))?
147 };146 };
modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
15 let parsed = parse(15 let parsed = parse(
16 STDLIB_STR,16 STDLIB_STR,
17 &ParserSettings {17 &ParserSettings {
18 file_name: Rc::new(PathBuf::from("std.jsonnet")),18 file_name: PathBuf::from("std.jsonnet").into(),
19 loc_data: true,19 loc_data: true,
20 },20 },
21 )21 )
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
177 0, s: ty!(string) => Val::Str;177 0, s: ty!(string) => Val::Str;
178 ], {178 ], {
179 let state = EvaluationState::default();179 let state = EvaluationState::default();
180 let path = Rc::new(PathBuf::from("std.parseJson"));180 let path = PathBuf::from("std.parseJson").into();
181 state.evaluate_snippet_raw(path ,s)181 state.evaluate_snippet_raw(path ,s)
182 })182 })
183}183}
modifiedcrates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth
1use jrsonnet_parser::{LocExpr, ParserSettings};1use jrsonnet_parser::{LocExpr, ParserSettings};
2use std::{path::PathBuf, rc::Rc};2use std::path::PathBuf;
33
4thread_local! {4thread_local! {
5 /// To avoid parsing again when issued from the same thread5 /// To avoid parsing again when issued from the same thread
25 jrsonnet_stdlib::STDLIB_STR,25 jrsonnet_stdlib::STDLIB_STR,
26 &ParserSettings {26 &ParserSettings {
27 loc_data: true,27 loc_data: true,
28 file_name: Rc::new(PathBuf::from("std.jsonnet")),28 file_name: PathBuf::from("std.jsonnet").into(),
29 },29 },
30 )30 )
31 .unwrap()31 .unwrap()
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
8use jrsonnet_types::ValType;8use jrsonnet_types::ValType;
9use std::{path::PathBuf, rc::Rc};9use std::{
10 path::{Path, PathBuf},
11 rc::Rc,
12};
10use thiserror::Error;13use thiserror::Error;
1114
87 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())90 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
88 )]91 )]
89 ImportSyntaxError {92 ImportSyntaxError {
90 path: Rc<PathBuf>,93 path: Rc<Path>,
91 source_code: IStr,94 source_code: IStr,
92 #[unsafe_ignore_trace]95 #[unsafe_ignore_trace]
93 error: Box<jrsonnet_parser::ParseError>,96 error: Box<jrsonnet_parser::ParseError>,
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
12};12};
13use jrsonnet_types::ValType;13use jrsonnet_types::ValType;
14use rustc_hash::{FxHashMap, FxHasher};14use rustc_hash::{FxHashMap, FxHasher};
15use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};15use std::{collections::HashMap, hash::BuildHasherDefault};
1616
17pub fn evaluate_binding_in_future(17pub fn evaluate_binding_in_future(
18 b: &BindSpec,18 b: &BindSpec,
843 }843 }
844 }844 }
845 Import(path) => {845 Import(path) => {
846 let mut tmp = loc846 let tmp = loc
847 .clone()847 .clone()
848 .expect("imports cannot be used without loc_data")848 .expect("imports cannot be used without loc_data")
849 .0;849 .0;
850 let import_location = Rc::make_mut(&mut tmp);850 let mut import_location = tmp.to_path_buf();
851 import_location.pop();851 import_location.pop();
852 push(852 push(
853 loc.as_ref(),853 loc.as_ref(),
854 || format!("import {:?}", path),854 || format!("import {:?}", path),
855 || with_state(|s| s.import_file(import_location, path)),855 || with_state(|s| s.import_file(&import_location, path)),
856 )?856 )?
857 }857 }
858 ImportStr(path) => {858 ImportStr(path) => {
859 let mut tmp = loc859 let tmp = loc
860 .clone()860 .clone()
861 .expect("imports cannot be used without loc_data")861 .expect("imports cannot be used without loc_data")
862 .0;862 .0;
863 let import_location = Rc::make_mut(&mut tmp);863 let mut import_location = tmp.to_path_buf();
864 import_location.pop();864 import_location.pop();
865 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)865 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
866 }866 }
867 })867 })
868}868}
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
10 any::Any,
11 cell::RefCell,
12 collections::HashMap,
13 path::{Path, PathBuf},
14 rc::Rc,
15};
1016
11/// Implements file resolution logic for `import` and `importStr`17/// Implements file resolution logic for `import` and `importStr`
12pub trait ImportResolver {18pub trait ImportResolver {
13 /// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond19 /// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
14 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`20 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
15 /// where `${vendor}` is a library path.21 /// where `${vendor}` is a library path.
16 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;22 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
1723
18 /// Reads file from filesystem, should be used only with path received from `resolve_file`24 /// Reads file from filesystem, should be used only with path received from `resolve_file`
19 fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr>;25 fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
2026
21 /// # Safety27 /// # Safety
22 ///28 ///
29/// Dummy resolver, can't resolve/load any file35/// Dummy resolver, can't resolve/load any file
30pub struct DummyImportResolver;36pub struct DummyImportResolver;
31impl ImportResolver for DummyImportResolver {37impl ImportResolver for DummyImportResolver {
32 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {38 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
33 throw!(ImportNotSupported(from.clone(), path.clone()))39 throw!(ImportNotSupported(from.into(), path.into()))
34 }40 }
3541
36 fn load_file_contents(&self, _resolved: &PathBuf) -> Result<IStr> {42 fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
37 // Can be only caused by library direct consumer, not by supplied jsonnet43 // Can be only caused by library direct consumer, not by supplied jsonnet
38 panic!("dummy resolver can't load any file")44 panic!("dummy resolver can't load any file")
39 }45 }
57 pub library_paths: Vec<PathBuf>,63 pub library_paths: Vec<PathBuf>,
58}64}
59impl ImportResolver for FileImportResolver {65impl ImportResolver for FileImportResolver {
60 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {66 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
61 let mut new_path = from.clone();67 let mut direct = from.to_path_buf();
62 new_path.push(path);68 direct.push(path);
63 if new_path.exists() {69 if direct.exists() {
64 Ok(Rc::new(new_path))70 Ok(direct.into())
65 } else {71 } else {
66 for library_path in self.library_paths.iter() {72 for library_path in self.library_paths.iter() {
67 let mut cloned = library_path.clone();73 let mut cloned = library_path.clone();
68 cloned.push(path);74 cloned.push(path);
69 if cloned.exists() {75 if cloned.exists() {
70 return Ok(Rc::new(cloned));76 return Ok(cloned.into());
71 }77 }
72 }78 }
73 throw!(ImportFileNotFound(from.clone(), path.clone()))79 throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
74 }80 }
75 }81 }
76 fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {82 fn load_file_contents(&self, id: &Path) -> Result<IStr> {
77 let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;83 let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
78 let mut out = String::new();84 let mut out = String::new();
79 file.read_to_string(&mut out)85 file.read_to_string(&mut out)
80 .map_err(|_e| ImportBadFileUtf8(id.clone()))?;86 .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
81 Ok(out.into())87 Ok(out.into())
82 }88 }
83 unsafe fn as_any(&self) -> &dyn Any {89 unsafe fn as_any(&self) -> &dyn Any {
8995
90/// Caches results of the underlying resolver96/// Caches results of the underlying resolver
91pub struct CachingImportResolver {97pub struct CachingImportResolver {
92 resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,98 resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
93 loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,99 loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
94 inner: Box<dyn ImportResolver>,100 inner: Box<dyn ImportResolver>,
95}101}
96impl ImportResolver for CachingImportResolver {102impl ImportResolver for CachingImportResolver {
97 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {103 fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
98 self.resolution_cache104 self.resolution_cache
99 .borrow_mut()105 .borrow_mut()
100 .entry((from.clone(), path.clone()))106 .entry((from.to_owned(), path.to_owned()))
101 .or_insert_with(|| self.inner.resolve_file(from, path))107 .or_insert_with(|| self.inner.resolve_file(from, path))
102 .clone()108 .clone()
103 }109 }
104110
105 fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {111 fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
106 self.loading_cache112 self.loading_cache
107 .borrow_mut()113 .borrow_mut()
108 .entry(resolved.clone())114 .entry(resolved.to_owned())
109 .or_insert_with(|| self.inner.load_file_contents(resolved))115 .or_insert_with(|| self.inner.load_file_contents(resolved))
110 .clone()116 .clone()
111 }117 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
27pub use function::parse_function_call;27pub use function::parse_function_call;
28use gc::{Finalize, Gc, Trace};28use gc::{Finalize, Gc, Trace};
29pub use import::*;29pub use import::*;
30use jrsonnet_interner::IStr;30pub use jrsonnet_interner::IStr;
31use jrsonnet_parser::*;31use jrsonnet_parser::*;
32use native::NativeCallback;32use native::NativeCallback;
33pub use obj::*;33pub use obj::*;
37 collections::HashMap,37 collections::HashMap,
38 fmt::Debug,38 fmt::Debug,
39 hash::BuildHasherDefault,39 hash::BuildHasherDefault,
40 path::PathBuf,40 path::{Path, PathBuf},
41 rc::Rc,41 rc::Rc,
42};42};
43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
110 /// Used for stack overflow detection, stacktrace is populated on unwind110 /// Used for stack overflow detection, stacktrace is populated on unwind
111 stack_depth: usize,111 stack_depth: usize,
112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
113 files: HashMap<Rc<PathBuf>, FileData>,113 files: HashMap<Rc<Path>, FileData>,
114 str_files: HashMap<Rc<PathBuf>, IStr>,114 str_files: HashMap<Rc<Path>, IStr>,
115}115}
116116
117pub struct FileData {117pub struct FileData {
157157
158impl EvaluationState {158impl EvaluationState {
159 /// Parses and adds file as loaded159 /// Parses and adds file as loaded
160 pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {160 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {
161 self.add_parsed_file(161 self.add_parsed_file(
162 path.clone(),162 path.clone(),
163 source_code.clone(),163 source_code.clone(),
170 )170 )
171 .map_err(|error| ImportSyntaxError {171 .map_err(|error| ImportSyntaxError {
172 error: Box::new(error),172 error: Box::new(error),
173 path,173 path: path.to_owned(),
174 source_code,174 source_code,
175 })?,175 })?,
176 )?;176 )?;
181 /// Adds file by source code and parsed expr181 /// Adds file by source code and parsed expr
182 pub fn add_parsed_file(182 pub fn add_parsed_file(
183 &self,183 &self,
184 name: Rc<PathBuf>,184 name: Rc<Path>,
185 source_code: IStr,185 source_code: IStr,
186 parsed: LocExpr,186 parsed: LocExpr,
187 ) -> Result<()> {187 ) -> Result<()> {
196196
197 Ok(())197 Ok(())
198 }198 }
199 pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {199 pub fn get_source(&self, name: &Path) -> Option<IStr> {
200 let ro_map = &self.data().files;200 let ro_map = &self.data().files;
201 ro_map.get(name).map(|value| value.source_code.clone())201 ro_map.get(name).map(|value| value.source_code.clone())
202 }202 }
203 pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {203 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {
204 offset_to_location(&self.get_source(file).unwrap(), locs)204 offset_to_location(&self.get_source(file).unwrap(), locs)
205 }205 }
206206
207 pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {207 pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
208 let file_path = self.resolve_file(from, path)?;208 let file_path = self.resolve_file(from, path)?;
209 {209 {
210 let data = self.data();210 let data = self.data();
211 let files = &data.files;211 let files = &data.files;
212 if files.contains_key(&file_path) {212 if files.contains_key(&file_path as &Path) {
213 drop(data);213 drop(data);
214 return self.evaluate_loaded_file_raw(&file_path);214 return self.evaluate_loaded_file_raw(&file_path);
215 }215 }
218 self.add_file(file_path.clone(), contents)?;218 self.add_file(file_path.clone(), contents)?;
219 self.evaluate_loaded_file_raw(&file_path)219 self.evaluate_loaded_file_raw(&file_path)
220 }220 }
221 pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {221 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {
222 let path = self.resolve_file(from, path)?;222 let path = self.resolve_file(from, path)?;
223 if !self.data().str_files.contains_key(&path) {223 if !self.data().str_files.contains_key(&path) {
224 let file_str = self.load_file_contents(&path)?;224 let file_str = self.load_file_contents(&path)?;
227 Ok(self.data().str_files.get(&path).cloned().unwrap())227 Ok(self.data().str_files.get(&path).cloned().unwrap())
228 }228 }
229229
230 fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {230 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {
231 let expr: LocExpr = {231 let expr: LocExpr = {
232 let ro_map = &self.data().files;232 let ro_map = &self.data().files;
233 let value = ro_map233 let value = ro_map
253 /// Adds standard library global variable (std) to this evaluator253 /// Adds standard library global variable (std) to this evaluator
254 pub fn with_stdlib(&self) -> &Self {254 pub fn with_stdlib(&self) -> &Self {
255 use jrsonnet_stdlib::STDLIB_STR;255 use jrsonnet_stdlib::STDLIB_STR;
256 let std_path = Rc::new(PathBuf::from("std.jsonnet"));256 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();
257 self.run_in_state(|| {257 self.run_in_state(|| {
258 self.add_parsed_file(258 self.add_parsed_file(
259 std_path.clone(),259 std_path.clone(),
381381
382/// Raw methods evaluate passed values but don't perform TLA execution382/// Raw methods evaluate passed values but don't perform TLA execution
383impl EvaluationState {383impl EvaluationState {
384 pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {384 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {
385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))
386 }386 }
387 pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {387 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {
388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))
389 }389 }
390 /// Parses and evaluates the given snippet390 /// Parses and evaluates the given snippet
391 pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {391 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {
392 let parsed = parse(392 let parsed = parse(
393 &code,393 &code,
394 &ParserSettings {394 &ParserSettings {
420 }420 }
421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {
422 let value =422 let value =
423 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;423 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;
424 self.add_ext_var(name, value);424 self.add_ext_var(name, value);
425 Ok(())425 Ok(())
426 }426 }
433 }433 }
434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
435 let value =435 let value =
436 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;436 self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
437 self.add_tla(name, value);437 self.add_tla(name, value);
438 Ok(())438 Ok(())
439 }439 }
440440
441 pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {441 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
442 self.settings().import_resolver.resolve_file(from, path)442 self.settings().import_resolver.resolve_file(from, path)
443 }443 }
444 pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {444 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {
445 self.settings().import_resolver.load_file_contents(path)445 self.settings().import_resolver.load_file_contents(path)
446 }446 }
447447
495 use jrsonnet_interner::IStr;495 use jrsonnet_interner::IStr;
496 use jrsonnet_parser::*;496 use jrsonnet_parser::*;
497 use std::{path::PathBuf, rc::Rc};497 use std::{
498 path::{Path, PathBuf},
499 rc::Rc,
500 };
498501
499 #[test]502 #[test]
503 state.run_in_state(|| {506 state.run_in_state(|| {
504 state507 state
505 .push(508 .push(
506 Some(&ExprLocation(509 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
507 Rc::new(PathBuf::from("test1.jsonnet")),
508 10,
509 20,
510 )),
511 || "outer".to_owned(),510 || "outer".to_owned(),
512 || {511 || {
513 state.push(512 state.push(
514 Some(&ExprLocation(513 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
515 Rc::new(PathBuf::from("test2.jsonnet")),
516 30,
517 40,
518 )),
533 assert!(primitive_equals(528 assert!(primitive_equals(
534 &state529 &state
535 .evaluate_snippet_raw(530 .evaluate_snippet_raw(
536 Rc::new(PathBuf::from("raw.jsonnet")),531 PathBuf::from("raw.jsonnet").into(),
537 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()532 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()
538 )533 )
539 .unwrap(),534 .unwrap(),
546 ($str: expr) => {541 ($str: expr) => {
547 EvaluationState::default()542 EvaluationState::default()
548 .with_stdlib()543 .with_stdlib()
549 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())544 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
550 .unwrap()545 .unwrap()
551 };546 };
552 }547 }
556 evaluator.with_stdlib();551 evaluator.with_stdlib();
557 evaluator.run_in_state(|| {552 evaluator.run_in_state(|| {
558 evaluator553 evaluator
559 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())554 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
560 .unwrap()555 .unwrap()
561 .to_json(0)556 .to_json(0)
562 .unwrap()557 .unwrap()
913 "{:?}",908 "{:?}",
914 jrsonnet_parser::parse(909 jrsonnet_parser::parse(
915 "{ x: 1, y: 2 } == { x: 1, y: 2 }",910 "{ x: 1, y: 2 } == { x: 1, y: 2 }",
916 &ParserSettings::default()911 &ParserSettings {
912 file_name: PathBuf::from("equality").into(),
913 loc_data: true,
914 }
917 )915 )
918 );916 );
919 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")917 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")
929 #[derive(gc::Trace, gc::Finalize)]927 #[derive(gc::Trace, gc::Finalize)]
930 struct NativeAdd;928 struct NativeAdd;
931 impl NativeCallbackHandler for NativeAdd {929 impl NativeCallbackHandler for NativeAdd {
932 fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> crate::error::Result<Val> {930 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {
933 assert_eq!(931 assert_eq!(
934 from.unwrap(),932 &from.unwrap() as &Path,
935 Rc::new(PathBuf::from("native_caller.jsonnet"))933 &PathBuf::from("native_caller.jsonnet")
936 );934 );
937 match (&args[0], &args[1]) {935 match (&args[0], &args[1]) {
938 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),936 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),
951 )),949 )),
952 );950 );
953 evaluator.evaluate_snippet_raw(951 evaluator.evaluate_snippet_raw(
954 Rc::new(PathBuf::from("native_caller.jsonnet")),952 PathBuf::from("native_caller.jsonnet").into(),
955 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),953 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),
956 )?;954 )?;
957 Ok(())955 Ok(())
10031001
1004 struct TestImportResolver(IStr);1002 struct TestImportResolver(IStr);
1005 impl crate::import::ImportResolver for TestImportResolver {1003 impl crate::import::ImportResolver for TestImportResolver {
1006 fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {1004 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {
1007 Ok(Rc::new(PathBuf::from("/test")))1005 Ok(PathBuf::from("/test").into())
1008 }1006 }
10091007
1010 fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {1008 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {
1011 Ok(self.0.clone())1009 Ok(self.0.clone())
1012 }1010 }
10131011
10301028
1031 let error = state1029 let error = state
1032 .evaluate_snippet_raw(1030 .evaluate_snippet_raw(
1033 Rc::new(PathBuf::from("issue40.jsonnet")),1031 PathBuf::from("issue40.jsonnet").into(),
1034 r#"1032 r#"
1035 local conf = {1033 local conf = {
1036 n: ""1034 n: ""
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
4use gc::{Finalize, Trace};4use gc::{Finalize, Trace};
5use jrsonnet_parser::ParamsDesc;5use jrsonnet_parser::ParamsDesc;
6use std::fmt::Debug;6use std::fmt::Debug;
7use std::path::PathBuf;7use std::path::Path;
8use std::rc::Rc;8use std::rc::Rc;
99
10pub trait NativeCallbackHandler: Trace {10pub trait NativeCallbackHandler: Trace {
11 fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val>;11 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> Result<Val>;
12}12}
1313
14#[derive(Trace, Finalize)]14#[derive(Trace, Finalize)]
20 pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {20 pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {
21 Self { params, handler }21 Self { params, handler }
22 }22 }
23 pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {23 pub fn call(&self, caller: Option<Rc<Path>>, args: &[Val]) -> Result<Val> {
24 self.handler.call(caller, args)24 self.handler.call(caller, args)
25 }25 }
26}26}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
22
3use crate::{error::Error, EvaluationState, LocError};3use crate::{error::Error, EvaluationState, LocError};
4pub use location::*;4pub use location::*;
5use std::path::PathBuf;5use std::path::{Path, PathBuf};
66
7/// The way paths should be displayed7/// The way paths should be displayed
8pub enum PathResolver {8pub enum PathResolver {
15}15}
1616
17impl PathResolver {17impl PathResolver {
18 pub fn resolve(&self, from: &PathBuf) -> String {18 pub fn resolve(&self, from: &Path) -> String {
19 match self {19 match self {
20 Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),20 Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),
21 Self::Absolute => from.to_string_lossy().into_owned(),21 Self::Absolute => from.to_string_lossy().into_owned(),
255 &self,255 &self,
256 out: &mut dyn std::fmt::Write,256 out: &mut dyn std::fmt::Write,
257 source: &str,257 source: &str,
258 origin: &PathBuf,258 origin: &Path,
259 start: &CodeLocation,259 start: &CodeLocation,
260 end: &CodeLocation,260 end: &CodeLocation,
261 desc: &str,261 desc: &str,
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
7use std::{7use std::{
8 fmt::{Debug, Display},8 fmt::{Debug, Display},
9 ops::Deref,9 ops::Deref,
10 path::PathBuf,10 path::{Path, PathBuf},
11 rc::Rc,11 rc::Rc,
12};12};
1313
405#[cfg_attr(feature = "serialize", derive(Serialize))]405#[cfg_attr(feature = "serialize", derive(Serialize))]
406#[cfg_attr(feature = "deserialize", derive(Deserialize))]406#[cfg_attr(feature = "deserialize", derive(Deserialize))]
407#[derive(Clone, PartialEq)]407#[derive(Clone, PartialEq)]
408pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);408pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
409impl Finalize for ExprLocation {}409impl Finalize for ExprLocation {}
410unsafe impl Trace for ExprLocation {410unsafe impl Trace for ExprLocation {
411 unsafe_empty_trace!();411 unsafe_empty_trace!();
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
22
3use peg::parser;3use peg::parser;
4use std::{path::PathBuf, rc::Rc};4use std::{
5 path::{Path, PathBuf},
6 rc::Rc,
7};
5mod expr;8mod expr;
6pub use expr::*;9pub use expr::*;
7pub use peg;10pub use peg;
811
9#[derive(Default)]
10pub struct ParserSettings {12pub struct ParserSettings {
11 pub loc_data: bool,13 pub loc_data: bool,
12 pub file_name: Rc<PathBuf>,14 pub file_name: Rc<Path>,
13}15}
1416
15parser! {17parser! {
304 use super::{expr::*, parse};306 use super::{expr::*, parse};
305 use crate::ParserSettings;307 use crate::ParserSettings;
306 use std::path::PathBuf;308 use std::path::PathBuf;
307 use std::rc::Rc;
308309
309 macro_rules! parse {310 macro_rules! parse {
310 ($s:expr) => {311 ($s:expr) => {
311 parse(312 parse(
312 $s,313 $s,
313 &ParserSettings {314 &ParserSettings {
314 loc_data: false,315 loc_data: false,
315 file_name: Rc::new(PathBuf::from("/test.jsonnet")),316 file_name: PathBuf::from("/test.jsonnet").into(),
316 },317 },
317 )318 )
318 .unwrap()319 .unwrap()