git.delta.rocks / jrsonnet / refs/commits / fc291eb6a7bd

difftreelog

Merge branch 'master' of github.com:CertainLach/jrsonnet

Yaroslav Bolyukin2021-08-13parents: #dfc17ed #5e5ab6b.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/README.mddiffbeforeafterboth
88
9- `serialized-stdlib`9- `serialized-stdlib`
10 - serializes standard library AST using serde10 - serializes standard library AST using serde
11 - slower than `codegenerated-stdlib` at runtime, but have no compilation speed penality11 - used by default
12- none12- none
13 - leaves only stdlib source code in binary, processing them same way as user supplied data13 - leaves only stdlib source code in binary, processing them same way as user supplied data
14 - slowest (as it involves parsing of standard library source code)14 - slowest (as it involves parsing of standard library source code)
1515
16Because of `codegenerated-stdlib` compilation slowdown, `serialized-stdlib` is used by default
17
18### Benchmark16### Benchmark
1917
20Can also be run via `cargo bench`18Can also be run via `cargo bench`
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
126 ("strReplace".into(), builtin_str_replace),126 ("strReplace".into(), builtin_str_replace),
127 ("splitLimit".into(), builtin_splitlimit),127 ("splitLimit".into(), builtin_splitlimit),
128 ("parseJson".into(), builtin_parse_json),128 ("parseJson".into(), builtin_parse_json),
129 ("asciiUpper".into(), builtin_ascii_upper),
130 ("asciiLower".into(), builtin_ascii_lower),
131 ("member".into(), builtin_member),
132 ("count".into(), builtin_count),
129 ].iter().cloned().collect()133 ].iter().cloned().collect()
130 };134 };
131}135}
828 })832 })
829}833}
834
835fn builtin_ascii_upper(
836 context: Context,
837 _loc: Option<&ExprLocation>,
838 args: &ArgsDesc,
839) -> Result<Val> {
840 parse_args!(context, "asciiUpper", args, 1, [
841 0, str: ty!(string) => Val::Str;
842 ], {
843 Ok(Val::Str(str.to_ascii_uppercase().into()))
844 })
845}
846
847fn builtin_ascii_lower(
848 context: Context,
849 _loc: Option<&ExprLocation>,
850 args: &ArgsDesc,
851) -> Result<Val> {
852 parse_args!(context, "asciiLower", args, 1, [
853 0, str: ty!(string) => Val::Str;
854 ], {
855 Ok(Val::Str(str.to_ascii_lowercase().into()))
856 })
857}
858
859fn builtin_member(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
860 parse_args!(context, "member", args, 2, [
861 0, arr: ty!((array | string));
862 1, x: ty!(any);
863 ], {
864 match arr {
865 Val::Str(s) => {
866 let x = x.try_cast_str("x should be string")?;
867 Ok(Val::Bool(!x.is_empty() && s.contains(&*x)))
868 }
869 Val::Arr(a) => {
870 for item in a.iter() {
871 let item = item?;
872 if equals(&item, &x)? {
873 return Ok(Val::Bool(true));
874 }
875 }
876 Ok(Val::Bool(false))
877 }
878 _ => unreachable!(),
879 }
880 })
881}
882
883fn builtin_count(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
884 parse_args!(context, "count", args, 2, [
885 0, arr: ty!(array) => Val::Arr;
886 1, x: ty!(any);
887 ], {
888 let mut count = 0;
889 for item in arr.iter() {
890 let item = item?;
891 if equals(&item, &x)? {
892 count += 1;
893 }
894 }
895 Ok(Val::Num(count as f64))
896 })
897}
830898
831pub fn call_builtin(899pub fn call_builtin(
832 context: Context,900 context: Context,
modifiedcrates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth
5 /// To avoid parsing again when issued from the same thread5 /// To avoid parsing again when issued from the same thread
6 #[allow(unreachable_code)]6 #[allow(unreachable_code)]
7 static PARSED_STDLIB: LocExpr = {7 static PARSED_STDLIB: LocExpr = {
8 #[cfg(feature = "codegenerated-stdlib")]
9 {
10 #[allow(clippy::all)]
11 return {
12 use jrsonnet_parser::*;
13 include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))
14 };
15 }
16
17 #[cfg(feature = "serialized-stdlib")]8 #[cfg(feature = "serialized-stdlib")]
18 {9 {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
1046 assert_eq!(error.error().to_string(), "assert failed: is number");1046 assert_eq!(error.error().to_string(), "assert failed: is number");
1047 }1047 }
1048
1049 #[test]
1050 fn test_ascii_upper_lower() {
1051 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);
1052 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);
1053 }
1054
1055 #[test]
1056 fn test_member() {
1057 assert_eval!(r#"!std.member("", "")"#);
1058 assert_eval!(r#"std.member("abc", "a")"#);
1059 assert_eval!(r#"!std.member("abc", "d")"#);
1060 assert_eval!(r#"!std.member([], "")"#);
1061 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);
1062 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);
1063 }
1064
1065 #[test]
1066 fn test_count() {
1067 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);
1068 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);
1069 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);
1070 }
1048}1071}
10491072
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
128128
129 strReplace:: $intrinsic(strReplace),129 strReplace:: $intrinsic(strReplace),
130130
131 asciiUpper(str)::131 asciiUpper:: $intrinsic(asciiUpper),
132 local cp = std.codepoint;
133 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then
134 std.char(cp(c) - 32)
135 else
136 c;
137 std.join('', std.map(up_letter, std.stringChars(str))),
138132
139 asciiLower(str)::133 asciiLower:: $intrinsic(asciiLower),
140 local cp = std.codepoint;
141 local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then
142 std.char(cp(c) + 32)
143 else
144 c;
145 std.join('', std.map(down_letter, std.stringChars(str))),
146134
147 range:: $intrinsic(range),135 range:: $intrinsic(range),
148136
155143
156 slice:: $intrinsic(slice),144 slice:: $intrinsic(slice),
157145
158 member(arr, x)::146 member:: $intrinsic(member),
159 if std.isArray(arr) then
160 std.count(arr, x) > 0
161 else if std.isString(arr) then
162 std.length(std.findSubstr(x, arr)) > 0
163 else error 'std.member first argument must be an array or a string',
164147
165 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),148 count:: $intrinsic(count),
166149
167 mod:: $intrinsic(mod),150 mod:: $intrinsic(mod),
168151