git.delta.rocks / jrsonnet / refs/commits / 6d7ca685fe74

difftreelog

refactor(stdlib) implement as a standalone crate

Yaroslav Bolyukin2022-07-23parent: #ab6ba99.patch.diff
in: master
New builtins system allows to split standard library to standalone crate

16 files changed

modifiedcrates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/Cargo.toml
+++ b/crates/jrsonnet-stdlib/Cargo.toml
@@ -7,5 +7,46 @@
 edition = "2021"
 
 [features]
+default = []
+# Serializes standard library AST, and deserialize on start, instead of parsing it every run from text
+serialized-stdlib = ["bincode", "jrsonnet-parser/serde"]
+# Enables legacy `std.thisFile` support, at the cost of worse caching
+legacy-this-file = []
+# Add order preservation flag to some functions
+exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
+# Preserve order for files parsed via `std.parseJson`
+# Shame it isn't possible to enable per parse call, instead of globally
+exp-serde-preserve-order = [
+    "serde_json/preserve_order",
+    "jrsonnet-evaluator/exp-serde-preserve-order",
+]
 
 [dependencies]
+jrsonnet-evaluator = { path = "../jrsonnet-evaluator", features = [
+    # std.parseJson parses file via serde, then converts Value to evaluator Val
+    "serde_json",
+], version = "0.4.2" }
+jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }
+jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
+jrsonnet-gcmodule = "0.3.4"
+
+# Used for stdlib AST serialization
+bincode = { version = "1.3", optional = true }
+# Used both for stdlib AST serialization and std.parseJson/std.parseYaml
+serde = "1.0"
+
+# std.md5
+md5 = "0.7.0"
+# std.base64
+base64 = "0.13.0"
+# std.parseJson
+serde_json = "1.0"
+# std.parseYaml, custom library fork is used for C++/golang compatibility
+serde_yaml_with_quirks = "0.8.24"
+
+[build-dependencies]
+jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2", features = [
+    "serde",
+] }
+serde = "1.0"
+bincode = "1.3"
deletedcrates/jrsonnet-stdlib/README.mddiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# jrsonnet-stdlib
-
-Jsonnet standard library packaged as crate
addedcrates/jrsonnet-stdlib/build.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -0,0 +1,21 @@
+use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
+
+use bincode::serialize;
+use jrsonnet_parser::{parse, ParserSettings, Source};
+
+fn main() {
+	let parsed = parse(
+		include_str!("./src/std.jsonnet"),
+		&ParserSettings {
+			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+		},
+	)
+	.expect("parse");
+
+	{
+		let out_dir = env::var("OUT_DIR").unwrap();
+		let dest_path = Path::new(&out_dir).join("stdlib.bincode");
+		let mut f = File::create(&dest_path).unwrap();
+		f.write_all(&serialize(&parsed).unwrap()).unwrap();
+	}
+}
addedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -0,0 +1,216 @@
+use jrsonnet_evaluator::{
+	error::Result,
+	function::{builtin, FuncVal},
+	throw_runtime,
+	typed::{Any, BoundedUsize, Typed, VecVal},
+	val::{equals, ArrValue, IndexableVal},
+	IStr, State, Val,
+};
+use jrsonnet_gcmodule::Cc;
+
+#[builtin]
+pub fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {
+	let mut out = Vec::with_capacity(sz);
+	for i in 0..sz {
+		out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);
+	}
+	Ok(VecVal(Cc::new(out)))
+}
+
+#[builtin]
+pub fn builtin_slice(
+	indexable: IndexableVal,
+	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
+) -> Result<Any> {
+	indexable.slice(index, end, step).map(Val::from).map(Any)
+}
+
+#[builtin]
+pub fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
+	arr.map(s.clone(), |val| {
+		func.evaluate_simple(s.clone(), &(Any(val),))
+	})
+}
+
+#[builtin]
+pub fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {
+	match arr {
+		IndexableVal::Str(str) => {
+			let mut out = String::new();
+			for c in str.chars() {
+				match func.evaluate_simple(s.clone(), &(c.to_string(),))? {
+					Val::Str(o) => out.push_str(&o),
+					Val::Null => continue,
+					_ => throw_runtime!("in std.join all items should be strings"),
+				};
+			}
+			Ok(IndexableVal::Str(out.into()))
+		}
+		IndexableVal::Arr(a) => {
+			let mut out = Vec::new();
+			for el in a.iter(s.clone()) {
+				let el = el?;
+				match func.evaluate_simple(s.clone(), &(Any(el),))? {
+					Val::Arr(o) => {
+						for oe in o.iter(s.clone()) {
+							out.push(oe?);
+						}
+					}
+					Val::Null => continue,
+					_ => throw_runtime!("in std.join all items should be arrays"),
+				};
+			}
+			Ok(IndexableVal::Arr(out.into()))
+		}
+	}
+}
+
+#[builtin]
+pub fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
+	arr.filter(s.clone(), |val| {
+		bool::from_untyped(
+			func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,
+			s.clone(),
+		)
+	})
+}
+
+#[builtin]
+pub fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
+	let mut acc = init.0;
+	for i in arr.iter(s.clone()) {
+		acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;
+	}
+	Ok(Any(acc))
+}
+
+#[builtin]
+pub fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
+	let mut acc = init.0;
+	for i in arr.iter(s.clone()).rev() {
+		acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;
+	}
+	Ok(Any(acc))
+}
+
+#[builtin]
+pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {
+	if to < from {
+		return Ok(ArrValue::new_eager());
+	}
+	Ok(ArrValue::new_range(from, to))
+}
+
+#[builtin]
+pub fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
+	Ok(match sep {
+		IndexableVal::Arr(joiner_items) => {
+			let mut out = Vec::new();
+
+			let mut first = true;
+			for item in arr.iter(s.clone()) {
+				let item = item?.clone();
+				if let Val::Arr(items) = item {
+					if !first {
+						out.reserve(joiner_items.len());
+						// TODO: extend
+						for item in joiner_items.iter(s.clone()) {
+							out.push(item?);
+						}
+					}
+					first = false;
+					out.reserve(items.len());
+					for item in items.iter(s.clone()) {
+						out.push(item?);
+					}
+				} else if matches!(item, Val::Null) {
+					continue;
+				} else {
+					throw_runtime!("in std.join all items should be arrays");
+				}
+			}
+
+			IndexableVal::Arr(out.into())
+		}
+		IndexableVal::Str(sep) => {
+			let mut out = String::new();
+
+			let mut first = true;
+			for item in arr.iter(s) {
+				let item = item?.clone();
+				if let Val::Str(item) = item {
+					if !first {
+						out += &sep;
+					}
+					first = false;
+					out += &item;
+				} else if matches!(item, Val::Null) {
+					continue;
+				} else {
+					throw_runtime!("in std.join all items should be strings");
+				}
+			}
+
+			IndexableVal::Str(out.into())
+		}
+	})
+}
+
+#[builtin]
+pub fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {
+	Ok(value.reversed())
+}
+
+#[builtin]
+pub fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {
+	for v in arr.iter(s.clone()) {
+		let v = bool::from_untyped(v?, s.clone())?;
+		if v {
+			return Ok(true);
+		}
+	}
+	Ok(false)
+}
+
+#[builtin]
+pub fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {
+	for v in arr.iter(s.clone()) {
+		let v = bool::from_untyped(v?, s.clone())?;
+		if !v {
+			return Ok(false);
+		}
+	}
+	Ok(true)
+}
+
+#[builtin]
+pub fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {
+	match arr {
+		IndexableVal::Str(str) => {
+			let x: IStr = IStr::from_untyped(x.0, s)?;
+			Ok(!x.is_empty() && str.contains(&*x))
+		}
+		IndexableVal::Arr(a) => {
+			for item in a.iter(s.clone()) {
+				let item = item?;
+				if equals(s.clone(), &item, &x.0)? {
+					return Ok(true);
+				}
+			}
+			Ok(false)
+		}
+	}
+}
+
+#[builtin]
+pub fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {
+	let mut count = 0;
+	for item in &arr {
+		if equals(s.clone(), &item.0, &v.0)? {
+			count += 1;
+		}
+	}
+	Ok(count)
+}
addedcrates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/encoding.rs
@@ -0,0 +1,41 @@
+use jrsonnet_evaluator::{
+	error::{Error::RuntimeError, Result},
+	function::builtin,
+	typed::{Either, Either2},
+	IBytes, IStr,
+};
+
+#[builtin]
+pub fn builtin_encode_utf8(str: IStr) -> Result<IBytes> {
+	Ok(str.cast_bytes())
+}
+
+#[builtin]
+pub fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {
+	Ok(arr
+		.cast_str()
+		.ok_or_else(|| RuntimeError("bad utf8".into()))?)
+}
+
+#[builtin]
+pub fn builtin_base64(input: Either![IBytes, IStr]) -> Result<String> {
+	use Either2::*;
+	Ok(match input {
+		A(a) => base64::encode(a.as_slice()),
+		B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
+	})
+}
+
+#[builtin]
+pub fn builtin_base64_decode_bytes(input: IStr) -> Result<IBytes> {
+	Ok(base64::decode(&input.as_bytes())
+		.map_err(|_| RuntimeError("bad base64".into()))?
+		.as_slice()
+		.into())
+}
+
+#[builtin]
+pub fn builtin_base64_decode(input: IStr) -> Result<String> {
+	let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;
+	Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)
+}
addedcrates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -0,0 +1,22 @@
+use std::borrow::Cow;
+
+use jrsonnet_parser::{LocExpr, ParserSettings, Source};
+
+pub const STDLIB_STR: &str = include_str!("./std.jsonnet");
+
+pub fn stdlib_expr() -> LocExpr {
+	#[cfg(feature = "serialized-stdlib")]
+	{
+		// Should not panic, stdlib.bincode is generated in build.rs
+		return bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))
+			.unwrap();
+	}
+
+	jrsonnet_parser::parse(
+		STDLIB_STR,
+		&ParserSettings {
+			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+		},
+	)
+	.unwrap()
+}
addedcrates/jrsonnet-stdlib/src/hash.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/hash.rs
@@ -0,0 +1,6 @@
+use jrsonnet_evaluator::{error::Result, function::builtin, IStr};
+
+#[builtin]
+pub fn builtin_md5(str: IStr) -> Result<String> {
+	Ok(format!("{:x}", md5::compute(&str.as_bytes())))
+}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
1use std::{
2 borrow::Cow,
3 cell::{Ref, RefCell, RefMut},
4 collections::HashMap,
5 rc::Rc,
6};
7
8use jrsonnet_evaluator::{
9 error::{Error::*, Result},
10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},
11 gc::TraceBox,
12 tb,
13 typed::{Any, Either, Either2, Either4, VecVal, M1},
14 val::ArrValue,
15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
16};
17use jrsonnet_gcmodule::Cc;
18use jrsonnet_macros::builtin;
19use jrsonnet_parser::Source;
20
21mod 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::*;
42
43pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {
44 let mut builder = ObjValueBuilder::new();
45
46 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");
51
52 builder.with_super(eval);
53
54 for (name, builtin) in [
55 ("length".into(), builtin_length::INST),
56 // Types
57 ("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 // Arrays
65 ("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 // Math
80 ("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 // Operator
96 ("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 // Sort
101 ("sort".into(), builtin_sort::INST),
102 // Hash
103 ("md5".into(), builtin_md5::INST),
104 // Encoding
105 ("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 // Objects
114 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),
115 ("objectHasEx".into(), builtin_object_has_ex::INST),
116 // Manifest
117 ("escapeStringJson".into(), builtin_escape_string_json::INST),
118 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
119 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
120 // Parsing
121 ("parseJson".into(), builtin_parse_json::INST),
122 ("parseYaml".into(), builtin_parse_yaml::INST),
123 // Misc
124 ("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 ]
132 .iter()
133 .cloned()
134 {
135 builder
136 .member(name)
137 .hide()
138 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))
139 .expect("no conflict");
140 }
141
142 builder
143 .member("extVar".into())
144 .hide()
145 .value(
146 s.clone(),
147 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {
148 settings: settings.clone()
149 })))),
150 )
151 .expect("no conflict");
152 builder
153 .member("native".into())
154 .hide()
155 .value(
156 s.clone(),
157 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {
158 settings: settings.clone()
159 })))),
160 )
161 .expect("no conflict");
162 builder
163 .member("trace".into())
164 .hide()
165 .value(
166 s.clone(),
167 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),
168 )
169 .expect("no conflict");
170
171 builder
172 .member("id".into())
173 .hide()
174 .value(s, Val::Func(FuncVal::Id))
175 .expect("no conflict");
176
177 builder.build()
178}
179
180pub trait TracePrinter {
181 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);
182}
183
184pub struct StdTracePrinter;
185impl TracePrinter for StdTracePrinter {
186 fn print_trace(&self, s: State, loc: CallLocation, value: IStr) {
187 eprint!("TRACE:");
188 if let Some(loc) = loc.0 {
189 let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
190 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
191 }
192 eprintln!(" {}", value);
193 }
194}
195
196pub struct Settings {
197 /// Used for `std.extVar`
198 pub ext_vars: HashMap<IStr, TlaArg>,
199 /// Used for `std.native`
200 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
201 /// Used for `std.trace`
202 pub trace_printer: Box<dyn TracePrinter>,
203}
204
205impl Default for Settings {
206 fn default() -> Self {
207 Self {
208 ext_vars: Default::default(),
209 ext_natives: Default::default(),
210 trace_printer: Box::new(StdTracePrinter),
211 }
212 }
213}
214
1pub const STDLIB_STR: &str = include_str!("./std.jsonnet");215pub fn extvar_source(name: &str) -> Source {
216 let source_name = format!("<extvar:{}>", name);
217 Source::new_virtual(Cow::Owned(source_name))
218}
219
220pub struct ContextInitializer {
221 // When we don't need to support legacy-this-file, we can reuse same context for all files
222 #[cfg(not(feature = "legacy-this-file"))]
223 context: Context,
224 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
225 #[cfg(feature = "legacy-this-file")]
226 stdlib_obj: ObjValue,
227 settings: Rc<RefCell<Settings>>,
228}
229impl ContextInitializer {
230 pub fn new(s: State) -> Self {
231 let settings = Rc::new(RefCell::new(Settings::default()));
232 Self {
233 #[cfg(not(feature = "legacy-this-file"))]
234 context: {
235 let mut context = ContextBuilder::with_capacity(1);
236 context.bind(
237 "std".into(),
238 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),
239 );
240 context.build()
241 },
242 #[cfg(feature = "legacy-this-file")]
243 stdlib_obj: stdlib_uncached(s, settings.clone()),
244 settings,
245 }
246 }
247 pub fn settings(&self) -> Ref<Settings> {
248 self.settings.borrow()
249 }
250 pub fn settings_mut(&self) -> RefMut<Settings> {
251 self.settings.borrow_mut()
252 }
253 pub fn add_ext_var(&self, name: IStr, value: Val) {
254 self.settings_mut()
255 .ext_vars
256 .insert(name, TlaArg::Val(value));
257 }
258 pub fn add_ext_str(&self, name: IStr, value: IStr) {
259 self.settings_mut()
260 .ext_vars
261 .insert(name, TlaArg::String(value));
262 }
263 pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
264 let source = extvar_source(name);
265 let parsed = jrsonnet_parser::parse(
266 &code,
267 &jrsonnet_parser::ParserSettings {
268 file_name: source.clone(),
269 },
270 )
271 .map_err(|e| ImportSyntaxError {
272 path: source,
273 source_code: code.clone().into(),
274 error: Box::new(e),
275 })?;
276 // self.data_mut().volatile_files.insert(source_name, code);
277 self.settings_mut()
278 .ext_vars
279 .insert(name.into(), TlaArg::Code(parsed));
280 Ok(())
281 }
282 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {
283 self.settings_mut().ext_natives.insert(name, cb);
284 }
285}
286impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
287 #[cfg(not(feature = "legacy-this-file"))]
288 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
289 self.context.clone()
290 }
291 #[cfg(feature = "legacy-this-file")]
292 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {
293 let mut builder = ObjValueBuilder::new();
294 builder.with_super(self.stdlib_obj.clone());
295 builder
296 .member("thisFile".into())
297 .hide()
298 .value(
299 s,
300 Val::Str(match source.repr() {
301 Ok(p) => p.display().to_string().into(),
302 // Virtual files end up as empty strings in std.thisFile
303 Err(_e) => "".into(),
304 }),
305 )
306 .expect("this object builder is empty");
307 let stdlib_with_this_file = builder.build();
308
309 let mut context = ContextBuilder::with_capacity(1);
310 context.bind(
311 "std".into(),
312 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
313 );
314 context.build()
315 }
316 unsafe fn as_any(&self) -> &dyn std::any::Any {
317 self
318 }
319}
320
321#[builtin]
322fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {
323 use Either4::*;
324 Ok(match x {
325 A(x) => x.chars().count(),
326 B(x) => x.len(),
327 C(x) => x.len(),
328 D(f) => f.params_len(),
329 })
330}
331
332#[builtin]
333const fn builtin_codepoint(str: char) -> Result<u32> {
334 Ok(str as u32)
335}
336
337#[builtin]
338fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {
339 Ok(str.chars().skip(from as usize).take(len as usize).collect())
340}
341
342#[builtin(fields(
343 settings: Rc<RefCell<Settings>>,
344))]
345fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {
346 let ctx = s.create_default_context(extvar_source(&x));
347 Ok(Any(this
348 .settings
349 .borrow()
350 .ext_vars
351 .get(&x)
352 .cloned()
353 .ok_or(UndefinedExternalVariable(x))?
354 .evaluate_arg(s.clone(), ctx, true)?
355 .evaluate(s)?))
356}
357
358#[builtin(fields(
359 settings: Rc<RefCell<Settings>>,
360))]
361fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {
362 Ok(Any(this
363 .settings
364 .borrow()
365 .ext_natives
366 .get(&name)
367 .cloned()
368 .map_or(Val::Null, |v| {
369 Val::Func(FuncVal::Builtin(v.clone()))
370 })))
371}
372
373#[builtin]
374fn builtin_char(n: u32) -> Result<char> {
375 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
376}
377
378#[builtin(fields(
379 settings: Rc<RefCell<Settings>>,
380))]
381fn builtin_trace(
382 this: &builtin_trace,
383 s: State,
384 loc: CallLocation,
385 str: IStr,
386 rest: Any,
387) -> Result<Any> {
388 this.settings
389 .borrow()
390 .trace_printer
391 .print_trace(s, loc, str);
392 Ok(rest) as Result<Any>
393}
394
395#[builtin]
396fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {
397 Ok(str.replace(&from as &str, &to as &str))
398}
399
400#[builtin]
401fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
402 use Either2::*;
403 Ok(VecVal(Cc::new(match maxsplits {
404 A(n) => str
405 .splitn(n + 1, &c as &str)
406 .map(|s| Val::Str(s.into()))
407 .collect(),
408 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
409 })))
410}
411
412#[builtin]
413fn builtin_ascii_upper(str: IStr) -> Result<String> {
414 Ok(str.to_ascii_uppercase())
415}
416
417#[builtin]
418fn builtin_ascii_lower(str: IStr) -> Result<String> {
419 Ok(str.to_ascii_lowercase())
420}
2421
addedcrates/jrsonnet-stdlib/src/manifest.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/manifest.rs
@@ -0,0 +1,65 @@
+use jrsonnet_evaluator::{
+	error::Result,
+	function::builtin,
+	stdlib::manifest::{
+		escape_string_json, manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
+		ManifestYamlOptions,
+	},
+	typed::Any,
+	IStr, State,
+};
+
+#[builtin]
+pub fn builtin_escape_string_json(str_: IStr) -> Result<String> {
+	Ok(escape_string_json(&str_))
+}
+
+#[builtin]
+pub fn builtin_manifest_json_ex(
+	s: State,
+	value: Any,
+	indent: IStr,
+	newline: Option<IStr>,
+	key_val_sep: Option<IStr>,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Result<String> {
+	let newline = newline.as_deref().unwrap_or("\n");
+	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
+	manifest_json_ex(
+		s,
+		&value.0,
+		&ManifestJsonOptions {
+			padding: &indent,
+			mtype: ManifestType::Std,
+			newline,
+			key_val_sep,
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order: preserve_order.unwrap_or(false),
+		},
+	)
+}
+
+#[builtin]
+pub fn builtin_manifest_yaml_doc(
+	s: State,
+	value: Any,
+	indent_array_in_object: Option<bool>,
+	quote_keys: Option<bool>,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Result<String> {
+	manifest_yaml_ex(
+		s,
+		&value.0,
+		&ManifestYamlOptions {
+			padding: "  ",
+			arr_element_padding: if indent_array_in_object.unwrap_or(false) {
+				"  "
+			} else {
+				""
+			},
+			quote_keys: quote_keys.unwrap_or(true),
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order: preserve_order.unwrap_or(false),
+		},
+	)
+}
addedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -0,0 +1,87 @@
+use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};
+
+#[builtin]
+pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
+	Ok(a % b)
+}
+
+#[builtin]
+pub fn builtin_floor(x: f64) -> Result<f64> {
+	Ok(x.floor())
+}
+
+#[builtin]
+pub fn builtin_ceil(x: f64) -> Result<f64> {
+	Ok(x.ceil())
+}
+
+#[builtin]
+pub fn builtin_log(n: f64) -> Result<f64> {
+	Ok(n.ln())
+}
+
+#[builtin]
+pub fn builtin_pow(x: f64, n: f64) -> Result<f64> {
+	Ok(x.powf(n))
+}
+
+#[builtin]
+pub fn builtin_sqrt(x: PositiveF64) -> Result<f64> {
+	Ok(x.0.sqrt())
+}
+
+#[builtin]
+pub fn builtin_sin(x: f64) -> Result<f64> {
+	Ok(x.sin())
+}
+
+#[builtin]
+pub fn builtin_cos(x: f64) -> Result<f64> {
+	Ok(x.cos())
+}
+
+#[builtin]
+pub fn builtin_tan(x: f64) -> Result<f64> {
+	Ok(x.tan())
+}
+
+#[builtin]
+pub fn builtin_asin(x: f64) -> Result<f64> {
+	Ok(x.asin())
+}
+
+#[builtin]
+pub fn builtin_acos(x: f64) -> Result<f64> {
+	Ok(x.acos())
+}
+
+#[builtin]
+pub fn builtin_atan(x: f64) -> Result<f64> {
+	Ok(x.atan())
+}
+
+#[builtin]
+pub fn builtin_exp(x: f64) -> Result<f64> {
+	Ok(x.exp())
+}
+
+fn frexp(s: f64) -> (f64, i16) {
+	if 0.0 == s {
+		(s, 0)
+	} else {
+		let lg = s.abs().log2();
+		let x = (lg - lg.floor() - 1.0).exp2();
+		let exp = lg.floor() + 1.0;
+		(s.signum() * x, exp as i16)
+	}
+}
+
+#[builtin]
+pub fn builtin_mantissa(x: f64) -> Result<f64> {
+	Ok(frexp(x).0)
+}
+
+#[builtin]
+pub fn builtin_exponent(x: f64) -> Result<i16> {
+	Ok(frexp(x).1)
+}
addedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -0,0 +1,27 @@
+use jrsonnet_evaluator::{
+	error::Result, function::builtin, typed::VecVal, val::Val, IStr, ObjValue,
+};
+use jrsonnet_gcmodule::Cc;
+
+#[builtin]
+pub fn builtin_object_fields_ex(
+	obj: ObjValue,
+	inc_hidden: bool,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Result<VecVal> {
+	#[cfg(feature = "exp-preserve-order")]
+	let preserve_order = preserve_order.unwrap_or(false);
+	let out = obj.fields_ex(
+		inc_hidden,
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order,
+	);
+	Ok(VecVal(Cc::new(
+		out.into_iter().map(Val::Str).collect::<Vec<_>>(),
+	)))
+}
+
+#[builtin]
+pub fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {
+	Ok(obj.has_field_ex(f, inc_hidden))
+}
addedcrates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -0,0 +1,40 @@
+//! Some jsonnet operations are desugared to stdlib functions...
+//! However, in our case we instead implement them in native, and implement native functions on top of core for backwards compatibility
+
+use jrsonnet_evaluator::{
+	error::Result,
+	function::builtin,
+	operator::evaluate_mod_op,
+	stdlib::std_format,
+	typed::{Any, Either, Either2},
+	val::{equals, primitive_equals},
+	IStr, State, Val,
+};
+
+#[builtin]
+pub fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {
+	use Either2::*;
+	Ok(Any(evaluate_mod_op(
+		s,
+		&match a {
+			A(v) => Val::Num(v),
+			B(s) => Val::Str(s),
+		},
+		&b.0,
+	)?))
+}
+
+#[builtin]
+pub fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {
+	primitive_equals(&a.0, &b.0)
+}
+
+#[builtin]
+pub fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {
+	equals(s, &a.0, &b.0)
+}
+
+#[builtin]
+pub fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {
+	std_format(s, str, vals.0)
+}
addedcrates/jrsonnet-stdlib/src/parse.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/parse.rs
@@ -0,0 +1,39 @@
+use jrsonnet_evaluator::{
+	error::{Error::RuntimeError, Result},
+	function::builtin,
+	typed::{Any, Typed},
+	IStr, State, Val,
+};
+use serde::Deserialize;
+
+#[builtin]
+pub fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {
+	use serde_json::Value;
+	let value: Value = serde_json::from_str(&s)
+		.map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;
+	Ok(Any(Value::into_untyped(value, st)?))
+}
+
+#[builtin]
+pub fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {
+	use serde_json::Value;
+	use serde_yaml_with_quirks::DeserializingQuirks;
+	let value = serde_yaml_with_quirks::Deserializer::from_str_with_quirks(
+		&s,
+		DeserializingQuirks { old_octals: true },
+	);
+	let mut out = vec![];
+	for item in value {
+		let value = Value::deserialize(item)
+			.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;
+		let val = Value::into_untyped(value, st.clone())?;
+		out.push(val);
+	}
+	Ok(Any(if out.is_empty() {
+		Val::Null
+	} else if out.len() == 1 {
+		out.into_iter().next().unwrap()
+	} else {
+		Val::Arr(out.into())
+	}))
+}
addedcrates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -0,0 +1,109 @@
+use jrsonnet_evaluator::{
+	error::Result,
+	function::{builtin, FuncVal},
+	throw_runtime,
+	typed::Any,
+	val::ArrValue,
+	State, Val,
+};
+use jrsonnet_gcmodule::Cc;
+
+#[derive(Copy, Clone)]
+enum SortKeyType {
+	Number,
+	String,
+	Unknown,
+}
+
+#[derive(PartialEq)]
+struct NonNaNf64(f64);
+impl PartialOrd for NonNaNf64 {
+	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+		self.0.partial_cmp(&other.0)
+	}
+}
+impl Eq for NonNaNf64 {}
+impl Ord for NonNaNf64 {
+	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
+		self.partial_cmp(other).expect("non nan")
+	}
+}
+
+fn get_sort_type<T>(
+	values: &mut [T],
+	key_getter: impl Fn(&mut T) -> &mut Val,
+) -> Result<SortKeyType> {
+	let mut sort_type = SortKeyType::Unknown;
+	for i in values.iter_mut() {
+		let i = key_getter(i);
+		match (i, sort_type) {
+			(Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,
+			(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
+			(Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
+			(Val::Str(_) | Val::Num(_), _) => {
+				throw_runtime!("sort elements should have same types")
+			}
+			_ => throw_runtime!("sort key should be string or number"),
+		}
+	}
+	Ok(sort_type)
+}
+
+/// * `key_getter` - None, if identity sort required
+pub fn sort(s: State, values: Cc<Vec<Val>>, key_getter: FuncVal) -> Result<Cc<Vec<Val>>> {
+	if values.len() <= 1 {
+		return Ok(values);
+	}
+	if key_getter.is_identity() {
+		// Fast path, identity key getter
+		let mut values = (*values).clone();
+		let sort_type = get_sort_type(&mut values, |k| k)?;
+		match sort_type {
+			SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
+				Val::Num(n) => NonNaNf64(*n),
+				_ => unreachable!(),
+			}),
+			SortKeyType::String => values.sort_unstable_by_key(|v| match v {
+				Val::Str(s) => s.clone(),
+				_ => unreachable!(),
+			}),
+			SortKeyType::Unknown => unreachable!(),
+		};
+		Ok(Cc::new(values))
+	} else {
+		// Slow path, user provided key getter
+		let mut vk = Vec::with_capacity(values.len());
+		for value in values.iter() {
+			vk.push((
+				value.clone(),
+				key_getter.evaluate_simple(s.clone(), &(Any(value.clone()),))?,
+			));
+		}
+		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
+		match sort_type {
+			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
+				Val::Num(n) => NonNaNf64(n),
+				_ => unreachable!(),
+			}),
+			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
+				Val::Str(s) => s.clone(),
+				_ => unreachable!(),
+			}),
+			SortKeyType::Unknown => unreachable!(),
+		};
+		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
+	}
+}
+
+#[builtin]
+#[allow(non_snake_case)]
+pub fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
+	if arr.len() <= 1 {
+		return Ok(arr);
+	}
+	Ok(ArrValue::Eager(super::sort::sort(
+		s.clone(),
+		arr.evaluated(s)?,
+		keyF.unwrap_or_else(FuncVal::identity),
+	)?))
+}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -2,62 +2,11 @@
   local std = self,
   local id = std.id,
 
