-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinspector.cc
More file actions
165 lines (155 loc) · 5 KB
/
inspector.cc
File metadata and controls
165 lines (155 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "inspector.hh"
#include <memory>
#include <nlohmann/json.hpp>
#include <nix/expr/attr-path.hh>
#include <nix/util/canon-path.hh>
#include <nix/cmd/command.hh>
#include <nix/expr/eval-gc.hh>
#include <nix/expr/eval.hh>
#include <nix/store/local-fs-store.hh>
#include <nix/expr/nixexpr.hh>
#include <nix/main/shared.hh>
#include <nix/store/store-api.hh>
#include <nix/expr/value-to-json.hh>
#include <nix/expr/value.hh>
#include <nix/flake/settings.hh>
#include <nix/expr/eval-settings.hh>
#include <string>
#include <nix/util/logging.hh>
const auto MAX_SIZE = 32768;
NixInspector::NixInspector(std::string expr)
: state(getEvalState().get_ptr().get()),
vRoot(*state->allocValue()),
autoArgs(*state->buildBindings(0).finish()) {
// auto attrs = state->buildBindings(1);
// Value* root = state->allocValue();
state->eval(
state->parseExprFromString(expr, state->rootPath(CanonPath::root)), vRoot
);
// attrs.insert(state->symbols.create("root"), root);
// vRoot.mkAttrs(attrs.finish());
}
// void NixInspector::initEnv() {
// env = &state->allocEnv(MAX_SIZE);
// env->up = &state->baseEnv;
// displ = 0;
// static_env->vars.clear();
// static_env->sort();
// }
// void NixInspector::addAttrsToScope(Value &attrs) {
// // state->forceAttrs(
// // attrs, [&]() { return attrs.determinePos(noPos); },
// // "while evaluating an attribute set to be merged in the global
// scope"); if (displ + attrs.attrs->size() >= MAX_SIZE)
// throw Error("environment full; cannot add more variables");
//
// for (auto i : *attrs.attrs) {
// static_env->vars.emplace_back(i.name, displ);
// env->values[displ++] = i.value;
// }
// static_env->sort();
// static_env->deduplicate();
// }
//
// Value NixInspector::_eval(std::string path) {
// Value vRes;
// auto expr = state->parseExprFromString(
// path, state->rootPath(CanonPath::root), static_env
// );
//
// expr->eval(*state, *env, vRes);
//
// return vRes;
// }
std::shared_ptr<Value> NixInspector::inspect(std::string &attrPath) {
// if (attrPath.length() == 0) {
// attrPath = "root";
// } else {
// attrPath = "root." + attrPath;
// }
Value &v(
*findAlongAttrPath(*state, std::string(attrPath), autoArgs, vRoot).first
);
state->forceValue(v, v.determinePos(noPos));
Value vRes;
state->autoCallFunction(autoArgs, v, vRes);
return std::make_shared<Value>(vRes);
}
int32_t NixInspector::v_int(const Value &value) { return value.integer().value; }
float_t NixInspector::v_float(const Value &value) { return value.fpoint(); }
bool NixInspector::v_bool(const Value &value) { return value.boolean(); }
std::string NixInspector::v_string(const Value &value) {
return std::string(value.string_view());
}
std::string NixInspector::v_path(const Value &value) {
return value.path().path.c_str();
}
nlohmann::json NixInspector::v_repr(const Value &value) {
switch (value.type()) {
case nix::nAttrs: {
auto collected = std::vector<std::string>();
for (auto x : *value.attrs()) {
auto name = state->symbols[x.name];
collected.push_back(std::string(name));
}
return collected;
}
case nix::nList: {
return value.listSize();
}
case nix::nString:
return value.string_view();
case nix::nPath:
return value.path().path.c_str();
case nix::nBool:
return value.boolean();
case nix::nFloat:
return value.fpoint();
case nix::nInt:
return value.integer().value;
case nix::nNull:
return nullptr;
case nix::nExternal:
case nix::nThunk:
case nix::nFunction:
return nullptr;
}
return nullptr;
}
// std::vector<NixAttr> NixInspector::v_attrs(const Value &value) {
// auto collected = std::vector<NixAttr>();
// for (auto x : *value.attrs) {
// auto name = state->symbols[x.name];
// auto value = std::make_shared<Value>(*x.value);
// collected.push_back(NixAttr{.key = (std::string)name, .value = value});
// ;
// }
// return collected;
// }
std::unique_ptr<std::vector<Value>> NixInspector::v_list(const Value &value) {
auto collected = std::vector<Value>();
auto listItems = value.listView();
for (auto x : listItems) {
collected.emplace_back(*x);
}
return std::make_unique<std::vector<Value>>(collected);
}
void init_nix_inspector() {
nix::initNix();
nix::initGC();
nix::flakeSettings.configureEvalSettings(nix::evalSettings);
logger = std::make_unique<CaptureLogger>();
}
ValueType NixInspector::v_type(const Value &value) { return value.type(); }
// Gets a attribute at a specific name and if the passed value is a thunk it
// evaluates it SAFETY: this function only safe to call if the value being
// passed is an attrset or a thunk that results in an attrset
std::shared_ptr<Value> NixInspector::v_child(
const Value &value, std::string key
) {
auto x = value.attrs()->get(state->symbols.create(std::string(key)));
Value vRes;
state->forceValue(*x->value, x->value->determinePos(noPos));
vRes = *x->value;
return std::make_shared<Value>(vRes);
}