Skip to content

Commit 75913c9

Browse files
praihanfacebook-github-bot
authored andcommitted
Implement rendering for partials
Summary: This diff implements rendering for partial blocks and statements as defined in the spec. To achieve this, we have an *implementation-defined* `partial_definition` class that is exposed to Whisker using a `native_handle<>`. I especially enjoy the demonstrate of the capabilities of partials in the [collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) example. Differential Revision: D68687668 fbshipit-source-id: c85e044109ec70ad8260056a23c7fb65332ffc5f
1 parent e3d9c77 commit 75913c9

File tree

4 files changed

+463
-45
lines changed

4 files changed

+463
-45
lines changed

thrift/compiler/whisker/eval_context.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ object::ptr eval_context::lexical_scope::lookup_property(
104104
return find_property(*this_ref_, identifier);
105105
}
106106

107-
eval_context::eval_context(map globals)
108-
: global_scope_(manage_owned<object>(
109-
w::make_native_object<global_scope_object>(std::move(globals)))),
107+
eval_context::eval_context(object::ptr globals)
108+
: global_scope_(std::move(globals)),
110109
stack_({lexical_scope(global_scope_)}) {}
111110

111+
eval_context::eval_context(map globals)
112+
: eval_context(manage_owned<object>(
113+
w::make_native_object<global_scope_object>(std::move(globals)))) {}
114+
112115
/* static */ eval_context eval_context::with_root_scope(
113116
object::ptr root_scope, map globals) {
114117
eval_context result{std::move(globals)};

thrift/compiler/whisker/eval_context.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <deque>
2424
#include <functional>
2525
#include <string>
26+
#include <type_traits>
2627
#include <variant>
2728
#include <vector>
2829

@@ -204,8 +205,8 @@ class eval_context {
204205
* - eval_name_already_bound_error if the name already exists as a local in
205206
* the current scope.
206207
*/
207-
expected<std::monostate, eval_name_already_bound_error> bind_local(
208-
std::string name, object::ptr value);
208+
[[nodiscard]] expected<std::monostate, eval_name_already_bound_error>
209+
bind_local(std::string name, object::ptr value);
209210

210211
/**
211212
* Performs a lexical search for an object by name (chain of properties)
@@ -228,7 +229,17 @@ class eval_context {
228229
std::variant<eval_scope_lookup_error, eval_property_lookup_error>>
229230
lookup_object(const std::vector<std::string>& path);
230231

232+
/**
233+
* Creates a new "derived" eval_context from the current one.
234+
*
235+
* A derived eval_context has a fresh stack (empty) but retains the same
236+
* global scope from the current one.
237+
*/
238+
eval_context make_derived() const { return eval_context(global_scope_); }
239+
231240
private:
241+
explicit eval_context(object::ptr globals);
242+
232243
/**
233244
* A lexical scope which determines how name lookups are performed within the
234245
* Whisker templating language.
@@ -285,5 +296,7 @@ class eval_context {
285296
// references passed around to those scope objects.
286297
std::deque<lexical_scope> stack_;
287298
};
299+
static_assert(std::is_copy_constructible_v<eval_context>);
300+
static_assert(std::is_move_constructible_v<eval_context>);
288301

289302
} // namespace whisker

0 commit comments

Comments
 (0)