-  # Magic legacy field
-  thisFile:: $intrinsicThisFile,
-  id:: $intrinsicId,
-
-  # Those functions aren't normally located in stdlib
-  length:: $intrinsic(length),
-  type:: $intrinsic(type),
-  makeArray:: $intrinsic(makeArray),
-  codepoint:: $intrinsic(codepoint),
-  objectFieldsEx:: $intrinsic(objectFieldsEx),
-  objectHasEx:: $intrinsic(objectHasEx),
-  primitiveEquals:: $intrinsic(primitiveEquals),
-  modulo:: $intrinsic(modulo),
-  floor:: $intrinsic(floor),
-  ceil:: $intrinsic(ceil),
-  extVar:: $intrinsic(extVar),
-  native:: $intrinsic(native),
-  filter:: $intrinsic(filter),
-  char:: $intrinsic(char),
-  encodeUTF8:: $intrinsic(encodeUTF8),
-  decodeUTF8:: $intrinsic(decodeUTF8),
-  md5:: $intrinsic(md5),
-  trace:: $intrinsic(trace),
-  parseJson:: $intrinsic(parseJson),
-  parseYaml:: $intrinsic(parseYaml),
-
-  log:: $intrinsic(log),
-  pow:: $intrinsic(pow),
-  sqrt:: $intrinsic(sqrt),
-
-  sin:: $intrinsic(sin),
-  cos:: $intrinsic(cos),
-  tan:: $intrinsic(tan),
-  asin:: $intrinsic(asin),
-  acos:: $intrinsic(acos),
-  atan:: $intrinsic(atan),
-
-  exp:: $intrinsic(exp),
-  mantissa:: $intrinsic(mantissa),
-  exponent:: $intrinsic(exponent),
+  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',
 
