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
use std::{
	cell::{Ref, RefCell, RefMut},
	collections::HashMap,
	rc::Rc,
};

use jrsonnet_evaluator::{
	error::{ErrorKind::*, Result},
	function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},
	gc::{GcHashMap, TraceBox},
	tb,
	trace::PathResolver,
	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
};
use jrsonnet_gcmodule::{Cc, 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::*;

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),
		("count", builtin_count::INST),
		// Math
		("abs", builtin_abs::INST),
		("sign", builtin_sign::INST),
		("max", builtin_max::INST),
		("min", builtin_min::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),
		("exp", builtin_exp::INST),
		("mantissa", builtin_mantissa::INST),
		("exponent", builtin_exponent::INST),
		// Operator
		("mod", builtin_mod::INST),
		("primitiveEquals", builtin_primitive_equals::INST),
		("equals", builtin_equals::INST),
		("format", builtin_format::INST),
		// Sort
		("sort", builtin_sort::INST),
		// Hash
		("md5", builtin_md5::INST),
		#[cfg(feature = "exp-more-hashes")]
		("sha256", builtin_sha256::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),
		("objectHasEx", builtin_object_has_ex::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),
		// 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),
		("splitLimit", builtin_splitlimit::INST),
		("asciiUpper", builtin_ascii_upper::INST),
		("asciiLower", builtin_ascii_lower::INST),
		("findSubstr", builtin_find_substr::INST),
		("parseInt", builtin_parse_int::INST),
		("parseOctal", builtin_parse_octal::INST),
		("parseHex", builtin_parse_hex::INST),
		// Misc
		("length", builtin_length::INST),
		("startsWith", builtin_starts_with::INST),
		("endsWith", builtin_ends_with::INST),
	]
	.iter()
	.cloned()
	{
		builder
			.member(name.into())
			.hide()
			.value(Val::Func(FuncVal::StaticBuiltin(builtin)))
			.expect("no conflict");
	}

	builder
		.member("extVar".into())
		.hide()
		.value(Val::Func(FuncVal::builtin(builtin_ext_var {
			settings: settings.clone(),
		})))
		.expect("no conflict");
	builder
		.member("native".into())
		.hide()
		.value(Val::Func(FuncVal::builtin(builtin_native {
			settings: settings.clone(),
		})))
		.expect("no conflict");
	builder
		.member("trace".into())
		.hide()
		.value(Val::Func(FuncVal::builtin(builtin_trace { settings })))
		.expect("no conflict");

	builder
		.member("id".into())
		.hide()
		.value(Val::Func(FuncVal::Id))
		.expect("no conflict");

	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, Cc<TraceBox<dyn Builtin>>>,
	/// Helper to add globals without implementing custom ContextInitializer
	pub globals: GcHashMap<IStr, Thunk<Val>>,
	/// 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)]
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: Context,
	// 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(),
			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: {
				let mut context = ContextBuilder::with_capacity(s, 1);
				context.bind(
					"std".into(),
					Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),
				);
				context.build()
			},
			#[cfg(feature = "legacy-this-file")]
			stdlib_obj: stdlib_uncached(settings.clone()),
			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: IStr, cb: impl Builtin) {
		self.settings_mut()
			.ext_natives
			.insert(name, Cc::new(tb!(cb)));
	}
}
impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
	#[cfg(not(feature = "legacy-this-file"))]
	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
		let out = self.context.clone();
		let globals = &self.settings().globals;
		if globals.is_empty() {
			return out;
		}

		let mut out = ContextBuilder::extend(out);
		for (k, v) in globals.iter() {
			out.bind(k.clone(), v.clone());
		}
		out.build()
	}
	#[cfg(feature = "legacy-this-file")]
	fn initialize(&self, s: State, source: Source) -> Context {
		use jrsonnet_evaluator::val::StrValue;

		let mut builder = ObjValueBuilder::new();
		builder.with_super(self.stdlib_obj.clone());
		builder
			.member("thisFile".into())
			.hide()
			.value(Val::Str(StrValue::Flat(
				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();

		let mut context = ContextBuilder::with_capacity(s, 1);
		context.bind(
			"std".into(),
			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
		);
		for (k, v) in self.settings().globals.iter() {
			context.bind(k.clone(), v.clone());
		}
		context.build()
	}
	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);
	fn add_global(&self, name: IStr, value: Thunk<Val>);
}

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)
	}
	fn add_global(&self, name: IStr, value: Thunk<Val>) {
		self.settings()
			.context_initializer
			.as_any()
			.downcast_ref::<ContextInitializer>()
			.expect("not standard context initializer")
			.settings_mut()
			.globals
			.insert(name, value);
	}
}