-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathlib.rs
More file actions
421 lines (403 loc) · 11.7 KB
/
lib.rs
File metadata and controls
421 lines (403 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashMap,
rc::Rc,
};
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
function::{CallLocation, FuncVal, TlaArg},
tb,
trace::PathResolver,
ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
};
use jrsonnet_gcmodule::Trace;
use jrsonnet_parser::Source;
mod expr;
mod types;
pub use types::*;
mod arrays;
pub use arrays::*;
mod math;
pub use math::*;
mod operator;
pub use operator::*;
mod sort;
pub use sort::*;
mod hash;
pub use hash::*;
mod encoding;
pub use encoding::*;
mod objects;
pub use objects::*;
mod manifest;
pub use manifest::*;
mod parse;
pub use parse::*;
mod strings;
pub use strings::*;
mod misc;
pub use misc::*;
mod sets;
pub use sets::*;
mod compat;
pub use compat::*;
#[cfg(feature = "exp-regex")]
mod regex;
#[cfg(feature = "exp-regex")]
pub use crate::regex::*;
pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
let mut builder = ObjValueBuilder::new();
let expr = expr::stdlib_expr();
let eval = jrsonnet_evaluator::evaluate(ContextBuilder::dangerous_empty_state().build(), &expr)
.expect("stdlib.jsonnet should have no errors")
.as_obj()
.expect("stdlib.jsonnet should evaluate to object");
builder.with_super(eval);
for (name, builtin) in [
// Types
("type", builtin_type::INST),
("isString", builtin_is_string::INST),
("isNumber", builtin_is_number::INST),
("isBoolean", builtin_is_boolean::INST),
("isObject", builtin_is_object::INST),
("isArray", builtin_is_array::INST),
("isFunction", builtin_is_function::INST),
// Arrays
("makeArray", builtin_make_array::INST),
("repeat", builtin_repeat::INST),
("slice", builtin_slice::INST),
("map", builtin_map::INST),
("flatMap", builtin_flatmap::INST),
("filter", builtin_filter::INST),
("foldl", builtin_foldl::INST),
("foldr", builtin_foldr::INST),
("range", builtin_range::INST),
("join", builtin_join::INST),
("reverse", builtin_reverse::INST),
("any", builtin_any::INST),
("all", builtin_all::INST),
("member", builtin_member::INST),
("contains", builtin_contains::INST),
("count", builtin_count::INST),
("avg", builtin_avg::INST),
("removeAt", builtin_remove_at::INST),
("remove", builtin_remove::INST),
("flattenArrays", builtin_flatten_arrays::INST),
("filterMap", builtin_filter_map::INST),
// Math
("abs", builtin_abs::INST),
("sign", builtin_sign::INST),
("max", builtin_max::INST),
("min", builtin_min::INST),
("sum", builtin_sum::INST),
("modulo", builtin_modulo::INST),
("floor", builtin_floor::INST),
("ceil", builtin_ceil::INST),
("log", builtin_log::INST),
("pow", builtin_pow::INST),
("sqrt", builtin_sqrt::INST),
("sin", builtin_sin::INST),
("cos", builtin_cos::INST),
("tan", builtin_tan::INST),
("asin", builtin_asin::INST),
("acos", builtin_acos::INST),
("atan", builtin_atan::INST),
("atan2", builtin_atan2::INST),
("exp", builtin_exp::INST),
("mantissa", builtin_mantissa::INST),
("exponent", builtin_exponent::INST),
("round", builtin_round::INST),
("isEven", builtin_is_even::INST),
("isOdd", builtin_is_odd::INST),
("isInteger", builtin_is_integer::INST),
("isDecimal", builtin_is_decimal::INST),
// Operator
("mod", builtin_mod::INST),
("primitiveEquals", builtin_primitive_equals::INST),
("equals", builtin_equals::INST),
("xor", builtin_xor::INST),
("xnor", builtin_xnor::INST),
("format", builtin_format::INST),
// Sort
("sort", builtin_sort::INST),
("uniq", builtin_uniq::INST),
("set", builtin_set::INST),
("minArray", builtin_min_array::INST),
("maxArray", builtin_max_array::INST),
// Hash
("md5", builtin_md5::INST),
("sha1", builtin_sha1::INST),
("sha256", builtin_sha256::INST),
("sha512", builtin_sha512::INST),
("sha3", builtin_sha3::INST),
// Encoding
("encodeUTF8", builtin_encode_utf8::INST),
("decodeUTF8", builtin_decode_utf8::INST),
("base64", builtin_base64::INST),
("base64Decode", builtin_base64_decode::INST),
("base64DecodeBytes", builtin_base64_decode_bytes::INST),
// Objects
("objectFieldsEx", builtin_object_fields_ex::INST),
("objectFields", builtin_object_fields::INST),
("objectFieldsAll", builtin_object_fields_all::INST),
("objectValues", builtin_object_values::INST),
("objectValuesAll", builtin_object_values_all::INST),
("objectKeysValues", builtin_object_keys_values::INST),
("objectKeysValuesAll", builtin_object_keys_values_all::INST),
("objectHasEx", builtin_object_has_ex::INST),
("objectHas", builtin_object_has::INST),
("objectHasAll", builtin_object_has_all::INST),
("objectRemoveKey", builtin_object_remove_key::INST),
// Manifest
("escapeStringJson", builtin_escape_string_json::INST),
("manifestJsonEx", builtin_manifest_json_ex::INST),
("manifestYamlDoc", builtin_manifest_yaml_doc::INST),
("manifestTomlEx", builtin_manifest_toml_ex::INST),
("toString", builtin_to_string::INST),
// Parsing
("parseJson", builtin_parse_json::INST),
("parseYaml", builtin_parse_yaml::INST),
// Strings
("codepoint", builtin_codepoint::INST),
("substr", builtin_substr::INST),
("char", builtin_char::INST),
("strReplace", builtin_str_replace::INST),
("isEmpty", builtin_is_empty::INST),
("equalsIgnoreCase", builtin_equals_ignore_case::INST),
("splitLimit", builtin_splitlimit::INST),
("asciiUpper", builtin_ascii_upper::INST),
("asciiLower", builtin_ascii_lower::INST),
("findSubstr", builtin_find_substr::INST),
("parseInt", builtin_parse_int::INST),
#[cfg(feature = "exp-bigint")]
("bigint", builtin_bigint::INST),
("parseOctal", builtin_parse_octal::INST),
("parseHex", builtin_parse_hex::INST),
("stringChars", builtin_string_chars::INST),
// Misc
("length", builtin_length::INST),
("startsWith", builtin_starts_with::INST),
("endsWith", builtin_ends_with::INST),
// Sets
("setMember", builtin_set_member::INST),
("setInter", builtin_set_inter::INST),
("setDiff", builtin_set_diff::INST),
("setUnion", builtin_set_union::INST),
// Regex
#[cfg(feature = "exp-regex")]
("regexQuoteMeta", builtin_regex_quote_meta::INST),
// Compat
("__compare", builtin___compare::INST),
]
.iter()
.cloned()
{
builder.method(name, builtin);
}
builder.method(
"extVar",
builtin_ext_var {
settings: settings.clone(),
},
);
builder.method(
"native",
builtin_native {
settings: settings.clone(),
},
);
builder.method("trace", builtin_trace { settings });
builder.method("id", FuncVal::Id);
#[cfg(feature = "exp-regex")]
{
// Regex
let regex_cache = RegexCache::default();
builder.method(
"regexFullMatch",
builtin_regex_full_match {
cache: regex_cache.clone(),
},
);
builder.method(
"regexPartialMatch",
builtin_regex_partial_match {
cache: regex_cache.clone(),
},
);
builder.method(
"regexReplace",
builtin_regex_replace {
cache: regex_cache.clone(),
},
);
builder.method(
"regexGlobalReplace",
builtin_regex_global_replace {
cache: regex_cache.clone(),
},
);
};
builder.build()
}
pub trait TracePrinter {
fn print_trace(&self, loc: CallLocation, value: IStr);
}
pub struct StdTracePrinter {
resolver: PathResolver,
}
impl StdTracePrinter {
pub fn new(resolver: PathResolver) -> Self {
Self { resolver }
}
}
impl TracePrinter for StdTracePrinter {
fn print_trace(&self, loc: CallLocation, value: IStr) {
eprint!("TRACE:");
if let Some(loc) = loc.0 {
let locs = loc.0.map_source_locations(&[loc.1]);
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}");
}
}
pub struct Settings {
/// Used for `std.extVar`
pub ext_vars: HashMap<IStr, TlaArg>,
/// Used for `std.native`
pub ext_natives: HashMap<IStr, FuncVal>,
/// Used for `std.trace`
pub trace_printer: Box<dyn TracePrinter>,
/// Used for `std.thisFile`
pub path_resolver: PathResolver,
}
fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
let source_name = format!("<extvar:{name}>");
Source::new_virtual(source_name.into(), code.into())
}
#[derive(Trace, Clone)]
pub struct ContextInitializer {
/// When we don't need to support legacy-this-file, we can reuse same context for all files
#[cfg(not(feature = "legacy-this-file"))]
context: jrsonnet_evaluator::Context,
/// For `populate`
#[cfg(not(feature = "legacy-this-file"))]
stdlib_thunk: Thunk<Val>,
/// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
#[cfg(feature = "legacy-this-file")]
stdlib_obj: ObjValue,
settings: Rc<RefCell<Settings>>,
}
impl ContextInitializer {
pub fn new(_s: State, resolver: PathResolver) -> Self {
let settings = Settings {
ext_vars: Default::default(),
ext_natives: Default::default(),
trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
path_resolver: resolver,
};
let settings = Rc::new(RefCell::new(settings));
let stdlib_obj = stdlib_uncached(settings.clone());
#[cfg(not(feature = "legacy-this-file"))]
let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));
Self {
#[cfg(not(feature = "legacy-this-file"))]
context: {
let mut context = ContextBuilder::with_capacity(_s, 1);
context.bind("std", stdlib_thunk.clone());
context.build()
},
#[cfg(not(feature = "legacy-this-file"))]
stdlib_thunk,
#[cfg(feature = "legacy-this-file")]
stdlib_obj,
settings,
}
}
pub fn settings(&self) -> Ref<Settings> {
self.settings.borrow()
}
pub fn settings_mut(&self) -> RefMut<Settings> {
self.settings.borrow_mut()
}
pub fn add_ext_var(&self, name: IStr, value: Val) {
self.settings_mut()
.ext_vars
.insert(name, TlaArg::Val(value));
}
pub fn add_ext_str(&self, name: IStr, value: IStr) {
self.settings_mut()
.ext_vars
.insert(name, TlaArg::String(value));
}
pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
let code = code.into();
let source = extvar_source(name, code.clone());
let parsed = jrsonnet_parser::parse(
&code,
&jrsonnet_parser::ParserSettings {
source: source.clone(),
},
)
.map_err(|e| ImportSyntaxError {
path: source,
error: Box::new(e),
})?;
// self.data_mut().volatile_files.insert(source_name, code);
self.settings_mut()
.ext_vars
.insert(name.into(), TlaArg::Code(parsed));
Ok(())
}
pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {
self.settings_mut()
.ext_natives
.insert(name.into(), cb.into());
}
}
impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
fn reserve_vars(&self) -> usize {
1
}
#[cfg(not(feature = "legacy-this-file"))]
fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
self.context.clone()
}
#[cfg(not(feature = "legacy-this-file"))]
fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
builder.bind("std", self.stdlib_thunk.clone());
}
#[cfg(feature = "legacy-this-file")]
fn populate(&self, source: Source, builder: &mut ContextBuilder) {
use jrsonnet_evaluator::val::StrValue;
let mut std = ObjValueBuilder::new();
std.with_super(self.stdlib_obj.clone());
std.field("thisFile")
.hide()
.value(match source.source_path().path() {
Some(p) => self.settings().path_resolver.resolve(p),
None => source.source_path().to_string(),
});
let stdlib_with_this_file = std.build();
builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
pub trait StateExt {
/// This method was previously implemented in jrsonnet-evaluator itself
fn with_stdlib(&self);
}
impl StateExt for State {
fn with_stdlib(&self) {
let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
self.settings_mut().context_initializer = tb!(initializer)
}
}