-  any:: $intrinsic(any),
-  all:: $intrinsic(all),
-
-  isString(v):: std.type(v) == 'string',
-  isNumber(v):: std.type(v) == 'number',
-  isBoolean(v):: std.type(v) == 'boolean',
-  isObject(v):: std.type(v) == 'object',
-  isArray(v):: std.type(v) == 'array',
-  isFunction(v):: std.type(v) == 'function',
-
   toString(a)::
     if std.type(a) == 'string' then a else '' + a,
 
-  substr:: $intrinsic(substr),
-
   startsWith(a, b)::
     if std.length(a) < std.length(b) then
       false
@@ -127,33 +76,13 @@
 
   split(str, c):: std.splitLimit(str, c, -1),
 
-  splitLimit:: $intrinsic(splitLimit),
-
-  strReplace:: $intrinsic(strReplace),
-
-  asciiUpper:: $intrinsic(asciiUpper),
-
-  asciiLower:: $intrinsic(asciiLower),
-
-  range:: $intrinsic(range),
-
   repeat(what, count)::
     local joiner =
       if std.isString(what) then ''
       else if std.isArray(what) then []
       else error 'std.repeat first argument must be an array or a string';
     std.join(joiner, std.makeArray(count, function(i) what)),
-
-  slice:: $intrinsic(slice),
 
-  member:: $intrinsic(member),
-
-  count:: $intrinsic(count),
-
-  mod:: $intrinsic(mod),
-
-  map:: $intrinsic(map),
-
   mapWithIndex(func, arr)::
     if !std.isFunction(func) then
       error ('std.mapWithIndex first param must be function, got ' + std.type(func))
@@ -169,11 +98,7 @@
       error ('std.mapWithKey second param must be object, got ' + std.type(obj))
     else
       { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
-
-  flatMap:: $intrinsic(flatMap),
 
-  join:: $intrinsic(join),
-
   lines(arr)::
     std.join('\n', arr + ['']),
 
@@ -184,14 +109,7 @@
       std.join('', [std.deepJoin(x) for x in arr])
     else
       error 'Expected string or array, got %s' % std.type(arr),
-
 
-  format:: $intrinsic(format),
-
-  foldr:: $intrinsic(foldr),
-
-  foldl:: $intrinsic(foldl),
-
   filterMap(filter_func, map_func, arr)::
     if !std.isFunction(filter_func) then
       error ('std.filterMap first param must be function, got ' + std.type(filter_func))
@@ -350,8 +268,6 @@
     else
       error 'TOML body must be an object. Got ' + std.type(value),
 
-  escapeStringJson:: $intrinsic(escapeStringJson),
-
   escapeStringPython(str)::
     std.escapeStringJson(str),
 
@@ -376,11 +292,7 @@
   manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,
 
   manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
-
-  manifestJsonEx:: $intrinsic(manifestJsonEx),
 
-  manifestYamlDoc:: $intrinsic(manifestYamlDoc),
-
   manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
     if !std.isArray(value) then
       error 'manifestYamlStream only takes arrays, got ' + std.type(value)
@@ -433,20 +345,7 @@
           std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);
 
       aux(value),
