Skip to content

Commit 128eeef

Browse files
hchokshifacebook-github-bot
authored andcommitted
Consolidate implementations of find_property
Summary: Remove duplicate implementation in `mstch_compat`, to prevent divergence in upcoming changes. Reviewed By: praihan Differential Revision: D79453615 fbshipit-source-id: c29f5665577e04d9c7608a93bed79be7ed079f86
1 parent ac41e23 commit 128eeef

File tree

3 files changed

+59
-64
lines changed

3 files changed

+59
-64
lines changed

third-party/thrift/src/thrift/compiler/whisker/eval_context.cc

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,42 @@ namespace whisker {
2727

2828
namespace {
2929

30+
/**
31+
* A class representing the bag of properties at the global scope (even before
32+
* the root scope).
33+
*
34+
* This is a bespoke implementation primarily for debugging purposes.
35+
*/
36+
class global_scope_object : public map {
37+
public:
38+
explicit global_scope_object(map::raw properties)
39+
: properties_(std::move(properties)) {}
40+
41+
std::optional<object> lookup_property(
42+
std::string_view identifier) const override {
43+
if (auto property = properties_.find(identifier);
44+
property != properties_.end()) {
45+
return property->second;
46+
}
47+
return std::nullopt;
48+
}
49+
50+
void print_to(tree_printer::scope& scope, const object_print_options& options)
51+
const override {
52+
scope.print("<global scope> (size={})", properties_.size());
53+
for (const auto& [key, value] : properties_) {
54+
whisker::print_to(value, scope.make_child("'{}' → ", key), options);
55+
}
56+
}
57+
58+
private:
59+
map::raw properties_;
60+
};
61+
62+
} // namespace
63+
64+
namespace detail {
65+
3066
std::optional<object> find_property(
3167
diagnostics_engine& diags,
3268
const object& self,
@@ -65,46 +101,14 @@ std::optional<object> find_property(
65101
});
66102
}
67103

68-
/**
69-
* A class representing the bag of properties at the global scope (even before
70-
* the root scope).
71-
*
72-
* This is a bespoke implementation primarily for debugging purposes.
73-
*/
74-
class global_scope_object : public map {
75-
public:
76-
explicit global_scope_object(map::raw properties)
77-
: properties_(std::move(properties)) {}
78-
79-
std::optional<object> lookup_property(
80-
std::string_view identifier) const override {
81-
if (auto property = properties_.find(identifier);
82-
property != properties_.end()) {
83-
return property->second;
84-
}
85-
return std::nullopt;
86-
}
87-
88-
void print_to(tree_printer::scope& scope, const object_print_options& options)
89-
const override {
90-
scope.print("<global scope> (size={})", properties_.size());
91-
for (const auto& [key, value] : properties_) {
92-
whisker::print_to(value, scope.make_child("'{}' → ", key), options);
93-
}
94-
}
95-
96-
private:
97-
map::raw properties_;
98-
};
99-
100-
} // namespace
104+
} // namespace detail
101105

102106
std::optional<object> eval_context::lexical_scope::lookup_property(
103107
diagnostics_engine& diags, const ast::identifier& identifier) {
104108
if (auto local = locals_.find(identifier.name); local != locals_.end()) {
105109
return local->second;
106110
}
107-
return find_property(diags, this_ref_, identifier);
111+
return detail::find_property(diags, this_ref_, identifier);
108112
}
109113

110114
eval_context::eval_context(diagnostics_engine& diags, object globals)
@@ -221,7 +225,7 @@ eval_context::look_up_object(const ast::variable_lookup& lookup) {
221225
++component) {
222226
try {
223227
std::optional<object> next =
224-
find_property(diags_, *current, component->property);
228+
detail::find_property(diags_, *current, component->property);
225229
if (!next.has_value()) {
226230
return unexpected(eval_property_lookup_error(
227231
*current, /* missing_from */

third-party/thrift/src/thrift/compiler/whisker/eval_context.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ class eval_name_already_bound_error {
140140
std::string name_;
141141
};
142142

143+
namespace detail {
144+
145+
/**
146+
* Try resolve a property on a Whisker object.
147+
* Returns std::nullopt if the property is not found.
148+
*/
149+
std::optional<object> find_property(
150+
diagnostics_engine& diags,
151+
const object& self,
152+
const ast::identifier& identifier);
153+
154+
} // namespace detail
155+
143156
/**
144157
* An eval_context is responsible for name resolution within Whisker templates
145158
* and maintains necessary state to achieve when a template is rendered (aka

third-party/thrift/src/thrift/compiler/whisker/mstch_compat.cc

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <boost/algorithm/string/predicate.hpp>
1818
#include <thrift/compiler/whisker/detail/overload.h>
19+
#include <thrift/compiler/whisker/eval_context.h>
1920
#include <thrift/compiler/whisker/mstch_compat.h>
2021

2122
#include <cassert>
@@ -202,37 +203,14 @@ class mstch_object_proxy
202203

203204
const mstch_object::lookup_result self_value =
204205
proxied_->at(self_property->first);
205-
const object* self_whisker_obj = std::get_if<object>(&self_value);
206-
if (self_whisker_obj == nullptr) {
207-
return std::nullopt;
206+
if (const object* self_whisker_obj = std::get_if<object>(&self_value)) {
207+
return detail::find_property(
208+
diags_,
209+
*self_whisker_obj,
210+
ast::identifier{source_range{}, std::string(self_property->second)});
208211
}
209212

210-
return self_whisker_obj->visit(
211-
[&](const map::ptr& m) -> std::optional<object> {
212-
return m->lookup_property(self_property->second);
213-
},
214-
[&](const native_handle<>& h) -> std::optional<object> {
215-
if (const whisker::prototype<>::descriptor* descriptor =
216-
h.proto()->find_descriptor(self_property->second)) {
217-
return detail::variant_match(
218-
*descriptor,
219-
[&](const prototype<>::property& prop)
220-
-> std::optional<object> {
221-
return prop.function->invoke(native_function::context{
222-
{} /* identifier location */,
223-
diags_,
224-
*self_whisker_obj,
225-
{} /* positional arguments */,
226-
{} /* named arguments */,
227-
});
228-
},
229-
[&](const prototype<>::fixed_object& fixed)
230-
-> std::optional<object> { return fixed.value; });
231-
}
232-
return std::nullopt;
233-
},
234-
// Default - no property found
235-
[](auto const&) -> std::optional<object> { return std::nullopt; });
213+
return std::nullopt;
236214
}
237215
};
238216

0 commit comments

Comments
 (0)