difftreelog
refactor simplify intrinsic handling
in: master
7 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,14 +7,11 @@
edition = "2018"
[features]
-default = ["serialized-stdlib", "faster", "explaining-traces", "serde-json"]
+default = ["serialized-stdlib", "explaining-traces", "serde-json"]
# Serializes standard library AST instead of parsing them every run
serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
# Allow to convert Val into serde_json::Value and backwards
serde-json = ["serde", "serde_json"]
-# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
-# Library works fine without this feature, but requires more memory and time for std function calls
-faster = []
# Rustc-like trace visualization
explaining-traces = ["annotate-snippets"]
# Allows library authors to throw custom errors
@@ -24,10 +21,10 @@
unstable = []
[dependencies]
-jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.0" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
-jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.0" }
+jrsonnet-interner = { path="../jrsonnet-interner", version="0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
+jrsonnet-types = { path="../jrsonnet-types", version="0.4.0" }
pathdiff = "0.2.0"
md5 = "0.7.0"
@@ -35,7 +32,7 @@
rustc-hash = "1.1.0"
thiserror = "1.0"
-jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
+jrsonnet-gc = { version="0.4.2", features=["derive"] }
[dependencies.anyhow]
version = "1.0"
@@ -61,7 +58,7 @@
optional = true
[build-dependencies]
-jrsonnet-parser = { path = "../jrsonnet-parser", features = ["serialize", "deserialize"], version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", features=["serialize", "deserialize"], version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
serde = "1.0"
bincode = "1.3.1"
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,14 +1,11 @@
use bincode::serialize;
-use jrsonnet_parser::{
- parse, Expr, FieldMember, FieldName, LocExpr, Member, ObjBody, ParserSettings,
-};
+use jrsonnet_parser::{parse, ParserSettings};
use jrsonnet_stdlib::STDLIB_STR;
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
- rc::Rc,
};
fn main() {
@@ -21,37 +18,6 @@
)
.expect("parse");
- let parsed = if cfg!(feature = "faster") {
- let LocExpr(expr, location) = parsed;
- LocExpr(
- Rc::new(match Rc::try_unwrap(expr).unwrap() {
- Expr::Obj(ObjBody::MemberList(members)) => Expr::Obj(ObjBody::MemberList(
- members
- .into_iter()
- .filter(|p| {
- !matches!(
- p,
- Member::Field(FieldMember {
- name: FieldName::Fixed(name),
- ..
- })
- if name == "join" || name == "manifestJsonEx" ||
- name == "escapeStringJson" || name == "equals" ||
- name == "base64" || name == "foldl" || name == "foldr" ||
- name == "sortImpl" || name == "format" || name == "range" ||
- name == "reverse" || name == "slice" || name == "mod" ||
- name == "strReplace" || name == "map"
- )
- })
- .collect(),
- )),
- _ => panic!("std value should be object"),
- }),
- location,
- )
- } else {
- parsed
- };
{
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("stdlib.bincode");
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth1use crate::{2 equals,3 error::{Error::*, Result},4 operator::evaluate_mod_op,5 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,6 FuncVal, IndexableVal, LazyVal, Val,7};8use format::{format_arr, format_obj};9use jrsonnet_gc::Gc;10use jrsonnet_interner::IStr;11use jrsonnet_parser::{ArgsDesc, ExprLocation};12use jrsonnet_types::ty;13use std::{collections::HashMap, path::PathBuf, rc::Rc};1415pub mod stdlib;16pub use stdlib::*;1718use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1920pub mod format;21pub mod manifest;22pub mod sort;2324pub fn std_format(str: IStr, vals: Val) -> Result<Val> {25 push(26 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),27 || format!("std.format of {}", str),28 || {29 Ok(match vals {30 Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),31 Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),32 o => Val::Str(format_arr(&str, &[o])?.into()),33 })34 },35 )36}3738pub fn std_slice(39 indexable: IndexableVal,40 index: Option<usize>,41 end: Option<usize>,42 step: Option<usize>,43) -> Result<Val> {44 let index = index.unwrap_or(0);45 let end = end.unwrap_or_else(|| match &indexable {46 IndexableVal::Str(_) => usize::MAX,47 IndexableVal::Arr(v) => v.len(),48 });49 let step = step.unwrap_or(1);50 match &indexable {51 IndexableVal::Str(s) => Ok(Val::Str(52 (s.chars()53 .skip(index)54 .take(end - index)55 .step_by(step)56 .collect::<String>())57 .into(),58 )),59 IndexableVal::Arr(arr) => Ok(Val::Arr(60 (arr.iter()61 .skip(index)62 .take(end - index)63 .step_by(step)64 .collect::<Result<Vec<Val>>>()?)65 .into(),66 )),67 }68}6970type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;7172type BuiltinsType = HashMap<Box<str>, Builtin>;7374thread_local! {75 static BUILTINS: BuiltinsType = {76 [77 ("length".into(), builtin_length as Builtin),78 ("type".into(), builtin_type),79 ("makeArray".into(), builtin_make_array),80 ("codepoint".into(), builtin_codepoint),81 ("objectFieldsEx".into(), builtin_object_fields_ex),82 ("objectHasEx".into(), builtin_object_has_ex),83 ("slice".into(), builtin_slice),84 ("primitiveEquals".into(), builtin_primitive_equals),85 ("equals".into(), builtin_equals),86 ("modulo".into(), builtin_modulo),87 ("mod".into(), builtin_mod),88 ("floor".into(), builtin_floor),89 ("log".into(), builtin_log),90 ("pow".into(), builtin_pow),91 ("extVar".into(), builtin_ext_var),92 ("native".into(), builtin_native),93 ("filter".into(), builtin_filter),94 ("map".into(), builtin_map),95 ("foldl".into(), builtin_foldl),96 ("foldr".into(), builtin_foldr),97 ("sortImpl".into(), builtin_sort_impl),98 ("format".into(), builtin_format),99 ("range".into(), builtin_range),100 ("char".into(), builtin_char),101 ("encodeUTF8".into(), builtin_encode_utf8),102 ("md5".into(), builtin_md5),103 ("base64".into(), builtin_base64),104 ("trace".into(), builtin_trace),105 ("join".into(), builtin_join),106 ("escapeStringJson".into(), builtin_escape_string_json),107 ("manifestJsonEx".into(), builtin_manifest_json_ex),108 ("reverse".into(), builtin_reverse),109 ("id".into(), builtin_id),110 ("strReplace".into(), builtin_str_replace),111 ("parseJson".into(), builtin_parse_json),112 ].iter().cloned().collect()113 };114}115116fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {117 parse_args!(context, "length", args, 1, [118 0, x: ty!((string | object | array));119 ], {120 Ok(match x {121 Val::Str(n) => Val::Num(n.chars().count() as f64),122 Val::Arr(a) => Val::Num(a.len() as f64),123 Val::Obj(o) => Val::Num(124 o.fields_visibility()125 .into_iter()126 .filter(|(_k, v)| *v)127 .count() as f64,128 ),129 _ => unreachable!(),130 })131 })132}133134fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {135 parse_args!(context, "type", args, 1, [136 0, x: ty!(any);137 ], {138 Ok(Val::Str(x.value_type().name().into()))139 })140}141142fn builtin_make_array(143 context: Context,144 _loc: Option<&ExprLocation>,145 args: &ArgsDesc,146) -> Result<Val> {147 parse_args!(context, "makeArray", args, 2, [148 0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;149 1, func: ty!(function) => Val::Func;150 ], {151 let mut out = Vec::with_capacity(sz as usize);152 for i in 0..sz as usize {153 out.push(LazyVal::new_resolved(func.evaluate_values(154 context.clone(),155 &[Val::Num(i as f64)]156 )?))157 }158 Ok(Val::Arr(out.into()))159 })160}161162fn builtin_codepoint(163 context: Context,164 _loc: Option<&ExprLocation>,165 args: &ArgsDesc,166) -> Result<Val> {167 parse_args!(context, "codepoint", args, 1, [168 0, str: ty!(char) => Val::Str;169 ], {170 Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))171 })172}173174fn builtin_object_fields_ex(175 context: Context,176 _loc: Option<&ExprLocation>,177 args: &ArgsDesc,178) -> Result<Val> {179 parse_args!(context, "objectFieldsEx", args, 2, [180 0, obj: ty!(object) => Val::Obj;181 1, inc_hidden: ty!(boolean) => Val::Bool;182 ], {183 let out = obj.fields_ex(inc_hidden);184 Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))185 })186}187188fn builtin_object_has_ex(189 context: Context,190 _loc: Option<&ExprLocation>,191 args: &ArgsDesc,192) -> Result<Val> {193 parse_args!(context, "objectHasEx", args, 3, [194 0, obj: ty!(object) => Val::Obj;195 1, f: ty!(string) => Val::Str;196 2, inc_hidden: ty!(boolean) => Val::Bool;197 ], {198 Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))199 })200}201202fn builtin_parse_json(203 context: Context,204 _loc: Option<&ExprLocation>,205 args: &ArgsDesc,206) -> Result<Val> {207 parse_args!(context, "parseJson", args, 1, [208 0, s: ty!(string) => Val::Str;209 ], {210 let state = EvaluationState::default();211 let path = PathBuf::from("std.parseJson").into();212 state.evaluate_snippet_raw(path ,s)213 })214}215216// faster217fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {218 parse_args!(context, "slice", args, 4, [219 0, indexable: ty!((string | array));220 1, index: ty!((number | null));221 2, end: ty!((number | null));222 3, step: ty!((number | null));223 ], {224 std_slice(225 indexable.to_indexable()?,226 index.try_cast_nullable_num("index")?.map(|v| v as usize),227 end.try_cast_nullable_num("end")?.map(|v| v as usize),228 step.try_cast_nullable_num("step")?.map(|v| v as usize),229 )230 })231}232233// faster234fn builtin_primitive_equals(235 context: Context,236 _loc: Option<&ExprLocation>,237 args: &ArgsDesc,238) -> Result<Val> {239 parse_args!(context, "primitiveEquals", args, 2, [240 0, a: ty!(any);241 1, b: ty!(any);242 ], {243 Ok(Val::Bool(primitive_equals(&a, &b)?))244 })245}246247// faster248fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {249 parse_args!(context, "equals", args, 2, [250 0, a: ty!(any);251 1, b: ty!(any);252 ], {253 Ok(Val::Bool(equals(&a, &b)?))254 })255}256257fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {258 parse_args!(context, "modulo", args, 2, [259 0, a: ty!(number) => Val::Num;260 1, b: ty!(number) => Val::Num;261 ], {262 Ok(Val::Num(a % b))263 })264}265266fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {267 parse_args!(context, "mod", args, 2, [268 0, a: ty!((number | string));269 1, b: ty!(any);270 ], {271 evaluate_mod_op(&a, &b)272 })273}274275fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {276 parse_args!(context, "floor", args, 1, [277 0, x: ty!(number) => Val::Num;278 ], {279 Ok(Val::Num(x.floor()))280 })281}282283fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {284 parse_args!(context, "log", args, 1, [285 0, n: ty!(number) => Val::Num;286 ], {287 Ok(Val::Num(n.ln()))288 })289}290291fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {292 parse_args!(context, "pow", args, 2, [293 0, x: ty!(number) => Val::Num;294 1, n: ty!(number) => Val::Num;295 ], {296 Ok(Val::Num(x.powf(n)))297 })298}299300fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {301 parse_args!(context, "extVar", args, 1, [302 0, x: ty!(string) => Val::Str;303 ], {304 Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)305 })306}307308fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {309 parse_args!(context, "native", args, 1, [310 0, x: ty!(string) => Val::Str;311 ], {312 Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)313 })314}315316fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {317 parse_args!(context, "filter", args, 2, [318 0, func: ty!(function) => Val::Func;319 1, arr: ty!(array) => Val::Arr;320 ], {321 Ok(Val::Arr(arr.filter(|val| func322 .evaluate_values(context.clone(), &[val.clone()])?323 .try_cast_bool("filter predicate"))?))324 })325}326327fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {328 parse_args!(context, "map", args, 2, [329 0, func: ty!(function) => Val::Func;330 1, arr: ty!(array) => Val::Arr;331 ], {332 Ok(Val::Arr(arr.map(|val| func333 .evaluate_values(context.clone(), &[val]))?))334 })335}336337fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {338 parse_args!(context, "foldl", args, 3, [339 0, func: ty!(function) => Val::Func;340 1, arr: ty!(array) => Val::Arr;341 2, init: ty!(any);342 ], {343 let mut acc = init;344 for i in arr.iter() {345 acc = func.evaluate_values(context.clone(), &[acc, i?])?;346 }347 Ok(acc)348 })349}350351fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {352 parse_args!(context, "foldr", args, 3, [353 0, func: ty!(function) => Val::Func;354 1, arr: ty!(array) => Val::Arr;355 2, init: ty!(any);356 ], {357 let mut acc = init;358 for i in arr.iter().rev() {359 acc = func.evaluate_values(context.clone(), &[acc, i?])?;360 }361 Ok(acc)362 })363}364365#[allow(non_snake_case)]366fn builtin_sort_impl(367 context: Context,368 _loc: Option<&ExprLocation>,369 args: &ArgsDesc,370) -> Result<Val> {371 parse_args!(context, "sort", args, 2, [372 0, arr: ty!(array) => Val::Arr;373 1, keyF: ty!(function) => Val::Func;374 ], {375 if arr.len() <= 1 {376 return Ok(Val::Arr(arr))377 }378 Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))379 })380}381382// faster383fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {384 parse_args!(context, "format", args, 2, [385 0, str: ty!(string) => Val::Str;386 1, vals: ty!(any)387 ], {388 std_format(str, vals)389 })390}391392fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {393 parse_args!(context, "range", args, 2, [394 0, from: ty!(number) => Val::Num;395 1, to: ty!(number) => Val::Num;396 ], {397 if to < from {398 return Ok(Val::Arr(ArrValue::new_eager()))399 }400 let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));401 for i in from as usize..=to as usize {402 out.push(Val::Num(i as f64));403 }404 Ok(Val::Arr(out.into()))405 })406}407408fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {409 parse_args!(context, "char", args, 1, [410 0, n: ty!(number) => Val::Num;411 ], {412 let mut out = String::new();413 out.push(std::char::from_u32(n as u32).ok_or_else(||414 InvalidUnicodeCodepointGot(n as u32)415 )?);416 Ok(Val::Str(out.into()))417 })418}419420fn builtin_encode_utf8(421 context: Context,422 _loc: Option<&ExprLocation>,423 args: &ArgsDesc,424) -> Result<Val> {425 parse_args!(context, "encodeUTF8", args, 1, [426 0, str: ty!(string) => Val::Str;427 ], {428 Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))429 })430}431432fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {433 parse_args!(context, "md5", args, 1, [434 0, str: ty!(string) => Val::Str;435 ], {436 Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))437 })438}439440fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {441 parse_args!(context, "trace", args, 2, [442 0, str: ty!(string) => Val::Str;443 1, rest: ty!(any);444 ], {445 eprint!("TRACE:");446 if let Some(loc) = loc {447 with_state(|s|{448 let locs = s.map_source_locations(&loc.0, &[loc.1]);449 eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);450 });451 }452 eprintln!(" {}", str);453 Ok(rest)454 })455}456457fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {458 parse_args!(context, "base64", args, 1, [459 0, input: ty!((string | (Array<number>)));460 ], {461 Ok(Val::Str(match input {462 Val::Str(s) => {463 base64::encode(s.bytes().collect::<Vec<_>>()).into()464 },465 Val::Arr(a) => {466 base64::encode(a.iter().map(|v| {467 Ok(v?.unwrap_num()? as u8)468 }).collect::<Result<Vec<_>>>()?).into()469 },470 _ => unreachable!()471 }))472 })473}474475fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {476 parse_args!(context, "join", args, 2, [477 0, sep: ty!((string | array));478 1, arr: ty!(array) => Val::Arr;479 ], {480 Ok(match sep {481 Val::Arr(joiner_items) => {482 let mut out = Vec::new();483484 let mut first = true;485 for item in arr.iter() {486 let item = item?.clone();487 if let Val::Arr(items) = item {488 if !first {489 out.reserve(joiner_items.len());490 // TODO: extend491 for item in joiner_items.iter() {492 out.push(item?);493 }494 }495 first = false;496 out.reserve(items.len());497 // TODO: extend498 for item in items.iter() {499 out.push(item?);500 }501 } else {502 throw!(RuntimeError("in std.join all items should be arrays".into()));503 }504 }505506 Val::Arr(out.into())507 },508 Val::Str(sep) => {509 let mut out = String::new();510511 let mut first = true;512 for item in arr.iter() {513 let item = item?.clone();514 if let Val::Str(item) = item {515 if !first {516 out += &sep;517 }518 first = false;519 out += &item;520 } else {521 throw!(RuntimeError("in std.join all items should be strings".into()));522 }523 }524525 Val::Str(out.into())526 },527 _ => unreachable!()528 })529 })530}531532// faster533fn builtin_escape_string_json(534 context: Context,535 _loc: Option<&ExprLocation>,536 args: &ArgsDesc,537) -> Result<Val> {538 parse_args!(context, "escapeStringJson", args, 1, [539 0, str_: ty!(string) => Val::Str;540 ], {541 Ok(Val::Str(escape_string_json(&str_).into()))542 })543}544545// faster546fn builtin_manifest_json_ex(547 context: Context,548 _loc: Option<&ExprLocation>,549 args: &ArgsDesc,550) -> Result<Val> {551 parse_args!(context, "manifestJsonEx", args, 2, [552 0, value: ty!(any);553 1, indent: ty!(string) => Val::Str;554 ], {555 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {556 padding: &indent,557 mtype: ManifestType::Std,558 })?.into()))559 })560}561562// faster563fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {564 parse_args!(context, "reverse", args, 1, [565 0, value: ty!(array) => Val::Arr;566 ], {567 Ok(Val::Arr(value.reversed()))568 })569}570571fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {572 parse_args!(context, "id", args, 1, [573 0, v: ty!(any);574 ], {575 Ok(v)576 })577}578579// faster580fn builtin_str_replace(581 context: Context,582 _loc: Option<&ExprLocation>,583 args: &ArgsDesc,584) -> Result<Val> {585 parse_args!(context, "strReplace", args, 3, [586 0, str: ty!(string) => Val::Str;587 1, from: ty!(string) => Val::Str;588 2, to: ty!(string) => Val::Str;589 ], {590 let mut out = String::new();591 let mut last_idx = 0;592 while let Some(idx) = (&str[last_idx..]).find(&from as &str) {593 out.push_str(&str[last_idx..last_idx+idx]);594 out.push_str(&to);595 last_idx += idx + from.len();596 }597 if last_idx == 0 {598 return Ok(Val::Str(str))599 }600 out.push_str(&str[last_idx..]);601 Ok(Val::Str(out.into()))602 })603}604605pub fn call_builtin(606 context: Context,607 loc: Option<&ExprLocation>,608 name: &str,609 args: &ArgsDesc,610) -> Result<Val> {611 BUILTINS612 .with(|builtins| builtins.get(name).copied())613 .ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)614}crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -546,8 +546,6 @@
|| {
if let Some(v) = v.get(s.clone())? {
Ok(v)
- } else if v.get("__intrinsic_namespace__".into())?.is_some() {
- Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
} else {
throw!(NoSuchField(s))
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -531,7 +531,6 @@
}
/// Calls `std.manifestJson`
- #[cfg(feature = "faster")]
pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
manifest_json_ex(
self,
@@ -543,30 +542,6 @@
.map(|s| s.into())
}
- /// Calls `std.manifestJson`
- #[cfg(not(feature = "faster"))]
- pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
- with_state(|s| {
- let ctx = s
- .create_default_context()?
- .with_var("__tmp__to_json__".into(), self.clone())?;
- Ok(evaluate(
- ctx,
- &el!(Expr::Apply(
- el!(Expr::Index(
- el!(Expr::Var("std".into())),
- el!(Expr::Str("manifestJsonEx".into()))
- )),
- ArgsDesc(vec![
- Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),
- Arg(None, el!(Expr::Str(" ".repeat(padding).into())))
- ]),
- false
- )),
- )?
- .try_cast_str("to json")?)
- })
- }
pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
with_state(|s| {
let ctx = s
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -192,6 +192,8 @@
pub rule expr_basic(s: &ParserSettings) -> LocExpr
= literal(s)
+ / quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}
+
/ string_expr(s) / number_expr(s)
/ array_expr(s)
/ obj_expr(s)
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -1,9 +1,29 @@
{
- __intrinsic_namespace__:: 'std',
-
local std = self,
local id = std.id,
+ # 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),
+ log:: $intrinsic(log),
+ pow:: $intrinsic(pow),
+ extVar:: $intrinsic(extVar),
+ native:: $intrinsic(native),
+ filter:: $intrinsic(filter),
+ char:: $intrinsic(char),
+ encodeUTF8:: $intrinsic(encodeUTF8),
+ md5:: $intrinsic(md5),
+ trace:: $intrinsic(trace),
+ id:: $intrinsic(id),
+ parseJson:: $intrinsic(parseJson),
+
isString(v):: std.type(v) == 'string',
isNumber(v):: std.type(v) == 'number',
isBoolean(v):: std.type(v) == 'boolean',
@@ -109,37 +129,8 @@
else
aux(str, delim, i2, arr, v + c) tailstrict;
aux(str, c, 0, [], ''),
-
- strReplace(str, from, to)::
- assert std.isString(str);
- assert std.isString(from);
- assert std.isString(to);
- assert from != '' : "'from' string must not be zero length.";
- // Cache for performance.
- local str_len = std.length(str);
- local from_len = std.length(from);
-
- // True if from is at str[i].
- local found_at(i) = str[i:i + from_len] == from;
-
- // Return the remainder of 'str' starting with 'start_index' where
- // all occurrences of 'from' after 'curr_index' are replaced with 'to'.
- local replace_after(start_index, curr_index, acc) =
- if curr_index > str_len then
- acc + str[start_index:curr_index]
- else if found_at(curr_index) then
- local new_index = curr_index + std.length(from);
- replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict
- else
- replace_after(start_index, curr_index + 1, acc) tailstrict;
-
- // if from_len==1, then we replace by splitting and rejoining the
- // string which is much faster than recursing on replace_after
- if from_len == 1 then
- std.join(to, std.split(str, from))
- else
- replace_after(0, 0, ''),
+ strReplace:: $intrinsic(strReplace),
asciiUpper(str)::
local cp = std.codepoint;
@@ -157,8 +148,7 @@
c;
std.join('', std.map(down_letter, std.stringChars(str))),
- range(from, to)::
- std.makeArray(to - from + 1, function(i) i + from),
+ range:: $intrinsic(range),
repeat(what, count)::
local joiner =
@@ -167,38 +157,7 @@
else error 'std.repeat first argument must be an array or a string';
std.join(joiner, std.makeArray(count, function(i) what)),
- slice(indexable, index, end, step)::
- local invar =
- // loop invariant with defaults applied
- {
- indexable: indexable,
- index:
- if index == null then 0
- else index,
- end:
- if end == null then std.length(indexable)
- else end,
- step:
- if step == null then 1
- else step,
- length: std.length(indexable),
- type: std.type(indexable),
- };
- assert invar.index >= 0 && invar.end >= 0 && invar.step >= 0 : 'got [%s:%s:%s] but negative index, end, and steps are not supported' % [invar.index, invar.end, invar.step];
- assert step != 0 : 'got %s but step must be greater than 0' % step;
- assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
- local build(slice, cur) =
- if cur >= invar.end || cur >= invar.length then
- slice
- else
- build(
- if invar.type == 'string' then
- slice + invar.indexable[cur]
- else
- slice + [invar.indexable[cur]],
- cur + invar.step
- ) tailstrict;
- build(if invar.type == 'string' then '' else [], invar.index),
+ slice:: $intrinsic(slice),
member(arr, x)::
if std.isArray(arr) then
@@ -209,21 +168,9 @@
count(arr, x):: std.length(std.filter(function(v) v == x, arr)),
- mod(a, b)::
- if std.isNumber(a) && std.isNumber(b) then
- std.modulo(a, b)
- else if std.isString(a) then
- std.format(a, b)
- else
- error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',
+ mod:: $intrinsic(mod),
- map(func, arr)::
- if !std.isFunction(func) then
- error ('std.map first param must be function, got ' + std.type(func))
- else if !std.isArray(arr) && !std.isString(arr) then
- error ('std.map second param must be array / string, got ' + std.type(arr))
- else
- std.makeArray(std.length(arr), function(i) func(arr[i])),
+ map:: $intrinsic(map),
mapWithIndex(func, arr)::
if !std.isFunction(func) then
@@ -250,26 +197,7 @@
std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),
- join(sep, arr)::
- local aux(arr, i, first, running) =
- if i >= std.length(arr) then
- running
- else if arr[i] == null then
- aux(arr, i + 1, first, running) tailstrict
- else if std.type(arr[i]) != std.type(sep) then
- error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]
- else if first then
- aux(arr, i + 1, false, running + arr[i]) tailstrict
- else
- aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;
- if !std.isArray(arr) then
- error 'join second parameter should be array, got ' + std.type(arr)
- else if std.isString(sep) then
- aux(arr, 0, true, '')
- else if std.isArray(sep) then
- aux(arr, 0, true, [])
- else
- error 'join first parameter should be string or array, got ' + std.type(sep),
+ join:: $intrinsic(join),
lines(arr)::
std.join('\n', arr + ['']),
@@ -281,479 +209,14 @@
std.join('', [std.deepJoin(x) for x in arr])
else
error 'Expected string or array, got %s' % std.type(arr),
-
-
- format(str, vals)::
-
- /////////////////////////////
- // Parse the mini-language //
- /////////////////////////////
-
- local try_parse_mapping_key(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == '(' then
- local consume(str, j, v) =
- if j >= std.length(str) then
- error 'Truncated format code.'
- else
- local c = str[j];
- if c != ')' then
- consume(str, j + 1, v + c)
- else
- { i: j + 1, v: v };
- consume(str, i + 1, '')
- else
- { i: i, v: null };
- local try_parse_cflags(str, i) =
- local consume(str, j, v) =
- assert j < std.length(str) : 'Truncated format code.';
- local c = str[j];
- if c == '#' then
- consume(str, j + 1, v { alt: true })
- else if c == '0' then
- consume(str, j + 1, v { zero: true })
- else if c == '-' then
- consume(str, j + 1, v { left: true })
- else if c == ' ' then
- consume(str, j + 1, v { blank: true })
- else if c == '+' then
- consume(str, j + 1, v { sign: true })
- else
- { i: j, v: v };
- consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });
- local try_parse_field_width(str, i) =
- if i < std.length(str) && str[i] == '*' then
- { i: i + 1, v: '*' }
- else
- local consume(str, j, v) =
- assert j < std.length(str) : 'Truncated format code.';
- local c = str[j];
- if c == '0' then
- consume(str, j + 1, v * 10 + 0)
- else if c == '1' then
- consume(str, j + 1, v * 10 + 1)
- else if c == '2' then
- consume(str, j + 1, v * 10 + 2)
- else if c == '3' then
- consume(str, j + 1, v * 10 + 3)
- else if c == '4' then
- consume(str, j + 1, v * 10 + 4)
- else if c == '5' then
- consume(str, j + 1, v * 10 + 5)
- else if c == '6' then
- consume(str, j + 1, v * 10 + 6)
- else if c == '7' then
- consume(str, j + 1, v * 10 + 7)
- else if c == '8' then
- consume(str, j + 1, v * 10 + 8)
- else if c == '9' then
- consume(str, j + 1, v * 10 + 9)
- else
- { i: j, v: v };
- consume(str, i, 0);
-
- local try_parse_precision(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == '.' then
- try_parse_field_width(str, i + 1)
- else
- { i: i, v: null };
-
- // Ignored, if it exists.
- local try_parse_length_modifier(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == 'h' || c == 'l' || c == 'L' then
- i + 1
- else
- i;
-
- local parse_conv_type(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == 'd' || c == 'i' || c == 'u' then
- { i: i + 1, v: 'd', caps: false }
- else if c == 'o' then
- { i: i + 1, v: 'o', caps: false }
- else if c == 'x' then
- { i: i + 1, v: 'x', caps: false }
- else if c == 'X' then
- { i: i + 1, v: 'x', caps: true }
- else if c == 'e' then
- { i: i + 1, v: 'e', caps: false }
- else if c == 'E' then
- { i: i + 1, v: 'e', caps: true }
- else if c == 'f' then
- { i: i + 1, v: 'f', caps: false }
- else if c == 'F' then
- { i: i + 1, v: 'f', caps: true }
- else if c == 'g' then
- { i: i + 1, v: 'g', caps: false }
- else if c == 'G' then
- { i: i + 1, v: 'g', caps: true }
- else if c == 'c' then
- { i: i + 1, v: 'c', caps: false }
- else if c == 's' then
- { i: i + 1, v: 's', caps: false }
- else if c == '%' then
- { i: i + 1, v: '%', caps: false }
- else
- error 'Unrecognised conversion type: ' + c;
-
-
- // Parsed initial %, now the rest.
- local parse_code(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local mkey = try_parse_mapping_key(str, i);
- local cflags = try_parse_cflags(str, mkey.i);
- local fw = try_parse_field_width(str, cflags.i);
- local prec = try_parse_precision(str, fw.i);
- local len_mod = try_parse_length_modifier(str, prec.i);
- local ctype = parse_conv_type(str, len_mod);
- {
- i: ctype.i,
- code: {
- mkey: mkey.v,
- cflags: cflags.v,
- fw: fw.v,
- prec: prec.v,
- ctype: ctype.v,
- caps: ctype.caps,
- },
- };
-
- // Parse a format string (containing none or more % format tags).
- local parse_codes(str, i, out, cur) =
- if i >= std.length(str) then
- out + [cur]
- else
- local c = str[i];
- if c == '%' then
- local r = parse_code(str, i + 1);
- parse_codes(str, r.i, out + [cur, r.code], '') tailstrict
- else
- parse_codes(str, i + 1, out, cur + c) tailstrict;
-
- local codes = parse_codes(str, 0, [], '');
-
-
- ///////////////////////
- // Format the values //
- ///////////////////////
-
- // Useful utilities
- local padding(w, s) =
- local aux(w, v) =
- if w <= 0 then
- v
- else
- aux(w - 1, v + s);
- aux(w, '');
-
- // Add s to the left of str so that its length is at least w.
- local pad_left(str, w, s) =
- padding(w - std.length(str), s) + str;
-
- // Add s to the right of str so that its length is at least w.
- local pad_right(str, w, s) =
- str + padding(w - std.length(str), s);
-
- // Render an integer (e.g., decimal or octal).
- local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
- local n_ = std.abs(n__);
- local aux(n) =
- if n == 0 then
- zero_prefix
- else
- aux(std.floor(n / radix)) + (n % radix);
- local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
- local neg = n__ < 0;
- local zp = min_chars - (if neg || blank || sign then 1 else 0);
- local zp2 = std.max(zp, min_digits);
- local dec2 = pad_left(dec, zp2, '0');
- (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;
-
- // Render an integer in hexadecimal.
- local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =
- local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']
- else ['a', 'b', 'c', 'd', 'e', 'f'];
- local n_ = std.abs(n__);
- local aux(n) =
- if n == 0 then
- ''
- else
- aux(std.floor(n / 16)) + numerals[n % 16];
- local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
- local neg = n__ < 0;
- local zp = min_chars - (if neg || blank || sign then 1 else 0)
- - (if add_zerox then 2 else 0);
- local zp2 = std.max(zp, min_digits);
- local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')
- + pad_left(hex, zp2, '0');
- (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;
-
- local strip_trailing_zero(str) =
- local aux(str, i) =
- if i < 0 then
- ''
- else
- if str[i] == '0' then
- aux(str, i - 1)
- else
- std.substr(str, 0, i + 1);
- aux(str, std.length(str) - 1);
-
- // Render floating point in decimal form
- local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =
- local n_ = std.abs(n__);
- local whole = std.floor(n_);
- local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
- local zp = zero_pad - prec - dot_size;
- local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');
- if prec == 0 then
- str + if ensure_pt then '.' else ''
- else
- local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);
- if trailing || frac > 0 then
- local frac_str = render_int(frac, prec, 0, false, false, 10, '');
- str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
- else
- str;
-
- // Render floating point in scientific form
- local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =
- local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));
- local suff = (if caps then 'E' else 'e')
- + render_int(exponent, 3, 0, false, true, 10, '');
- local mantissa = if exponent == -324 then
- // Avoid a rounding error where std.pow(10, -324) is 0
- // -324 is the smallest exponent possible.
- n__ * 10 / std.pow(10, exponent + 1)
- else
- n__ / std.pow(10, exponent);
- local zp2 = zero_pad - std.length(suff);
- render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;
-
- // Render a value with an arbitrary format code.
- local format_code(val, code, fw, prec_or_null, i) =
- local cflags = code.cflags;
- local fpprec = if prec_or_null != null then prec_or_null else 6;
- local iprec = if prec_or_null != null then prec_or_null else 0;
- local zp = if cflags.zero && !cflags.left then fw else 0;
- if code.ctype == 's' then
- std.toString(val)
- else if code.ctype == 'd' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')
- else if code.ctype == 'o' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- local zero_prefix = if cflags.alt then '0' else '';
- render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)
- else if code.ctype == 'x' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_hex(val,
- zp,
- iprec,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- code.caps)
- else if code.ctype == 'f' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_float_dec(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- true,
- fpprec)
- else if code.ctype == 'e' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_float_sci(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- true,
- code.caps,
- fpprec)
- else if code.ctype == 'g' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
- if exponent < -4 || exponent >= fpprec then
- render_float_sci(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- cflags.alt,
- code.caps,
- fpprec - 1)
- else
- local digits_before_pt = std.max(1, exponent + 1);
- render_float_dec(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- cflags.alt,
- fpprec - digits_before_pt)
- else if code.ctype == 'c' then
- if std.type(val) == 'number' then
- std.char(val)
- else if std.type(val) == 'string' then
- if std.length(val) == 1 then
- val
- else
- error '%c expected 1-sized string got: ' + std.length(val)
- else
- error '%c expected number / string, got: ' + std.type(val)
- else
- error 'Unknown code: ' + code.ctype;
+ format:: $intrinsic(format),
- // Render a parsed format string with an array of values.
- local format_codes_arr(codes, arr, i, j, v) =
- if i >= std.length(codes) then
- if j < std.length(arr) then
- error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)
- else
- v
- else
- local code = codes[i];
- if std.type(code) == 'string' then
- format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
- else
- local tmp = if code.fw == '*' then {
- j: j + 1,
- fw: if j >= std.length(arr) then
- error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
- else
- arr[j],
- } else {
- j: j,
- fw: code.fw,
- };
- local tmp2 = if code.prec == '*' then {
- j: tmp.j + 1,
- prec: if tmp.j >= std.length(arr) then
- error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
- else
- arr[tmp.j],
- } else {
- j: tmp.j,
- prec: code.prec,
- };
- local j2 = tmp2.j;
- local val =
- if j2 < std.length(arr) then
- arr[j2]
- else
- error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);
- local s =
- if code.ctype == '%' then
- '%'
- else
- format_code(val, code, tmp.fw, tmp2.prec, j2);
- local s_padded =
- if code.cflags.left then
- pad_right(s, tmp.fw, ' ')
- else
- pad_left(s, tmp.fw, ' ');
- local j3 =
- if code.ctype == '%' then
- j2
- else
- j2 + 1;
- format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
+ foldr:: $intrinsic(foldr),
- // Render a parsed format string with an object of values.
- local format_codes_obj(codes, obj, i, v) =
- if i >= std.length(codes) then
- v
- else
- local code = codes[i];
- if std.type(code) == 'string' then
- format_codes_obj(codes, obj, i + 1, v + code) tailstrict
- else
- local f =
- if code.mkey == null then
- error 'Mapping keys required.'
- else
- code.mkey;
- local fw =
- if code.fw == '*' then
- error 'Cannot use * field width with object.'
- else
- code.fw;
- local prec =
- if code.prec == '*' then
- error 'Cannot use * precision with object.'
- else
- code.prec;
- local val =
- if std.objectHasAll(obj, f) then
- obj[f]
- else
- error 'No such field: ' + f;
- local s =
- if code.ctype == '%' then
- '%'
- else
- format_code(val, code, fw, prec, f);
- local s_padded =
- if code.cflags.left then
- pad_right(s, fw, ' ')
- else
- pad_left(s, fw, ' ');
- format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;
-
- if std.isArray(vals) then
- format_codes_arr(codes, vals, 0, 0, '')
- else if std.isObject(vals) then
- format_codes_obj(codes, vals, 0, '')
- else
- format_codes_arr(codes, [vals], 0, 0, ''),
-
- foldr(func, arr, init)::
- local aux(func, arr, running, idx) =
- if idx < 0 then
- running
- else
- aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;
- aux(func, arr, init, std.length(arr) - 1),
+ foldl:: $intrinsic(foldl),
- foldl(func, arr, init)::
- local aux(func, arr, running, idx) =
- if idx >= std.length(arr) then
- running
- else
- aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;
- aux(func, arr, init, 0),
-
-
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))
@@ -912,30 +375,7 @@
else
error 'TOML body must be an object. Got ' + std.type(value),
- escapeStringJson(str_)::
- local str = std.toString(str_);
- local trans(ch) =
- if ch == '"' then
- '\\"'
- else if ch == '\\' then
- '\\\\'
- else if ch == '\b' then
- '\\b'
- else if ch == '\f' then
- '\\f'
- else if ch == '\n' then
- '\\n'
- else if ch == '\r' then
- '\\r'
- else if ch == '\t' then
- '\\t'
- else
- local cp = std.codepoint(ch);
- if cp < 32 || (cp >= 127 && cp <= 159) then
- '\\u%04x' % [cp]
- else
- ch;
- '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),
+ escapeStringJson:: $intrinsic(escapeStringJson),
escapeStringPython(str)::
std.escapeStringJson(str),
@@ -960,42 +400,7 @@
manifestJson(value):: std.manifestJsonEx(value, ' '),
- manifestJsonEx(value, indent)::
- local aux(v, path, cindent) =
- if v == true then
- 'true'
- else if v == false then
- 'false'
- else if v == null then
- 'null'
- else if std.isNumber(v) then
- '' + v
- else if std.isString(v) then
- std.escapeStringJson(v)
- else if std.isFunction(v) then
- error 'Tried to manifest function at ' + path
- else if std.isArray(v) then
- local range = std.range(0, std.length(v) - 1);
- local new_indent = cindent + indent;
- local lines = ['[\n']
- + std.join([',\n'],
- [
- [new_indent + aux(v[i], path + [i], new_indent)]
- for i in range
- ])
- + ['\n' + cindent + ']'];
- std.join('', lines)
- else if std.isObject(v) then
- local lines = ['{\n']
- + std.join([',\n'],
- [
- [cindent + indent + std.escapeStringJson(k) + ': '
- + aux(v[k], path + [k], cindent + indent)]
- for k in std.objectFields(v)
- ])
- + ['\n' + cindent + '}'];
- std.join('', lines);
- aux(value, [], ''),
+ manifestJsonEx:: $intrinsic(manifestJsonEx),
manifestYamlDoc(value, indent_array_in_object=false)::
local aux(v, path, cindent) =
@@ -1136,52 +541,7 @@
local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
- base64(input)::
- local bytes =
- if std.isString(input) then
- std.map(function(c) std.codepoint(c), input)
- else
- input;
-
- local aux(arr, i, r) =
- if i >= std.length(arr) then
- r
- else if i + 1 >= std.length(arr) then
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i
- base64_table[(arr[i] & 3) << 4] +
- '==';
- aux(arr, i + 3, r + str) tailstrict
- else if i + 2 >= std.length(arr) then
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i, 4 MSB of i+1
- base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
- // 4 LSB of i+1
- base64_table[(arr[i + 1] & 15) << 2] +
- '=';
- aux(arr, i + 3, r + str) tailstrict
- else
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i, 4 MSB of i+1
- base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
- // 4 LSB of i+1, 2 MSB of i+2
- base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +
- // 6 LSB of i+2
- base64_table[(arr[i + 2] & 63)];
- aux(arr, i + 3, r + str) tailstrict;
-
- local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);
- if !sanity then
- error 'Can only base64 encode strings / arrays of single bytes.'
- else
- aux(bytes, 0, ''),
-
+ base64:: $intrinsic(base64),
base64DecodeBytes(str)::
if std.length(str) % 4 != 0 then
@@ -1207,47 +567,11 @@
base64Decode(str)::
local bytes = std.base64DecodeBytes(str);
std.join('', std.map(function(b) std.char(b), bytes)),
-
- reverse(arr)::
- local l = std.length(arr);
- std.makeArray(l, function(i) arr[l - i - 1]),
- // Merge-sort for long arrays and naive quicksort for shorter ones
- sortImpl(arr, keyF)::
- local quickSort(arr, keyF=id) =
- local l = std.length(arr);
- if std.length(arr) <= 1 then
- arr
- else
- local pos = 0;
- local pivot = keyF(arr[pos]);
- local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);
- local left = std.filter(function(x) keyF(x) < pivot, rest);
- local right = std.filter(function(x) keyF(x) >= pivot, rest);
- quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);
+ reverse:: $intrinsic(reverse),
- local merge(a, b) =
- local la = std.length(a), lb = std.length(b);
- local aux(i, j, prefix) =
- if i == la then
- prefix + b[j:]
- else if j == lb then
- prefix + a[i:]
- else
- if keyF(a[i]) <= keyF(b[j]) then
- aux(i + 1, j, prefix + [a[i]]) tailstrict
- else
- aux(i, j + 1, prefix + [b[j]]) tailstrict;
- aux(0, 0, []);
+ sortImpl:: $intrinsic(sortImpl),
- local l = std.length(arr);
- if std.length(arr) <= 30 then
- quickSort(arr, keyF=keyF)
- else
- local mid = std.floor(l / 2);
- local left = arr[:mid], right = arr[mid:];
- merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
-
sort(arr, keyF=id)::
std.sortImpl(arr, keyF),
@@ -1356,42 +680,7 @@
objectValuesAll(o)::
[o[k] for k in std.objectFieldsAll(o)],
- equals(a, b)::
- local ta = std.type(a);
- local tb = std.type(b);
- if !std.primitiveEquals(ta, tb) then
- false
- else
- if std.primitiveEquals(ta, 'array') then
- local la = std.length(a);
- if !std.primitiveEquals(la, std.length(b)) then
- false
- else
- local aux(a, b, i) =
- if i >= la then
- true
- else if a[i] != b[i] then
- false
- else
- aux(a, b, i + 1) tailstrict;
- aux(a, b, 0)
- else if std.primitiveEquals(ta, 'object') then
- local fields = std.objectFields(a);
- local lfields = std.length(fields);
- if fields != std.objectFields(b) then
- false
- else
- local aux(a, b, i) =
- if i >= lfields then
- true
- else if local f = fields[i]; a[f] != b[f] then
- false
- else
- aux(a, b, i + 1) tailstrict;
- aux(a, b, 0)
- else
- std.primitiveEquals(a, b),
-
+ equals:: $intrinsic(equals),
resolvePath(f, r)::
local arr = std.split(f, '/');