difftreelog
refactor simplify intrinsic handling
in: master
7 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,14 +7,11 @@
edition = "2018"
[features]
-default = ["serialized-stdlib", "faster", "explaining-traces", "serde-json"]
+default = ["serialized-stdlib", "explaining-traces", "serde-json"]
# Serializes standard library AST instead of parsing them every run
serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
# Allow to convert Val into serde_json::Value and backwards
serde-json = ["serde", "serde_json"]
-# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
-# Library works fine without this feature, but requires more memory and time for std function calls
-faster = []
# Rustc-like trace visualization
explaining-traces = ["annotate-snippets"]
# Allows library authors to throw custom errors
@@ -24,10 +21,10 @@
unstable = []
[dependencies]
-jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.0" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
-jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.0" }
+jrsonnet-interner = { path="../jrsonnet-interner", version="0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
+jrsonnet-types = { path="../jrsonnet-types", version="0.4.0" }
pathdiff = "0.2.0"
md5 = "0.7.0"
@@ -35,7 +32,7 @@
rustc-hash = "1.1.0"
thiserror = "1.0"
-jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
+jrsonnet-gc = { version="0.4.2", features=["derive"] }
[dependencies.anyhow]
version = "1.0"
@@ -61,7 +58,7 @@
optional = true
[build-dependencies]
-jrsonnet-parser = { path = "../jrsonnet-parser", features = ["serialize", "deserialize"], version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", features=["serialize", "deserialize"], version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
serde = "1.0"
bincode = "1.3.1"
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,14 +1,11 @@
use bincode::serialize;
-use jrsonnet_parser::{
- parse, Expr, FieldMember, FieldName, LocExpr, Member, ObjBody, ParserSettings,
-};
+use jrsonnet_parser::{parse, ParserSettings};
use jrsonnet_stdlib::STDLIB_STR;
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
- rc::Rc,
};
fn main() {
@@ -21,37 +18,6 @@
)
.expect("parse");
- let parsed = if cfg!(feature = "faster") {
- let LocExpr(expr, location) = parsed;
- LocExpr(
- Rc::new(match Rc::try_unwrap(expr).unwrap() {
- Expr::Obj(ObjBody::MemberList(members)) => Expr::Obj(ObjBody::MemberList(
- members
- .into_iter()
- .filter(|p| {
- !matches!(
- p,
- Member::Field(FieldMember {
- name: FieldName::Fixed(name),
- ..
- })
- if name == "join" || name == "manifestJsonEx" ||
- name == "escapeStringJson" || name == "equals" ||
- name == "base64" || name == "foldl" || name == "foldr" ||
- name == "sortImpl" || name == "format" || name == "range" ||
- name == "reverse" || name == "slice" || name == "mod" ||
- name == "strReplace" || name == "map"
- )
- })
- .collect(),
- )),
- _ => panic!("std value should be object"),
- }),
- location,
- )
- } else {
- parsed
- };
{
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("stdlib.bincode");
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -213,7 +213,6 @@
})
}
-// faster
fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "slice", args, 4, [
0, indexable: ty!((string | array));
@@ -230,7 +229,6 @@
})
}
-// faster
fn builtin_primitive_equals(
context: Context,
_loc: Option<&ExprLocation>,
@@ -244,7 +242,6 @@
})
}
-// faster
fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "equals", args, 2, [
0, a: ty!(any);
@@ -379,7 +376,6 @@
})
}
-// faster
fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "format", args, 2, [
0, str: ty!(string) => Val::Str;
@@ -529,7 +525,6 @@
})
}
-// faster
fn builtin_escape_string_json(
context: Context,
_loc: Option<&ExprLocation>,
@@ -542,7 +537,6 @@
})
}
-// faster
fn builtin_manifest_json_ex(
context: Context,
_loc: Option<&ExprLocation>,
@@ -559,7 +553,6 @@
})
}
-// faster
fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "reverse", args, 1, [
0, value: ty!(array) => Val::Arr;
@@ -576,7 +569,6 @@
})
}
-// faster
fn builtin_str_replace(
context: Context,
_loc: Option<&ExprLocation>,
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use crate::{2 builtin::std_slice,3 error::Error::*,4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},5 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,6 FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,7 Result, Val,8};9use jrsonnet_gc::{Gc, Trace};10use jrsonnet_interner::IStr;11use jrsonnet_parser::{12 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,13 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,14};15use jrsonnet_types::ValType;16use rustc_hash::{FxHashMap, FxHasher};17use std::{collections::HashMap, hash::BuildHasherDefault};18pub mod operator;1920pub fn evaluate_binding_in_future(21 b: &BindSpec,22 context_creator: FutureWrapper<Context>,23) -> LazyVal {24 let b = b.clone();25 if let Some(params) = &b.params {26 let params = params.clone();2728 #[derive(Trace)]29 #[trivially_drop]30 struct LazyMethodBinding {31 context_creator: FutureWrapper<Context>,32 name: IStr,33 params: ParamsDesc,34 value: LocExpr,35 }36 impl LazyValValue for LazyMethodBinding {37 fn get(self: Box<Self>) -> Result<Val> {38 Ok(evaluate_method(39 self.context_creator.unwrap(),40 self.name,41 self.params,42 self.value,43 ))44 }45 }4647 LazyVal::new(Box::new(LazyMethodBinding {48 context_creator,49 name: b.name.clone(),50 params,51 value: b.value.clone(),52 }))53 } else {54 #[derive(Trace)]55 #[trivially_drop]56 struct LazyNamedBinding {57 context_creator: FutureWrapper<Context>,58 name: IStr,59 value: LocExpr,60 }61 impl LazyValValue for LazyNamedBinding {62 fn get(self: Box<Self>) -> Result<Val> {63 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)64 }65 }66 LazyVal::new(Box::new(LazyNamedBinding {67 context_creator,68 name: b.name.clone(),69 value: b.value,70 }))71 }72}7374pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {75 let b = b.clone();76 if let Some(params) = &b.params {77 let params = params.clone();7879 #[derive(Trace)]80 #[trivially_drop]81 struct BindableMethodLazyVal {82 this: Option<ObjValue>,83 super_obj: Option<ObjValue>,8485 context_creator: ContextCreator,86 name: IStr,87 params: ParamsDesc,88 value: LocExpr,89 }90 impl LazyValValue for BindableMethodLazyVal {91 fn get(self: Box<Self>) -> Result<Val> {92 Ok(evaluate_method(93 self.context_creator.create(self.this, self.super_obj)?,94 self.name,95 self.params,96 self.value,97 ))98 }99 }100101 #[derive(Trace)]102 #[trivially_drop]103 struct BindableMethod {104 context_creator: ContextCreator,105 name: IStr,106 params: ParamsDesc,107 value: LocExpr,108 }109 impl Bindable for BindableMethod {110 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {111 Ok(LazyVal::new(Box::new(BindableMethodLazyVal {112 this,113 super_obj,114115 context_creator: self.context_creator.clone(),116 name: self.name.clone(),117 params: self.params.clone(),118 value: self.value.clone(),119 })))120 }121 }122123 (124 b.name.clone(),125 LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {126 context_creator,127 name: b.name.clone(),128 params,129 value: b.value.clone(),130 }))),131 )132 } else {133 #[derive(Trace)]134 #[trivially_drop]135 struct BindableNamedLazyVal {136 this: Option<ObjValue>,137 super_obj: Option<ObjValue>,138139 context_creator: ContextCreator,140 name: IStr,141 value: LocExpr,142 }143 impl LazyValValue for BindableNamedLazyVal {144 fn get(self: Box<Self>) -> Result<Val> {145 evaluate_named(146 self.context_creator.create(self.this, self.super_obj)?,147 &self.value,148 self.name,149 )150 }151 }152153 #[derive(Trace)]154 #[trivially_drop]155 struct BindableNamed {156 context_creator: ContextCreator,157 name: IStr,158 value: LocExpr,159 }160 impl Bindable for BindableNamed {161 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {162 Ok(LazyVal::new(Box::new(BindableNamedLazyVal {163 this,164 super_obj,165166 context_creator: self.context_creator.clone(),167 name: self.name.clone(),168 value: self.value.clone(),169 })))170 }171 }172173 (174 b.name.clone(),175 LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {176 context_creator,177 name: b.name.clone(),178 value: b.value.clone(),179 }))),180 )181 }182}183184pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {185 Val::Func(Gc::new(FuncVal::Normal(FuncDesc {186 name,187 ctx,188 params,189 body,190 })))191}192193pub fn evaluate_field_name(194 context: Context,195 field_name: &jrsonnet_parser::FieldName,196) -> Result<Option<IStr>> {197 Ok(match field_name {198 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),199 jrsonnet_parser::FieldName::Dyn(expr) => {200 let value = evaluate(context, expr)?;201 if matches!(value, Val::Null) {202 None203 } else {204 Some(value.try_cast_str("dynamic field name")?)205 }206 }207 })208}209210pub fn evaluate_comp(211 context: Context,212 specs: &[CompSpec],213 callback: &mut impl FnMut(Context) -> Result<()>,214) -> Result<()> {215 match specs.get(0) {216 None => callback(context)?,217 Some(CompSpec::IfSpec(IfSpecData(cond))) => {218 if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {219 evaluate_comp(context, &specs[1..], callback)?220 }221 }222 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223 Val::Arr(list) => {224 for item in list.iter() {225 evaluate_comp(226 context.clone().with_var(var.clone(), item?.clone()),227 &specs[1..],228 callback,229 )?230 }231 }232 _ => throw!(InComprehensionCanOnlyIterateOverArray),233 },234 }235 Ok(())236}237238pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {239 let new_bindings = FutureWrapper::new();240 let future_this = FutureWrapper::new();241 let context_creator = ContextCreator(context.clone(), new_bindings.clone());242 {243 let mut bindings: FxHashMap<IStr, LazyBinding> =244 FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());245 for (n, b) in members246 .iter()247 .filter_map(|m| match m {248 Member::BindStmt(b) => Some(b.clone()),249 _ => None,250 })251 .map(|b| evaluate_binding(&b, context_creator.clone()))252 {253 bindings.insert(n, b);254 }255 new_bindings.fill(bindings);256 }257258 let mut builder = ObjValueBuilder::new();259 for member in members.iter() {260 match member {261 Member::Field(FieldMember {262 name,263 plus,264 params: None,265 visibility,266 value,267 }) => {268 let name = evaluate_field_name(context.clone(), name)?;269 if name.is_none() {270 continue;271 }272 let name = name.unwrap();273274 #[derive(Trace)]275 #[trivially_drop]276 struct ObjMemberBinding {277 context_creator: ContextCreator,278 value: LocExpr,279 name: IStr,280 }281 impl Bindable for ObjMemberBinding {282 fn bind(283 &self,284 this: Option<ObjValue>,285 super_obj: Option<ObjValue>,286 ) -> Result<LazyVal> {287 Ok(LazyVal::new_resolved(evaluate_named(288 self.context_creator.create(this, super_obj)?,289 &self.value,290 self.name.clone(),291 )?))292 }293 }294 builder295 .member(name.clone())296 .with_add(*plus)297 .with_visibility(*visibility)298 .with_location(value.1.clone())299 .bindable(Box::new(ObjMemberBinding {300 context_creator: context_creator.clone(),301 value: value.clone(),302 name,303 }));304 }305 Member::Field(FieldMember {306 name,307 params: Some(params),308 value,309 ..310 }) => {311 let name = evaluate_field_name(context.clone(), name)?;312 if name.is_none() {313 continue;314 }315 let name = name.unwrap();316 #[derive(Trace)]317 #[trivially_drop]318 struct ObjMemberBinding {319 context_creator: ContextCreator,320 value: LocExpr,321 params: ParamsDesc,322 name: IStr,323 }324 impl Bindable for ObjMemberBinding {325 fn bind(326 &self,327 this: Option<ObjValue>,328 super_obj: Option<ObjValue>,329 ) -> Result<LazyVal> {330 Ok(LazyVal::new_resolved(evaluate_method(331 self.context_creator.create(this, super_obj)?,332 self.name.clone(),333 self.params.clone(),334 self.value.clone(),335 )))336 }337 }338 builder339 .member(name.clone())340 .hide()341 .with_location(value.1.clone())342 .binding(LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {343 context_creator: context_creator.clone(),344 value: value.clone(),345 params: params.clone(),346 name,347 }))));348 }349 Member::BindStmt(_) => {}350 Member::AssertStmt(stmt) => {351 #[derive(Trace)]352 #[trivially_drop]353 struct ObjectAssert {354 context_creator: ContextCreator,355 assert: AssertStmt,356 }357 impl ObjectAssertion for ObjectAssert {358 fn run(359 &self,360 this: Option<ObjValue>,361 super_obj: Option<ObjValue>,362 ) -> Result<()> {363 let ctx = self.context_creator.create(this, super_obj)?;364 evaluate_assert(ctx, &self.assert)365 }366 }367 builder.assert(Box::new(ObjectAssert {368 context_creator: context_creator.clone(),369 assert: stmt.clone(),370 }));371 }372 }373 }374 let this = builder.build();375 future_this.fill(this.clone());376 Ok(this)377}378379pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {380 Ok(match object {381 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,382 ObjBody::ObjComp(obj) => {383 let future_this = FutureWrapper::new();384 let mut builder = ObjValueBuilder::new();385 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {386 let new_bindings = FutureWrapper::new();387 let context_creator = ContextCreator(context.clone(), new_bindings.clone());388 let mut bindings: FxHashMap<IStr, LazyBinding> =389 FxHashMap::with_capacity_and_hasher(390 obj.pre_locals.len() + obj.post_locals.len(),391 BuildHasherDefault::default(),392 );393 for (n, b) in obj394 .pre_locals395 .iter()396 .chain(obj.post_locals.iter())397 .map(|b| evaluate_binding(b, context_creator.clone()))398 {399 bindings.insert(n, b);400 }401 new_bindings.fill(bindings.clone());402 let ctx = ctx.extend_unbound(bindings, None, None, None)?;403 let key = evaluate(ctx.clone(), &obj.key)?;404405 match key {406 Val::Null => {}407 Val::Str(n) => {408 #[derive(Trace)]409 #[trivially_drop]410 struct ObjCompBinding {411 context: Context,412 value: LocExpr,413 }414 impl Bindable for ObjCompBinding {415 fn bind(416 &self,417 this: Option<ObjValue>,418 _super_obj: Option<ObjValue>,419 ) -> Result<LazyVal> {420 Ok(LazyVal::new_resolved(evaluate(421 self.context.clone().extend(422 FxHashMap::default(),423 None,424 this,425 None,426 ),427 &self.value,428 )?))429 }430 }431 builder432 .member(n)433 .with_location(obj.value.1.clone())434 .bindable(Box::new(ObjCompBinding {435 context: ctx,436 value: obj.value.clone(),437 }));438 }439 v => throw!(FieldMustBeStringGot(v.value_type())),440 }441442 Ok(())443 })?;444445 let this = builder.build();446 future_this.fill(this.clone());447 this448 }449 })450}451452pub fn evaluate_apply(453 context: Context,454 value: &LocExpr,455 args: &ArgsDesc,456 loc: Option<&ExprLocation>,457 tailstrict: bool,458) -> Result<Val> {459 let value = evaluate(context.clone(), value)?;460 Ok(match value {461 Val::Func(f) => {462 let body = || f.evaluate(context, loc, args, tailstrict);463 if tailstrict {464 body()?465 } else {466 push(loc, || format!("function <{}> call", f.name()), body)?467 }468 }469 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),470 })471}472473pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {474 let value = &assertion.0;475 let msg = &assertion.1;476 let assertion_result = push(477 value.1.as_ref(),478 || "assertion condition".to_owned(),479 || {480 evaluate(context.clone(), value)?481 .try_cast_bool("assertion condition should be of type `boolean`")482 },483 )?;484 if !assertion_result {485 push(486 value.1.as_ref(),487 || "assertion failure".to_owned(),488 || {489 if let Some(msg) = msg {490 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));491 } else {492 throw!(AssertionFailed(Val::Null.to_string()?));493 }494 },495 )?496 }497 Ok(())498}499500pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {501 use Expr::*;502 let LocExpr(expr, _loc) = lexpr;503 Ok(match &**expr {504 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),505 _ => evaluate(context, lexpr)?,506 })507}508509pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {510 use Expr::*;511 let LocExpr(expr, loc) = expr;512 Ok(match &**expr {513 Literal(LiteralType::This) => {514 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)515 }516 Literal(LiteralType::Super) => Val::Obj(517 context518 .super_obj()519 .clone()520 .ok_or(NoSuperFound)?521 .with_this(context.this().clone().unwrap()),522 ),523 Literal(LiteralType::Dollar) => {524 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)525 }526 Literal(LiteralType::True) => Val::Bool(true),527 Literal(LiteralType::False) => Val::Bool(false),528 Literal(LiteralType::Null) => Val::Null,529 Parened(e) => evaluate(context, e)?,530 Str(v) => Val::Str(v.clone()),531 Num(v) => Val::new_checked_num(*v)?,532 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,533 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,534 Var(name) => push(535 loc.as_ref(),536 || format!("variable <{}>", name),537 || context.binding(name.clone())?.evaluate(),538 )?,539 Index(value, index) => {540 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {541 (Val::Obj(v), Val::Str(s)) => {542 let sn = s.clone();543 push(544 loc.as_ref(),545 || format!("field <{}> access", sn),546 || {547 if let Some(v) = v.get(s.clone())? {548 Ok(v)549 } else if v.get("__intrinsic_namespace__".into())?.is_some() {550 Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))551 } else {552 throw!(NoSuchField(s))553 }554 },555 )?556 }557 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(558 ValType::Obj,559 ValType::Str,560 n.value_type(),561 )),562563 (Val::Arr(v), Val::Num(n)) => {564 if n.fract() > f64::EPSILON {565 throw!(FractionalIndex)566 }567 v.get(n as usize)?568 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?569 }570 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),571 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(572 ValType::Arr,573 ValType::Num,574 n.value_type(),575 )),576577 (Val::Str(s), Val::Num(n)) => Val::Str(578 s.chars()579 .skip(n as usize)580 .take(1)581 .collect::<String>()582 .into(),583 ),584 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(585 ValType::Str,586 ValType::Num,587 n.value_type(),588 )),589590 (v, _) => throw!(CantIndexInto(v.value_type())),591 }592 }593 LocalExpr(bindings, returned) => {594 let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(595 bindings.len(),596 BuildHasherDefault::<FxHasher>::default(),597 );598 let future_context = Context::new_future();599 for b in bindings {600 new_bindings.insert(601 b.name.clone(),602 evaluate_binding_in_future(b, future_context.clone()),603 );604 }605 let context = context606 .extend_bound(new_bindings)607 .into_future(future_context);608 evaluate(context, &returned.clone())?609 }610 Arr(items) => {611 let mut out = Vec::with_capacity(items.len());612 for item in items {613 // TODO: Implement ArrValue::Lazy with same context for every element?614 #[derive(Trace)]615 #[trivially_drop]616 struct ArrayElement {617 context: Context,618 item: LocExpr,619 }620 impl LazyValValue for ArrayElement {621 fn get(self: Box<Self>) -> Result<Val> {622 evaluate(self.context, &self.item)623 }624 }625 out.push(LazyVal::new(Box::new(ArrayElement {626 context: context.clone(),627 item: item.clone(),628 })));629 }630 Val::Arr(out.into())631 }632 ArrComp(expr, comp_specs) => {633 let mut out = Vec::new();634 evaluate_comp(context, comp_specs, &mut |ctx| {635 out.push(evaluate(ctx, expr)?);636 Ok(())637 })?;638 Val::Arr(ArrValue::Eager(Gc::new(out)))639 }640 Obj(body) => Val::Obj(evaluate_object(context, body)?),641 ObjExtend(s, t) => evaluate_add_op(642 &evaluate(context.clone(), s)?,643 &Val::Obj(evaluate_object(context, t)?),644 )?,645 Apply(value, args, tailstrict) => {646 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?647 }648 Function(params, body) => {649 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())650 }651 Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),652 AssertExpr(assert, returned) => {653 evaluate_assert(context.clone(), assert)?;654 evaluate(context, returned)?655 }656 ErrorStmt(e) => push(657 loc.as_ref(),658 || "error statement".to_owned(),659 || {660 throw!(RuntimeError(661 evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,662 ))663 },664 )?,665 IfElse {666 cond,667 cond_then,668 cond_else,669 } => {670 if push(671 loc.as_ref(),672 || "if condition".to_owned(),673 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),674 )? {675 evaluate(context, cond_then)?676 } else {677 match cond_else {678 Some(v) => evaluate(context, v)?,679 None => Val::Null,680 }681 }682 }683 Slice(value, desc) => {684 let indexable = evaluate(context.clone(), value)?;685686 fn parse_num(687 context: &Context,688 expr: Option<&LocExpr>,689 desc: &'static str,690 ) -> Result<Option<usize>> {691 Ok(match expr {692 Some(s) => evaluate(context.clone(), &s)?693 .try_cast_nullable_num(desc)?694 .map(|v| v as usize),695 None => None,696 })697 }698699 let start = parse_num(&context, desc.start.as_ref(), "start")?;700 let end = parse_num(&context, desc.end.as_ref(), "end")?;701 let step = parse_num(&context, desc.step.as_ref(), "step")?;702703 std_slice(indexable.to_indexable()?, start, end, step)?704 }705 Import(path) => {706 let tmp = loc707 .clone()708 .expect("imports cannot be used without loc_data")709 .0;710 let mut import_location = tmp.to_path_buf();711 import_location.pop();712 push(713 loc.as_ref(),714 || format!("import {:?}", path),715 || with_state(|s| s.import_file(&import_location, path)),716 )?717 }718 ImportStr(path) => {719 let tmp = loc720 .clone()721 .expect("imports cannot be used without loc_data")722 .0;723 let mut import_location = tmp.to_path_buf();724 import_location.pop();725 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)726 }727 })728}1use crate::{2 builtin::std_slice,3 error::Error::*,4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},5 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,6 FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,7 Result, Val,8};9use jrsonnet_gc::{Gc, Trace};10use jrsonnet_interner::IStr;11use jrsonnet_parser::{12 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,13 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,14};15use jrsonnet_types::ValType;16use rustc_hash::{FxHashMap, FxHasher};17use std::{collections::HashMap, hash::BuildHasherDefault};18pub mod operator;1920pub fn evaluate_binding_in_future(21 b: &BindSpec,22 context_creator: FutureWrapper<Context>,23) -> LazyVal {24 let b = b.clone();25 if let Some(params) = &b.params {26 let params = params.clone();2728 #[derive(Trace)]29 #[trivially_drop]30 struct LazyMethodBinding {31 context_creator: FutureWrapper<Context>,32 name: IStr,33 params: ParamsDesc,34 value: LocExpr,35 }36 impl LazyValValue for LazyMethodBinding {37 fn get(self: Box<Self>) -> Result<Val> {38 Ok(evaluate_method(39 self.context_creator.unwrap(),40 self.name,41 self.params,42 self.value,43 ))44 }45 }4647 LazyVal::new(Box::new(LazyMethodBinding {48 context_creator,49 name: b.name.clone(),50 params,51 value: b.value.clone(),52 }))53 } else {54 #[derive(Trace)]55 #[trivially_drop]56 struct LazyNamedBinding {57 context_creator: FutureWrapper<Context>,58 name: IStr,59 value: LocExpr,60 }61 impl LazyValValue for LazyNamedBinding {62 fn get(self: Box<Self>) -> Result<Val> {63 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)64 }65 }66 LazyVal::new(Box::new(LazyNamedBinding {67 context_creator,68 name: b.name.clone(),69 value: b.value,70 }))71 }72}7374pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {75 let b = b.clone();76 if let Some(params) = &b.params {77 let params = params.clone();7879 #[derive(Trace)]80 #[trivially_drop]81 struct BindableMethodLazyVal {82 this: Option<ObjValue>,83 super_obj: Option<ObjValue>,8485 context_creator: ContextCreator,86 name: IStr,87 params: ParamsDesc,88 value: LocExpr,89 }90 impl LazyValValue for BindableMethodLazyVal {91 fn get(self: Box<Self>) -> Result<Val> {92 Ok(evaluate_method(93 self.context_creator.create(self.this, self.super_obj)?,94 self.name,95 self.params,96 self.value,97 ))98 }99 }100101 #[derive(Trace)]102 #[trivially_drop]103 struct BindableMethod {104 context_creator: ContextCreator,105 name: IStr,106 params: ParamsDesc,107 value: LocExpr,108 }109 impl Bindable for BindableMethod {110 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {111 Ok(LazyVal::new(Box::new(BindableMethodLazyVal {112 this,113 super_obj,114115 context_creator: self.context_creator.clone(),116 name: self.name.clone(),117 params: self.params.clone(),118 value: self.value.clone(),119 })))120 }121 }122123 (124 b.name.clone(),125 LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {126 context_creator,127 name: b.name.clone(),128 params,129 value: b.value.clone(),130 }))),131 )132 } else {133 #[derive(Trace)]134 #[trivially_drop]135 struct BindableNamedLazyVal {136 this: Option<ObjValue>,137 super_obj: Option<ObjValue>,138139 context_creator: ContextCreator,140 name: IStr,141 value: LocExpr,142 }143 impl LazyValValue for BindableNamedLazyVal {144 fn get(self: Box<Self>) -> Result<Val> {145 evaluate_named(146 self.context_creator.create(self.this, self.super_obj)?,147 &self.value,148 self.name,149 )150 }151 }152153 #[derive(Trace)]154 #[trivially_drop]155 struct BindableNamed {156 context_creator: ContextCreator,157 name: IStr,158 value: LocExpr,159 }160 impl Bindable for BindableNamed {161 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {162 Ok(LazyVal::new(Box::new(BindableNamedLazyVal {163 this,164 super_obj,165166 context_creator: self.context_creator.clone(),167 name: self.name.clone(),168 value: self.value.clone(),169 })))170 }171 }172173 (174 b.name.clone(),175 LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {176 context_creator,177 name: b.name.clone(),178 value: b.value.clone(),179 }))),180 )181 }182}183184pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {185 Val::Func(Gc::new(FuncVal::Normal(FuncDesc {186 name,187 ctx,188 params,189 body,190 })))191}192193pub fn evaluate_field_name(194 context: Context,195 field_name: &jrsonnet_parser::FieldName,196) -> Result<Option<IStr>> {197 Ok(match field_name {198 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),199 jrsonnet_parser::FieldName::Dyn(expr) => {200 let value = evaluate(context, expr)?;201 if matches!(value, Val::Null) {202 None203 } else {204 Some(value.try_cast_str("dynamic field name")?)205 }206 }207 })208}209210pub fn evaluate_comp(211 context: Context,212 specs: &[CompSpec],213 callback: &mut impl FnMut(Context) -> Result<()>,214) -> Result<()> {215 match specs.get(0) {216 None => callback(context)?,217 Some(CompSpec::IfSpec(IfSpecData(cond))) => {218 if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {219 evaluate_comp(context, &specs[1..], callback)?220 }221 }222 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223 Val::Arr(list) => {224 for item in list.iter() {225 evaluate_comp(226 context.clone().with_var(var.clone(), item?.clone()),227 &specs[1..],228 callback,229 )?230 }231 }232 _ => throw!(InComprehensionCanOnlyIterateOverArray),233 },234 }235 Ok(())236}237238pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {239 let new_bindings = FutureWrapper::new();240 let future_this = FutureWrapper::new();241 let context_creator = ContextCreator(context.clone(), new_bindings.clone());242 {243 let mut bindings: FxHashMap<IStr, LazyBinding> =244 FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());245 for (n, b) in members246 .iter()247 .filter_map(|m| match m {248 Member::BindStmt(b) => Some(b.clone()),249 _ => None,250 })251 .map(|b| evaluate_binding(&b, context_creator.clone()))252 {253 bindings.insert(n, b);254 }255 new_bindings.fill(bindings);256 }257258 let mut builder = ObjValueBuilder::new();259 for member in members.iter() {260 match member {261 Member::Field(FieldMember {262 name,263 plus,264 params: None,265 visibility,266 value,267 }) => {268 let name = evaluate_field_name(context.clone(), name)?;269 if name.is_none() {270 continue;271 }272 let name = name.unwrap();273274 #[derive(Trace)]275 #[trivially_drop]276 struct ObjMemberBinding {277 context_creator: ContextCreator,278 value: LocExpr,279 name: IStr,280 }281 impl Bindable for ObjMemberBinding {282 fn bind(283 &self,284 this: Option<ObjValue>,285 super_obj: Option<ObjValue>,286 ) -> Result<LazyVal> {287 Ok(LazyVal::new_resolved(evaluate_named(288 self.context_creator.create(this, super_obj)?,289 &self.value,290 self.name.clone(),291 )?))292 }293 }294 builder295 .member(name.clone())296 .with_add(*plus)297 .with_visibility(*visibility)298 .with_location(value.1.clone())299 .bindable(Box::new(ObjMemberBinding {300 context_creator: context_creator.clone(),301 value: value.clone(),302 name,303 }));304 }305 Member::Field(FieldMember {306 name,307 params: Some(params),308 value,309 ..310 }) => {311 let name = evaluate_field_name(context.clone(), name)?;312 if name.is_none() {313 continue;314 }315 let name = name.unwrap();316 #[derive(Trace)]317 #[trivially_drop]318 struct ObjMemberBinding {319 context_creator: ContextCreator,320 value: LocExpr,321 params: ParamsDesc,322 name: IStr,323 }324 impl Bindable for ObjMemberBinding {325 fn bind(326 &self,327 this: Option<ObjValue>,328 super_obj: Option<ObjValue>,329 ) -> Result<LazyVal> {330 Ok(LazyVal::new_resolved(evaluate_method(331 self.context_creator.create(this, super_obj)?,332 self.name.clone(),333 self.params.clone(),334 self.value.clone(),335 )))336 }337 }338 builder339 .member(name.clone())340 .hide()341 .with_location(value.1.clone())342 .binding(LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {343 context_creator: context_creator.clone(),344 value: value.clone(),345 params: params.clone(),346 name,347 }))));348 }349 Member::BindStmt(_) => {}350 Member::AssertStmt(stmt) => {351 #[derive(Trace)]352 #[trivially_drop]353 struct ObjectAssert {354 context_creator: ContextCreator,355 assert: AssertStmt,356 }357 impl ObjectAssertion for ObjectAssert {358 fn run(359 &self,360 this: Option<ObjValue>,361 super_obj: Option<ObjValue>,362 ) -> Result<()> {363 let ctx = self.context_creator.create(this, super_obj)?;364 evaluate_assert(ctx, &self.assert)365 }366 }367 builder.assert(Box::new(ObjectAssert {368 context_creator: context_creator.clone(),369 assert: stmt.clone(),370 }));371 }372 }373 }374 let this = builder.build();375 future_this.fill(this.clone());376 Ok(this)377}378379pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {380 Ok(match object {381 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,382 ObjBody::ObjComp(obj) => {383 let future_this = FutureWrapper::new();384 let mut builder = ObjValueBuilder::new();385 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {386 let new_bindings = FutureWrapper::new();387 let context_creator = ContextCreator(context.clone(), new_bindings.clone());388 let mut bindings: FxHashMap<IStr, LazyBinding> =389 FxHashMap::with_capacity_and_hasher(390 obj.pre_locals.len() + obj.post_locals.len(),391 BuildHasherDefault::default(),392 );393 for (n, b) in obj394 .pre_locals395 .iter()396 .chain(obj.post_locals.iter())397 .map(|b| evaluate_binding(b, context_creator.clone()))398 {399 bindings.insert(n, b);400 }401 new_bindings.fill(bindings.clone());402 let ctx = ctx.extend_unbound(bindings, None, None, None)?;403 let key = evaluate(ctx.clone(), &obj.key)?;404405 match key {406 Val::Null => {}407 Val::Str(n) => {408 #[derive(Trace)]409 #[trivially_drop]410 struct ObjCompBinding {411 context: Context,412 value: LocExpr,413 }414 impl Bindable for ObjCompBinding {415 fn bind(416 &self,417 this: Option<ObjValue>,418 _super_obj: Option<ObjValue>,419 ) -> Result<LazyVal> {420 Ok(LazyVal::new_resolved(evaluate(421 self.context.clone().extend(422 FxHashMap::default(),423 None,424 this,425 None,426 ),427 &self.value,428 )?))429 }430 }431 builder432 .member(n)433 .with_location(obj.value.1.clone())434 .bindable(Box::new(ObjCompBinding {435 context: ctx,436 value: obj.value.clone(),437 }));438 }439 v => throw!(FieldMustBeStringGot(v.value_type())),440 }441442 Ok(())443 })?;444445 let this = builder.build();446 future_this.fill(this.clone());447 this448 }449 })450}451452pub fn evaluate_apply(453 context: Context,454 value: &LocExpr,455 args: &ArgsDesc,456 loc: Option<&ExprLocation>,457 tailstrict: bool,458) -> Result<Val> {459 let value = evaluate(context.clone(), value)?;460 Ok(match value {461 Val::Func(f) => {462 let body = || f.evaluate(context, loc, args, tailstrict);463 if tailstrict {464 body()?465 } else {466 push(loc, || format!("function <{}> call", f.name()), body)?467 }468 }469 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),470 })471}472473pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {474 let value = &assertion.0;475 let msg = &assertion.1;476 let assertion_result = push(477 value.1.as_ref(),478 || "assertion condition".to_owned(),479 || {480 evaluate(context.clone(), value)?481 .try_cast_bool("assertion condition should be of type `boolean`")482 },483 )?;484 if !assertion_result {485 push(486 value.1.as_ref(),487 || "assertion failure".to_owned(),488 || {489 if let Some(msg) = msg {490 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));491 } else {492 throw!(AssertionFailed(Val::Null.to_string()?));493 }494 },495 )?496 }497 Ok(())498}499500pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {501 use Expr::*;502 let LocExpr(expr, _loc) = lexpr;503 Ok(match &**expr {504 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),505 _ => evaluate(context, lexpr)?,506 })507}508509pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {510 use Expr::*;511 let LocExpr(expr, loc) = expr;512 Ok(match &**expr {513 Literal(LiteralType::This) => {514 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)515 }516 Literal(LiteralType::Super) => Val::Obj(517 context518 .super_obj()519 .clone()520 .ok_or(NoSuperFound)?521 .with_this(context.this().clone().unwrap()),522 ),523 Literal(LiteralType::Dollar) => {524 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)525 }526 Literal(LiteralType::True) => Val::Bool(true),527 Literal(LiteralType::False) => Val::Bool(false),528 Literal(LiteralType::Null) => Val::Null,529 Parened(e) => evaluate(context, e)?,530 Str(v) => Val::Str(v.clone()),531 Num(v) => Val::new_checked_num(*v)?,532 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,533 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,534 Var(name) => push(535 loc.as_ref(),536 || format!("variable <{}>", name),537 || context.binding(name.clone())?.evaluate(),538 )?,539 Index(value, index) => {540 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {541 (Val::Obj(v), Val::Str(s)) => {542 let sn = s.clone();543 push(544 loc.as_ref(),545 || format!("field <{}> access", sn),546 || {547 if let Some(v) = v.get(s.clone())? {548 Ok(v)549 } else {550 throw!(NoSuchField(s))551 }552 },553 )?554 }555 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(556 ValType::Obj,557 ValType::Str,558 n.value_type(),559 )),560561 (Val::Arr(v), Val::Num(n)) => {562 if n.fract() > f64::EPSILON {563 throw!(FractionalIndex)564 }565 v.get(n as usize)?566 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?567 }568 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),569 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(570 ValType::Arr,571 ValType::Num,572 n.value_type(),573 )),574575 (Val::Str(s), Val::Num(n)) => Val::Str(576 s.chars()577 .skip(n as usize)578 .take(1)579 .collect::<String>()580 .into(),581 ),582 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(583 ValType::Str,584 ValType::Num,585 n.value_type(),586 )),587588 (v, _) => throw!(CantIndexInto(v.value_type())),589 }590 }591 LocalExpr(bindings, returned) => {592 let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(593 bindings.len(),594 BuildHasherDefault::<FxHasher>::default(),595 );596 let future_context = Context::new_future();597 for b in bindings {598 new_bindings.insert(599 b.name.clone(),600 evaluate_binding_in_future(b, future_context.clone()),601 );602 }603 let context = context604 .extend_bound(new_bindings)605 .into_future(future_context);606 evaluate(context, &returned.clone())?607 }608 Arr(items) => {609 let mut out = Vec::with_capacity(items.len());610 for item in items {611 // TODO: Implement ArrValue::Lazy with same context for every element?612 #[derive(Trace)]613 #[trivially_drop]614 struct ArrayElement {615 context: Context,616 item: LocExpr,617 }618 impl LazyValValue for ArrayElement {619 fn get(self: Box<Self>) -> Result<Val> {620 evaluate(self.context, &self.item)621 }622 }623 out.push(LazyVal::new(Box::new(ArrayElement {624 context: context.clone(),625 item: item.clone(),626 })));627 }628 Val::Arr(out.into())629 }630 ArrComp(expr, comp_specs) => {631 let mut out = Vec::new();632 evaluate_comp(context, comp_specs, &mut |ctx| {633 out.push(evaluate(ctx, expr)?);634 Ok(())635 })?;636 Val::Arr(ArrValue::Eager(Gc::new(out)))637 }638 Obj(body) => Val::Obj(evaluate_object(context, body)?),639 ObjExtend(s, t) => evaluate_add_op(640 &evaluate(context.clone(), s)?,641 &Val::Obj(evaluate_object(context, t)?),642 )?,643 Apply(value, args, tailstrict) => {644 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?645 }646 Function(params, body) => {647 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())648 }649 Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),650 AssertExpr(assert, returned) => {651 evaluate_assert(context.clone(), assert)?;652 evaluate(context, returned)?653 }654 ErrorStmt(e) => push(655 loc.as_ref(),656 || "error statement".to_owned(),657 || {658 throw!(RuntimeError(659 evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,660 ))661 },662 )?,663 IfElse {664 cond,665 cond_then,666 cond_else,667 } => {668 if push(669 loc.as_ref(),670 || "if condition".to_owned(),671 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),672 )? {673 evaluate(context, cond_then)?674 } else {675 match cond_else {676 Some(v) => evaluate(context, v)?,677 None => Val::Null,678 }679 }680 }681 Slice(value, desc) => {682 let indexable = evaluate(context.clone(), value)?;683684 fn parse_num(685 context: &Context,686 expr: Option<&LocExpr>,687 desc: &'static str,688 ) -> Result<Option<usize>> {689 Ok(match expr {690 Some(s) => evaluate(context.clone(), &s)?691 .try_cast_nullable_num(desc)?692 .map(|v| v as usize),693 None => None,694 })695 }696697 let start = parse_num(&context, desc.start.as_ref(), "start")?;698 let end = parse_num(&context, desc.end.as_ref(), "end")?;699 let step = parse_num(&context, desc.step.as_ref(), "step")?;700701 std_slice(indexable.to_indexable()?, start, end, step)?702 }703 Import(path) => {704 let tmp = loc705 .clone()706 .expect("imports cannot be used without loc_data")707 .0;708 let mut import_location = tmp.to_path_buf();709 import_location.pop();710 push(711 loc.as_ref(),712 || format!("import {:?}", path),713 || with_state(|s| s.import_file(&import_location, path)),714 )?715 }716 ImportStr(path) => {717 let tmp = loc718 .clone()719 .expect("imports cannot be used without loc_data")720 .0;721 let mut import_location = tmp.to_path_buf();722 import_location.pop();723 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)724 }725 })726}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -531,7 +531,6 @@
}
/// Calls `std.manifestJson`
- #[cfg(feature = "faster")]
pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
manifest_json_ex(
self,
@@ -543,30 +542,6 @@
.map(|s| s.into())
}
- /// Calls `std.manifestJson`
- #[cfg(not(feature = "faster"))]
- pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
- with_state(|s| {
- let ctx = s
- .create_default_context()?
- .with_var("__tmp__to_json__".into(), self.clone())?;
- Ok(evaluate(
- ctx,
- &el!(Expr::Apply(
- el!(Expr::Index(
- el!(Expr::Var("std".into())),
- el!(Expr::Str("manifestJsonEx".into()))
- )),
- ArgsDesc(vec![
- Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),
- Arg(None, el!(Expr::Str(" ".repeat(padding).into())))
- ]),
- false
- )),
- )?
- .try_cast_str("to json")?)
- })
- }
pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
with_state(|s| {
let ctx = s
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -192,6 +192,8 @@
pub rule expr_basic(s: &ParserSettings) -> LocExpr
= literal(s)
+ / quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}
+
/ string_expr(s) / number_expr(s)
/ array_expr(s)
/ obj_expr(s)
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -1,9 +1,29 @@
{
- __intrinsic_namespace__:: 'std',
-
local std = self,
local id = std.id,
+ # Those functions aren't normally located in stdlib
+ length:: $intrinsic(length),
+ type:: $intrinsic(type),
+ makeArray:: $intrinsic(makeArray),
+ codepoint:: $intrinsic(codepoint),
+ objectFieldsEx:: $intrinsic(objectFieldsEx),
+ objectHasEx:: $intrinsic(objectHasEx),
+ primitiveEquals:: $intrinsic(primitiveEquals),
+ modulo:: $intrinsic(modulo),
+ floor:: $intrinsic(floor),
+ log:: $intrinsic(log),
+ pow:: $intrinsic(pow),
+ extVar:: $intrinsic(extVar),
+ native:: $intrinsic(native),
+ filter:: $intrinsic(filter),
+ char:: $intrinsic(char),
+ encodeUTF8:: $intrinsic(encodeUTF8),
+ md5:: $intrinsic(md5),
+ trace:: $intrinsic(trace),
+ id:: $intrinsic(id),
+ parseJson:: $intrinsic(parseJson),
+
isString(v):: std.type(v) == 'string',
isNumber(v):: std.type(v) == 'number',
isBoolean(v):: std.type(v) == 'boolean',
@@ -109,37 +129,8 @@
else
aux(str, delim, i2, arr, v + c) tailstrict;
aux(str, c, 0, [], ''),
-
- strReplace(str, from, to)::
- assert std.isString(str);
- assert std.isString(from);
- assert std.isString(to);
- assert from != '' : "'from' string must not be zero length.";
- // Cache for performance.
- local str_len = std.length(str);
- local from_len = std.length(from);
-
- // True if from is at str[i].
- local found_at(i) = str[i:i + from_len] == from;
-
- // Return the remainder of 'str' starting with 'start_index' where
- // all occurrences of 'from' after 'curr_index' are replaced with 'to'.
- local replace_after(start_index, curr_index, acc) =
- if curr_index > str_len then
- acc + str[start_index:curr_index]
- else if found_at(curr_index) then
- local new_index = curr_index + std.length(from);
- replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict
- else
- replace_after(start_index, curr_index + 1, acc) tailstrict;
-
- // if from_len==1, then we replace by splitting and rejoining the
- // string which is much faster than recursing on replace_after
- if from_len == 1 then
- std.join(to, std.split(str, from))
- else
- replace_after(0, 0, ''),
+ strReplace:: $intrinsic(strReplace),
asciiUpper(str)::
local cp = std.codepoint;
@@ -157,8 +148,7 @@
c;
std.join('', std.map(down_letter, std.stringChars(str))),
- range(from, to)::
- std.makeArray(to - from + 1, function(i) i + from),
+ range:: $intrinsic(range),
repeat(what, count)::
local joiner =
@@ -167,38 +157,7 @@
else error 'std.repeat first argument must be an array or a string';
std.join(joiner, std.makeArray(count, function(i) what)),
- slice(indexable, index, end, step)::
- local invar =
- // loop invariant with defaults applied
- {
- indexable: indexable,
- index:
- if index == null then 0
- else index,
- end:
- if end == null then std.length(indexable)
- else end,
- step:
- if step == null then 1
- else step,
- length: std.length(indexable),
- type: std.type(indexable),
- };
- assert invar.index >= 0 && invar.end >= 0 && invar.step >= 0 : 'got [%s:%s:%s] but negative index, end, and steps are not supported' % [invar.index, invar.end, invar.step];
- assert step != 0 : 'got %s but step must be greater than 0' % step;
- assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
- local build(slice, cur) =
- if cur >= invar.end || cur >= invar.length then
- slice
- else
- build(
- if invar.type == 'string' then
- slice + invar.indexable[cur]
- else
- slice + [invar.indexable[cur]],
- cur + invar.step
- ) tailstrict;
- build(if invar.type == 'string' then '' else [], invar.index),
+ slice:: $intrinsic(slice),
member(arr, x)::
if std.isArray(arr) then
@@ -209,21 +168,9 @@
count(arr, x):: std.length(std.filter(function(v) v == x, arr)),
- mod(a, b)::
- if std.isNumber(a) && std.isNumber(b) then
- std.modulo(a, b)
- else if std.isString(a) then
- std.format(a, b)
- else
- error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',
+ mod:: $intrinsic(mod),
- map(func, arr)::
- if !std.isFunction(func) then
- error ('std.map first param must be function, got ' + std.type(func))
- else if !std.isArray(arr) && !std.isString(arr) then
- error ('std.map second param must be array / string, got ' + std.type(arr))
- else
- std.makeArray(std.length(arr), function(i) func(arr[i])),
+ map:: $intrinsic(map),
mapWithIndex(func, arr)::
if !std.isFunction(func) then
@@ -250,26 +197,7 @@
std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),
- join(sep, arr)::
- local aux(arr, i, first, running) =
- if i >= std.length(arr) then
- running
- else if arr[i] == null then
- aux(arr, i + 1, first, running) tailstrict
- else if std.type(arr[i]) != std.type(sep) then
- error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]
- else if first then
- aux(arr, i + 1, false, running + arr[i]) tailstrict
- else
- aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;
- if !std.isArray(arr) then
- error 'join second parameter should be array, got ' + std.type(arr)
- else if std.isString(sep) then
- aux(arr, 0, true, '')
- else if std.isArray(sep) then
- aux(arr, 0, true, [])
- else
- error 'join first parameter should be string or array, got ' + std.type(sep),
+ join:: $intrinsic(join),
lines(arr)::
std.join('\n', arr + ['']),
@@ -281,479 +209,14 @@
std.join('', [std.deepJoin(x) for x in arr])
else
error 'Expected string or array, got %s' % std.type(arr),
-
-
- format(str, vals)::
-
- /////////////////////////////
- // Parse the mini-language //
- /////////////////////////////
-
- local try_parse_mapping_key(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == '(' then
- local consume(str, j, v) =
- if j >= std.length(str) then
- error 'Truncated format code.'
- else
- local c = str[j];
- if c != ')' then
- consume(str, j + 1, v + c)
- else
- { i: j + 1, v: v };
- consume(str, i + 1, '')
- else
- { i: i, v: null };
- local try_parse_cflags(str, i) =
- local consume(str, j, v) =
- assert j < std.length(str) : 'Truncated format code.';
- local c = str[j];
- if c == '#' then
- consume(str, j + 1, v { alt: true })
- else if c == '0' then
- consume(str, j + 1, v { zero: true })
- else if c == '-' then
- consume(str, j + 1, v { left: true })
- else if c == ' ' then
- consume(str, j + 1, v { blank: true })
- else if c == '+' then
- consume(str, j + 1, v { sign: true })
- else
- { i: j, v: v };
- consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });
- local try_parse_field_width(str, i) =
- if i < std.length(str) && str[i] == '*' then
- { i: i + 1, v: '*' }
- else
- local consume(str, j, v) =
- assert j < std.length(str) : 'Truncated format code.';
- local c = str[j];
- if c == '0' then
- consume(str, j + 1, v * 10 + 0)
- else if c == '1' then
- consume(str, j + 1, v * 10 + 1)
- else if c == '2' then
- consume(str, j + 1, v * 10 + 2)
- else if c == '3' then
- consume(str, j + 1, v * 10 + 3)
- else if c == '4' then
- consume(str, j + 1, v * 10 + 4)
- else if c == '5' then
- consume(str, j + 1, v * 10 + 5)
- else if c == '6' then
- consume(str, j + 1, v * 10 + 6)
- else if c == '7' then
- consume(str, j + 1, v * 10 + 7)
- else if c == '8' then
- consume(str, j + 1, v * 10 + 8)
- else if c == '9' then
- consume(str, j + 1, v * 10 + 9)
- else
- { i: j, v: v };
- consume(str, i, 0);
-
- local try_parse_precision(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == '.' then
- try_parse_field_width(str, i + 1)
- else
- { i: i, v: null };
-
- // Ignored, if it exists.
- local try_parse_length_modifier(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == 'h' || c == 'l' || c == 'L' then
- i + 1
- else
- i;
-
- local parse_conv_type(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local c = str[i];
- if c == 'd' || c == 'i' || c == 'u' then
- { i: i + 1, v: 'd', caps: false }
- else if c == 'o' then
- { i: i + 1, v: 'o', caps: false }
- else if c == 'x' then
- { i: i + 1, v: 'x', caps: false }
- else if c == 'X' then
- { i: i + 1, v: 'x', caps: true }
- else if c == 'e' then
- { i: i + 1, v: 'e', caps: false }
- else if c == 'E' then
- { i: i + 1, v: 'e', caps: true }
- else if c == 'f' then
- { i: i + 1, v: 'f', caps: false }
- else if c == 'F' then
- { i: i + 1, v: 'f', caps: true }
- else if c == 'g' then
- { i: i + 1, v: 'g', caps: false }
- else if c == 'G' then
- { i: i + 1, v: 'g', caps: true }
- else if c == 'c' then
- { i: i + 1, v: 'c', caps: false }
- else if c == 's' then
- { i: i + 1, v: 's', caps: false }
- else if c == '%' then
- { i: i + 1, v: '%', caps: false }
- else
- error 'Unrecognised conversion type: ' + c;
-
-
- // Parsed initial %, now the rest.
- local parse_code(str, i) =
- assert i < std.length(str) : 'Truncated format code.';
- local mkey = try_parse_mapping_key(str, i);
- local cflags = try_parse_cflags(str, mkey.i);
- local fw = try_parse_field_width(str, cflags.i);
- local prec = try_parse_precision(str, fw.i);
- local len_mod = try_parse_length_modifier(str, prec.i);
- local ctype = parse_conv_type(str, len_mod);
- {
- i: ctype.i,
- code: {
- mkey: mkey.v,
- cflags: cflags.v,
- fw: fw.v,
- prec: prec.v,
- ctype: ctype.v,
- caps: ctype.caps,
- },
- };
-
- // Parse a format string (containing none or more % format tags).
- local parse_codes(str, i, out, cur) =
- if i >= std.length(str) then
- out + [cur]
- else
- local c = str[i];
- if c == '%' then
- local r = parse_code(str, i + 1);
- parse_codes(str, r.i, out + [cur, r.code], '') tailstrict
- else
- parse_codes(str, i + 1, out, cur + c) tailstrict;
-
- local codes = parse_codes(str, 0, [], '');
-
-
- ///////////////////////
- // Format the values //
- ///////////////////////
-
- // Useful utilities
- local padding(w, s) =
- local aux(w, v) =
- if w <= 0 then
- v
- else
- aux(w - 1, v + s);
- aux(w, '');
-
- // Add s to the left of str so that its length is at least w.
- local pad_left(str, w, s) =
- padding(w - std.length(str), s) + str;
-
- // Add s to the right of str so that its length is at least w.
- local pad_right(str, w, s) =
- str + padding(w - std.length(str), s);
-
- // Render an integer (e.g., decimal or octal).
- local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
- local n_ = std.abs(n__);
- local aux(n) =
- if n == 0 then
- zero_prefix
- else
- aux(std.floor(n / radix)) + (n % radix);
- local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
- local neg = n__ < 0;
- local zp = min_chars - (if neg || blank || sign then 1 else 0);
- local zp2 = std.max(zp, min_digits);
- local dec2 = pad_left(dec, zp2, '0');
- (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;
-
- // Render an integer in hexadecimal.
- local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =
- local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']
- else ['a', 'b', 'c', 'd', 'e', 'f'];
- local n_ = std.abs(n__);
- local aux(n) =
- if n == 0 then
- ''
- else
- aux(std.floor(n / 16)) + numerals[n % 16];
- local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
- local neg = n__ < 0;
- local zp = min_chars - (if neg || blank || sign then 1 else 0)
- - (if add_zerox then 2 else 0);
- local zp2 = std.max(zp, min_digits);
- local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')
- + pad_left(hex, zp2, '0');
- (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;
-
- local strip_trailing_zero(str) =
- local aux(str, i) =
- if i < 0 then
- ''
- else
- if str[i] == '0' then
- aux(str, i - 1)
- else
- std.substr(str, 0, i + 1);
- aux(str, std.length(str) - 1);
-
- // Render floating point in decimal form
- local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =
- local n_ = std.abs(n__);
- local whole = std.floor(n_);
- local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
- local zp = zero_pad - prec - dot_size;
- local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');
- if prec == 0 then
- str + if ensure_pt then '.' else ''
- else
- local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);
- if trailing || frac > 0 then
- local frac_str = render_int(frac, prec, 0, false, false, 10, '');
- str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
- else
- str;
-
- // Render floating point in scientific form
- local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =
- local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));
- local suff = (if caps then 'E' else 'e')
- + render_int(exponent, 3, 0, false, true, 10, '');
- local mantissa = if exponent == -324 then
- // Avoid a rounding error where std.pow(10, -324) is 0
- // -324 is the smallest exponent possible.
- n__ * 10 / std.pow(10, exponent + 1)
- else
- n__ / std.pow(10, exponent);
- local zp2 = zero_pad - std.length(suff);
- render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;
-
- // Render a value with an arbitrary format code.
- local format_code(val, code, fw, prec_or_null, i) =
- local cflags = code.cflags;
- local fpprec = if prec_or_null != null then prec_or_null else 6;
- local iprec = if prec_or_null != null then prec_or_null else 0;
- local zp = if cflags.zero && !cflags.left then fw else 0;
- if code.ctype == 's' then
- std.toString(val)
- else if code.ctype == 'd' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')
- else if code.ctype == 'o' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- local zero_prefix = if cflags.alt then '0' else '';
- render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)
- else if code.ctype == 'x' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_hex(val,
- zp,
- iprec,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- code.caps)
- else if code.ctype == 'f' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_float_dec(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- true,
- fpprec)
- else if code.ctype == 'e' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- render_float_sci(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- true,
- code.caps,
- fpprec)
- else if code.ctype == 'g' then
- if std.type(val) != 'number' then
- error 'Format required number at '
- + i + ', got ' + std.type(val)
- else
- local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
- if exponent < -4 || exponent >= fpprec then
- render_float_sci(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- cflags.alt,
- code.caps,
- fpprec - 1)
- else
- local digits_before_pt = std.max(1, exponent + 1);
- render_float_dec(val,
- zp,
- cflags.blank,
- cflags.sign,
- cflags.alt,
- cflags.alt,
- fpprec - digits_before_pt)
- else if code.ctype == 'c' then
- if std.type(val) == 'number' then
- std.char(val)
- else if std.type(val) == 'string' then
- if std.length(val) == 1 then
- val
- else
- error '%c expected 1-sized string got: ' + std.length(val)
- else
- error '%c expected number / string, got: ' + std.type(val)
- else
- error 'Unknown code: ' + code.ctype;
+ format:: $intrinsic(format),
- // Render a parsed format string with an array of values.
- local format_codes_arr(codes, arr, i, j, v) =
- if i >= std.length(codes) then
- if j < std.length(arr) then
- error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)
- else
- v
- else
- local code = codes[i];
- if std.type(code) == 'string' then
- format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
- else
- local tmp = if code.fw == '*' then {
- j: j + 1,
- fw: if j >= std.length(arr) then
- error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
- else
- arr[j],
- } else {
- j: j,
- fw: code.fw,
- };
- local tmp2 = if code.prec == '*' then {
- j: tmp.j + 1,
- prec: if tmp.j >= std.length(arr) then
- error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
- else
- arr[tmp.j],
- } else {
- j: tmp.j,
- prec: code.prec,
- };
- local j2 = tmp2.j;
- local val =
- if j2 < std.length(arr) then
- arr[j2]
- else
- error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);
- local s =
- if code.ctype == '%' then
- '%'
- else
- format_code(val, code, tmp.fw, tmp2.prec, j2);
- local s_padded =
- if code.cflags.left then
- pad_right(s, tmp.fw, ' ')
- else
- pad_left(s, tmp.fw, ' ');
- local j3 =
- if code.ctype == '%' then
- j2
- else
- j2 + 1;
- format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
+ foldr:: $intrinsic(foldr),
- // Render a parsed format string with an object of values.
- local format_codes_obj(codes, obj, i, v) =
- if i >= std.length(codes) then
- v
- else
- local code = codes[i];
- if std.type(code) == 'string' then
- format_codes_obj(codes, obj, i + 1, v + code) tailstrict
- else
- local f =
- if code.mkey == null then
- error 'Mapping keys required.'
- else
- code.mkey;
- local fw =
- if code.fw == '*' then
- error 'Cannot use * field width with object.'
- else
- code.fw;
- local prec =
- if code.prec == '*' then
- error 'Cannot use * precision with object.'
- else
- code.prec;
- local val =
- if std.objectHasAll(obj, f) then
- obj[f]
- else
- error 'No such field: ' + f;
- local s =
- if code.ctype == '%' then
- '%'
- else
- format_code(val, code, fw, prec, f);
- local s_padded =
- if code.cflags.left then
- pad_right(s, fw, ' ')
- else
- pad_left(s, fw, ' ');
- format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;
-
- if std.isArray(vals) then
- format_codes_arr(codes, vals, 0, 0, '')
- else if std.isObject(vals) then
- format_codes_obj(codes, vals, 0, '')
- else
- format_codes_arr(codes, [vals], 0, 0, ''),
-
- foldr(func, arr, init)::
- local aux(func, arr, running, idx) =
- if idx < 0 then
- running
- else
- aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;
- aux(func, arr, init, std.length(arr) - 1),
+ foldl:: $intrinsic(foldl),
- foldl(func, arr, init)::
- local aux(func, arr, running, idx) =
- if idx >= std.length(arr) then
- running
- else
- aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;
- aux(func, arr, init, 0),
-
-
filterMap(filter_func, map_func, arr)::
if !std.isFunction(filter_func) then
error ('std.filterMap first param must be function, got ' + std.type(filter_func))
@@ -912,30 +375,7 @@
else
error 'TOML body must be an object. Got ' + std.type(value),
- escapeStringJson(str_)::
- local str = std.toString(str_);
- local trans(ch) =
- if ch == '"' then
- '\\"'
- else if ch == '\\' then
- '\\\\'
- else if ch == '\b' then
- '\\b'
- else if ch == '\f' then
- '\\f'
- else if ch == '\n' then
- '\\n'
- else if ch == '\r' then
- '\\r'
- else if ch == '\t' then
- '\\t'
- else
- local cp = std.codepoint(ch);
- if cp < 32 || (cp >= 127 && cp <= 159) then
- '\\u%04x' % [cp]
- else
- ch;
- '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),
+ escapeStringJson:: $intrinsic(escapeStringJson),
escapeStringPython(str)::
std.escapeStringJson(str),
@@ -960,42 +400,7 @@
manifestJson(value):: std.manifestJsonEx(value, ' '),
- manifestJsonEx(value, indent)::
- local aux(v, path, cindent) =
- if v == true then
- 'true'
- else if v == false then
- 'false'
- else if v == null then
- 'null'
- else if std.isNumber(v) then
- '' + v
- else if std.isString(v) then
- std.escapeStringJson(v)
- else if std.isFunction(v) then
- error 'Tried to manifest function at ' + path
- else if std.isArray(v) then
- local range = std.range(0, std.length(v) - 1);
- local new_indent = cindent + indent;
- local lines = ['[\n']
- + std.join([',\n'],
- [
- [new_indent + aux(v[i], path + [i], new_indent)]
- for i in range
- ])
- + ['\n' + cindent + ']'];
- std.join('', lines)
- else if std.isObject(v) then
- local lines = ['{\n']
- + std.join([',\n'],
- [
- [cindent + indent + std.escapeStringJson(k) + ': '
- + aux(v[k], path + [k], cindent + indent)]
- for k in std.objectFields(v)
- ])
- + ['\n' + cindent + '}'];
- std.join('', lines);
- aux(value, [], ''),
+ manifestJsonEx:: $intrinsic(manifestJsonEx),
manifestYamlDoc(value, indent_array_in_object=false)::
local aux(v, path, cindent) =
@@ -1136,52 +541,7 @@
local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
- base64(input)::
- local bytes =
- if std.isString(input) then
- std.map(function(c) std.codepoint(c), input)
- else
- input;
-
- local aux(arr, i, r) =
- if i >= std.length(arr) then
- r
- else if i + 1 >= std.length(arr) then
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i
- base64_table[(arr[i] & 3) << 4] +
- '==';
- aux(arr, i + 3, r + str) tailstrict
- else if i + 2 >= std.length(arr) then
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i, 4 MSB of i+1
- base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
- // 4 LSB of i+1
- base64_table[(arr[i + 1] & 15) << 2] +
- '=';
- aux(arr, i + 3, r + str) tailstrict
- else
- local str =
- // 6 MSB of i
- base64_table[(arr[i] & 252) >> 2] +
- // 2 LSB of i, 4 MSB of i+1
- base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
- // 4 LSB of i+1, 2 MSB of i+2
- base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +
- // 6 LSB of i+2
- base64_table[(arr[i + 2] & 63)];
- aux(arr, i + 3, r + str) tailstrict;
-
- local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);
- if !sanity then
- error 'Can only base64 encode strings / arrays of single bytes.'
- else
- aux(bytes, 0, ''),
-
+ base64:: $intrinsic(base64),
base64DecodeBytes(str)::
if std.length(str) % 4 != 0 then
@@ -1207,47 +567,11 @@
base64Decode(str)::
local bytes = std.base64DecodeBytes(str);
std.join('', std.map(function(b) std.char(b), bytes)),
-
- reverse(arr)::
- local l = std.length(arr);
- std.makeArray(l, function(i) arr[l - i - 1]),
- // Merge-sort for long arrays and naive quicksort for shorter ones
- sortImpl(arr, keyF)::
- local quickSort(arr, keyF=id) =
- local l = std.length(arr);
- if std.length(arr) <= 1 then
- arr
- else
- local pos = 0;
- local pivot = keyF(arr[pos]);
- local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);
- local left = std.filter(function(x) keyF(x) < pivot, rest);
- local right = std.filter(function(x) keyF(x) >= pivot, rest);
- quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);
+ reverse:: $intrinsic(reverse),
- local merge(a, b) =
- local la = std.length(a), lb = std.length(b);
- local aux(i, j, prefix) =
- if i == la then
- prefix + b[j:]
- else if j == lb then
- prefix + a[i:]
- else
- if keyF(a[i]) <= keyF(b[j]) then
- aux(i + 1, j, prefix + [a[i]]) tailstrict
- else
- aux(i, j + 1, prefix + [b[j]]) tailstrict;
- aux(0, 0, []);
+ sortImpl:: $intrinsic(sortImpl),
- local l = std.length(arr);
- if std.length(arr) <= 30 then
- quickSort(arr, keyF=keyF)
- else
- local mid = std.floor(l / 2);
- local left = arr[:mid], right = arr[mid:];
- merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
-
sort(arr, keyF=id)::
std.sortImpl(arr, keyF),
@@ -1356,42 +680,7 @@
objectValuesAll(o)::
[o[k] for k in std.objectFieldsAll(o)],
- equals(a, b)::
- local ta = std.type(a);
- local tb = std.type(b);
- if !std.primitiveEquals(ta, tb) then
- false
- else
- if std.primitiveEquals(ta, 'array') then
- local la = std.length(a);
- if !std.primitiveEquals(la, std.length(b)) then
- false
- else
- local aux(a, b, i) =
- if i >= la then
- true
- else if a[i] != b[i] then
- false
- else
- aux(a, b, i + 1) tailstrict;
- aux(a, b, 0)
- else if std.primitiveEquals(ta, 'object') then
- local fields = std.objectFields(a);
- local lfields = std.length(fields);
- if fields != std.objectFields(b) then
- false
- else
- local aux(a, b, i) =
- if i >= lfields then
- true
- else if local f = fields[i]; a[f] != b[f] then
- false
- else
- aux(a, b, i + 1) tailstrict;
- aux(a, b, 0)
- else
- std.primitiveEquals(a, b),
-
+ equals:: $intrinsic(equals),
resolvePath(f, r)::
local arr = std.split(f, '/');