mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Merge pull request #11914 from roberth/evalstate-get-builtins
EvalState::getBuiltins
This commit is contained in:
commit
32becc87fe
6 changed files with 57 additions and 6 deletions
|
@ -40,6 +40,12 @@ namespace nix {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value * maybeThunk(std::string input, bool forceValue = true) {
|
||||||
|
Expr * e = state.parseExprFromString(input, state.rootPath(CanonPath::root));
|
||||||
|
assert(e);
|
||||||
|
return e->maybeThunk(state, state.baseEnv);
|
||||||
|
}
|
||||||
|
|
||||||
Symbol createSymbol(const char * value) {
|
Symbol createSymbol(const char * value) {
|
||||||
return state.symbols.create(value);
|
return state.symbols.create(value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,4 +138,27 @@ TEST(nix_isAllowedURI, non_scheme_colon) {
|
||||||
ASSERT_FALSE(isAllowedURI("https://foo/bar:baz", allowed));
|
ASSERT_FALSE(isAllowedURI("https://foo/bar:baz", allowed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class EvalStateTest : public LibExprTest {};
|
||||||
|
|
||||||
|
TEST_F(EvalStateTest, getBuiltins_ok) {
|
||||||
|
auto evaled = maybeThunk("builtins");
|
||||||
|
auto & builtins = state.getBuiltins();
|
||||||
|
ASSERT_TRUE(builtins.type() == nAttrs);
|
||||||
|
ASSERT_EQ(evaled, &builtins);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(EvalStateTest, getBuiltin_ok) {
|
||||||
|
auto & builtin = state.getBuiltin("toString");
|
||||||
|
ASSERT_TRUE(builtin.type() == nFunction);
|
||||||
|
// FIXME
|
||||||
|
// auto evaled = maybeThunk("builtins.toString");
|
||||||
|
// ASSERT_EQ(evaled, &builtin);
|
||||||
|
auto & builtin2 = state.getBuiltin("true");
|
||||||
|
ASSERT_EQ(state.forceBool(builtin2, noPos, "in unit test"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(EvalStateTest, getBuiltin_fail) {
|
||||||
|
ASSERT_THROW(state.getBuiltin("nonexistent"), EvalError);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
|
@ -448,7 +448,7 @@ void EvalState::addConstant(const std::string & name, Value * v, Constant info)
|
||||||
/* Install value the base environment. */
|
/* Install value the base environment. */
|
||||||
staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
|
staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
|
||||||
baseEnv.values[baseEnvDispl++] = v;
|
baseEnv.values[baseEnvDispl++] = v;
|
||||||
baseEnv.values[0]->payload.attrs->push_back(Attr(symbols.create(name2), v));
|
getBuiltins().payload.attrs->push_back(Attr(symbols.create(name2), v));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,16 +516,26 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
|
||||||
else {
|
else {
|
||||||
staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
|
staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
|
||||||
baseEnv.values[baseEnvDispl++] = v;
|
baseEnv.values[baseEnvDispl++] = v;
|
||||||
baseEnv.values[0]->payload.attrs->push_back(Attr(symbols.create(primOp.name), v));
|
getBuiltins().payload.attrs->push_back(Attr(symbols.create(primOp.name), v));
|
||||||
}
|
}
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Value & EvalState::getBuiltins()
|
||||||
|
{
|
||||||
|
return *baseEnv.values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Value & EvalState::getBuiltin(const std::string & name)
|
Value & EvalState::getBuiltin(const std::string & name)
|
||||||
{
|
{
|
||||||
return *baseEnv.values[0]->attrs()->find(symbols.create(name))->value;
|
auto it = getBuiltins().attrs()->get(symbols.create(name));
|
||||||
|
if (it)
|
||||||
|
return *it->value;
|
||||||
|
else
|
||||||
|
error<EvalError>("builtin '%1%' not found", name).debugThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -623,8 +623,19 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a specific builtin, equivalent to evaluating `builtins.${name}`.
|
||||||
|
* @param name The attribute name of the builtin to retrieve.
|
||||||
|
* @throws EvalError if the builtin does not exist.
|
||||||
|
*/
|
||||||
Value & getBuiltin(const std::string & name);
|
Value & getBuiltin(const std::string & name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the `builtins` attrset, equivalent to evaluating the reference `builtins`.
|
||||||
|
* Always returns an attribute set value.
|
||||||
|
*/
|
||||||
|
Value & getBuiltins();
|
||||||
|
|
||||||
struct Doc
|
struct Doc
|
||||||
{
|
{
|
||||||
Pos pos;
|
Pos pos;
|
||||||
|
|
|
@ -4938,7 +4938,7 @@ void EvalState::createBaseEnv()
|
||||||
|
|
||||||
/* Now that we've added all primops, sort the `builtins' set,
|
/* Now that we've added all primops, sort the `builtins' set,
|
||||||
because attribute lookups expect it to be sorted. */
|
because attribute lookups expect it to be sorted. */
|
||||||
baseEnv.values[0]->payload.attrs->sort();
|
getBuiltins().payload.attrs->sort();
|
||||||
|
|
||||||
staticBaseEnv->sort();
|
staticBaseEnv->sort();
|
||||||
|
|
||||||
|
|
|
@ -435,7 +435,8 @@ void mainWrapped(int argc, char * * argv)
|
||||||
evalSettings.pureEval = false;
|
evalSettings.pureEval = false;
|
||||||
EvalState state({}, openStore("dummy://"), fetchSettings, evalSettings);
|
EvalState state({}, openStore("dummy://"), fetchSettings, evalSettings);
|
||||||
auto builtinsJson = nlohmann::json::object();
|
auto builtinsJson = nlohmann::json::object();
|
||||||
for (auto & builtin : *state.baseEnv.values[0]->attrs()) {
|
for (auto & builtinPtr : state.getBuiltins().attrs()->lexicographicOrder(state.symbols)) {
|
||||||
|
auto & builtin = *builtinPtr;
|
||||||
auto b = nlohmann::json::object();
|
auto b = nlohmann::json::object();
|
||||||
if (!builtin.value->isPrimOp()) continue;
|
if (!builtin.value->isPrimOp()) continue;
|
||||||
auto primOp = builtin.value->primOp();
|
auto primOp = builtin.value->primOp();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue