diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-04-16T15·28+0200 |
---|---|---|
committer | glittershark <grfn@gws.fyi> | 2020-11-27T19·59+0000 |
commit | a30e616efbd51eba0e651df7e45d9114775195e1 (patch) | |
tree | f34d58ad6b07004469d5d5ef0ca9c929fd858a23 /third_party/nix | |
parent | 89f1489916b0b727a13ddf49ec90d00616454c65 (diff) |
refactor(tvix): JSONSax: Use a RootValue r/1938
More #3377. Backported from upstream at 9f46f54de4e55267df492456fc0393f74616366 Change-Id: I11bfca4ec56bd4e45291ce3f98a60f198dff0196 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2140 Tested-by: BuildkiteCI Reviewed-by: andi <andi@notmuch.email>
Diffstat (limited to 'third_party/nix')
-rw-r--r-- | third_party/nix/src/libexpr/json-to-value.cc | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/third_party/nix/src/libexpr/json-to-value.cc b/third_party/nix/src/libexpr/json-to-value.cc index 368bc9f5b575..66912aa084e6 100644 --- a/third_party/nix/src/libexpr/json-to-value.cc +++ b/third_party/nix/src/libexpr/json-to-value.cc @@ -16,22 +16,21 @@ class JSONSax : nlohmann::json_sax<json> { class JSONState { protected: std::unique_ptr<JSONState> parent; - Value* v; + std::shared_ptr<Value*> v; public: virtual std::unique_ptr<JSONState> resolve(EvalState&) { throw std::logic_error("tried to close toplevel json parser state"); - }; - explicit JSONState(std::unique_ptr<JSONState>&& p) - : parent(std::move(p)), v(nullptr){}; - explicit JSONState(Value* v) : v(v){}; + } + explicit JSONState(std::unique_ptr<JSONState>&& p) : parent(std::move(p)) {} + explicit JSONState(Value* v) : v(allocRootValue(v)) {} JSONState(JSONState& p) = delete; Value& value(EvalState& state) { - if (v == nullptr) v = state.allocValue(); - return *v; - }; - virtual ~JSONState(){}; - virtual void add(){}; + if (!v) v = allocRootValue(state.allocValue()); + return **v; + } + virtual ~JSONState() {} + virtual void add() {} }; class JSONObjectState : public JSONState { @@ -63,7 +62,7 @@ class JSONSax : nlohmann::json_sax<json> { return std::move(parent); } void add() override { - values.push_back(v); + values.push_back(*v); v = nullptr; }; @@ -87,50 +86,52 @@ class JSONSax : nlohmann::json_sax<json> { public: JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){}; - bool null() { return handle_value(mkNull); } + bool null() override { return handle_value(mkNull); } - bool boolean(bool val) { return handle_value(mkBool, val); } + bool boolean(bool val) override { return handle_value(mkBool, val); } - bool number_integer(number_integer_t val) { return handle_value(mkInt, val); } + bool number_integer(number_integer_t val) override { + return handle_value(mkInt, val); + } - bool number_unsigned(number_unsigned_t val) { + bool number_unsigned(number_unsigned_t val) override { return handle_value(mkInt, val); } - bool number_float(number_float_t val, const string_t& s) { + bool number_float(number_float_t val, const string_t&) override { return handle_value(mkFloat, val); } - bool string(string_t& val) { + bool string(string_t& val) override { return handle_value<void(Value&, const char*)>(mkString, val.c_str()); } - bool start_object(std::size_t len) { + bool start_object(std::size_t) override { rs = std::make_unique<JSONObjectState>(std::move(rs)); return true; } - bool key(string_t& name) { + bool key(string_t& name) override { dynamic_cast<JSONObjectState*>(rs.get())->key(name, state); return true; } - bool end_object() { + bool end_object() override { rs = rs->resolve(state); rs->add(); return true; } - bool end_array() { return end_object(); } + bool end_array() override { return end_object(); } - bool start_array(size_t len) { + bool start_array(size_t len) override { rs = std::make_unique<JSONListState>( std::move(rs), len != std::numeric_limits<size_t>::max() ? len : 128); return true; } bool parse_error(std::size_t, const std::string&, - const nlohmann::detail::exception& ex) { + const nlohmann::detail::exception& ex) override { throw JSONParseError(ex.what()); } }; |