From a30e616efbd51eba0e651df7e45d9114775195e1 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 16 Apr 2020 17:28:32 +0200 Subject: refactor(tvix): JSONSax: Use a RootValue 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 --- third_party/nix/src/libexpr/json-to-value.cc | 47 ++++++++++++++-------------- 1 file 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 { class JSONState { protected: std::unique_ptr parent; - Value* v; + std::shared_ptr v; public: virtual std::unique_ptr resolve(EvalState&) { throw std::logic_error("tried to close toplevel json parser state"); - }; - explicit JSONState(std::unique_ptr&& p) - : parent(std::move(p)), v(nullptr){}; - explicit JSONState(Value* v) : v(v){}; + } + explicit JSONState(std::unique_ptr&& 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 { 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 { 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(mkString, val.c_str()); } - bool start_object(std::size_t len) { + bool start_object(std::size_t) override { rs = std::make_unique(std::move(rs)); return true; } - bool key(string_t& name) { + bool key(string_t& name) override { dynamic_cast(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( std::move(rs), len != std::numeric_limits::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()); } }; -- cgit 1.4.1