refactor(tvix): json-to-value: use unique_ptr instead of raw pointers
Backported from upstream at a350d0beb0e13d0f58698510bd6a96d894cd06fd Change-Id: Ib486bc1b36ef65041fab1a4634a0a82e13036fd4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2139 Tested-by: BuildkiteCI Reviewed-by: andi <andi@notmuch.email>
This commit is contained in:
parent
83e586a6f2
commit
89f1489916
1 changed files with 18 additions and 21 deletions
39
third_party/nix/src/libexpr/json-to-value.cc
vendored
39
third_party/nix/src/libexpr/json-to-value.cc
vendored
|
@ -15,14 +15,15 @@ namespace nix {
|
||||||
class JSONSax : nlohmann::json_sax<json> {
|
class JSONSax : nlohmann::json_sax<json> {
|
||||||
class JSONState {
|
class JSONState {
|
||||||
protected:
|
protected:
|
||||||
JSONState* parent;
|
std::unique_ptr<JSONState> parent;
|
||||||
Value* v;
|
Value* v;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual JSONState* resolve(EvalState&) {
|
virtual std::unique_ptr<JSONState> resolve(EvalState&) {
|
||||||
throw std::logic_error("tried to close toplevel json parser state");
|
throw std::logic_error("tried to close toplevel json parser state");
|
||||||
};
|
};
|
||||||
explicit JSONState(JSONState* p) : parent(p), v(nullptr){};
|
explicit JSONState(std::unique_ptr<JSONState>&& p)
|
||||||
|
: parent(std::move(p)), v(nullptr){};
|
||||||
explicit JSONState(Value* v) : v(v){};
|
explicit JSONState(Value* v) : v(v){};
|
||||||
JSONState(JSONState& p) = delete;
|
JSONState(JSONState& p) = delete;
|
||||||
Value& value(EvalState& state) {
|
Value& value(EvalState& state) {
|
||||||
|
@ -36,13 +37,13 @@ class JSONSax : nlohmann::json_sax<json> {
|
||||||
class JSONObjectState : public JSONState {
|
class JSONObjectState : public JSONState {
|
||||||
using JSONState::JSONState;
|
using JSONState::JSONState;
|
||||||
ValueMap attrs = ValueMap();
|
ValueMap attrs = ValueMap();
|
||||||
virtual JSONState* resolve(EvalState& state) override {
|
std::unique_ptr<JSONState> resolve(EvalState& state) override {
|
||||||
Value& v = parent->value(state);
|
Value& v = parent->value(state);
|
||||||
state.mkAttrs(v, attrs.size());
|
state.mkAttrs(v, attrs.size());
|
||||||
for (auto& i : attrs) v.attrs->push_back(Attr(i.first, i.second));
|
for (auto& i : attrs) v.attrs->push_back(Attr(i.first, i.second));
|
||||||
return parent;
|
return std::move(parent);
|
||||||
}
|
}
|
||||||
virtual void add() override { v = nullptr; };
|
void add() override { v = nullptr; };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void key(string_t& name, EvalState& state) {
|
void key(string_t& name, EvalState& state) {
|
||||||
|
@ -51,29 +52,30 @@ class JSONSax : nlohmann::json_sax<json> {
|
||||||
};
|
};
|
||||||
|
|
||||||
class JSONListState : public JSONState {
|
class JSONListState : public JSONState {
|
||||||
|
using JSONState::JSONState;
|
||||||
std::vector<Value*> values;
|
std::vector<Value*> values;
|
||||||
virtual JSONState* resolve(EvalState& state) override {
|
std::unique_ptr<JSONState> resolve(EvalState& state) override {
|
||||||
Value& v = parent->value(state);
|
Value& v = parent->value(state);
|
||||||
state.mkList(v, values.size());
|
state.mkList(v, values.size());
|
||||||
for (size_t n = 0; n < values.size(); ++n) {
|
for (size_t n = 0; n < values.size(); ++n) {
|
||||||
(*v.list)[n] = values[n];
|
(*v.list)[n] = values[n];
|
||||||
}
|
}
|
||||||
return parent;
|
return std::move(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add() override {
|
void add() override {
|
||||||
values.push_back(v);
|
values.push_back(v);
|
||||||
v = nullptr;
|
v = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JSONListState(JSONState* p, std::size_t reserve) : JSONState(p) {
|
JSONListState(std::unique_ptr<JSONState>&& p, std::size_t reserve)
|
||||||
|
: JSONState(std::move(p)) {
|
||||||
values.reserve(reserve);
|
values.reserve(reserve);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EvalState& state;
|
EvalState& state;
|
||||||
JSONState* rs;
|
std::unique_ptr<JSONState> rs;
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
inline bool handle_value(T f, Args... args) {
|
inline bool handle_value(T f, Args... args) {
|
||||||
|
@ -84,7 +86,6 @@ class JSONSax : nlohmann::json_sax<json> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){};
|
JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){};
|
||||||
~JSONSax() { delete rs; };
|
|
||||||
|
|
||||||
bool null() { return handle_value(mkNull); }
|
bool null() { return handle_value(mkNull); }
|
||||||
|
|
||||||
|
@ -105,20 +106,17 @@ class JSONSax : nlohmann::json_sax<json> {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start_object(std::size_t len) {
|
bool start_object(std::size_t len) {
|
||||||
JSONState* old = rs;
|
rs = std::make_unique<JSONObjectState>(std::move(rs));
|
||||||
rs = new JSONObjectState(old);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool key(string_t& name) {
|
bool key(string_t& name) {
|
||||||
dynamic_cast<JSONObjectState*>(rs)->key(name, state);
|
dynamic_cast<JSONObjectState*>(rs.get())->key(name, state);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool end_object() {
|
bool end_object() {
|
||||||
JSONState* old = rs;
|
rs = rs->resolve(state);
|
||||||
rs = old->resolve(state);
|
|
||||||
delete old;
|
|
||||||
rs->add();
|
rs->add();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -126,9 +124,8 @@ class JSONSax : nlohmann::json_sax<json> {
|
||||||
bool end_array() { return end_object(); }
|
bool end_array() { return end_object(); }
|
||||||
|
|
||||||
bool start_array(size_t len) {
|
bool start_array(size_t len) {
|
||||||
JSONState* old = rs;
|
rs = std::make_unique<JSONListState>(
|
||||||
rs = new JSONListState(
|
std::move(rs), len != std::numeric_limits<size_t>::max() ? len : 128);
|
||||||
old, len != std::numeric_limits<size_t>::max() ? len : 128);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue