mirror of
https://github.com/NixOS/nix.git
synced 2024-11-25 08:12:29 +00:00
Add EvalState::getBuiltins
This commit is contained in:
parent
a58e38dab7
commit
8a36d2d8a7
@ -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
|
@ -523,13 +523,19 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Value & EvalState::getBuiltins()
|
||||||
|
{
|
||||||
|
return *baseEnv.values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Value & EvalState::getBuiltin(const std::string & name)
|
Value & EvalState::getBuiltin(const std::string & name)
|
||||||
{
|
{
|
||||||
auto it = baseEnv.values[0]->attrs()->get(symbols.create(name));
|
auto it = baseEnv.values[0]->attrs()->get(symbols.create(name));
|
||||||
if (it)
|
if (it)
|
||||||
return *it->value;
|
return *it->value;
|
||||||
else
|
else
|
||||||
throw EvalError("builtin '%1%' not found", name);
|
error<EvalError>("builtin '%1%' not found", name).debugThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -630,6 +630,12 @@ public:
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user