difftreelog
perf unify function calls
in: master
8 files changed
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -39,7 +39,7 @@
if **name == *"join" || **name == *"manifestJsonEx" ||
**name == *"escapeStringJson" || **name == *"equals" ||
**name == *"base64" || **name == *"foldl" || **name == *"foldr" ||
- **name == *"sortImpl" || **name == *"format" || **name == *"range"
+ **name == *"sortImpl" || **name == *"format" || **name == *"range" || **name == *"reverse"
)
})
.collect(),
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -117,9 +117,7 @@
}
buf.push('}');
}
- Val::Func(_) | Val::Intristic(_, _) | Val::NativeExt(_, _) => {
- throw!(RuntimeError("tried to manifest function".into()))
- }
+ Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
Val::Lazy(_) => unreachable!(),
};
Ok(())
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,5 +1,354 @@
+use crate::{
+ equals,
+ error::{Error::*, Result},
+ evaluate, parse_args, primitive_equals, push, throw, with_state, Context, FuncVal, Val,
+ ValType,
+};
+use format::{format_arr, format_obj};
+use jrsonnet_parser::{ArgsDesc, ExprLocation};
+use manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};
+use std::{path::PathBuf, rc::Rc};
+
pub mod stdlib;
pub use stdlib::*;
pub mod format;
pub mod manifest;
+pub mod sort;
+
+pub fn call_builtin(
+ context: Context,
+ loc: &Option<ExprLocation>,
+ ns: &str,
+ name: &str,
+ args: &ArgsDesc,
+) -> Result<Val> {
+ Ok(match (ns, &name as &str) {
+ // arr/string/function
+ ("std", "length") => parse_args!(context, "std.length", args, 1, [
+ 0, x: [Val::Str|Val::Arr|Val::Obj], vec![ValType::Str, ValType::Arr, ValType::Obj];
+ ], {
+ Ok(match x {
+ Val::Str(n) => Val::Num(n.chars().count() as f64),
+ Val::Arr(i) => Val::Num(i.len() as f64),
+ Val::Obj(o) => Val::Num(
+ o.fields_visibility()
+ .into_iter()
+ .filter(|(_k, v)| *v)
+ .count() as f64,
+ ),
+ _ => unreachable!(),
+ })
+ })?,
+ // any
+ ("std", "type") => parse_args!(context, "std.type", args, 1, [
+ 0, x, vec![];
+ ], {
+ Ok(Val::Str(x.value_type()?.name().into()))
+ })?,
+ // length, idx=>any
+ ("std", "makeArray") => parse_args!(context, "std.makeArray", args, 2, [
+ 0, sz: [Val::Num]!!Val::Num, vec![ValType::Num];
+ 1, func: [Val::Func]!!Val::Func, vec![ValType::Func];
+ ], {
+ if sz < 0.0 {
+ throw!(RuntimeError(format!("makeArray requires size >= 0, got {}", sz).into()));
+ }
+ let mut out = Vec::with_capacity(sz as usize);
+ for i in 0..sz as usize {
+ out.push(func.evaluate_values(
+ Context::new(),
+ &[Val::Num(i as f64)]
+ )?)
+ }
+ Ok(Val::Arr(Rc::new(out)))
+ })?,
+ // string
+ ("std", "codepoint") => parse_args!(context, "std.codepoint", args, 1, [
+ 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ assert!(
+ str.chars().count() == 1,
+ "std.codepoint should receive single char string"
+ );
+ Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))
+ })?,
+ // object, includeHidden
+ ("std", "objectFieldsEx") => parse_args!(context, "std.objectFieldsEx",args, 2, [
+ 0, obj: [Val::Obj]!!Val::Obj, vec![ValType::Obj];
+ 1, inc_hidden: [Val::Bool]!!Val::Bool, vec![ValType::Bool];
+ ], {
+ let mut out = obj.fields_visibility()
+ .into_iter()
+ .filter(|(_k, v)| *v || inc_hidden)
+ .map(|(k, _v)|k)
+ .collect::<Vec<_>>();
+ out.sort();
+ Ok(Val::Arr(Rc::new(out.into_iter().map(Val::Str).collect())))
+ })?,
+ // object, field, includeHidden
+ ("std", "objectHasEx") => parse_args!(context, "std.objectHasEx", args, 3, [
+ 0, obj: [Val::Obj]!!Val::Obj, vec![ValType::Obj];
+ 1, f: [Val::Str]!!Val::Str, vec![ValType::Str];
+ 2, inc_hidden: [Val::Bool]!!Val::Bool, vec![ValType::Bool];
+ ], {
+ Ok(Val::Bool(
+ obj.fields_visibility()
+ .into_iter()
+ .filter(|(_k, v)| *v || inc_hidden)
+ .any(|(k, _v)| *k == *f),
+ ))
+ })?,
+ ("std", "primitiveEquals") => parse_args!(context, "std.primitiveEquals", args, 2, [
+ 0, a, vec![];
+ 1, b, vec![];
+ ], {
+ Ok(Val::Bool(primitive_equals(&a, &b)?))
+ })?,
+ // faster
+ ("std", "equals") => parse_args!(context, "std.equals", args, 2, [
+ 0, a, vec![];
+ 1, b, vec![];
+ ], {
+ Ok(Val::Bool(equals(&a, &b)?))
+ })?,
+ ("std", "modulo") => parse_args!(context, "std.modulo", args, 2, [
+ 0, a: [Val::Num]!!Val::Num, vec![ValType::Num];
+ 1, b: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ Ok(Val::Num(a % b))
+ })?,
+ ("std", "floor") => parse_args!(context, "std.floor", args, 1, [
+ 0, x: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ Ok(Val::Num(x.floor()))
+ })?,
+ ("std", "log") => parse_args!(context, "std.log", args, 2, [
+ 0, n: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ Ok(Val::Num(n.ln()))
+ })?,
+ ("std", "trace") => parse_args!(context, "std.trace", args, 2, [
+ 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
+ 1, rest, vec![];
+ ], {
+ eprint!("TRACE:");
+ if let Some(loc) = loc {
+ with_state(|s|{
+ let locs = s.map_source_locations(&loc.0, &[loc.1]);
+ eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);
+ });
+ }
+ eprintln!(" {}", str);
+ Ok(rest)
+ })?,
+ ("std", "pow") => parse_args!(context, "std.modulo", args, 2, [
+ 0, x: [Val::Num]!!Val::Num, vec![ValType::Num];
+ 1, n: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ Ok(Val::Num(x.powf(n)))
+ })?,
+ ("std", "extVar") => parse_args!(context, "std.extVar", args, 1, [
+ 0, x: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or_else(
+ || UndefinedExternalVariable(x),
+ )?)
+ })?,
+ ("std", "native") => parse_args!(context, "std.native", args, 1, [
+ 0, x: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(FuncVal::NativeExt(x.clone(), v))).ok_or_else(
+ || UndefinedExternalFunction(x),
+ )?)
+ })?,
+ ("std", "filter") => parse_args!(context, "std.filter", args, 2, [
+ 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
+ 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ ], {
+ Ok(Val::Arr(Rc::new(
+ arr.iter()
+ .cloned()
+ .filter(|e| {
+ func
+ .evaluate_values(context.clone(), &[e.clone()])
+ .unwrap()
+ .try_cast_bool("filter predicate")
+ .unwrap()
+ })
+ .collect(),
+ )))
+ })?,
+ // faster
+ ("std", "foldl") => parse_args!(context, "std.foldl", args, 3, [
+ 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
+ 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ 2, init, vec![];
+ ], {
+ let mut acc = init;
+ for i in arr.iter().cloned() {
+ acc = func.evaluate_values(context.clone(), &[acc, i])?;
+ }
+ Ok(acc)
+ })?,
+ // faster
+ ("std", "foldr") => parse_args!(context, "std.foldr", args, 3, [
+ 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
+ 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ 2, init, vec![];
+ ], {
+ let mut acc = init;
+ for i in arr.iter().rev().cloned() {
+ acc = func.evaluate_values(context.clone(), &[acc, i])?;
+ }
+ Ok(acc)
+ })?,
+ // faster
+ #[allow(non_snake_case)]
+ ("std", "sortImpl") => parse_args!(context, "std.sort", args, 2, [
+ 0, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ 1, keyF: [Val::Func]!!Val::Func, vec![ValType::Func];
+ ], {
+ if arr.len() <= 1 {
+ return Ok(Val::Arr(arr))
+ }
+ Ok(Val::Arr(sort::sort(context, arr, keyF)?))
+ })?,
+ // faster
+ ("std", "format") => parse_args!(context, "std.format", args, 2, [
+ 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
+ 1, vals, vec![]
+ ], {
+ push(&Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)), ||format!("std.format of {}", str), ||{
+ Ok(match vals {
+ Val::Arr(vals) => Val::Str(format_arr(&str, &vals)?.into()),
+ Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),
+ o => Val::Str(format_arr(&str, &[o])?.into()),
+ })
+ })
+ })?,
+ // faster
+ ("std", "range") => parse_args!(context, "std.range", args, 2, [
+ 0, from: [Val::Num]!!Val::Num, vec![ValType::Num];
+ 1, to: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));
+ for i in from as usize..=to as usize {
+ out.push(Val::Num(i as f64));
+ }
+ Ok(Val::Arr(Rc::new(out)))
+ })?,
+ ("std", "char") => parse_args!(context, "std.char", args, 1, [
+ 0, n: [Val::Num]!!Val::Num, vec![ValType::Num];
+ ], {
+ let mut out = String::new();
+ out.push(std::char::from_u32(n as u32).ok_or_else(||
+ InvalidUnicodeCodepointGot(n as u32)
+ )?);
+ Ok(Val::Str(out.into()))
+ })?,
+ ("std", "encodeUTF8") => parse_args!(context, "std.encodeUtf8", args, 1, [
+ 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(Val::Arr(Rc::new(str.bytes().map(|b| Val::Num(b as f64)).collect())))
+ })?,
+ ("std", "md5") => parse_args!(context, "std.md5", args, 1, [
+ 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))
+ })?,
+ // faster
+ ("std", "base64") => parse_args!(context, "std.base64", args, 1, [
+ 0, input: [Val::Str | Val::Arr], vec![ValType::Arr, ValType::Str];
+ ], {
+ Ok(Val::Str(match input {
+ Val::Str(s) => {
+ base64::encode(s.bytes().collect::<Vec<_>>()).into()
+ },
+ Val::Arr(a) => {
+ base64::encode(a.iter().map(|v| {
+ Ok(v.clone().try_cast_num("base64 array")? as u8)
+ }).collect::<Result<Vec<_>>>()?).into()
+ },
+ _ => unreachable!()
+ }))
+ })?,
+ // faster
+ ("std", "join") => parse_args!(context, "std.join", args, 2, [
+ 0, sep: [Val::Str|Val::Arr], vec![ValType::Str, ValType::Arr];
+ 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ ], {
+ Ok(match sep {
+ Val::Arr(joiner_items) => {
+ let mut out = Vec::new();
+
+ let mut first = true;
+ for item in arr.iter().cloned() {
+ if let Val::Arr(items) = item.unwrap_if_lazy()? {
+ if !first {
+ out.reserve(joiner_items.len());
+ out.extend(joiner_items.iter().cloned());
+ }
+ first = false;
+ out.reserve(items.len());
+ out.extend(items.iter().cloned());
+ } else {
+ throw!(RuntimeError("in std.join all items should be arrays".into()));
+ }
+ }
+
+ Val::Arr(Rc::new(out))
+ },
+ Val::Str(sep) => {
+ let mut out = String::new();
+
+ let mut first = true;
+ for item in arr.iter().cloned() {
+ if let Val::Str(item) = item.unwrap_if_lazy()? {
+ if !first {
+ out += &sep;
+ }
+ first = false;
+ out += &item;
+ } else {
+ throw!(RuntimeError("in std.join all items should be strings".into()));
+ }
+ }
+
+ Val::Str(out.into())
+ },
+ _ => unreachable!()
+ })
+ })?,
+ // Faster
+ ("std", "escapeStringJson") => parse_args!(context, "std.escapeStringJson", args, 1, [
+ 0, str_: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(Val::Str(escape_string_json(&str_).into()))
+ })?,
+ // Faster
+ ("std", "manifestJsonEx") => parse_args!(context, "std.manifestJsonEx", args, 2, [
+ 0, value, vec![];
+ 1, indent: [Val::Str]!!Val::Str, vec![ValType::Str];
+ ], {
+ Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {
+ padding: &indent,
+ mtype: ManifestType::Std,
+ })?.into()))
+ })?,
+ // Faster
+ ("std", "reverse") => parse_args!(context, "std.reverse", args, 1, [
+ 0, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
+ ], {
+ let mut marr = arr;
+ Rc::make_mut(&mut marr).reverse();
+ Ok(Val::Arr(marr))
+ })?,
+ ("std", "id") => parse_args!(context, "std.id", args, 1, [
+ 0, v, vec![];
+ ], {
+ Ok(v)
+ })?,
+ (ns, name) => throw!(IntristicNotFound(ns.into(), name.into())),
+ })
+}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,4 +1,7 @@
-use crate::{builtin::format::FormatError, ValType};
+use crate::{
+ builtin::{format::FormatError, sort::SortError},
+ ValType,
+};
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use std::{path::PathBuf, rc::Rc};
@@ -69,6 +72,7 @@
InvalidUnicodeCodepointGot(u32),
Format(FormatError),
+ Sort(SortError),
}
impl From<Error> for LocError {
fn from(e: Error) -> Self {
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,13 +1,7 @@
use crate::{
- builtin::{
- format::{format_arr, format_obj},
- manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType},
- },
- context_creator, equals,
- error::Error::*,
- future_wrapper, lazy_val, parse_args, parse_function_call, primitive_equals, push, throw,
- with_state, Context, ContextCreator, FuncDesc, LazyBinding, LazyVal, LocError, ObjMember,
- ObjValue, Result, Val, ValType,
+ context_creator, error::Error::*, future_wrapper, lazy_val, push, throw, with_state, Context,
+ ContextCreator, FuncDesc, FuncVal, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+ ValType,
};
use closure::closure;
use jrsonnet_parser::{
@@ -15,7 +9,7 @@
ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
Visibility,
};
-use std::{cmp::Ordering, collections::HashMap, path::PathBuf, rc::Rc};
+use std::{collections::HashMap, rc::Rc};
pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (Rc<str>, LazyBinding) {
let b = b.clone();
@@ -51,12 +45,12 @@
}
pub fn evaluate_method(ctx: Context, name: Rc<str>, params: ParamsDesc, body: LocExpr) -> Val {
- Val::Func(Rc::new(FuncDesc {
+ Val::Func(FuncVal::Normal(Rc::new(FuncDesc {
name,
ctx,
params,
body,
- }))
+ })))
}
pub fn evaluate_field_name(
@@ -389,21 +383,6 @@
})
}
-/// Extracts code block and disables inlining for them
-/// Fixes WASM to java bytecode compilation failing because of very large method
-#[cfg(feature = "unstable")]
-macro_rules! noinline {
- ($e:expr) => {
- (#![inline(never)] move || $e)()
- };
-}
-#[cfg(not(feature = "unstable"))]
-macro_rules! noinline {
- ($e:expr) => {
- (move || $e)()
- };
-}
-
pub fn evaluate_apply(
context: Context,
value: &LocExpr,
@@ -414,397 +393,12 @@
let lazy = evaluate(context.clone(), value)?;
let value = lazy.unwrap_if_lazy()?;
Ok(match value {
- Val::Intristic(ns, name) => push(
- loc,
- || format!("intristic <{}.{}> call", ns, name),
- || {
- Ok(match (&ns as &str, &name as &str) {
- // arr/string/function
- ("std", "length") => parse_args!(context, "std.length", args, 1, [
- 0, x: [Val::Str|Val::Arr|Val::Obj], vec![ValType::Str, ValType::Arr, ValType::Obj];
- ], {
- Ok(match x {
- Val::Str(n) => Val::Num(n.chars().count() as f64),
- Val::Arr(i) => Val::Num(i.len() as f64),
- Val::Obj(o) => Val::Num(
- o.fields_visibility()
- .into_iter()
- .filter(|(_k, v)| *v)
- .count() as f64,
- ),
- _ => unreachable!(),
- })
- })?,
- // any
- ("std", "type") => parse_args!(context, "std.type", args, 1, [
- 0, x, vec![];
- ], {
- Ok(Val::Str(x.value_type()?.name().into()))
- })?,
- // length, idx=>any
- ("std", "makeArray") => {
- noinline!(parse_args!(context, "std.makeArray", args, 2, [
- 0, sz: [Val::Num]!!Val::Num, vec![ValType::Num];
- 1, func: [Val::Func]!!Val::Func, vec![ValType::Func];
- ], {
- if sz < 0.0 {
- throw!(RuntimeError(format!("makeArray requires size >= 0, got {}", sz).into()));
- }
- let mut out = Vec::with_capacity(sz as usize);
- for i in 0..sz as usize {
- out.push(func.evaluate_values(
- Context::new(),
- &[Val::Num(i as f64)]
- )?)
- }
- Ok(Val::Arr(Rc::new(out)))
- }))?
- }
- // string
- ("std", "codepoint") => parse_args!(context, "std.codepoint", args, 1, [
- 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- assert!(
- str.chars().count() == 1,
- "std.codepoint should receive single char string"
- );
- Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))
- })?,
- // object, includeHidden
- ("std", "objectFieldsEx") => {
- noinline!(parse_args!(context, "std.objectFieldsEx",args, 2, [
- 0, obj: [Val::Obj]!!Val::Obj, vec![ValType::Obj];
- 1, inc_hidden: [Val::Bool]!!Val::Bool, vec![ValType::Bool];
- ], {
- let mut out = obj.fields_visibility()
- .into_iter()
- .filter(|(_k, v)| *v || inc_hidden)
- .map(|(k, _v)|k)
- .collect::<Vec<_>>();
- out.sort();
- Ok(Val::Arr(Rc::new(out.into_iter().map(Val::Str).collect())))
- }))?
- }
- // object, field, includeHidden
- ("std", "objectHasEx") => parse_args!(context, "std.objectHasEx", args, 3, [
- 0, obj: [Val::Obj]!!Val::Obj, vec![ValType::Obj];
- 1, f: [Val::Str]!!Val::Str, vec![ValType::Str];
- 2, inc_hidden: [Val::Bool]!!Val::Bool, vec![ValType::Bool];
- ], {
- Ok(Val::Bool(
- obj.fields_visibility()
- .into_iter()
- .filter(|(_k, v)| *v || inc_hidden)
- .any(|(k, _v)| *k == *f),
- ))
- })?,
- ("std", "primitiveEquals") => {
- parse_args!(context, "std.primitiveEquals", args, 2, [
- 0, a, vec![];
- 1, b, vec![];
- ], {
- Ok(Val::Bool(primitive_equals(&a, &b)?))
- })?
- }
- // faster
- ("std", "equals") => parse_args!(context, "std.equals", args, 2, [
- 0, a, vec![];
- 1, b, vec![];
- ], {
- Ok(Val::Bool(equals(&a, &b)?))
- })?,
- ("std", "modulo") => parse_args!(context, "std.modulo", args, 2, [
- 0, a: [Val::Num]!!Val::Num, vec![ValType::Num];
- 1, b: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- Ok(Val::Num(a % b))
- })?,
- ("std", "floor") => parse_args!(context, "std.floor", args, 1, [
- 0, x: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- Ok(Val::Num(x.floor()))
- })?,
- ("std", "log") => parse_args!(context, "std.log", args, 2, [
- 0, n: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- Ok(Val::Num(n.ln()))
- })?,
- ("std", "trace") => parse_args!(context, "std.trace", args, 2, [
- 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
- 1, rest, vec![];
- ], {
- eprint!("TRACE:");
- if let Some(loc) = loc {
- with_state(|s|{
- let locs = s.map_source_locations(&loc.0, &[loc.1]);
- eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);
- });
- }
- eprintln!(" {}", str);
- Ok(rest)
- })?,
- ("std", "pow") => parse_args!(context, "std.modulo", args, 2, [
- 0, x: [Val::Num]!!Val::Num, vec![ValType::Num];
- 1, n: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- Ok(Val::Num(x.powf(n)))
- })?,
- ("std", "extVar") => parse_args!(context, "std.extVar", args, 1, [
- 0, x: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or_else(
- || UndefinedExternalVariable(x),
- )?)
- })?,
- ("std", "native") => parse_args!(context, "std.native", args, 1, [
- 0, x: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::NativeExt(x.clone(), v)).ok_or_else(
- || UndefinedExternalFunction(x),
- )?)
- })?,
- ("std", "filter") => noinline!(parse_args!(context, "std.filter", args, 2, [
- 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
- 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
- ], {
- Ok(Val::Arr(Rc::new(
- arr.iter()
- .cloned()
- .filter(|e| {
- func
- .evaluate_values(context.clone(), &[e.clone()])
- .unwrap()
- .try_cast_bool("filter predicate")
- .unwrap()
- })
- .collect(),
- )))
- }))?,
- // faster
- ("std", "foldl") => noinline!(parse_args!(context, "std.foldl", args, 3, [
- 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
- 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
- 2, init, vec![];
- ], {
- let mut acc = init;
- for i in arr.iter().cloned() {
- acc = func.evaluate_values(context.clone(), &[acc, i])?;
- }
- Ok(acc)
- }))?,
- // faster
- ("std", "foldr") => noinline!(parse_args!(context, "std.foldr", args, 3, [
- 0, func: [Val::Func]!!Val::Func, vec![ValType::Func];
- 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
- 2, init, vec![];
- ], {
- let mut acc = init;
- for i in arr.iter().rev().cloned() {
- acc = func.evaluate_values(context.clone(), &[acc, i])?;
- }
- Ok(acc)
- }))?,
- // faster
- #[allow(non_snake_case)]
- ("std", "sortImpl") => noinline!(parse_args!(context, "std.sort", args, 2, [
- 0, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
- 1, keyF: [Val::Func]!!Val::Func, vec![ValType::Func];
- ], {
- if arr.len() <= 1 {
- return Ok(Val::Arr(arr))
- }
- let mut new_arr = arr.iter().cloned().collect::<Vec<_>>();
- match keyF.evaluate_values(context.clone(), &[new_arr[0].clone()])? {
- Val::Str(_) => {
- let mut err = None;
- new_arr.sort_by_cached_key(|k| {
- match keyF.evaluate_values(context.clone(), &[k.clone()]) {
- Ok(Val::Str(v)) => v,
- Ok(_) => {
- err = Some(LocError::new(RuntimeError("types of all array elements should equal".into())));
- "".into()
- }
- Err(e) => {
- err = Some(e);
- "".into()
- }
- }
- });
- if let Some(e) = err {
- return Err(e);
- }
- },
- Val::Num(_) => {
- let mut err = None;
- new_arr.sort_unstable_by(|a, b| {
- match (keyF.evaluate_values(context.clone(), &[a.clone()]), keyF.evaluate_values(context.clone(), &[b.clone()])) {
- (Ok(Val::Num(a)), Ok(Val::Num(b))) => a.partial_cmp(&b).unwrap(),
- (Ok(_a), Ok(_b)) => {
- err = Some(RuntimeError("types of all array elements should equal".into()).into());
- Ordering::Equal
- }
- (Err(e), _) | (_, Err(e)) => {
- err = Some(e);
- Ordering::Equal
- }
- }
- });
- if let Some(e) = err {
- return Err(e);
- }
- },
- _ => throw!(RuntimeError("keys should be number or string".into()))
- }
- Ok(Val::Arr(Rc::new(new_arr)))
- }))?,
- // faster
- ("std", "format") => parse_args!(context, "std.format", args, 2, [
- 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
- 1, vals, vec![]
- ], {
- push(&Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)), ||format!("std.format of {}", str), ||{
- Ok(match vals {
- Val::Arr(vals) => Val::Str(format_arr(&str, &vals)?.into()),
- Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),
- o => Val::Str(format_arr(&str, &[o])?.into()),
- })
- })
- })?,
- // faster
- ("std", "range") => parse_args!(context, "std.range", args, 2, [
- 0, from: [Val::Num]!!Val::Num, vec![ValType::Num];
- 1, to: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));
- for i in from as usize..=to as usize {
- out.push(Val::Num(i as f64));
- }
- Ok(Val::Arr(Rc::new(out)))
- })?,
- ("std", "char") => parse_args!(context, "std.char", args, 1, [
- 0, n: [Val::Num]!!Val::Num, vec![ValType::Num];
- ], {
- let mut out = String::new();
- out.push(std::char::from_u32(n as u32).ok_or_else(||
- InvalidUnicodeCodepointGot(n as u32)
- )?);
- Ok(Val::Str(out.into()))
- })?,
- ("std", "encodeUTF8") => parse_args!(context, "std.encodeUtf8", args, 1, [
- 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(Val::Arr(Rc::new(str.bytes().map(|b| Val::Num(b as f64)).collect())))
- })?,
- ("std", "md5") => noinline!(parse_args!(context, "std.md5", args, 1, [
- 0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))
- }))?,
- // faster
- ("std", "base64") => parse_args!(context, "std.base64", args, 1, [
- 0, input: [Val::Str | Val::Arr], vec![ValType::Arr, ValType::Str];
- ], {
- Ok(Val::Str(match input {
- Val::Str(s) => {
- base64::encode(s.bytes().collect::<Vec<_>>()).into()
- },
- Val::Arr(a) => {
- base64::encode(a.iter().map(|v| {
- Ok(v.clone().try_cast_num("base64 array")? as u8)
- }).collect::<Result<Vec<_>>>()?).into()
- },
- _ => unreachable!()
- }))
- })?,
- // faster
- ("std", "join") => noinline!(parse_args!(context, "std.join", args, 2, [
- 0, sep: [Val::Str|Val::Arr], vec![ValType::Str, ValType::Arr];
- 1, arr: [Val::Arr]!!Val::Arr, vec![ValType::Arr];
- ], {
- Ok(match sep {
- Val::Arr(joiner_items) => {
- let mut out = Vec::new();
-
- let mut first = true;
- for item in arr.iter().cloned() {
- if let Val::Arr(items) = item.unwrap_if_lazy()? {
- if !first {
- out.reserve(joiner_items.len());
- out.extend(joiner_items.iter().cloned());
- }
- first = false;
- out.reserve(items.len());
- out.extend(items.iter().cloned());
- } else {
- throw!(RuntimeError("in std.join all items should be arrays".into()));
- }
- }
-
- Val::Arr(Rc::new(out))
- },
- Val::Str(sep) => {
- let mut out = String::new();
-
- let mut first = true;
- for item in arr.iter().cloned() {
- if let Val::Str(item) = item.unwrap_if_lazy()? {
- if !first {
- out += &sep;
- }
- first = false;
- out += &item;
- } else {
- throw!(RuntimeError("in std.join all items should be strings".into()));
- }
- }
-
- Val::Str(out.into())
- },
- _ => unreachable!()
- })
- }))?,
- // Faster
- ("std", "escapeStringJson") => {
- parse_args!(context, "std.escapeStringJson", args, 1, [
- 0, str_: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(Val::Str(escape_string_json(&str_).into()))
- })?
- }
- // Faster
- ("std", "manifestJsonEx") => {
- parse_args!(context, "std.manifestJsonEx", args, 2, [
- 0, value, vec![];
- 1, indent: [Val::Str]!!Val::Str, vec![ValType::Str];
- ], {
- Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {
- padding: &indent,
- mtype: ManifestType::Std,
- })?.into()))
- })?
- }
- (ns, name) => throw!(IntristicNotFound(ns.into(), name.into())),
- })
- },
- )?,
- Val::NativeExt(n, f) => push(
- loc,
- || format!("native <{}> call", n),
- || {
- let args = parse_function_call(context, None, &f.params, args, true)?;
- let mut out_args = Vec::with_capacity(f.params.len());
- for p in f.params.0.iter() {
- out_args.push(args.binding(p.0.clone())?.evaluate()?);
- }
- Ok(f.call(&out_args)?)
- },
- )?,
Val::Func(f) => {
- let body = || f.evaluate(context, args, tailstrict);
+ let body = || f.evaluate(context, loc, args, tailstrict);
if tailstrict {
body()?
} else {
- push(loc, || format!("function <{}> call", f.name), body)?
+ push(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type()?)),
@@ -874,7 +468,7 @@
} else if let Some(Val::Str(n)) =
v.get("__intristic_namespace__".into())?
{
- Ok(Val::Intristic(n, s))
+ Ok(Val::Func(FuncVal::Intristic(n, s)))
} else {
throw!(NoSuchField(s))
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -40,9 +40,7 @@
}
Value::Object(out)
}
- Val::Func(_) | Val::Intristic(_, _) | Val::NativeExt(_, _) => {
- throw!(RuntimeError("tried to manifest function".into()))
- }
+ Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
})
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,12 +1,15 @@
use crate::{
- builtin::manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
+ builtin::{
+ call_builtin,
+ manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
+ },
error::Error::*,
evaluate,
function::{parse_function_call, parse_function_call_map, place_args},
native::NativeCallback,
throw, with_state, Context, ObjValue, Result,
};
-use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParamsDesc};
+use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LocExpr, ParamsDesc};
use std::{
cell::RefCell,
collections::HashMap,
@@ -67,18 +70,66 @@
pub params: ParamsDesc,
pub body: LocExpr,
}
-impl FuncDesc {
- /// This function is always inlined to make tailstrict work
- pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {
- let ctx = parse_function_call(
- call_ctx,
- Some(self.ctx.clone()),
- &self.params,
- args,
- tailstrict,
- )?;
- evaluate(ctx, &self.body)
+
+#[derive(Debug, Clone)]
+pub enum FuncVal {
+ /// Plain function implemented in jsonnet
+ Normal(Rc<FuncDesc>),
+ /// Standard library function
+ Intristic(Rc<str>, Rc<str>),
+ /// Library functions implemented in native
+ NativeExt(Rc<str>, Rc<NativeCallback>),
+}
+impl PartialEq for FuncVal {
+ fn eq(&self, other: &Self) -> bool {
+ match (self, other) {
+ (FuncVal::Normal(a), FuncVal::Normal(b)) => a == b,
+ (FuncVal::Intristic(ans, an), FuncVal::Intristic(bns, bn)) => ans == bns && an == bn,
+ (FuncVal::NativeExt(an, _), FuncVal::NativeExt(bn, _)) => an == bn,
+ (..) => false,
+ }
+ }
+}
+impl FuncVal {
+ pub fn is_ident(&self) -> bool {
+ matches!(&self, FuncVal::Intristic(ns, n) if ns as &str == "std" && n as &str == "id")
+ }
+ pub fn name(&self) -> Rc<str> {
+ match self {
+ FuncVal::Normal(normal) => normal.name.clone(),
+ FuncVal::Intristic(ns, name) => format!("intristic.{}.{}", ns, name).into(),
+ FuncVal::NativeExt(n, _) => format!("native.{}", n).into(),
+ }
}
+ pub fn evaluate(
+ &self,
+ call_ctx: Context,
+ loc: &Option<ExprLocation>,
+ args: &ArgsDesc,
+ tailstrict: bool,
+ ) -> Result<Val> {
+ match self {
+ FuncVal::Normal(func) => {
+ let ctx = parse_function_call(
+ call_ctx,
+ Some(func.ctx.clone()),
+ &func.params,
+ args,
+ tailstrict,
+ )?;
+ evaluate(ctx, &func.body)
+ }
+ FuncVal::Intristic(ns, name) => call_builtin(call_ctx, loc, &ns, &name, args),
+ FuncVal::NativeExt(_name, handler) => {
+ let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;
+ let mut out_args = Vec::with_capacity(handler.params.len());
+ for p in handler.params.0.iter() {
+ out_args.push(args.binding(p.0.clone())?.evaluate()?);
+ }
+ Ok(handler.call(&out_args)?)
+ }
+ }
+ }
pub fn evaluate_map(
&self,
@@ -86,19 +137,31 @@
args: &HashMap<Rc<str>, Val>,
tailstrict: bool,
) -> Result<Val> {
- let ctx = parse_function_call_map(
- call_ctx,
- Some(self.ctx.clone()),
- &self.params,
- args,
- tailstrict,
- )?;
- evaluate(ctx, &self.body)
+ match self {
+ FuncVal::Normal(func) => {
+ let ctx = parse_function_call_map(
+ call_ctx,
+ Some(func.ctx.clone()),
+ &func.params,
+ args,
+ tailstrict,
+ )?;
+ evaluate(ctx, &func.body)
+ }
+ FuncVal::Intristic(_, _) => todo!(),
+ FuncVal::NativeExt(_, _) => todo!(),
+ }
}
pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {
- let ctx = place_args(call_ctx, Some(self.ctx.clone()), &self.params, args)?;
- evaluate(ctx, &self.body)
+ match self {
+ FuncVal::Normal(func) => {
+ let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;
+ evaluate(ctx, &func.body)
+ }
+ FuncVal::Intristic(_, _) => todo!(),
+ FuncVal::NativeExt(_, _) => todo!(),
+ }
}
}
@@ -149,11 +212,7 @@
Lazy(LazyVal),
Arr(Rc<Vec<Val>>),
Obj(ObjValue),
- Func(Rc<FuncDesc>),
-
- // Library functions implemented in native
- Intristic(Rc<str>, Rc<str>),
- NativeExt(Rc<str>, Rc<NativeCallback>),
+ Func(FuncVal),
}
macro_rules! matches_unwrap {
($e: expr, $p: pat, $r: expr) => {
@@ -193,6 +252,12 @@
self.assert_type(context, ValType::Num)?;
Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Num(v), v))
}
+ pub fn inplace_unwrap(&mut self) -> Result<()> {
+ while let Val::Lazy(lazy) = self {
+ *self = lazy.evaluate()?;
+ }
+ Ok(())
+ }
pub fn unwrap_if_lazy(&self) -> Result<Self> {
Ok(if let Val::Lazy(v) = self {
v.evaluate()?.unwrap_if_lazy()?
@@ -208,7 +273,7 @@
Val::Obj(..) => ValType::Obj,
Val::Bool(_) => ValType::Bool,
Val::Null => ValType::Null,
- Val::Func(..) | Val::Intristic(_, _) | Val::NativeExt(_, _) => ValType::Func,
+ Val::Func(..) => ValType::Func,
Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,
})
}
@@ -374,7 +439,7 @@
}
fn is_function_like(val: &Val) -> bool {
- matches!(val, Val::Func(_) | Val::Intristic(_, _) | Val::NativeExt(_, _))
+ matches!(val, Val::Func(_))
}
/// Implements std.primitiveEquals builtin
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth1{2 __intristic_namespace__:: 'std',34 local std = self,5 local id = function(x) x,67 isString(v):: std.type(v) == 'string',8 isNumber(v):: std.type(v) == 'number',9 isBoolean(v):: std.type(v) == 'boolean',10 isObject(v):: std.type(v) == 'object',11 isArray(v):: std.type(v) == 'array',12 isFunction(v):: std.type(v) == 'function',1314 toString(a)::15 if std.type(a) == 'string' then a else '' + a,1617 substr(str, from, len)::18 assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);19 assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);20 assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);21 assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;22 std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),2324 startsWith(a, b)::25 if std.length(a) < std.length(b) then26 false27 else28 std.substr(a, 0, std.length(b)) == b,2930 endsWith(a, b)::31 if std.length(a) < std.length(b) then32 false33 else34 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,3536 lstripChars(str, chars)::37 if std.length(str) > 0 && std.member(chars, str[0]) then38 std.lstripChars(str[1:], chars)39 else40 str,4142 rstripChars(str, chars)::43 local len = std.length(str);44 if len > 0 && std.member(chars, str[len - 1]) then45 std.rstripChars(str[:len - 1], chars)46 else47 str,4849 stripChars(str, chars)::50 std.lstripChars(std.rstripChars(str, chars), chars),5152 stringChars(str)::53 std.makeArray(std.length(str), function(i) str[i]),5455 local parse_nat(str, base) =56 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;57 // These codepoints are in ascending order:58 local zero_code = std.codepoint('0');59 local upper_a_code = std.codepoint('A');60 local lower_a_code = std.codepoint('a');61 local addDigit(aggregate, char) =62 local code = std.codepoint(char);63 local digit = if code >= lower_a_code then64 code - lower_a_code + 1065 else if code >= upper_a_code then66 code - upper_a_code + 1067 else68 code - zero_code;69 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];70 base * aggregate + digit;71 std.foldl(addDigit, std.stringChars(str), 0),7273 parseInt(str)::74 assert std.isString(str) : 'Expected string, got ' + std.type(str);75 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];76 if str[0] == '-' then77 -parse_nat(str[1:], 10)78 else79 parse_nat(str, 10),8081 parseOctal(str)::82 assert std.isString(str) : 'Expected string, got ' + std.type(str);83 assert std.length(str) > 0 : 'Not an octal number: ""';84 parse_nat(str, 8),8586 parseHex(str)::87 assert std.isString(str) : 'Expected string, got ' + std.type(str);88 assert std.length(str) > 0 : 'Not hexadecimal: ""';89 parse_nat(str, 16),9091 split(str, c)::92 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);93 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);94 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);95 std.splitLimit(str, c, -1),9697 splitLimit(str, c, maxsplits)::98 assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);99 assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);100 assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);101 assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);102 local aux(str, delim, i, arr, v) =103 local c = str[i];104 local i2 = i + 1;105 if i >= std.length(str) then106 arr + [v]107 else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then108 aux(str, delim, i2, arr + [v], '') tailstrict109 else110 aux(str, delim, i2, arr, v + c) tailstrict;111 aux(str, c, 0, [], ''),112113 strReplace(str, from, to)::114 assert std.isString(str);115 assert std.isString(from);116 assert std.isString(to);117 assert from != '' : "'from' string must not be zero length.";118119 // Cache for performance.120 local str_len = std.length(str);121 local from_len = std.length(from);122123 // True if from is at str[i].124 local found_at(i) = str[i:i + from_len] == from;125126 // Return the remainder of 'str' starting with 'start_index' where127 // all occurrences of 'from' after 'curr_index' are replaced with 'to'.128 local replace_after(start_index, curr_index, acc) =129 if curr_index > str_len then130 acc + str[start_index:curr_index]131 else if found_at(curr_index) then132 local new_index = curr_index + std.length(from);133 replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict134 else135 replace_after(start_index, curr_index + 1, acc) tailstrict;136137 // if from_len==1, then we replace by splitting and rejoining the138 // string which is much faster than recursing on replace_after139 if from_len == 1 then140 std.join(to, std.split(str, from))141 else142 replace_after(0, 0, ''),143144 asciiUpper(str)::145 local cp = std.codepoint;146 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then147 std.char(cp(c) - 32)148 else149 c;150 std.join('', std.map(up_letter, std.stringChars(str))),151152 asciiLower(str)::153 local cp = std.codepoint;154 local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then155 std.char(cp(c) + 32)156 else157 c;158 std.join('', std.map(down_letter, std.stringChars(str))),159160 range(from, to)::161 std.makeArray(to - from + 1, function(i) i + from),162163 repeat(what, count)::164 local joiner =165 if std.isString(what) then ''166 else if std.isArray(what) then []167 else error 'std.repeat first argument must be an array or a string';168 std.join(joiner, std.makeArray(count, function(i) what)),169170 slice(indexable, index, end, step)::171 local invar =172 // loop invariant with defaults applied173 {174 indexable: indexable,175 index:176 if index == null then 0177 else index,178 end:179 if end == null then std.length(indexable)180 else end,181 step:182 if step == null then 1183 else step,184 length: std.length(indexable),185 type: std.type(indexable),186 };187 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];188 assert step != 0 : 'got %s but step must be greater than 0' % step;189 assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);190 local build(slice, cur) =191 if cur >= invar.end || cur >= invar.length then192 slice193 else194 build(195 if invar.type == 'string' then196 slice + invar.indexable[cur]197 else198 slice + [invar.indexable[cur]],199 cur + invar.step200 ) tailstrict;201 build(if invar.type == 'string' then '' else [], invar.index),202203 member(arr, x)::204 if std.isArray(arr) then205 std.count(arr, x) > 0206 else if std.isString(arr) then207 std.length(std.findSubstr(x, arr)) > 0208 else error 'std.member first argument must be an array or a string',209210 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),211212 mod(a, b)::213 if std.isNumber(a) && std.isNumber(b) then214 std.modulo(a, b)215 else if std.isString(a) then216 std.format(a, b)217 else218 error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',219220 map(func, arr)::221 if !std.isFunction(func) then222 error ('std.map first param must be function, got ' + std.type(func))223 else if !std.isArray(arr) && !std.isString(arr) then224 error ('std.map second param must be array / string, got ' + std.type(arr))225 else226 std.makeArray(std.length(arr), function(i) func(arr[i])),227228 mapWithIndex(func, arr)::229 if !std.isFunction(func) then230 error ('std.mapWithIndex first param must be function, got ' + std.type(func))231 else if !std.isArray(arr) && !std.isString(arr) then232 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))233 else234 std.makeArray(std.length(arr), function(i) func(i, arr[i])),235236 mapWithKey(func, obj)::237 if !std.isFunction(func) then238 error ('std.mapWithKey first param must be function, got ' + std.type(func))239 else if !std.isObject(obj) then240 error ('std.mapWithKey second param must be object, got ' + std.type(obj))241 else242 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },243244 flatMap(func, arr)::245 if !std.isFunction(func) then246 error ('std.flatMap first param must be function, got ' + std.type(func))247 else if std.isArray(arr) then248 std.flattenArrays(std.makeArray(std.length(arr), function(i) func(arr[i])))249 else if std.isString(arr) then250 std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))251 else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),252253 join(sep, arr)::254 local aux(arr, i, first, running) =255 if i >= std.length(arr) then256 running257 else if arr[i] == null then258 aux(arr, i + 1, first, running) tailstrict259 else if std.type(arr[i]) != std.type(sep) then260 error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]261 else if first then262 aux(arr, i + 1, false, running + arr[i]) tailstrict263 else264 aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;265 if !std.isArray(arr) then266 error 'join second parameter should be array, got ' + std.type(arr)267 else if std.isString(sep) then268 aux(arr, 0, true, '')269 else if std.isArray(sep) then270 aux(arr, 0, true, [])271 else272 error 'join first parameter should be string or array, got ' + std.type(sep),273274 lines(arr)::275 std.join('\n', arr + ['']),276277 deepJoin(arr)::278 if std.isString(arr) then279 arr280 else if std.isArray(arr) then281 std.join('', [std.deepJoin(x) for x in arr])282 else283 error 'Expected string or array, got %s' % std.type(arr),284285286 format(str, vals)::287288 /////////////////////////////289 // Parse the mini-language //290 /////////////////////////////291292 local try_parse_mapping_key(str, i) =293 assert i < std.length(str) : 'Truncated format code.';294 local c = str[i];295 if c == '(' then296 local consume(str, j, v) =297 if j >= std.length(str) then298 error 'Truncated format code.'299 else300 local c = str[j];301 if c != ')' then302 consume(str, j + 1, v + c)303 else304 { i: j + 1, v: v };305 consume(str, i + 1, '')306 else307 { i: i, v: null };308309 local try_parse_cflags(str, i) =310 local consume(str, j, v) =311 assert j < std.length(str) : 'Truncated format code.';312 local c = str[j];313 if c == '#' then314 consume(str, j + 1, v { alt: true })315 else if c == '0' then316 consume(str, j + 1, v { zero: true })317 else if c == '-' then318 consume(str, j + 1, v { left: true })319 else if c == ' ' then320 consume(str, j + 1, v { blank: true })321 else if c == '+' then322 consume(str, j + 1, v { sign: true })323 else324 { i: j, v: v };325 consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });326327 local try_parse_field_width(str, i) =328 if i < std.length(str) && str[i] == '*' then329 { i: i + 1, v: '*' }330 else331 local consume(str, j, v) =332 assert j < std.length(str) : 'Truncated format code.';333 local c = str[j];334 if c == '0' then335 consume(str, j + 1, v * 10 + 0)336 else if c == '1' then337 consume(str, j + 1, v * 10 + 1)338 else if c == '2' then339 consume(str, j + 1, v * 10 + 2)340 else if c == '3' then341 consume(str, j + 1, v * 10 + 3)342 else if c == '4' then343 consume(str, j + 1, v * 10 + 4)344 else if c == '5' then345 consume(str, j + 1, v * 10 + 5)346 else if c == '6' then347 consume(str, j + 1, v * 10 + 6)348 else if c == '7' then349 consume(str, j + 1, v * 10 + 7)350 else if c == '8' then351 consume(str, j + 1, v * 10 + 8)352 else if c == '9' then353 consume(str, j + 1, v * 10 + 9)354 else355 { i: j, v: v };356 consume(str, i, 0);357358 local try_parse_precision(str, i) =359 assert i < std.length(str) : 'Truncated format code.';360 local c = str[i];361 if c == '.' then362 try_parse_field_width(str, i + 1)363 else364 { i: i, v: null };365366 // Ignored, if it exists.367 local try_parse_length_modifier(str, i) =368 assert i < std.length(str) : 'Truncated format code.';369 local c = str[i];370 if c == 'h' || c == 'l' || c == 'L' then371 i + 1372 else373 i;374375 local parse_conv_type(str, i) =376 assert i < std.length(str) : 'Truncated format code.';377 local c = str[i];378 if c == 'd' || c == 'i' || c == 'u' then379 { i: i + 1, v: 'd', caps: false }380 else if c == 'o' then381 { i: i + 1, v: 'o', caps: false }382 else if c == 'x' then383 { i: i + 1, v: 'x', caps: false }384 else if c == 'X' then385 { i: i + 1, v: 'x', caps: true }386 else if c == 'e' then387 { i: i + 1, v: 'e', caps: false }388 else if c == 'E' then389 { i: i + 1, v: 'e', caps: true }390 else if c == 'f' then391 { i: i + 1, v: 'f', caps: false }392 else if c == 'F' then393 { i: i + 1, v: 'f', caps: true }394 else if c == 'g' then395 { i: i + 1, v: 'g', caps: false }396 else if c == 'G' then397 { i: i + 1, v: 'g', caps: true }398 else if c == 'c' then399 { i: i + 1, v: 'c', caps: false }400 else if c == 's' then401 { i: i + 1, v: 's', caps: false }402 else if c == '%' then403 { i: i + 1, v: '%', caps: false }404 else405 error 'Unrecognised conversion type: ' + c;406407408 // Parsed initial %, now the rest.409 local parse_code(str, i) =410 assert i < std.length(str) : 'Truncated format code.';411 local mkey = try_parse_mapping_key(str, i);412 local cflags = try_parse_cflags(str, mkey.i);413 local fw = try_parse_field_width(str, cflags.i);414 local prec = try_parse_precision(str, fw.i);415 local len_mod = try_parse_length_modifier(str, prec.i);416 local ctype = parse_conv_type(str, len_mod);417 {418 i: ctype.i,419 code: {420 mkey: mkey.v,421 cflags: cflags.v,422 fw: fw.v,423 prec: prec.v,424 ctype: ctype.v,425 caps: ctype.caps,426 },427 };428429 // Parse a format string (containing none or more % format tags).430 local parse_codes(str, i, out, cur) =431 if i >= std.length(str) then432 out + [cur]433 else434 local c = str[i];435 if c == '%' then436 local r = parse_code(str, i + 1);437 parse_codes(str, r.i, out + [cur, r.code], '') tailstrict438 else439 parse_codes(str, i + 1, out, cur + c) tailstrict;440441 local codes = parse_codes(str, 0, [], '');442443444 ///////////////////////445 // Format the values //446 ///////////////////////447448 // Useful utilities449 local padding(w, s) =450 local aux(w, v) =451 if w <= 0 then452 v453 else454 aux(w - 1, v + s);455 aux(w, '');456457 // Add s to the left of str so that its length is at least w.458 local pad_left(str, w, s) =459 padding(w - std.length(str), s) + str;460461 // Add s to the right of str so that its length is at least w.462 local pad_right(str, w, s) =463 str + padding(w - std.length(str), s);464465 // Render an integer (e.g., decimal or octal).466 local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =467 local n_ = std.abs(n__);468 local aux(n) =469 if n == 0 then470 zero_prefix471 else472 aux(std.floor(n / radix)) + (n % radix);473 local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));474 local neg = n__ < 0;475 local zp = min_chars - (if neg || blank || sign then 1 else 0);476 local zp2 = std.max(zp, min_digits);477 local dec2 = pad_left(dec, zp2, '0');478 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;479480 // Render an integer in hexadecimal.481 local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =482 local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]483 + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']484 else ['a', 'b', 'c', 'd', 'e', 'f'];485 local n_ = std.abs(n__);486 local aux(n) =487 if n == 0 then488 ''489 else490 aux(std.floor(n / 16)) + numerals[n % 16];491 local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));492 local neg = n__ < 0;493 local zp = min_chars - (if neg || blank || sign then 1 else 0)494 - (if add_zerox then 2 else 0);495 local zp2 = std.max(zp, min_digits);496 local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')497 + pad_left(hex, zp2, '0');498 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;499500 local strip_trailing_zero(str) =501 local aux(str, i) =502 if i < 0 then503 ''504 else505 if str[i] == '0' then506 aux(str, i - 1)507 else508 std.substr(str, 0, i + 1);509 aux(str, std.length(str) - 1);510511 // Render floating point in decimal form512 local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =513 local n_ = std.abs(n__);514 local whole = std.floor(n_);515 local dot_size = if prec == 0 && !ensure_pt then 0 else 1;516 local zp = zero_pad - prec - dot_size;517 local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');518 if prec == 0 then519 str + if ensure_pt then '.' else ''520 else521 local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);522 if trailing || frac > 0 then523 local frac_str = render_int(frac, prec, 0, false, false, 10, '');524 str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str525 else526 str;527528 // Render floating point in scientific form529 local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =530 local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));531 local suff = (if caps then 'E' else 'e')532 + render_int(exponent, 3, 0, false, true, 10, '');533 local mantissa = if exponent == -324 then534 // Avoid a rounding error where std.pow(10, -324) is 0535 // -324 is the smallest exponent possible.536 n__ * 10 / std.pow(10, exponent + 1)537 else538 n__ / std.pow(10, exponent);539 local zp2 = zero_pad - std.length(suff);540 render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;541542 // Render a value with an arbitrary format code.543 local format_code(val, code, fw, prec_or_null, i) =544 local cflags = code.cflags;545 local fpprec = if prec_or_null != null then prec_or_null else 6;546 local iprec = if prec_or_null != null then prec_or_null else 0;547 local zp = if cflags.zero && !cflags.left then fw else 0;548 if code.ctype == 's' then549 std.toString(val)550 else if code.ctype == 'd' then551 if std.type(val) != 'number' then552 error 'Format required number at '553 + i + ', got ' + std.type(val)554 else555 render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')556 else if code.ctype == 'o' then557 if std.type(val) != 'number' then558 error 'Format required number at '559 + i + ', got ' + std.type(val)560 else561 local zero_prefix = if cflags.alt then '0' else '';562 render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)563 else if code.ctype == 'x' then564 if std.type(val) != 'number' then565 error 'Format required number at '566 + i + ', got ' + std.type(val)567 else568 render_hex(val,569 zp,570 iprec,571 cflags.blank,572 cflags.sign,573 cflags.alt,574 code.caps)575 else if code.ctype == 'f' then576 if std.type(val) != 'number' then577 error 'Format required number at '578 + i + ', got ' + std.type(val)579 else580 render_float_dec(val,581 zp,582 cflags.blank,583 cflags.sign,584 cflags.alt,585 true,586 fpprec)587 else if code.ctype == 'e' then588 if std.type(val) != 'number' then589 error 'Format required number at '590 + i + ', got ' + std.type(val)591 else592 render_float_sci(val,593 zp,594 cflags.blank,595 cflags.sign,596 cflags.alt,597 true,598 code.caps,599 fpprec)600 else if code.ctype == 'g' then601 if std.type(val) != 'number' then602 error 'Format required number at '603 + i + ', got ' + std.type(val)604 else605 local exponent = std.floor(std.log(std.abs(val)) / std.log(10));606 if exponent < -4 || exponent >= fpprec then607 render_float_sci(val,608 zp,609 cflags.blank,610 cflags.sign,611 cflags.alt,612 cflags.alt,613 code.caps,614 fpprec - 1)615 else616 local digits_before_pt = std.max(1, exponent + 1);617 render_float_dec(val,618 zp,619 cflags.blank,620 cflags.sign,621 cflags.alt,622 cflags.alt,623 fpprec - digits_before_pt)624 else if code.ctype == 'c' then625 if std.type(val) == 'number' then626 std.char(val)627 else if std.type(val) == 'string' then628 if std.length(val) == 1 then629 val630 else631 error '%c expected 1-sized string got: ' + std.length(val)632 else633 error '%c expected number / string, got: ' + std.type(val)634 else635 error 'Unknown code: ' + code.ctype;636637 // Render a parsed format string with an array of values.638 local format_codes_arr(codes, arr, i, j, v) =639 if i >= std.length(codes) then640 if j < std.length(arr) then641 error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)642 else643 v644 else645 local code = codes[i];646 if std.type(code) == 'string' then647 format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict648 else649 local tmp = if code.fw == '*' then {650 j: j + 1,651 fw: if j >= std.length(arr) then652 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)653 else654 arr[j],655 } else {656 j: j,657 fw: code.fw,658 };659 local tmp2 = if code.prec == '*' then {660 j: tmp.j + 1,661 prec: if tmp.j >= std.length(arr) then662 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)663 else664 arr[tmp.j],665 } else {666 j: tmp.j,667 prec: code.prec,668 };669 local j2 = tmp2.j;670 local val =671 if j2 < std.length(arr) then672 arr[j2]673 else674 error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);675 local s =676 if code.ctype == '%' then677 '%'678 else679 format_code(val, code, tmp.fw, tmp2.prec, j2);680 local s_padded =681 if code.cflags.left then682 pad_right(s, tmp.fw, ' ')683 else684 pad_left(s, tmp.fw, ' ');685 local j3 =686 if code.ctype == '%' then687 j2688 else689 j2 + 1;690 format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;691692 // Render a parsed format string with an object of values.693 local format_codes_obj(codes, obj, i, v) =694 if i >= std.length(codes) then695 v696 else697 local code = codes[i];698 if std.type(code) == 'string' then699 format_codes_obj(codes, obj, i + 1, v + code) tailstrict700 else701 local f =702 if code.mkey == null then703 error 'Mapping keys required.'704 else705 code.mkey;706 local fw =707 if code.fw == '*' then708 error 'Cannot use * field width with object.'709 else710 code.fw;711 local prec =712 if code.prec == '*' then713 error 'Cannot use * precision with object.'714 else715 code.prec;716 local val =717 if std.objectHasAll(obj, f) then718 obj[f]719 else720 error 'No such field: ' + f;721 local s =722 if code.ctype == '%' then723 '%'724 else725 format_code(val, code, fw, prec, f);726 local s_padded =727 if code.cflags.left then728 pad_right(s, fw, ' ')729 else730 pad_left(s, fw, ' ');731 format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;732733 if std.isArray(vals) then734 format_codes_arr(codes, vals, 0, 0, '')735 else if std.isObject(vals) then736 format_codes_obj(codes, vals, 0, '')737 else738 format_codes_arr(codes, [vals], 0, 0, ''),739740 foldr(func, arr, init)::741 local aux(func, arr, running, idx) =742 if idx < 0 then743 running744 else745 aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;746 aux(func, arr, init, std.length(arr) - 1),747748 foldl(func, arr, init)::749 local aux(func, arr, running, idx) =750 if idx >= std.length(arr) then751 running752 else753 aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;754 aux(func, arr, init, 0),755756757 filterMap(filter_func, map_func, arr)::758 if !std.isFunction(filter_func) then759 error ('std.filterMap first param must be function, got ' + std.type(filter_func))760 else if !std.isFunction(map_func) then761 error ('std.filterMap second param must be function, got ' + std.type(map_func))762 else if !std.isArray(arr) then763 error ('std.filterMap third param must be array, got ' + std.type(arr))764 else765 std.map(map_func, std.filter(filter_func, arr)),766767 assertEqual(a, b)::768 if a == b then769 true770 else771 error 'Assertion failed. ' + a + ' != ' + b,772773 abs(n)::774 if !std.isNumber(n) then775 error 'std.abs expected number, got ' + std.type(n)776 else777 if n > 0 then n else -n,778779 sign(n)::780 if !std.isNumber(n) then781 error 'std.sign expected number, got ' + std.type(n)782 else783 if n > 0 then784 1785 else if n < 0 then786 -1787 else 0,788789 max(a, b)::790 if !std.isNumber(a) then791 error 'std.max first param expected number, got ' + std.type(a)792 else if !std.isNumber(b) then793 error 'std.max second param expected number, got ' + std.type(b)794 else795 if a > b then a else b,796797 min(a, b)::798 if !std.isNumber(a) then799 error 'std.max first param expected number, got ' + std.type(a)800 else if !std.isNumber(b) then801 error 'std.max second param expected number, got ' + std.type(b)802 else803 if a < b then a else b,804805 clamp(x, minVal, maxVal)::806 if x < minVal then minVal807 else if x > maxVal then maxVal808 else x,809810 flattenArrays(arrs)::811 std.foldl(function(a, b) a + b, arrs, []),812813 manifestIni(ini)::814 local body_lines(body) =815 std.join([], [816 local value_or_values = body[k];817 if std.isArray(value_or_values) then818 ['%s = %s' % [k, value] for value in value_or_values]819 else820 ['%s = %s' % [k, value_or_values]]821822 for k in std.objectFields(body)823 ]);824825 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),826 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],827 all_sections = [828 section_lines(k, ini.sections[k])829 for k in std.objectFields(ini.sections)830 ];831 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),832833 escapeStringJson(str_)::834 local str = std.toString(str_);835 local trans(ch) =836 if ch == '"' then837 '\\"'838 else if ch == '\\' then839 '\\\\'840 else if ch == '\b' then841 '\\b'842 else if ch == '\f' then843 '\\f'844 else if ch == '\n' then845 '\\n'846 else if ch == '\r' then847 '\\r'848 else if ch == '\t' then849 '\\t'850 else851 local cp = std.codepoint(ch);852 if cp < 32 || (cp >= 127 && cp <= 159) then853 '\\u%04x' % [cp]854 else855 ch;856 '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),857858 escapeStringPython(str)::859 std.escapeStringJson(str),860861 escapeStringBash(str_)::862 local str = std.toString(str_);863 local trans(ch) =864 if ch == "'" then865 "'\"'\"'"866 else867 ch;868 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),869870 escapeStringDollars(str_)::871 local str = std.toString(str_);872 local trans(ch) =873 if ch == '$' then874 '$$'875 else876 ch;877 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),878879 manifestJson(value):: std.manifestJsonEx(value, ' '),880881 manifestJsonEx(value, indent)::882 local aux(v, path, cindent) =883 if v == true then884 'true'885 else if v == false then886 'false'887 else if v == null then888 'null'889 else if std.isNumber(v) then890 '' + v891 else if std.isString(v) then892 std.escapeStringJson(v)893 else if std.isFunction(v) then894 error 'Tried to manifest function at ' + path895 else if std.isArray(v) then896 local range = std.range(0, std.length(v) - 1);897 local new_indent = cindent + indent;898 local lines = ['[\n']899 + std.join([',\n'],900 [901 [new_indent + aux(v[i], path + [i], new_indent)]902 for i in range903 ])904 + ['\n' + cindent + ']'];905 std.join('', lines)906 else if std.isObject(v) then907 local lines = ['{\n']908 + std.join([',\n'],909 [910 [cindent + indent + std.escapeStringJson(k) + ': '911 + aux(v[k], path + [k], cindent + indent)]912 for k in std.objectFields(v)913 ])914 + ['\n' + cindent + '}'];915 std.join('', lines);916 aux(value, [], ''),917918 manifestYamlDoc(value, indent_array_in_object=false)::919 local aux(v, path, cindent) =920 if v == true then921 'true'922 else if v == false then923 'false'924 else if v == null then925 'null'926 else if std.isNumber(v) then927 '' + v928 else if std.isString(v) then929 local len = std.length(v);930 if len == 0 then931 '""'932 else if v[len - 1] == '\n' then933 local split = std.split(v, '\n');934 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])935 else936 std.escapeStringJson(v)937 else if std.isFunction(v) then938 error 'Tried to manifest function at ' + path939 else if std.isArray(v) then940 if std.length(v) == 0 then941 '[]'942 else943 local params(value) =944 if std.isArray(value) && std.length(value) > 0 then {945 // While we could avoid the new line, it yields YAML that is946 // hard to read, e.g.:947 // - - - 1948 // - 2949 // - - 3950 // - 4951 new_indent: cindent + ' ',952 space: '\n' + self.new_indent,953 } else if std.isObject(value) && std.length(value) > 0 then {954 new_indent: cindent + ' ',955 // In this case we can start on the same line as the - because the indentation956 // matches up then. The converse is not true, because fields are not always957 // 1 character long.958 space: ' ',959 } else {960 // In this case, new_indent is only used in the case of multi-line strings.961 new_indent: cindent,962 space: ' ',963 };964 local range = std.range(0, std.length(v) - 1);965 local parts = [966 '-' + param.space + aux(v[i], path + [i], param.new_indent)967 for i in range968 for param in [params(v[i])]969 ];970 std.join('\n' + cindent, parts)971 else if std.isObject(v) then972 if std.length(v) == 0 then973 '{}'974 else975 local params(value) =976 if std.isArray(value) && std.length(value) > 0 then {977 // Not indenting allows e.g.978 // ports:979 // - 80980 // instead of981 // ports:982 // - 80983 new_indent: if indent_array_in_object then cindent + ' ' else cindent,984 space: '\n' + self.new_indent,985 } else if std.isObject(value) && std.length(value) > 0 then {986 new_indent: cindent + ' ',987 space: '\n' + self.new_indent,988 } else {989 // In this case, new_indent is only used in the case of multi-line strings.990 new_indent: cindent,991 space: ' ',992 };993 local lines = [994 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)995 for k in std.objectFields(v)996 for param in [params(v[k])]997 ];998 std.join('\n' + cindent, lines);999 aux(value, [], ''),10001001 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::1002 if !std.isArray(value) then1003 error 'manifestYamlStream only takes arrays, got ' + std.type(value)1004 else1005 '---\n' + std.join(1006 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]1007 ) + if c_document_end then '\n...\n' else '\n',100810091010 manifestPython(v)::1011 if std.isObject(v) then1012 local fields = [1013 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]1014 for k in std.objectFields(v)1015 ];1016 '{%s}' % [std.join(', ', fields)]1017 else if std.isArray(v) then1018 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]1019 else if std.isString(v) then1020 '%s' % [std.escapeStringPython(v)]1021 else if std.isFunction(v) then1022 error 'cannot manifest function'1023 else if std.isNumber(v) then1024 std.toString(v)1025 else if v == true then1026 'True'1027 else if v == false then1028 'False'1029 else if v == null then1030 'None',10311032 manifestPythonVars(conf)::1033 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];1034 std.join('\n', vars + ['']),10351036 manifestXmlJsonml(value)::1037 if !std.isArray(value) then1038 error 'Expected a JSONML value (an array), got %s' % std.type(value)1039 else1040 local aux(v) =1041 if std.isString(v) then1042 v1043 else1044 local tag = v[0];1045 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);1046 local attrs = if has_attrs then v[1] else {};1047 local children = if has_attrs then v[2:] else v[1:];1048 local attrs_str =1049 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);1050 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);10511052 aux(value),10531054 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',1055 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },10561057 base64(input)::1058 local bytes =1059 if std.isString(input) then1060 std.map(function(c) std.codepoint(c), input)1061 else1062 input;10631064 local aux(arr, i, r) =1065 if i >= std.length(arr) then1066 r1067 else if i + 1 >= std.length(arr) then1068 local str =1069 // 6 MSB of i1070 base64_table[(arr[i] & 252) >> 2] +1071 // 2 LSB of i1072 base64_table[(arr[i] & 3) << 4] +1073 '==';1074 aux(arr, i + 3, r + str) tailstrict1075 else if i + 2 >= std.length(arr) then1076 local str =1077 // 6 MSB of i1078 base64_table[(arr[i] & 252) >> 2] +1079 // 2 LSB of i, 4 MSB of i+11080 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +1081 // 4 LSB of i+11082 base64_table[(arr[i + 1] & 15) << 2] +1083 '=';1084 aux(arr, i + 3, r + str) tailstrict1085 else1086 local str =1087 // 6 MSB of i1088 base64_table[(arr[i] & 252) >> 2] +1089 // 2 LSB of i, 4 MSB of i+11090 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +1091 // 4 LSB of i+1, 2 MSB of i+21092 base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +1093 // 6 LSB of i+21094 base64_table[(arr[i + 2] & 63)];1095 aux(arr, i + 3, r + str) tailstrict;10961097 local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);1098 if !sanity then1099 error 'Can only base64 encode strings / arrays of single bytes.'1100 else1101 aux(bytes, 0, ''),110211031104 base64DecodeBytes(str)::1105 if std.length(str) % 4 != 0 then1106 error 'Not a base64 encoded string "%s"' % str1107 else1108 local aux(str, i, r) =1109 if i >= std.length(str) then1110 r1111 else1112 // all 6 bits of i, 2 MSB of i+11113 local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];1114 // 4 LSB of i+1, 4MSB of i+21115 local n2 =1116 if str[i + 2] == '=' then []1117 else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];1118 // 2 LSB of i+2, all 6 bits of i+31119 local n3 =1120 if str[i + 3] == '=' then []1121 else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];1122 aux(str, i + 4, r + n1 + n2 + n3) tailstrict;1123 aux(str, 0, []),11241125 base64Decode(str)::1126 local bytes = std.base64DecodeBytes(str);1127 std.join('', std.map(function(b) std.char(b), bytes)),11281129 reverse(arr)::1130 local l = std.length(arr);1131 std.makeArray(l, function(i) arr[l - i - 1]),11321133 // Merge-sort for long arrays and naive quicksort for shorter ones1134 sortImpl(arr, keyF)::1135 local quickSort(arr, keyF=id) =1136 local l = std.length(arr);1137 if std.length(arr) <= 1 then1138 arr1139 else1140 local pos = 0;1141 local pivot = keyF(arr[pos]);1142 local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);1143 local left = std.filter(function(x) keyF(x) < pivot, rest);1144 local right = std.filter(function(x) keyF(x) >= pivot, rest);1145 quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);11461147 local merge(a, b) =1148 local la = std.length(a), lb = std.length(b);1149 local aux(i, j, prefix) =1150 if i == la then1151 prefix + b[j:]1152 else if j == lb then1153 prefix + a[i:]1154 else1155 if keyF(a[i]) <= keyF(b[j]) then1156 aux(i + 1, j, prefix + [a[i]]) tailstrict1157 else1158 aux(i, j + 1, prefix + [b[j]]) tailstrict;1159 aux(0, 0, []);11601161 local l = std.length(arr);1162 if std.length(arr) <= 30 then1163 quickSort(arr, keyF=keyF)1164 else1165 local mid = std.floor(l / 2);1166 local left = arr[:mid], right = arr[mid:];1167 merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),11681169 sort(arr, keyF=id)::1170 std.sortImpl(arr, keyF),11711172 uniq(arr, keyF=id)::1173 local f(a, b) =1174 if std.length(a) == 0 then1175 [b]1176 else if keyF(a[std.length(a) - 1]) == keyF(b) then1177 a1178 else1179 a + [b];1180 std.foldl(f, arr, []),11811182 set(arr, keyF=id)::1183 std.uniq(std.sort(arr, keyF), keyF),11841185 setMember(x, arr, keyF=id)::1186 // TODO(dcunnin): Binary chop for O(log n) complexity1187 std.length(std.setInter([x], arr, keyF)) > 0,11881189 setUnion(a, b, keyF=id)::1190 // NOTE: order matters, values in `a` win1191 local aux(a, b, i, j, acc) =1192 if i >= std.length(a) then1193 acc + b[j:]1194 else if j >= std.length(b) then1195 acc + a[i:]1196 else1197 local ak = keyF(a[i]);1198 local bk = keyF(b[j]);1199 if ak == bk then1200 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict1201 else if ak < bk then1202 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict1203 else1204 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;1205 aux(a, b, 0, 0, []),12061207 setInter(a, b, keyF=id)::1208 local aux(a, b, i, j, acc) =1209 if i >= std.length(a) || j >= std.length(b) then1210 acc1211 else1212 if keyF(a[i]) == keyF(b[j]) then1213 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict1214 else if keyF(a[i]) < keyF(b[j]) then1215 aux(a, b, i + 1, j, acc) tailstrict1216 else1217 aux(a, b, i, j + 1, acc) tailstrict;1218 aux(a, b, 0, 0, []) tailstrict,12191220 setDiff(a, b, keyF=id)::1221 local aux(a, b, i, j, acc) =1222 if i >= std.length(a) then1223 acc1224 else if j >= std.length(b) then1225 acc + a[i:]1226 else1227 if keyF(a[i]) == keyF(b[j]) then1228 aux(a, b, i + 1, j + 1, acc) tailstrict1229 else if keyF(a[i]) < keyF(b[j]) then1230 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict1231 else1232 aux(a, b, i, j + 1, acc) tailstrict;1233 aux(a, b, 0, 0, []) tailstrict,12341235 mergePatch(target, patch)::1236 if std.isObject(patch) then1237 local target_object =1238 if std.isObject(target) then target else {};12391240 local target_fields =1241 if std.isObject(target_object) then std.objectFields(target_object) else [];12421243 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];1244 local both_fields = std.setUnion(target_fields, std.objectFields(patch));12451246 {1247 [k]:1248 if !std.objectHas(patch, k) then1249 target_object[k]1250 else if !std.objectHas(target_object, k) then1251 std.mergePatch(null, patch[k]) tailstrict1252 else1253 std.mergePatch(target_object[k], patch[k]) tailstrict1254 for k in std.setDiff(both_fields, null_fields)1255 }1256 else1257 patch,12581259 objectFields(o)::1260 std.objectFieldsEx(o, false),12611262 objectFieldsAll(o)::1263 std.objectFieldsEx(o, true),12641265 objectHas(o, f)::1266 std.objectHasEx(o, f, false),12671268 objectHasAll(o, f)::1269 std.objectHasEx(o, f, true),12701271 equals(a, b)::1272 local ta = std.type(a);1273 local tb = std.type(b);1274 if !std.primitiveEquals(ta, tb) then1275 false1276 else1277 if std.primitiveEquals(ta, 'array') then1278 local la = std.length(a);1279 if !std.primitiveEquals(la, std.length(b)) then1280 false1281 else1282 local aux(a, b, i) =1283 if i >= la then1284 true1285 else if a[i] != b[i] then1286 false1287 else1288 aux(a, b, i + 1) tailstrict;1289 aux(a, b, 0)1290 else if std.primitiveEquals(ta, 'object') then1291 local fields = std.objectFields(a);1292 local lfields = std.length(fields);1293 if fields != std.objectFields(b) then1294 false1295 else1296 local aux(a, b, i) =1297 if i >= lfields then1298 true1299 else if local f = fields[i]; a[f] != b[f] then1300 false1301 else1302 aux(a, b, i + 1) tailstrict;1303 aux(a, b, 0)1304 else1305 std.primitiveEquals(a, b),130613071308 resolvePath(f, r)::1309 local arr = std.split(f, '/');1310 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),13111312 prune(a)::1313 local isContent(b) =1314 if b == null then1315 false1316 else if std.isArray(b) then1317 std.length(b) > 01318 else if std.isObject(b) then1319 std.length(b) > 01320 else1321 true;1322 if std.isArray(a) then1323 [std.prune(x) for x in a if isContent($.prune(x))]1324 else if std.isObject(a) then {1325 [x]: $.prune(a[x])1326 for x in std.objectFields(a)1327 if isContent(std.prune(a[x]))1328 } else1329 a,13301331 findSubstr(pat, str)::1332 if !std.isString(pat) then1333 error 'findSubstr first parameter should be a string, got ' + std.type(pat)1334 else if !std.isString(str) then1335 error 'findSubstr second parameter should be a string, got ' + std.type(str)1336 else1337 local pat_len = std.length(pat);1338 local str_len = std.length(str);1339 if pat_len == 0 || str_len == 0 || pat_len > str_len then1340 []1341 else1342 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),13431344 find(value, arr)::1345 if !std.isArray(arr) then1346 error 'find second parameter should be an array, got ' + std.type(arr)1347 else1348 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),1349}1{2 __intristic_namespace__:: 'std',34 local std = self,5 local id = std.id,67 isString(v):: std.type(v) == 'string',8 isNumber(v):: std.type(v) == 'number',9 isBoolean(v):: std.type(v) == 'boolean',10 isObject(v):: std.type(v) == 'object',11 isArray(v):: std.type(v) == 'array',12 isFunction(v):: std.type(v) == 'function',1314 toString(a)::15 if std.type(a) == 'string' then a else '' + a,1617 substr(str, from, len)::18 assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);19 assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);20 assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);21 assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;22 std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),2324 startsWith(a, b)::25 if std.length(a) < std.length(b) then26 false27 else28 std.substr(a, 0, std.length(b)) == b,2930 endsWith(a, b)::31 if std.length(a) < std.length(b) then32 false33 else34 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,3536 lstripChars(str, chars)::37 if std.length(str) > 0 && std.member(chars, str[0]) then38 std.lstripChars(str[1:], chars)39 else40 str,4142 rstripChars(str, chars)::43 local len = std.length(str);44 if len > 0 && std.member(chars, str[len - 1]) then45 std.rstripChars(str[:len - 1], chars)46 else47 str,4849 stripChars(str, chars)::50 std.lstripChars(std.rstripChars(str, chars), chars),5152 stringChars(str)::53 std.makeArray(std.length(str), function(i) str[i]),5455 local parse_nat(str, base) =56 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;57 // These codepoints are in ascending order:58 local zero_code = std.codepoint('0');59 local upper_a_code = std.codepoint('A');60 local lower_a_code = std.codepoint('a');61 local addDigit(aggregate, char) =62 local code = std.codepoint(char);63 local digit = if code >= lower_a_code then64 code - lower_a_code + 1065 else if code >= upper_a_code then66 code - upper_a_code + 1067 else68 code - zero_code;69 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];70 base * aggregate + digit;71 std.foldl(addDigit, std.stringChars(str), 0),7273 parseInt(str)::74 assert std.isString(str) : 'Expected string, got ' + std.type(str);75 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];76 if str[0] == '-' then77 -parse_nat(str[1:], 10)78 else79 parse_nat(str, 10),8081 parseOctal(str)::82 assert std.isString(str) : 'Expected string, got ' + std.type(str);83 assert std.length(str) > 0 : 'Not an octal number: ""';84 parse_nat(str, 8),8586 parseHex(str)::87 assert std.isString(str) : 'Expected string, got ' + std.type(str);88 assert std.length(str) > 0 : 'Not hexadecimal: ""';89 parse_nat(str, 16),9091 split(str, c)::92 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);93 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);94 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);95 std.splitLimit(str, c, -1),9697 splitLimit(str, c, maxsplits)::98 assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);99 assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);100 assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);101 assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);102 local aux(str, delim, i, arr, v) =103 local c = str[i];104 local i2 = i + 1;105 if i >= std.length(str) then106 arr + [v]107 else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then108 aux(str, delim, i2, arr + [v], '') tailstrict109 else110 aux(str, delim, i2, arr, v + c) tailstrict;111 aux(str, c, 0, [], ''),112113 strReplace(str, from, to)::114 assert std.isString(str);115 assert std.isString(from);116 assert std.isString(to);117 assert from != '' : "'from' string must not be zero length.";118119 // Cache for performance.120 local str_len = std.length(str);121 local from_len = std.length(from);122123 // True if from is at str[i].124 local found_at(i) = str[i:i + from_len] == from;125126 // Return the remainder of 'str' starting with 'start_index' where127 // all occurrences of 'from' after 'curr_index' are replaced with 'to'.128 local replace_after(start_index, curr_index, acc) =129 if curr_index > str_len then130 acc + str[start_index:curr_index]131 else if found_at(curr_index) then132 local new_index = curr_index + std.length(from);133 replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict134 else135 replace_after(start_index, curr_index + 1, acc) tailstrict;136137 // if from_len==1, then we replace by splitting and rejoining the138 // string which is much faster than recursing on replace_after139 if from_len == 1 then140 std.join(to, std.split(str, from))141 else142 replace_after(0, 0, ''),143144 asciiUpper(str)::145 local cp = std.codepoint;146 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then147 std.char(cp(c) - 32)148 else149 c;150 std.join('', std.map(up_letter, std.stringChars(str))),151152 asciiLower(str)::153 local cp = std.codepoint;154 local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then155 std.char(cp(c) + 32)156 else157 c;158 std.join('', std.map(down_letter, std.stringChars(str))),159160 range(from, to)::161 std.makeArray(to - from + 1, function(i) i + from),162163 repeat(what, count)::164 local joiner =165 if std.isString(what) then ''166 else if std.isArray(what) then []167 else error 'std.repeat first argument must be an array or a string';168 std.join(joiner, std.makeArray(count, function(i) what)),169170 slice(indexable, index, end, step)::171 local invar =172 // loop invariant with defaults applied173 {174 indexable: indexable,175 index:176 if index == null then 0177 else index,178 end:179 if end == null then std.length(indexable)180 else end,181 step:182 if step == null then 1183 else step,184 length: std.length(indexable),185 type: std.type(indexable),186 };187 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];188 assert step != 0 : 'got %s but step must be greater than 0' % step;189 assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);190 local build(slice, cur) =191 if cur >= invar.end || cur >= invar.length then192 slice193 else194 build(195 if invar.type == 'string' then196 slice + invar.indexable[cur]197 else198 slice + [invar.indexable[cur]],199 cur + invar.step200 ) tailstrict;201 build(if invar.type == 'string' then '' else [], invar.index),202203 member(arr, x)::204 if std.isArray(arr) then205 std.count(arr, x) > 0206 else if std.isString(arr) then207 std.length(std.findSubstr(x, arr)) > 0208 else error 'std.member first argument must be an array or a string',209210 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),211212 mod(a, b)::213 if std.isNumber(a) && std.isNumber(b) then214 std.modulo(a, b)215 else if std.isString(a) then216 std.format(a, b)217 else218 error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',219220 map(func, arr)::221 if !std.isFunction(func) then222 error ('std.map first param must be function, got ' + std.type(func))223 else if !std.isArray(arr) && !std.isString(arr) then224 error ('std.map second param must be array / string, got ' + std.type(arr))225 else226 std.makeArray(std.length(arr), function(i) func(arr[i])),227228 mapWithIndex(func, arr)::229 if !std.isFunction(func) then230 error ('std.mapWithIndex first param must be function, got ' + std.type(func))231 else if !std.isArray(arr) && !std.isString(arr) then232 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))233 else234 std.makeArray(std.length(arr), function(i) func(i, arr[i])),235236 mapWithKey(func, obj)::237 if !std.isFunction(func) then238 error ('std.mapWithKey first param must be function, got ' + std.type(func))239 else if !std.isObject(obj) then240 error ('std.mapWithKey second param must be object, got ' + std.type(obj))241 else242 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },243244 flatMap(func, arr)::245 if !std.isFunction(func) then246 error ('std.flatMap first param must be function, got ' + std.type(func))247 else if std.isArray(arr) then248 std.flattenArrays(std.makeArray(std.length(arr), function(i) func(arr[i])))249 else if std.isString(arr) then250 std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))251 else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),252253 join(sep, arr)::254 local aux(arr, i, first, running) =255 if i >= std.length(arr) then256 running257 else if arr[i] == null then258 aux(arr, i + 1, first, running) tailstrict259 else if std.type(arr[i]) != std.type(sep) then260 error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]261 else if first then262 aux(arr, i + 1, false, running + arr[i]) tailstrict263 else264 aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;265 if !std.isArray(arr) then266 error 'join second parameter should be array, got ' + std.type(arr)267 else if std.isString(sep) then268 aux(arr, 0, true, '')269 else if std.isArray(sep) then270 aux(arr, 0, true, [])271 else272 error 'join first parameter should be string or array, got ' + std.type(sep),273274 lines(arr)::275 std.join('\n', arr + ['']),276277 deepJoin(arr)::278 if std.isString(arr) then279 arr280 else if std.isArray(arr) then281 std.join('', [std.deepJoin(x) for x in arr])282 else283 error 'Expected string or array, got %s' % std.type(arr),284285286 format(str, vals)::287288 /////////////////////////////289 // Parse the mini-language //290 /////////////////////////////291292 local try_parse_mapping_key(str, i) =293 assert i < std.length(str) : 'Truncated format code.';294 local c = str[i];295 if c == '(' then296 local consume(str, j, v) =297 if j >= std.length(str) then298 error 'Truncated format code.'299 else300 local c = str[j];301 if c != ')' then302 consume(str, j + 1, v + c)303 else304 { i: j + 1, v: v };305 consume(str, i + 1, '')306 else307 { i: i, v: null };308309 local try_parse_cflags(str, i) =310 local consume(str, j, v) =311 assert j < std.length(str) : 'Truncated format code.';312 local c = str[j];313 if c == '#' then314 consume(str, j + 1, v { alt: true })315 else if c == '0' then316 consume(str, j + 1, v { zero: true })317 else if c == '-' then318 consume(str, j + 1, v { left: true })319 else if c == ' ' then320 consume(str, j + 1, v { blank: true })321 else if c == '+' then322 consume(str, j + 1, v { sign: true })323 else324 { i: j, v: v };325 consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });326327 local try_parse_field_width(str, i) =328 if i < std.length(str) && str[i] == '*' then329 { i: i + 1, v: '*' }330 else331 local consume(str, j, v) =332 assert j < std.length(str) : 'Truncated format code.';333 local c = str[j];334 if c == '0' then335 consume(str, j + 1, v * 10 + 0)336 else if c == '1' then337 consume(str, j + 1, v * 10 + 1)338 else if c == '2' then339 consume(str, j + 1, v * 10 + 2)340 else if c == '3' then341 consume(str, j + 1, v * 10 + 3)342 else if c == '4' then343 consume(str, j + 1, v * 10 + 4)344 else if c == '5' then345 consume(str, j + 1, v * 10 + 5)346 else if c == '6' then347 consume(str, j + 1, v * 10 + 6)348 else if c == '7' then349 consume(str, j + 1, v * 10 + 7)350 else if c == '8' then351 consume(str, j + 1, v * 10 + 8)352 else if c == '9' then353 consume(str, j + 1, v * 10 + 9)354 else355 { i: j, v: v };356 consume(str, i, 0);357358 local try_parse_precision(str, i) =359 assert i < std.length(str) : 'Truncated format code.';360 local c = str[i];361 if c == '.' then362 try_parse_field_width(str, i + 1)363 else364 { i: i, v: null };365366 // Ignored, if it exists.367 local try_parse_length_modifier(str, i) =368 assert i < std.length(str) : 'Truncated format code.';369 local c = str[i];370 if c == 'h' || c == 'l' || c == 'L' then371 i + 1372 else373 i;374375 local parse_conv_type(str, i) =376 assert i < std.length(str) : 'Truncated format code.';377 local c = str[i];378 if c == 'd' || c == 'i' || c == 'u' then379 { i: i + 1, v: 'd', caps: false }380 else if c == 'o' then381 { i: i + 1, v: 'o', caps: false }382 else if c == 'x' then383 { i: i + 1, v: 'x', caps: false }384 else if c == 'X' then385 { i: i + 1, v: 'x', caps: true }386 else if c == 'e' then387 { i: i + 1, v: 'e', caps: false }388 else if c == 'E' then389 { i: i + 1, v: 'e', caps: true }390 else if c == 'f' then391 { i: i + 1, v: 'f', caps: false }392 else if c == 'F' then393 { i: i + 1, v: 'f', caps: true }394 else if c == 'g' then395 { i: i + 1, v: 'g', caps: false }396 else if c == 'G' then397 { i: i + 1, v: 'g', caps: true }398 else if c == 'c' then399 { i: i + 1, v: 'c', caps: false }400 else if c == 's' then401 { i: i + 1, v: 's', caps: false }402 else if c == '%' then403 { i: i + 1, v: '%', caps: false }404 else405 error 'Unrecognised conversion type: ' + c;406407408 // Parsed initial %, now the rest.409 local parse_code(str, i) =410 assert i < std.length(str) : 'Truncated format code.';411 local mkey = try_parse_mapping_key(str, i);412 local cflags = try_parse_cflags(str, mkey.i);413 local fw = try_parse_field_width(str, cflags.i);414 local prec = try_parse_precision(str, fw.i);415 local len_mod = try_parse_length_modifier(str, prec.i);416 local ctype = parse_conv_type(str, len_mod);417 {418 i: ctype.i,419 code: {420 mkey: mkey.v,421 cflags: cflags.v,422 fw: fw.v,423 prec: prec.v,424 ctype: ctype.v,425 caps: ctype.caps,426 },427 };428429 // Parse a format string (containing none or more % format tags).430 local parse_codes(str, i, out, cur) =431 if i >= std.length(str) then432 out + [cur]433 else434 local c = str[i];435 if c == '%' then436 local r = parse_code(str, i + 1);437 parse_codes(str, r.i, out + [cur, r.code], '') tailstrict438 else439 parse_codes(str, i + 1, out, cur + c) tailstrict;440441 local codes = parse_codes(str, 0, [], '');442443444 ///////////////////////445 // Format the values //446 ///////////////////////447448 // Useful utilities449 local padding(w, s) =450 local aux(w, v) =451 if w <= 0 then452 v453 else454 aux(w - 1, v + s);455 aux(w, '');456457 // Add s to the left of str so that its length is at least w.458 local pad_left(str, w, s) =459 padding(w - std.length(str), s) + str;460461 // Add s to the right of str so that its length is at least w.462 local pad_right(str, w, s) =463 str + padding(w - std.length(str), s);464465 // Render an integer (e.g., decimal or octal).466 local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =467 local n_ = std.abs(n__);468 local aux(n) =469 if n == 0 then470 zero_prefix471 else472 aux(std.floor(n / radix)) + (n % radix);473 local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));474 local neg = n__ < 0;475 local zp = min_chars - (if neg || blank || sign then 1 else 0);476 local zp2 = std.max(zp, min_digits);477 local dec2 = pad_left(dec, zp2, '0');478 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;479480 // Render an integer in hexadecimal.481 local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =482 local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]483 + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']484 else ['a', 'b', 'c', 'd', 'e', 'f'];485 local n_ = std.abs(n__);486 local aux(n) =487 if n == 0 then488 ''489 else490 aux(std.floor(n / 16)) + numerals[n % 16];491 local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));492 local neg = n__ < 0;493 local zp = min_chars - (if neg || blank || sign then 1 else 0)494 - (if add_zerox then 2 else 0);495 local zp2 = std.max(zp, min_digits);496 local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')497 + pad_left(hex, zp2, '0');498 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;499500 local strip_trailing_zero(str) =501 local aux(str, i) =502 if i < 0 then503 ''504 else505 if str[i] == '0' then506 aux(str, i - 1)507 else508 std.substr(str, 0, i + 1);509 aux(str, std.length(str) - 1);510511 // Render floating point in decimal form512 local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =513 local n_ = std.abs(n__);514 local whole = std.floor(n_);515 local dot_size = if prec == 0 && !ensure_pt then 0 else 1;516 local zp = zero_pad - prec - dot_size;517 local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');518 if prec == 0 then519 str + if ensure_pt then '.' else ''520 else521 local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);522 if trailing || frac > 0 then523 local frac_str = render_int(frac, prec, 0, false, false, 10, '');524 str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str525 else526 str;527528 // Render floating point in scientific form529 local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =530 local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));531 local suff = (if caps then 'E' else 'e')532 + render_int(exponent, 3, 0, false, true, 10, '');533 local mantissa = if exponent == -324 then534 // Avoid a rounding error where std.pow(10, -324) is 0535 // -324 is the smallest exponent possible.536 n__ * 10 / std.pow(10, exponent + 1)537 else538 n__ / std.pow(10, exponent);539 local zp2 = zero_pad - std.length(suff);540 render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;541542 // Render a value with an arbitrary format code.543 local format_code(val, code, fw, prec_or_null, i) =544 local cflags = code.cflags;545 local fpprec = if prec_or_null != null then prec_or_null else 6;546 local iprec = if prec_or_null != null then prec_or_null else 0;547 local zp = if cflags.zero && !cflags.left then fw else 0;548 if code.ctype == 's' then549 std.toString(val)550 else if code.ctype == 'd' then551 if std.type(val) != 'number' then552 error 'Format required number at '553 + i + ', got ' + std.type(val)554 else555 render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')556 else if code.ctype == 'o' then557 if std.type(val) != 'number' then558 error 'Format required number at '559 + i + ', got ' + std.type(val)560 else561 local zero_prefix = if cflags.alt then '0' else '';562 render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)563 else if code.ctype == 'x' then564 if std.type(val) != 'number' then565 error 'Format required number at '566 + i + ', got ' + std.type(val)567 else568 render_hex(val,569 zp,570 iprec,571 cflags.blank,572 cflags.sign,573 cflags.alt,574 code.caps)575 else if code.ctype == 'f' then576 if std.type(val) != 'number' then577 error 'Format required number at '578 + i + ', got ' + std.type(val)579 else580 render_float_dec(val,581 zp,582 cflags.blank,583 cflags.sign,584 cflags.alt,585 true,586 fpprec)587 else if code.ctype == 'e' then588 if std.type(val) != 'number' then589 error 'Format required number at '590 + i + ', got ' + std.type(val)591 else592 render_float_sci(val,593 zp,594 cflags.blank,595 cflags.sign,596 cflags.alt,597 true,598 code.caps,599 fpprec)600 else if code.ctype == 'g' then601 if std.type(val) != 'number' then602 error 'Format required number at '603 + i + ', got ' + std.type(val)604 else605 local exponent = std.floor(std.log(std.abs(val)) / std.log(10));606 if exponent < -4 || exponent >= fpprec then607 render_float_sci(val,608 zp,609 cflags.blank,610 cflags.sign,611 cflags.alt,612 cflags.alt,613 code.caps,614 fpprec - 1)615 else616 local digits_before_pt = std.max(1, exponent + 1);617 render_float_dec(val,618 zp,619 cflags.blank,620 cflags.sign,621 cflags.alt,622 cflags.alt,623 fpprec - digits_before_pt)624 else if code.ctype == 'c' then625 if std.type(val) == 'number' then626 std.char(val)627 else if std.type(val) == 'string' then628 if std.length(val) == 1 then629 val630 else631 error '%c expected 1-sized string got: ' + std.length(val)632 else633 error '%c expected number / string, got: ' + std.type(val)634 else635 error 'Unknown code: ' + code.ctype;636637 // Render a parsed format string with an array of values.638 local format_codes_arr(codes, arr, i, j, v) =639 if i >= std.length(codes) then640 if j < std.length(arr) then641 error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)642 else643 v644 else645 local code = codes[i];646 if std.type(code) == 'string' then647 format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict648 else649 local tmp = if code.fw == '*' then {650 j: j + 1,651 fw: if j >= std.length(arr) then652 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)653 else654 arr[j],655 } else {656 j: j,657 fw: code.fw,658 };659 local tmp2 = if code.prec == '*' then {660 j: tmp.j + 1,661 prec: if tmp.j >= std.length(arr) then662 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)663 else664 arr[tmp.j],665 } else {666 j: tmp.j,667 prec: code.prec,668 };669 local j2 = tmp2.j;670 local val =671 if j2 < std.length(arr) then672 arr[j2]673 else674 error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);675 local s =676 if code.ctype == '%' then677 '%'678 else679 format_code(val, code, tmp.fw, tmp2.prec, j2);680 local s_padded =681 if code.cflags.left then682 pad_right(s, tmp.fw, ' ')683 else684 pad_left(s, tmp.fw, ' ');685 local j3 =686 if code.ctype == '%' then687 j2688 else689 j2 + 1;690 format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;691692 // Render a parsed format string with an object of values.693 local format_codes_obj(codes, obj, i, v) =694 if i >= std.length(codes) then695 v696 else697 local code = codes[i];698 if std.type(code) == 'string' then699 format_codes_obj(codes, obj, i + 1, v + code) tailstrict700 else701 local f =702 if code.mkey == null then703 error 'Mapping keys required.'704 else705 code.mkey;706 local fw =707 if code.fw == '*' then708 error 'Cannot use * field width with object.'709 else710 code.fw;711 local prec =712 if code.prec == '*' then713 error 'Cannot use * precision with object.'714 else715 code.prec;716 local val =717 if std.objectHasAll(obj, f) then718 obj[f]719 else720 error 'No such field: ' + f;721 local s =722 if code.ctype == '%' then723 '%'724 else725 format_code(val, code, fw, prec, f);726 local s_padded =727 if code.cflags.left then728 pad_right(s, fw, ' ')729 else730 pad_left(s, fw, ' ');731 format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;732733 if std.isArray(vals) then734 format_codes_arr(codes, vals, 0, 0, '')735 else if std.isObject(vals) then736 format_codes_obj(codes, vals, 0, '')737 else738 format_codes_arr(codes, [vals], 0, 0, ''),739740 foldr(func, arr, init)::741 local aux(func, arr, running, idx) =742 if idx < 0 then743 running744 else745 aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;746 aux(func, arr, init, std.length(arr) - 1),747748 foldl(func, arr, init)::749 local aux(func, arr, running, idx) =750 if idx >= std.length(arr) then751 running752 else753 aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;754 aux(func, arr, init, 0),755756757 filterMap(filter_func, map_func, arr)::758 if !std.isFunction(filter_func) then759 error ('std.filterMap first param must be function, got ' + std.type(filter_func))760 else if !std.isFunction(map_func) then761 error ('std.filterMap second param must be function, got ' + std.type(map_func))762 else if !std.isArray(arr) then763 error ('std.filterMap third param must be array, got ' + std.type(arr))764 else765 std.map(map_func, std.filter(filter_func, arr)),766767 assertEqual(a, b)::768 if a == b then769 true770 else771 error 'Assertion failed. ' + a + ' != ' + b,772773 abs(n)::774 if !std.isNumber(n) then775 error 'std.abs expected number, got ' + std.type(n)776 else777 if n > 0 then n else -n,778779 sign(n)::780 if !std.isNumber(n) then781 error 'std.sign expected number, got ' + std.type(n)782 else783 if n > 0 then784 1785 else if n < 0 then786 -1787 else 0,788789 max(a, b)::790 if !std.isNumber(a) then791 error 'std.max first param expected number, got ' + std.type(a)792 else if !std.isNumber(b) then793 error 'std.max second param expected number, got ' + std.type(b)794 else795 if a > b then a else b,796797 min(a, b)::798 if !std.isNumber(a) then799 error 'std.max first param expected number, got ' + std.type(a)800 else if !std.isNumber(b) then801 error 'std.max second param expected number, got ' + std.type(b)802 else803 if a < b then a else b,804805 clamp(x, minVal, maxVal)::806 if x < minVal then minVal807 else if x > maxVal then maxVal808 else x,809810 flattenArrays(arrs)::811 std.foldl(function(a, b) a + b, arrs, []),812813 manifestIni(ini)::814 local body_lines(body) =815 std.join([], [816 local value_or_values = body[k];817 if std.isArray(value_or_values) then818 ['%s = %s' % [k, value] for value in value_or_values]819 else820 ['%s = %s' % [k, value_or_values]]821822 for k in std.objectFields(body)823 ]);824825 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),826 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],827 all_sections = [828 section_lines(k, ini.sections[k])829 for k in std.objectFields(ini.sections)830 ];831 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),832833 escapeStringJson(str_)::834 local str = std.toString(str_);835 local trans(ch) =836 if ch == '"' then837 '\\"'838 else if ch == '\\' then839 '\\\\'840 else if ch == '\b' then841 '\\b'842 else if ch == '\f' then843 '\\f'844 else if ch == '\n' then845 '\\n'846 else if ch == '\r' then847 '\\r'848 else if ch == '\t' then849 '\\t'850 else851 local cp = std.codepoint(ch);852 if cp < 32 || (cp >= 127 && cp <= 159) then853 '\\u%04x' % [cp]854 else855 ch;856 '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),857858 escapeStringPython(str)::859 std.escapeStringJson(str),860861 escapeStringBash(str_)::862 local str = std.toString(str_);863 local trans(ch) =864 if ch == "'" then865 "'\"'\"'"866 else867 ch;868 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),869870 escapeStringDollars(str_)::871 local str = std.toString(str_);872 local trans(ch) =873 if ch == '$' then874 '$$'875 else876 ch;877 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),878879 manifestJson(value):: std.manifestJsonEx(value, ' '),880881 manifestJsonEx(value, indent)::882 local aux(v, path, cindent) =883 if v == true then884 'true'885 else if v == false then886 'false'887 else if v == null then888 'null'889 else if std.isNumber(v) then890 '' + v891 else if std.isString(v) then892 std.escapeStringJson(v)893 else if std.isFunction(v) then894 error 'Tried to manifest function at ' + path895 else if std.isArray(v) then896 local range = std.range(0, std.length(v) - 1);897 local new_indent = cindent + indent;898 local lines = ['[\n']899 + std.join([',\n'],900 [901 [new_indent + aux(v[i], path + [i], new_indent)]902 for i in range903 ])904 + ['\n' + cindent + ']'];905 std.join('', lines)906 else if std.isObject(v) then907 local lines = ['{\n']908 + std.join([',\n'],909 [910 [cindent + indent + std.escapeStringJson(k) + ': '911 + aux(v[k], path + [k], cindent + indent)]912 for k in std.objectFields(v)913 ])914 + ['\n' + cindent + '}'];915 std.join('', lines);916 aux(value, [], ''),917918 manifestYamlDoc(value, indent_array_in_object=false)::919 local aux(v, path, cindent) =920 if v == true then921 'true'922 else if v == false then923 'false'924 else if v == null then925 'null'926 else if std.isNumber(v) then927 '' + v928 else if std.isString(v) then929 local len = std.length(v);930 if len == 0 then931 '""'932 else if v[len - 1] == '\n' then933 local split = std.split(v, '\n');934 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])935 else936 std.escapeStringJson(v)937 else if std.isFunction(v) then938 error 'Tried to manifest function at ' + path939 else if std.isArray(v) then940 if std.length(v) == 0 then941 '[]'942 else943 local params(value) =944 if std.isArray(value) && std.length(value) > 0 then {945 // While we could avoid the new line, it yields YAML that is946 // hard to read, e.g.:947 // - - - 1948 // - 2949 // - - 3950 // - 4951 new_indent: cindent + ' ',952 space: '\n' + self.new_indent,953 } else if std.isObject(value) && std.length(value) > 0 then {954 new_indent: cindent + ' ',955 // In this case we can start on the same line as the - because the indentation956 // matches up then. The converse is not true, because fields are not always957 // 1 character long.958 space: ' ',959 } else {960 // In this case, new_indent is only used in the case of multi-line strings.961 new_indent: cindent,962 space: ' ',963 };964 local range = std.range(0, std.length(v) - 1);965 local parts = [966 '-' + param.space + aux(v[i], path + [i], param.new_indent)967 for i in range968 for param in [params(v[i])]969 ];970 std.join('\n' + cindent, parts)971 else if std.isObject(v) then972 if std.length(v) == 0 then973 '{}'974 else975 local params(value) =976 if std.isArray(value) && std.length(value) > 0 then {977 // Not indenting allows e.g.978 // ports:979 // - 80980 // instead of981 // ports:982 // - 80983 new_indent: if indent_array_in_object then cindent + ' ' else cindent,984 space: '\n' + self.new_indent,985 } else if std.isObject(value) && std.length(value) > 0 then {986 new_indent: cindent + ' ',987 space: '\n' + self.new_indent,988 } else {989 // In this case, new_indent is only used in the case of multi-line strings.990 new_indent: cindent,991 space: ' ',992 };993 local lines = [994 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)995 for k in std.objectFields(v)996 for param in [params(v[k])]997 ];998 std.join('\n' + cindent, lines);999 aux(value, [], ''),10001001 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::1002 if !std.isArray(value) then1003 error 'manifestYamlStream only takes arrays, got ' + std.type(value)1004 else1005 '---\n' + std.join(1006 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]1007 ) + if c_document_end then '\n...\n' else '\n',100810091010 manifestPython(v)::1011 if std.isObject(v) then1012 local fields = [1013 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]1014 for k in std.objectFields(v)1015 ];1016 '{%s}' % [std.join(', ', fields)]1017 else if std.isArray(v) then1018 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]1019 else if std.isString(v) then1020 '%s' % [std.escapeStringPython(v)]1021 else if std.isFunction(v) then1022 error 'cannot manifest function'1023 else if std.isNumber(v) then1024 std.toString(v)1025 else if v == true then1026 'True'1027 else if v == false then1028 'False'1029 else if v == null then1030 'None',10311032 manifestPythonVars(conf)::1033 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];1034 std.join('\n', vars + ['']),10351036 manifestXmlJsonml(value)::1037 if !std.isArray(value) then1038 error 'Expected a JSONML value (an array), got %s' % std.type(value)1039 else1040 local aux(v) =1041 if std.isString(v) then1042 v1043 else1044 local tag = v[0];1045 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);1046 local attrs = if has_attrs then v[1] else {};1047 local children = if has_attrs then v[2:] else v[1:];1048 local attrs_str =1049 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);1050 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);10511052 aux(value),10531054 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',1055 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },10561057 base64(input)::1058 local bytes =1059 if std.isString(input) then1060 std.map(function(c) std.codepoint(c), input)1061 else1062 input;10631064 local aux(arr, i, r) =1065 if i >= std.length(arr) then1066 r1067 else if i + 1 >= std.length(arr) then1068 local str =1069 // 6 MSB of i1070 base64_table[(arr[i] & 252) >> 2] +1071 // 2 LSB of i1072 base64_table[(arr[i] & 3) << 4] +1073 '==';1074 aux(arr, i + 3, r + str) tailstrict1075 else if i + 2 >= std.length(arr) then1076 local str =1077 // 6 MSB of i1078 base64_table[(arr[i] & 252) >> 2] +1079 // 2 LSB of i, 4 MSB of i+11080 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +1081 // 4 LSB of i+11082 base64_table[(arr[i + 1] & 15) << 2] +1083 '=';1084 aux(arr, i + 3, r + str) tailstrict1085 else1086 local str =1087 // 6 MSB of i1088 base64_table[(arr[i] & 252) >> 2] +1089 // 2 LSB of i, 4 MSB of i+11090 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +1091 // 4 LSB of i+1, 2 MSB of i+21092 base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +1093 // 6 LSB of i+21094 base64_table[(arr[i + 2] & 63)];1095 aux(arr, i + 3, r + str) tailstrict;10961097 local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);1098 if !sanity then1099 error 'Can only base64 encode strings / arrays of single bytes.'1100 else1101 aux(bytes, 0, ''),110211031104 base64DecodeBytes(str)::1105 if std.length(str) % 4 != 0 then1106 error 'Not a base64 encoded string "%s"' % str1107 else1108 local aux(str, i, r) =1109 if i >= std.length(str) then1110 r1111 else1112 // all 6 bits of i, 2 MSB of i+11113 local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];1114 // 4 LSB of i+1, 4MSB of i+21115 local n2 =1116 if str[i + 2] == '=' then []1117 else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];1118 // 2 LSB of i+2, all 6 bits of i+31119 local n3 =1120 if str[i + 3] == '=' then []1121 else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];1122 aux(str, i + 4, r + n1 + n2 + n3) tailstrict;1123 aux(str, 0, []),11241125 base64Decode(str)::1126 local bytes = std.base64DecodeBytes(str);1127 std.join('', std.map(function(b) std.char(b), bytes)),11281129 reverse(arr)::1130 local l = std.length(arr);1131 std.makeArray(l, function(i) arr[l - i - 1]),11321133 // Merge-sort for long arrays and naive quicksort for shorter ones1134 sortImpl(arr, keyF)::1135 local quickSort(arr, keyF=id) =1136 local l = std.length(arr);1137 if std.length(arr) <= 1 then1138 arr1139 else1140 local pos = 0;1141 local pivot = keyF(arr[pos]);1142 local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);1143 local left = std.filter(function(x) keyF(x) < pivot, rest);1144 local right = std.filter(function(x) keyF(x) >= pivot, rest);1145 quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);11461147 local merge(a, b) =1148 local la = std.length(a), lb = std.length(b);1149 local aux(i, j, prefix) =1150 if i == la then1151 prefix + b[j:]1152 else if j == lb then1153 prefix + a[i:]1154 else1155 if keyF(a[i]) <= keyF(b[j]) then1156 aux(i + 1, j, prefix + [a[i]]) tailstrict1157 else1158 aux(i, j + 1, prefix + [b[j]]) tailstrict;1159 aux(0, 0, []);11601161 local l = std.length(arr);1162 if std.length(arr) <= 30 then1163 quickSort(arr, keyF=keyF)1164 else1165 local mid = std.floor(l / 2);1166 local left = arr[:mid], right = arr[mid:];1167 merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),11681169 sort(arr, keyF=id)::1170 std.sortImpl(arr, keyF),11711172 uniq(arr, keyF=id)::1173 local f(a, b) =1174 if std.length(a) == 0 then1175 [b]1176 else if keyF(a[std.length(a) - 1]) == keyF(b) then1177 a1178 else1179 a + [b];1180 std.foldl(f, arr, []),11811182 set(arr, keyF=id)::1183 std.uniq(std.sort(arr, keyF), keyF),11841185 setMember(x, arr, keyF=id)::1186 // TODO(dcunnin): Binary chop for O(log n) complexity1187 std.length(std.setInter([x], arr, keyF)) > 0,11881189 setUnion(a, b, keyF=id)::1190 // NOTE: order matters, values in `a` win1191 local aux(a, b, i, j, acc) =1192 if i >= std.length(a) then1193 acc + b[j:]1194 else if j >= std.length(b) then1195 acc + a[i:]1196 else1197 local ak = keyF(a[i]);1198 local bk = keyF(b[j]);1199 if ak == bk then1200 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict1201 else if ak < bk then1202 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict1203 else1204 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;1205 aux(a, b, 0, 0, []),12061207 setInter(a, b, keyF=id)::1208 local aux(a, b, i, j, acc) =1209 if i >= std.length(a) || j >= std.length(b) then1210 acc1211 else1212 if keyF(a[i]) == keyF(b[j]) then1213 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict1214 else if keyF(a[i]) < keyF(b[j]) then1215 aux(a, b, i + 1, j, acc) tailstrict1216 else1217 aux(a, b, i, j + 1, acc) tailstrict;1218 aux(a, b, 0, 0, []) tailstrict,12191220 setDiff(a, b, keyF=id)::1221 local aux(a, b, i, j, acc) =1222 if i >= std.length(a) then1223 acc1224 else if j >= std.length(b) then1225 acc + a[i:]1226 else1227 if keyF(a[i]) == keyF(b[j]) then1228 aux(a, b, i + 1, j + 1, acc) tailstrict1229 else if keyF(a[i]) < keyF(b[j]) then1230 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict1231 else1232 aux(a, b, i, j + 1, acc) tailstrict;1233 aux(a, b, 0, 0, []) tailstrict,12341235 mergePatch(target, patch)::1236 if std.isObject(patch) then1237 local target_object =1238 if std.isObject(target) then target else {};12391240 local target_fields =1241 if std.isObject(target_object) then std.objectFields(target_object) else [];12421243 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];1244 local both_fields = std.setUnion(target_fields, std.objectFields(patch));12451246 {1247 [k]:1248 if !std.objectHas(patch, k) then1249 target_object[k]1250 else if !std.objectHas(target_object, k) then1251 std.mergePatch(null, patch[k]) tailstrict1252 else1253 std.mergePatch(target_object[k], patch[k]) tailstrict1254 for k in std.setDiff(both_fields, null_fields)1255 }1256 else1257 patch,12581259 objectFields(o)::1260 std.objectFieldsEx(o, false),12611262 objectFieldsAll(o)::1263 std.objectFieldsEx(o, true),12641265 objectHas(o, f)::1266 std.objectHasEx(o, f, false),12671268 objectHasAll(o, f)::1269 std.objectHasEx(o, f, true),12701271 equals(a, b)::1272 local ta = std.type(a);1273 local tb = std.type(b);1274 if !std.primitiveEquals(ta, tb) then1275 false1276 else1277 if std.primitiveEquals(ta, 'array') then1278 local la = std.length(a);1279 if !std.primitiveEquals(la, std.length(b)) then1280 false1281 else1282 local aux(a, b, i) =1283 if i >= la then1284 true1285 else if a[i] != b[i] then1286 false1287 else1288 aux(a, b, i + 1) tailstrict;1289 aux(a, b, 0)1290 else if std.primitiveEquals(ta, 'object') then1291 local fields = std.objectFields(a);1292 local lfields = std.length(fields);1293 if fields != std.objectFields(b) then1294 false1295 else1296 local aux(a, b, i) =1297 if i >= lfields then1298 true1299 else if local f = fields[i]; a[f] != b[f] then1300 false1301 else1302 aux(a, b, i + 1) tailstrict;1303 aux(a, b, 0)1304 else1305 std.primitiveEquals(a, b),130613071308 resolvePath(f, r)::1309 local arr = std.split(f, '/');1310 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),13111312 prune(a)::1313 local isContent(b) =1314 if b == null then1315 false1316 else if std.isArray(b) then1317 std.length(b) > 01318 else if std.isObject(b) then1319 std.length(b) > 01320 else1321 true;1322 if std.isArray(a) then1323 [std.prune(x) for x in a if isContent($.prune(x))]1324 else if std.isObject(a) then {1325 [x]: $.prune(a[x])1326 for x in std.objectFields(a)1327 if isContent(std.prune(a[x]))1328 } else1329 a,13301331 findSubstr(pat, str)::1332 if !std.isString(pat) then1333 error 'findSubstr first parameter should be a string, got ' + std.type(pat)1334 else if !std.isString(str) then1335 error 'findSubstr second parameter should be a string, got ' + std.type(str)1336 else1337 local pat_len = std.length(pat);1338 local str_len = std.length(str);1339 if pat_len == 0 || str_len == 0 || pat_len > str_len then1340 []1341 else1342 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),13431344 find(value, arr)::1345 if !std.isArray(arr) then1346 error 'find second parameter should be an array, got ' + std.type(arr)1347 else1348 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),1349}