Skip to content

Commit

Permalink
array.at
Browse files Browse the repository at this point in the history
Summary: This diff adds a function to the standard library to access items in an array by index. Basic stuff. Will come in handy in future diffs where arrays are used like tuples.

Reviewed By: iahs

Differential Revision: D68004374

fbshipit-source-id: e66e9925d81119f7497d18ca63ec1fae1771160d
  • Loading branch information
praihan authored and facebook-github-bot committed Jan 12, 2025
1 parent d512396 commit a1687a5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
35 changes: 35 additions & 0 deletions thrift/compiler/whisker/standard_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,41 @@ map::value_type create_array_functions() {
array_functions["empty?"] = w::make_native_function<array_empty>();
}

{
/**
* Gets the object from an array at a given index. If the index is negative,
* or larger than the size of the array, then an error is thrown.
*
* Name: array.at
*
* Arguments:
* - [whisker::array] — The array to get from
* - [i64] — The index of the item to get
*
* Returns:
* [object] the item at the given index.
*/
class array_at : public named_native_function {
public:
array_at() : named_native_function("array.at") {}

object::ptr invoke(context ctx) override {
ctx.declare_named_arguments({});
ctx.declare_arity(2);

auto a = ctx.argument<array>(0);
auto index = ctx.argument<i64>(1);

if (index < 0 || std::size_t(index) >= a.size()) {
ctx.error(
"Index '{}' is out of bounds (size is {}).", index, a.size());
}
return a.at(index);
}
};
array_functions["at"] = w::make_native_function<array_at>();
}

return map::value_type{"array", std::move(array_functions)};
}

Expand Down
42 changes: 42 additions & 0 deletions thrift/compiler/whisker/test/standard_library_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,48 @@ TEST_F(StandardLibraryTest, array_empty) {
}
}

TEST_F(StandardLibraryTest, array_at) {
{
auto result = render(
"{{ (array.at (array.of 1 \"foo\" 2) 0) }}\n"
"{{ (array.at (array.of 1 \"foo\" 2) 1) }}\n"
"{{ (array.at (array.of 1 \"foo\" 2) 2) }}\n",
w::null);
EXPECT_THAT(diagnostics(), testing::IsEmpty());
EXPECT_EQ(
*result,
"1\n"
"foo\n"
"2\n");
}

{
auto result = render("{{ (array.at (array.of) 0) }}\n", w::null);
EXPECT_FALSE(result.has_value());
EXPECT_THAT(
diagnostics(),
testing::ElementsAre(diagnostic(
diagnostic_level::error,
"Function 'array.at' threw an error:\n"
"Index '0' is out of bounds (size is 0).",
path_to_file,
1)));
}

{
auto result = render("{{ (array.at (array.of 0 1 2) -1) }}\n", w::null);
EXPECT_FALSE(result.has_value());
EXPECT_THAT(
diagnostics(),
testing::ElementsAre(diagnostic(
diagnostic_level::error,
"Function 'array.at' threw an error:\n"
"Index '-1' is out of bounds (size is 3).",
path_to_file,
1)));
}
}

TEST_F(StandardLibraryTest, string_len) {
{
auto result = render(
Expand Down

0 comments on commit a1687a5

Please sign in to comment.