Skip to content

Commit

Permalink
refactor(lang): re-write the part of the standard library that is wri…
Browse files Browse the repository at this point in the history
…tten in Jsonnet
  • Loading branch information
eduardosm committed Nov 6, 2024
1 parent 551011f commit 6d8209b
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 146 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ Licensed under either of

at your option.

The following are taken from the [C++ implementation](https://github.com/google/jsonnet),
which is licensed under Apache 2.0.

* The part of the standard library that is implemented in Jsonnet
([rsjsonnet-lang/src/program/std.jsonnet](rsjsonnet-lang/src/program/std.jsonnet)).
* Part of the test suite ([ui-tests/jsonnet-0.20.0](ui-tests/jsonnet-0.20.0)).
Part of the test suite ([ui-tests/jsonnet-0.20.0](ui-tests/jsonnet-0.20.0)) is
taken from the [C++ implementation](https://github.com/google/jsonnet), which
is licensed under Apache 2.0.
116 changes: 0 additions & 116 deletions rsjsonnet-lang/src/program/std.jsonnet

This file was deleted.

76 changes: 76 additions & 0 deletions rsjsonnet-lang/src/program/std.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// This is the part of the standard library that is written in Jsonnet.

{
abs(n)::
assert self.isNumber(n) : "first argument of `std.abs` is expected to be function " + self.type(n);
if n > 0 then n else -n,

sign(n)::
assert self.isNumber(n) : "first argument of `std.sign` is expected to be function " + self.type(n);
if n > 0 then
1
else if n < 0 then
-1
else 0,

max(a, b)::
assert self.isNumber(a) : "first argument of `std.max` is expected to be function " + self.type(a);
assert self.isNumber(b) : "second argument of `std.max` is expected to be function " + self.type(b);
if a > b then a else b,

min(a, b)::
assert self.isNumber(a) : "first argument of `std.min` is expected to be function " + self.type(a);
assert self.isNumber(b) : "second argument of `std.min` is expected to be function " + self.type(b);
if a < b then a else b,

clamp(x, minVal, maxVal)::
assert self.isNumber(x) : "first argument of `std.clamp` is expected to be function " + self.type(x);
assert self.isNumber(minVal) : "second argument of `std.clamp` is expected to be function " + self.type(minVal);
assert self.isNumber(maxVal) : "third argument of `std.clamp` is expected to be function " + self.type(maxVal);
if x < minVal then
minVal
else if x > maxVal then
maxVal
else
x,

round(x):: self.floor(x + 0.5),

isEmpty(str):: self.length(str) == 0,

manifestToml(value):: self.manifestTomlEx(value, " "),

manifestJson(value):: self.manifestJsonEx(value, " "),

manifestJsonMinified(value):: self.manifestJsonEx(value, "", "", ":"),

lines(arr):: self.join("\n", arr + [""]),

sum(arr):: self.foldl(function(a, b) a + b, arr, 0),

get(o, f, default=null, inc_hidden=true):: if self.objectHasEx(o, f, inc_hidden) then o[f] else default,

objectHas(o, f):: self.objectHasEx(o, f, false),
objectHasAll(o, f):: self.objectHasEx(o, f, true),

objectFields(o):: self.objectFieldsEx(o, false),
objectFieldsAll(o):: self.objectFieldsEx(o, true),

objectValues(o):: [o[key] for key in self.objectFields(o)],
objectValuesAll(o):: [o[key] for key in self.objectFieldsAll(o)],

objectKeysValues(o):: [{ key: key, value: o[key] } for key in self.objectFields(o)],
objectKeysValuesAll(o):: [{ key: key, value: o[key] } for key in self.objectFieldsAll(o)],

xor(x, y):: x != y,
xnor(x, y):: x == y,

resolvePath(f, r)::
local parts = self.split(f, "/");
self.join("/", parts[:self.length(parts) - 1] + [r]),

__array_less(arr1, arr2):: self.__compare_array(arr1, arr2) < 0,
__array_less_or_equal(arr1, arr2):: self.__compare_array(arr1, arr2) <= 0,
__array_greater(arr1, arr2):: self.__compare_array(arr1, arr2) > 0,
__array_greater_or_equal(arr1, arr2):: self.__compare_array(arr1, arr2) >= 0,
}
5 changes: 2 additions & 3 deletions rsjsonnet-lang/src/program/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use crate::interner::{InternedStr, StrInterner};
use crate::span::SpanContextId;
use crate::{ast, FHashMap};

// From jsonnet 0.20.0
pub(super) const STDLIB_DATA: &[u8] = include_bytes!("std.jsonnet");
pub(super) const STDLIB_DATA: &[u8] = include_bytes!("std.libsonnet");

impl<'p> Program<'p> {
pub(super) fn load_stdlib(&mut self, span_ctx: SpanContextId) {
let stdlib_data = self.stdlib_data;
let stdlib_thunk = self
.load_source(span_ctx, stdlib_data, false, "std.jsonnet")
.load_source(span_ctx, stdlib_data, false, "std.libsonnet")
.expect("failed to load stdlib");
let stdlib_value = self
.eval_value_internal(&stdlib_thunk)
Expand Down
6 changes: 3 additions & 3 deletions ui-tests/fail/stdlib/manifestToml/inner_error.jsonnet.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ note: while manifesting array item 1
note: while manifesting object field "y"
note: while manifesting object field "x"
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> inner_error.jsonnet:1:1
|
Expand Down
6 changes: 3 additions & 3 deletions ui-tests/fail/stdlib/manifestToml/invalid_arg.jsonnet.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error: first argument of `std.manifestTomlEx` is expected to be object, got null
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> invalid_arg.jsonnet:1:1
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: functions cannot be manifested
note: while manifesting object field "x"
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> manifest_function.jsonnet:1:1
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: cannot manifest null in TOML
note: while manifesting object field "x"
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> manifest_null.jsonnet:1:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error: assertion failed
1 | std.manifestToml({ assert false })
| ^^^^^^^^^^^^
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> object_assert_failed_root.jsonnet:1:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ error: assertion failed
| ^^^^^^^^^^^^
note: while manifesting object field "x"
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> object_assert_failed_sub_table.jsonnet:1:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ error: assertion failed
note: while manifesting array item 0
note: while manifesting object field "x"
note: while evaluating call to `manifestTomlEx`
--> <stdlib>:65:25
--> <stdlib>:41:25
|
65 | manifestToml(value):: std.manifestTomlEx(value, ' '),
| -------------------------------
41 | manifestToml(value):: self.manifestTomlEx(value, " "),
| --------------------------------
note: while evaluating call to function
--> object_assert_failed_value.jsonnet:1:1
|
Expand Down

0 comments on commit 6d8209b

Please sign in to comment.