-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This diff introduces the "Whisker standard library" — a place to put *extremely* generic functionality around template data. The initial implementation just has the following functions: - `array.of(...)` — array literals - `array.len` - `array.empty?` - `str.len` I picked a small set of (non-controversial) functions just to validate the approach. Reviewed By: yoney Differential Revision: D67958812 fbshipit-source-id: 36200dc21c1f053ad31664d8d3ee562ab641109a
- Loading branch information
1 parent
f983897
commit 8009217
Showing
6 changed files
with
331 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
third-party/thrift/src/thrift/compiler/whisker/standard_library.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <thrift/compiler/whisker/standard_library.h> | ||
|
||
namespace w = whisker::make; | ||
|
||
namespace whisker { | ||
|
||
namespace { | ||
|
||
class named_native_function : public native_function { | ||
public: | ||
explicit named_native_function(std::string_view name) | ||
: name_(std::move(name)) {} | ||
|
||
void print_to( | ||
tree_printer::scope scope, const object_print_options&) const final { | ||
scope.println("<function {}>", name_); | ||
} | ||
|
||
private: | ||
std::string_view name_; | ||
}; | ||
|
||
map::value_type create_array_functions() { | ||
map array_functions; | ||
|
||
{ | ||
/** | ||
* Creates an array with the provided arguments in order. This function can | ||
* be used to form an "array literal". | ||
* | ||
* Name: array.of | ||
* | ||
* Arguments: | ||
* 0 or more objects (variadic) | ||
* | ||
* Returns: | ||
* [array] provided arguments in order. | ||
*/ | ||
class array_of : public named_native_function { | ||
public: | ||
array_of() : named_native_function("array.of") {} | ||
|
||
object::ptr invoke(context ctx) override { | ||
ctx.declare_named_arguments({}); | ||
array result; | ||
result.reserve(ctx.arity()); | ||
for (const object::ptr& arg : ctx.arguments()) { | ||
result.emplace_back(object(*arg)); | ||
} | ||
return object::owned(w::array(std::move(result))); | ||
} | ||
}; | ||
array_functions["of"] = w::make_native_function<array_of>(); | ||
} | ||
|
||
{ | ||
/** | ||
* Produces the length of an array or array-like object. | ||
* | ||
* Name: array.len | ||
* | ||
* Arguments: | ||
* - [array] — The array to find length of. | ||
* | ||
* Returns: | ||
* [i64] length of the provided array. | ||
*/ | ||
class array_len : public named_native_function { | ||
public: | ||
array_len() : named_native_function("array.len") {} | ||
|
||
object::ptr invoke(context ctx) override { | ||
ctx.declare_named_arguments({}); | ||
ctx.declare_arity(1); | ||
auto len = i64(ctx.argument<array>(0).size()); | ||
return object::owned(w::i64(len)); | ||
} | ||
}; | ||
array_functions["len"] = w::make_native_function<array_len>(); | ||
} | ||
|
||
{ | ||
/** | ||
* Checks an array for emptiness. | ||
* | ||
* Name: array.empty? | ||
* | ||
* Arguments: | ||
* - [array] — The array to check for emptiness. | ||
* | ||
* Returns: | ||
* [boolean] indicating whether the array is empty. | ||
*/ | ||
class array_empty : public named_native_function { | ||
public: | ||
array_empty() : named_native_function("array.empty?") {} | ||
|
||
object::ptr invoke(context ctx) override { | ||
ctx.declare_named_arguments({}); | ||
ctx.declare_arity(1); | ||
return object::as_static( | ||
ctx.argument<array>(0).size() == 0 ? w::true_ : w::false_); | ||
} | ||
}; | ||
array_functions["empty?"] = w::make_native_function<array_empty>(); | ||
} | ||
|
||
return map::value_type{"array", std::move(array_functions)}; | ||
} | ||
|
||
map::value_type create_string_functions() { | ||
map string_functions; | ||
|
||
{ | ||
/** | ||
* Produces the length of string. | ||
* | ||
* Name: string.len | ||
* | ||
* Arguments: | ||
* - [string] — The string to find length of. | ||
* | ||
* Returns: | ||
* [i64] length of the provided string. | ||
*/ | ||
class string_len : public named_native_function { | ||
public: | ||
string_len() : named_native_function("string.len") {} | ||
|
||
object::ptr invoke(context ctx) override { | ||
ctx.declare_named_arguments({}); | ||
ctx.declare_arity(1); | ||
auto len = i64(ctx.argument<string>(0)->length()); | ||
return object::owned(w::i64(len)); | ||
} | ||
}; | ||
string_functions["len"] = w::make_native_function<string_len>(); | ||
} | ||
|
||
return map::value_type{"string", std::move(string_functions)}; | ||
} | ||
|
||
} // namespace | ||
|
||
void load_standard_library(map& module) { | ||
module.emplace(create_array_functions()); | ||
module.emplace(create_string_functions()); | ||
} | ||
|
||
} // namespace whisker |
31 changes: 31 additions & 0 deletions
31
third-party/thrift/src/thrift/compiler/whisker/standard_library.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <thrift/compiler/whisker/object.h> | ||
|
||
namespace whisker { | ||
|
||
/** | ||
* Loads Whisker's standard library into the provided map. A common use case is | ||
* calling this function on render_options::globals map object. | ||
* | ||
* See standard_library.cc for available functions. | ||
*/ | ||
void load_standard_library(map& module); | ||
|
||
} // namespace whisker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.