-
-  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
-  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
-
-  base64:: $intrinsic(base64),
-
-  base64DecodeBytes:: $intrinsic(base64DecodeBytes),
-
-  base64Decode:: $intrinsic(base64Decode),
-
-  reverse:: $intrinsic(reverse),
 
-  sort:: $intrinsic(sort),
-
   uniq(arr, keyF=id)::
     local f(a, b) =
       if std.length(a) == 0 then
@@ -534,7 +433,7 @@
     else
       patch,
 
-  get(o, f, default = null, inc_hidden = true)::
+  get(o, f, default=null, inc_hidden=true)::
     if std.objectHasEx(o, f, inc_hidden) then o[f] else default,
 
   objectFields(o)::
@@ -554,8 +453,6 @@
 
   objectValuesAll(o)::
     [o[k] for k in std.objectFieldsAll(o)],
-
-  equals:: $intrinsic(equals),
 
   resolvePath(f, r)::
     local arr = std.split(f, '/');
addedcrates/jrsonnet-stdlib/src/types.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/types.rs
@@ -0,0 +1,31 @@
+use jrsonnet_evaluator::{error::Result, function::builtin, typed::Any, IStr, Val};
+
+#[builtin]
+pub fn builtin_type(x: Any) -> Result<IStr> {
+	Ok(x.0.value_type().name().into())
+}
+
+#[builtin]
+pub fn builtin_is_string(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Str(_)))
+}
+#[builtin]
+pub fn builtin_is_number(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Num(_)))
+}
+#[builtin]
+pub fn builtin_is_boolean(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Bool(_)))
+}
+#[builtin]
+pub fn builtin_is_object(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Obj(_)))
+}
+#[builtin]
+pub fn builtin_is_array(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Arr(_)))
+}
+#[builtin]
+pub fn builtin_is_function(x: Any) -> Result<bool> {
+	Ok(matches!(x.0, Val::Func(_)))
+}