libcmd, nix: remove duplicated arguments

installables already have a ref<EvalState>. why are we passing the same
eval state in again, by reference, everywhere? that's just unnecessary.

Change-Id: I8225ea2575146edc55d283c0b5173b804553ceec
This commit is contained in:
eldritch horrors
2024-11-29 13:29:31 +00:00
parent 9fb5315d06
commit 564f464772
15 changed files with 45 additions and 48 deletions
+4 -4
View File
@@ -24,16 +24,16 @@ InstallableAttrPath::InstallableAttrPath(
, extendedOutputsSpec(std::move(extendedOutputsSpec))
{ }
std::pair<Value *, PosIdx> InstallableAttrPath::toValue(EvalState & state)
std::pair<Value *, PosIdx> InstallableAttrPath::toValue()
{
auto [vRes, pos] = findAlongAttrPath(state, attrPath, *cmd.getAutoArgs(state), **v);
state.forceValue(*vRes, pos);
auto [vRes, pos] = findAlongAttrPath(*state, attrPath, *cmd.getAutoArgs(*state), **v);
state->forceValue(*vRes, pos);
return {vRes, pos};
}
DerivedPathsWithInfo InstallableAttrPath::toDerivedPaths()
{
auto [v, pos] = toValue(*state);
auto [v, pos] = toValue();
if (std::optional derivedPathWithInfo = trySinglePathToDerivedPaths(
*v,
+1 -1
View File
@@ -27,7 +27,7 @@ class InstallableAttrPath : public InstallableValue
std::string what() const override { return attrPath; };
std::pair<Value *, PosIdx> toValue(EvalState & state) override;
std::pair<Value *, PosIdx> toValue() override;
DerivedPathsWithInfo toDerivedPaths() override;
+11 -11
View File
@@ -41,16 +41,16 @@ std::vector<std::string> InstallableFlake::getActualAttrPaths()
return res;
}
Value * InstallableFlake::getFlakeOutputs(EvalState & state, const flake::LockedFlake & lockedFlake)
Value * InstallableFlake::getFlakeOutputs(const flake::LockedFlake & lockedFlake)
{
auto vFlake = state.mem.allocValue();
auto vFlake = state->mem.allocValue();
callFlake(state, lockedFlake, *vFlake);
callFlake(*state, lockedFlake, *vFlake);
auto aOutputs = vFlake->attrs->get(state.symbols.create("outputs"));
auto aOutputs = vFlake->attrs->get(state->symbols.create("outputs"));
assert(aOutputs);
state.forceValue(*aOutputs->value, aOutputs->value->determinePos(noPos));
state->forceValue(*aOutputs->value, aOutputs->value->determinePos(noPos));
return aOutputs->value;
}
@@ -89,7 +89,7 @@ DerivedPathsWithInfo InstallableFlake::toDerivedPaths()
{
Activity act(*logger, lvlTalkative, actUnknown, fmt("evaluating derivation '%s'", what()));
auto attr = getCursor(*state);
auto attr = getCursor();
auto attrPath = attr->getAttrPathStr();
@@ -164,15 +164,15 @@ DerivedPathsWithInfo InstallableFlake::toDerivedPaths()
}};
}
std::pair<Value *, PosIdx> InstallableFlake::toValue(EvalState & state)
std::pair<Value *, PosIdx> InstallableFlake::toValue()
{
return {&getCursor(state)->forceValue(), noPos};
return {&getCursor()->forceValue(), noPos};
}
std::vector<ref<eval_cache::AttrCursor>>
InstallableFlake::getCursors(EvalState & state)
InstallableFlake::getCursors()
{
auto evalCache = openEvalCache(state, getLockedFlake());
auto evalCache = openEvalCache(*state, getLockedFlake());
auto root = evalCache->getRoot();
@@ -184,7 +184,7 @@ InstallableFlake::getCursors(EvalState & state)
for (auto & attrPath : attrPaths) {
debug("trying flake output attribute '%s'", attrPath);
auto attr = root->findAlongAttrPath(parseAttrPath(state, attrPath));
auto attr = root->findAlongAttrPath(parseAttrPath(*state, attrPath));
if (attr) {
res.push_back(ref(*attr));
} else {
+3 -4
View File
@@ -52,18 +52,17 @@ struct InstallableFlake : InstallableValue
std::vector<std::string> getActualAttrPaths();
Value * getFlakeOutputs(EvalState & state, const flake::LockedFlake & lockedFlake);
Value * getFlakeOutputs(const flake::LockedFlake & lockedFlake);
DerivedPathsWithInfo toDerivedPaths() override;
std::pair<Value *, PosIdx> toValue(EvalState & state) override;
std::pair<Value *, PosIdx> toValue() override;
/**
* Get a cursor to every attrpath in getActualAttrPaths() that
* exists. However if none exists, throw an exception.
*/
std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state) override;
std::vector<ref<eval_cache::AttrCursor>> getCursors() override;
std::shared_ptr<flake::LockedFlake> getLockedFlake() const;
+5 -5
View File
@@ -5,20 +5,20 @@
namespace nix {
std::vector<ref<eval_cache::AttrCursor>>
InstallableValue::getCursors(EvalState & state)
InstallableValue::getCursors()
{
auto evalCache =
std::make_shared<nix::eval_cache::EvalCache>(std::nullopt, state,
[&]() { return toValue(state).first; });
std::make_shared<nix::eval_cache::EvalCache>(std::nullopt, *state,
[&]() { return toValue().first; });
return {evalCache->getRoot()};
}
ref<eval_cache::AttrCursor>
InstallableValue::getCursor(EvalState & state)
InstallableValue::getCursor()
{
/* Although getCursors should return at least one element, in case it doesn't,
bound check to avoid an undefined behavior for vector[0] */
return getCursors(state).at(0);
return getCursors().at(0);
}
static UsageError nonValueInstallable(Installable & installable)
+4 -6
View File
@@ -77,24 +77,22 @@ struct InstallableValue : Installable
virtual ~InstallableValue() { }
virtual std::pair<Value *, PosIdx> toValue(EvalState & state) = 0;
virtual std::pair<Value *, PosIdx> toValue() = 0;
/**
* Get a cursor to each value this Installable could refer to.
* However if none exists, throw exception instead of returning
* empty vector.
*/
virtual std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state);
virtual std::vector<ref<eval_cache::AttrCursor>> getCursors();
/**
* Get the first and most preferred cursor this Installable could
* refer to, or throw an exception if none exists.
*/
virtual ref<eval_cache::AttrCursor>
getCursor(EvalState & state);
virtual ref<eval_cache::AttrCursor> getCursor();
UnresolvedApp toApp(EvalState & state);
UnresolvedApp toApp();
static InstallableValue & require(Installable & installable);
static ref<InstallableValue> require(ref<Installable> installable);
+7 -7
View File
@@ -51,15 +51,15 @@ std::string resolveString(
return rewriteStrings(toResolve, rewrites);
}
UnresolvedApp InstallableValue::toApp(EvalState & state)
UnresolvedApp InstallableValue::toApp()
{
auto cursor = getCursor(state);
auto cursor = getCursor();
auto attrPath = cursor->getAttrPath();
auto type = cursor->getAttr("type")->getString();
std::string expected = !attrPath.empty() &&
(state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
(state->symbols[attrPath[0]] == "apps" || state->symbols[attrPath[0]] == "defaultApp")
? "app" : "derivation";
if (type != expected)
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expected);
@@ -99,11 +99,11 @@ UnresolvedApp InstallableValue::toApp(EvalState & state)
else if (type == "derivation") {
auto drvPath = cursor->forceDerivation();
auto outPath = cursor->getAttr(state.s.outPath)->getString();
auto outputName = cursor->getAttr(state.s.outputName)->getString();
auto name = cursor->getAttr(state.s.name)->getString();
auto outPath = cursor->getAttr(state->s.outPath)->getString();
auto outputName = cursor->getAttr(state->s.outputName)->getString();
auto name = cursor->getAttr(state->s.name)->getString();
auto aPname = cursor->maybeGetAttr("pname");
auto aMeta = cursor->maybeGetAttr(state.s.meta);
auto aMeta = cursor->maybeGetAttr(state->s.meta);
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
auto mainProgram =
aMainProgram
+2 -2
View File
@@ -77,7 +77,7 @@ struct CmdBundle : InstallableCommand
auto const installableValue = InstallableValue::require(installable);
auto val = installableValue->toValue(*evalState).first;
auto val = installableValue->toValue().first;
auto [bundlerFlakeRef, bundlerName, extendedOutputsSpec] = parseFlakeRefWithFragmentAndExtendedOutputsSpec(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false };
@@ -91,7 +91,7 @@ struct CmdBundle : InstallableCommand
};
auto vRes = evalState->mem.allocValue();
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
evalState->callFunction(*bundler.toValue().first, *val, *vRes, noPos);
if (!evalState->isDerivation(*vRes))
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
+1 -1
View File
@@ -32,7 +32,7 @@ struct CmdEdit : InstallableCommand
auto const installableValue = InstallableValue::require(installable);
const auto [file, line] = [&] {
auto [v, pos] = installableValue->toValue(*state);
auto [v, pos] = installableValue->toValue();
try {
return findPackageFilename(*state, *v, installable->what());
+1 -1
View File
@@ -63,7 +63,7 @@ struct CmdEval : MixJSON, InstallableCommand, MixReadOnlyOption
auto state = getEvalState();
auto [v, pos] = installableValue->toValue(*state);
auto [v, pos] = installableValue->toValue();
NixStringContext context;
if (apply) {
+1 -1
View File
@@ -862,7 +862,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
defaultTemplateAttrPathsPrefixes,
lockFlags);
auto cursor = installable.getCursor(*evalState);
auto cursor = installable.getCursor();
auto templateDirAttr = cursor->getAttr("path");
auto templateDir = templateDirAttr->getString();
+1 -1
View File
@@ -34,7 +34,7 @@ struct CmdFmt : SourceExprCommand {
auto installable_ = parseInstallable(store, ".");
auto & installable = InstallableValue::require(*installable_);
auto app = installable.toApp(*evalState).resolve(evalStore, store);
auto app = installable.toApp().resolve(evalStore, store);
Strings programArgs{app.program};
+2 -2
View File
@@ -71,7 +71,7 @@ struct CmdRepl : RawInstallablesCommand
auto & installable = InstallableValue::require(*installable_);
auto what = installable.what();
if (file){
auto [val, pos] = installable.toValue(*state);
auto [val, pos] = installable.toValue();
auto what = installable.what();
state->forceValue(*val, pos);
auto autoArgs = getAutoArgs(*state);
@@ -80,7 +80,7 @@ struct CmdRepl : RawInstallablesCommand
state->forceValue(*valPost, pos);
values.push_back( {valPost, what });
} else {
auto [val, pos] = installable.toValue(*state);
auto [val, pos] = installable.toValue();
values.push_back( {val, what} );
}
}
+1 -1
View File
@@ -205,7 +205,7 @@ struct CmdRun : InstallableCommand
auto installableValue = InstallableValue::require(installable);
lockFlags.applyNixConfig = true;
auto app = installableValue->toApp(*state).resolve(getEvalStore(), store);
auto app = installableValue->toApp().resolve(getEvalStore(), store);
Strings allArgs{app.program};
for (auto & i : args) allArgs.push_back(i);
+1 -1
View File
@@ -194,7 +194,7 @@ struct CmdSearch : InstallableCommand, MixJSON
}
};
for (auto & cursor : installableValue->getCursors(*state))
for (auto & cursor : installableValue->getCursors())
visit(*cursor, cursor->getAttrPath(), true);
if (json)