libexpr: associate memory with EvalContext

Change-Id: I791ae16cd42a674972c782a869b7d790880313ff
This commit is contained in:
eldritch horrors 2024-12-03 20:38:41 +01:00
parent 5db5eed43d
commit 1f37fc85ac
14 changed files with 109 additions and 109 deletions

View file

@ -174,7 +174,7 @@ static void loadSourceExpr(EvalState & state, const SourcePath & path, Value & v
~/.nix-defexpr directory that includes some system-wide
directory). */
else if (st.type == InputAccessor::tDirectory) {
auto attrs = state.buildBindings(maxAttrs);
auto attrs = state.ctx.buildBindings(maxAttrs);
attrs.alloc("_combineChannels").mkList(0);
StringSet seen;
getAllExprs(state, path, seen, attrs);
@ -700,7 +700,7 @@ static void opUpgrade(Globals & globals, Strings opFlags, Strings opArgs)
static void setMetaFlag(EvalState & state, DrvInfo & drv,
const std::string & name, const std::string & value)
{
auto v = state.mem.allocValue();
auto v = state.ctx.mem.allocValue();
v->mkString(value);
drv.setMeta(state, name, v);
}

View file

@ -34,7 +34,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
/* Construct the whole top level derivation. */
StorePathSet references;
Value manifest = state.mem.newList(elems.size());
Value manifest = state.ctx.mem.newList(elems.size());
size_t n = 0;
for (auto & i : elems) {
/* Create a pseudo-derivation containing the name, system,
@ -44,7 +44,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
DrvInfo::Outputs outputs = i.queryOutputs(state, true, true);
StringSet metaNames = i.queryMetaNames(state);
auto attrs = state.buildBindings(7 + outputs.size());
auto attrs = state.ctx.buildBindings(7 + outputs.size());
attrs.alloc(state.ctx.s.type).mkString("derivation");
attrs.alloc(state.ctx.s.name).mkString(i.queryName(state));
@ -57,10 +57,10 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
// Copy each output meant for installation.
auto & vOutputs = attrs.alloc(state.ctx.s.outputs);
vOutputs = state.mem.newList(outputs.size());
vOutputs = state.ctx.mem.newList(outputs.size());
for (const auto & [m, j] : enumerate(outputs)) {
(vOutputs.listElems()[m] = state.mem.allocValue())->mkString(j.first);
auto outputAttrs = state.buildBindings(2);
(vOutputs.listElems()[m] = state.ctx.mem.allocValue())->mkString(j.first);
auto outputAttrs = state.ctx.buildBindings(2);
outputAttrs.alloc(state.ctx.s.outPath).mkString(state.ctx.store->printStorePath(*j.second));
attrs.alloc(j.first).mkAttrs(outputAttrs);
@ -73,7 +73,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
}
// Copy the meta attributes.
auto meta = state.buildBindings(metaNames.size());
auto meta = state.ctx.buildBindings(metaNames.size());
for (auto & j : metaNames) {
Value * v = i.queryMeta(state, j);
if (!v) continue;
@ -82,7 +82,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
attrs.alloc(state.ctx.s.meta).mkAttrs(meta);
(manifest.listElems()[n++] = state.mem.allocValue())->mkAttrs(attrs);
(manifest.listElems()[n++] = state.ctx.mem.allocValue())->mkAttrs(attrs);
if (drvPath) references.insert(*drvPath);
}
@ -103,7 +103,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
/* Construct a Nix expression that calls the user environment
builder with the manifest as argument. */
auto attrs = state.buildBindings(3);
auto attrs = state.ctx.buildBindings(3);
state.paths.mkStorePathString(manifestFile, attrs.alloc("manifest"));
attrs.insert(state.ctx.symbols.create("derivations"), &manifest);
Value args;

View file

@ -21,7 +21,7 @@ DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
if (pathExists(manifestFile)) {
Value v;
state.evalFile(CanonPath(manifestFile), v);
Bindings & bindings(*state.mem.allocBindings(0));
Bindings & bindings(*state.ctx.mem.allocBindings(0));
getDerivations(state, v, "", bindings, elems, false);
}
return elems;

View file

@ -405,7 +405,7 @@ ref<eval_cache::EvalCache> openEvalCache(
if (getEnv("NIX_ALLOW_EVAL").value_or("1") == "0")
throw Error("not everything is cached, but evaluation is not allowed");
auto vFlake = state.mem.allocValue();
auto vFlake = state.ctx.mem.allocValue();
flake::callFlake(state, *lockedFlake, *vFlake);
state.forceAttrs(*vFlake, noPos, "while parsing cached flake data");

View file

@ -45,7 +45,7 @@ std::pair<Value *, PosIdx> findAlongAttrPath(EvalState & state, const std::strin
auto attrIndex = string2Int<unsigned int>(attr);
/* Evaluate the expression. */
Value * vNew = state.mem.allocValue();
Value * vNew = state.ctx.mem.allocValue();
state.autoCallFunction(autoArgs, *v, *vNew);
v = vNew;
state.forceValue(*v, noPos);
@ -100,7 +100,7 @@ std::pair<SourcePath, uint32_t> findPackageFilename(EvalState & state, Value & v
{
Value * v2;
try {
auto dummyArgs = state.mem.allocBindings(0);
auto dummyArgs = state.ctx.mem.allocBindings(0);
v2 = findAlongAttrPath(state, "meta.position", *dummyArgs, v).first;
} catch (Error &) {
throw NoPositionInfo("package '%s' has no source location information", what);

View file

@ -799,7 +799,7 @@ void EvalState::mkPos(Value & v, PosIdx p)
{
auto origin = ctx.positions.originOf(p);
if (auto path = std::get_if<SourcePath>(&origin)) {
auto attrs = buildBindings(3);
auto attrs = ctx.buildBindings(3);
attrs.alloc(ctx.s.file).mkString(path->path.abs());
makePositionThunks(*this, p, attrs.alloc(ctx.s.line), attrs.alloc(ctx.s.column));
v.mkAttrs(attrs);
@ -889,7 +889,7 @@ void EvalState::mkSingleDerivedPathString(
of thunks allocated. */
Value * Expr::maybeThunk(EvalState & state, Env & env)
{
Value * v = state.mem.allocValue();
Value * v = state.ctx.mem.allocValue();
v->mkThunk(&env, *this);
state.stats.nrThunks++;
return v;
@ -1056,7 +1056,7 @@ void ExprPath::eval(EvalState & state, Env & env, Value & v)
Env * ExprAttrs::buildInheritFromEnv(EvalState & state, Env & up)
{
Env & inheritEnv = state.mem.allocEnv(inheritFromExprs->size());
Env & inheritEnv = state.ctx.mem.allocEnv(inheritFromExprs->size());
inheritEnv.up = &up;
Displacement displ = 0;
@ -1068,13 +1068,13 @@ Env * ExprAttrs::buildInheritFromEnv(EvalState & state, Env & up)
void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
{
v.mkAttrs(state.buildBindings(attrs.size() + dynamicAttrs.size()).finish());
v.mkAttrs(state.ctx.buildBindings(attrs.size() + dynamicAttrs.size()).finish());
auto dynamicEnv = &env;
if (recursive) {
/* Create a new environment that contains the attributes in
this `rec'. */
Env & env2(state.mem.allocEnv(attrs.size()));
Env & env2(state.ctx.mem.allocEnv(attrs.size()));
env2.up = &env;
dynamicEnv = &env2;
Env * inheritEnv = inheritFromExprs ? buildInheritFromEnv(state, env2) : nullptr;
@ -1089,7 +1089,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
for (auto & i : attrs) {
Value * vAttr;
if (hasOverrides && i.second.kind != AttrDef::Kind::Inherited) {
vAttr = state.mem.allocValue();
vAttr = state.ctx.mem.allocValue();
vAttr->mkThunk(i.second.chooseByKind(&env2, &env, inheritEnv), *i.second.e);
state.stats.nrThunks++;
} else
@ -1109,7 +1109,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
if (hasOverrides) {
Value * vOverrides = (*v.attrs)[overrides->second.displ].value;
state.forceAttrs(*vOverrides, vOverrides->determinePos(noPos), "while evaluating the `__overrides` attribute");
Bindings * newBnds = state.mem.allocBindings(v.attrs->capacity() + vOverrides->attrs->size());
Bindings * newBnds = state.ctx.mem.allocBindings(v.attrs->capacity() + vOverrides->attrs->size());
for (auto & i : *v.attrs)
newBnds->push_back(i);
for (auto & i : *vOverrides->attrs) {
@ -1162,7 +1162,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
{
/* Create a new environment that contains the attributes in this
`let'. */
Env & env2(state.mem.allocEnv(attrs->attrs.size()));
Env & env2(state.ctx.mem.allocEnv(attrs->attrs.size()));
env2.up = &env;
Env * inheritEnv = attrs->inheritFromExprs ? attrs->buildInheritFromEnv(state, env2) : nullptr;
@ -1196,7 +1196,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
void ExprList::eval(EvalState & state, Env & env, Value & v)
{
v = state.mem.newList(elems.size());
v = state.ctx.mem.newList(elems.size());
for (auto [n, v2] : enumerate(v.listItems()))
const_cast<Value * &>(v2) = elems[n]->maybeThunk(state, env);
}
@ -1499,7 +1499,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
{
vRes = vCur;
for (size_t i = 0; i < nrArgs; ++i) {
auto fun2 = mem.allocValue();
auto fun2 = ctx.mem.allocValue();
*fun2 = vRes;
vRes.mkPrimOpApp(fun2, args[i]);
}
@ -1516,7 +1516,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
auto size =
(!lambda.arg ? 0 : 1) +
(lambda.hasFormals() ? lambda.formals->formals.size() : 0);
Env & env2(mem.allocEnv(size));
Env & env2(ctx.mem.allocEnv(size));
env2.up = vCur.lambda.env;
Displacement displ = 0;
@ -1687,7 +1687,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
/* 'vCur' may be allocated on the stack of the calling
function, but for functors we may keep a reference, so
heap-allocate a copy and use that instead. */
Value * args2[] = {mem.allocValue(), args[0]};
Value * args2[] = {ctx.mem.allocValue(), args[0]};
*args2[0] = vCur;
try {
callFunction(*functor->value, 2, args2, vCur, functor->pos);
@ -1760,7 +1760,7 @@ void EvalState::autoCallFunction(Bindings & args, Value & fun, Value & res)
if (fun.type() == nAttrs) {
auto found = fun.attrs->find(ctx.s.functor);
if (found != fun.attrs->end()) {
Value * v = mem.allocValue();
Value * v = ctx.mem.allocValue();
callFunction(*found->value, fun, *v, pos);
forceValue(*v, pos);
return autoCallFunction(args, *v, res);
@ -1772,7 +1772,7 @@ void EvalState::autoCallFunction(Bindings & args, Value & fun, Value & res)
return;
}
auto attrs = buildBindings(std::max(static_cast<uint32_t>(fun.lambda.fun->formals->formals.size()), args.size()));
auto attrs = ctx.buildBindings(std::max(static_cast<uint32_t>(fun.lambda.fun->formals->formals.size()), args.size()));
if (fun.lambda.fun->formals->ellipsis) {
// If the formals have an ellipsis (eg the function accepts extra args) pass
@ -1797,13 +1797,13 @@ https://docs.lix.systems/manual/lix/stable/language/constructs.html#functions)",
}
}
callFunction(fun, mem.allocValue()->mkAttrs(attrs), res, pos);
callFunction(fun, ctx.mem.allocValue()->mkAttrs(attrs), res, pos);
}
void ExprWith::eval(EvalState & state, Env & env, Value & v)
{
Env & env2(state.mem.allocEnv(1));
Env & env2(state.ctx.mem.allocEnv(1));
env2.up = &env;
env2.values[0] = attrs->maybeThunk(state, env);
@ -1880,7 +1880,7 @@ void ExprOpUpdate::eval(EvalState & state, Env & env, Value & v)
if (v1.attrs->size() == 0) { v = v2; return; }
if (v2.attrs->size() == 0) { v = v1; return; }
auto attrs = state.buildBindings(v1.attrs->size() + v2.attrs->size());
auto attrs = state.ctx.buildBindings(v1.attrs->size() + v2.attrs->size());
/* Merge the sets, preferring values from the second set. Make
sure to keep the resulting vector in sorted order. */
@ -1934,7 +1934,7 @@ void EvalState::concatLists(Value & v, size_t nrLists, Value * * lists, const Po
return;
}
v = mem.newList(len);
v = ctx.mem.newList(len);
auto out = v.listElems();
for (size_t n = 0, pos = 0; n < nrLists; ++n) {
auto l = lists[n]->listSize();

View file

@ -730,7 +730,7 @@ public:
BindingsBuilder buildBindings(size_t capacity)
{
return mem.buildBindings(ctx.symbols, capacity);
return ctx.mem.buildBindings(ctx.symbols, capacity);
}
void mkPos(Value & v, PosIdx pos);

View file

@ -773,11 +773,11 @@ void callFlake(EvalState & state,
const LockedFlake & lockedFlake,
Value & vRes)
{
auto vLocks = state.mem.allocValue();
auto vRootSrc = state.mem.allocValue();
auto vRootSubdir = state.mem.allocValue();
auto vTmp1 = state.mem.allocValue();
auto vTmp2 = state.mem.allocValue();
auto vLocks = state.ctx.mem.allocValue();
auto vRootSrc = state.ctx.mem.allocValue();
auto vRootSubdir = state.ctx.mem.allocValue();
auto vTmp1 = state.ctx.mem.allocValue();
auto vTmp2 = state.ctx.mem.allocValue();
vLocks->mkString(lockedFlake.lockFile.to_string());
@ -792,7 +792,7 @@ void callFlake(EvalState & state,
vRootSubdir->mkString(lockedFlake.flake.lockedRef.subdir);
if (!state.caches.vCallFlake) {
state.caches.vCallFlake = allocRootValue(state.mem.allocValue());
state.caches.vCallFlake = allocRootValue(state.ctx.mem.allocValue());
state.eval(state.parseExprFromString(
#include "call-flake.nix.gen.hh"
, CanonPath::root), **state.caches.vCallFlake);
@ -830,7 +830,7 @@ void prim_parseFlakeRef(
std::string flakeRefS(state.forceStringNoCtx(*args[0], pos,
"while evaluating the argument passed to builtins.parseFlakeRef"));
auto attrs = parseFlakeRef(flakeRefS, {}, true).toAttrs();
auto binds = state.buildBindings(attrs.size());
auto binds = state.ctx.buildBindings(attrs.size());
for (const auto & [key, value] : attrs) {
auto s = state.ctx.symbols.create(key);
auto & vv = binds.alloc(s);

View file

@ -334,7 +334,7 @@ bool DrvInfo::queryMetaBool(EvalState & state, const std::string & name, bool de
void DrvInfo::setMeta(EvalState & state, const std::string & name, Value * v)
{
getMeta(state);
auto attrs = state.buildBindings(1 + (meta ? meta->size() : 0));
auto attrs = state.ctx.buildBindings(1 + (meta ? meta->size() : 0));
auto sym = state.ctx.symbols.create(name);
if (meta)
for (auto i : *meta)

View file

@ -28,7 +28,7 @@ class JSONSax : nlohmann::json_sax<json> {
Value & value(EvalState & state)
{
if (!v)
v = allocRootValue(state.mem.allocValue());
v = allocRootValue(state.ctx.mem.allocValue());
return **v;
}
virtual ~JSONState() {}
@ -40,7 +40,7 @@ class JSONSax : nlohmann::json_sax<json> {
ValueMap attrs;
std::unique_ptr<JSONState> resolve(EvalState & state) override
{
auto attrs2 = state.buildBindings(attrs.size());
auto attrs2 = state.ctx.buildBindings(attrs.size());
for (auto & i : attrs)
attrs2.insert(i.first, i.second);
parent->value(state).mkAttrs(attrs2.alreadySorted());
@ -59,7 +59,7 @@ class JSONSax : nlohmann::json_sax<json> {
std::unique_ptr<JSONState> resolve(EvalState & state) override
{
Value & v = parent->value(state);
v = state.mem.newList(values.size());
v = state.ctx.mem.newList(values.size());
for (size_t n = 0; n < values.size(); ++n) {
v.listElems()[n] = values[n];
}

View file

@ -190,24 +190,24 @@ static void import(EvalState & state, const PosIdx pos, Value & vPath, Value * v
if (auto storePath = isValidDerivationInStore()) {
Derivation drv = state.ctx.store->readDerivation(*storePath);
auto attrs = state.buildBindings(3 + drv.outputs.size());
auto attrs = state.ctx.buildBindings(3 + drv.outputs.size());
attrs.alloc(state.ctx.s.drvPath).mkString(path2, {
NixStringContextElem::DrvDeep { .drvPath = *storePath },
});
attrs.alloc(state.ctx.s.name).mkString(drv.env["name"]);
auto & outputsVal = attrs.alloc(state.ctx.s.outputs);
outputsVal = state.mem.newList(drv.outputs.size());
outputsVal = state.ctx.mem.newList(drv.outputs.size());
for (const auto & [i, o] : enumerate(drv.outputs)) {
mkOutputString(state, attrs, *storePath, o);
(outputsVal.listElems()[i] = state.mem.allocValue())->mkString(o.first);
(outputsVal.listElems()[i] = state.ctx.mem.allocValue())->mkString(o.first);
}
auto w = state.mem.allocValue();
auto w = state.ctx.mem.allocValue();
w->mkAttrs(attrs);
if (!state.caches.vImportedDrvToDerivation) {
state.caches.vImportedDrvToDerivation = allocRootValue(state.mem.allocValue());
state.caches.vImportedDrvToDerivation = allocRootValue(state.ctx.mem.allocValue());
state.eval(state.parseExprFromString(
#include "imported-drv-to-derivation.nix.gen.hh"
, CanonPath::root), **state.caches.vImportedDrvToDerivation);
@ -234,7 +234,7 @@ static void import(EvalState & state, const PosIdx pos, Value & vPath, Value * v
else {
state.forceAttrs(*vScope, pos, "while evaluating the first argument passed to builtins.scopedImport");
Env * env = &state.mem.allocEnv(vScope->attrs->size());
Env * env = &state.ctx.mem.allocEnv(vScope->attrs->size());
env->up = &state.builtins.env;
auto staticEnv = std::make_shared<StaticEnv>(
@ -563,7 +563,7 @@ static void prim_genericClosure(EvalState & state, const PosIdx pos, Value * * a
}
/* Create the result list. */
v = state.mem.newList(res.size());
v = state.ctx.mem.newList(res.size());
unsigned int n = 0;
for (auto & i : res)
v.listElems()[n++] = i;
@ -640,7 +640,7 @@ static void prim_floor(EvalState & state, const PosIdx pos, Value * * args, Valu
* else => {success=false; value=false;} */
static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{
auto attrs = state.buildBindings(2);
auto attrs = state.ctx.buildBindings(2);
std::optional<MaintainCount<int>> trylevel;
std::unique_ptr<DebugState> savedDebug;
@ -1114,7 +1114,7 @@ drvName, Bindings * attrs, Value & v)
drvHashes.lock()->insert_or_assign(drvPath, h);
}
auto result = state.buildBindings(1 + drv.outputs.size());
auto result = state.ctx.buildBindings(1 + drv.outputs.size());
result.alloc(state.ctx.s.drvPath).mkString(drvPathS, {
NixStringContextElem::DrvDeep { .drvPath = drvPath },
});
@ -1365,7 +1365,7 @@ static void prim_readDir(EvalState & state, const PosIdx pos, Value * * args, Va
// This is similar to `getFileType` but is optimized to reduce system calls
// on many systems.
auto entries = path.readDirectory();
auto attrs = state.buildBindings(entries.size());
auto attrs = state.ctx.buildBindings(entries.size());
// If we hit unknown directory entry types we may need to fallback to
// using `getFileType` on some systems.
@ -1379,7 +1379,7 @@ static void prim_readDir(EvalState & state, const PosIdx pos, Value * * args, Va
// Some filesystems or operating systems may not be able to return
// detailed node info quickly in this case we produce a thunk to
// query the file type lazily.
auto epath = state.mem.allocValue();
auto epath = state.ctx.mem.allocValue();
epath->mkPath(path + name);
if (!readFileType)
readFileType = &state.builtins.get("readFileType");
@ -1623,11 +1623,11 @@ static void prim_attrNames(EvalState & state, const PosIdx pos, Value * * args,
{
state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrNames");
v = state.mem.newList(args[0]->attrs->size());
v = state.ctx.mem.newList(args[0]->attrs->size());
size_t n = 0;
for (auto & i : *args[0]->attrs)
(v.listElems()[n++] = state.mem.allocValue())->mkString(state.ctx.symbols[i.name]);
(v.listElems()[n++] = state.ctx.mem.allocValue())->mkString(state.ctx.symbols[i.name]);
std::sort(v.listElems(), v.listElems() + n,
[](Value * v1, Value * v2) { return strcmp(v1->string.s, v2->string.s) < 0; });
@ -1639,7 +1639,7 @@ static void prim_attrValues(EvalState & state, const PosIdx pos, Value * * args,
{
state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrValues");
v = state.mem.newList(args[0]->attrs->size());
v = state.ctx.mem.newList(args[0]->attrs->size());
// FIXME: this is incredibly evil, *why*
// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast)
@ -1730,7 +1730,7 @@ static struct LazyPosAcessors {
void operator()(EvalState & state, const PosIdx pos, Value & line, Value & column)
{
Value * posV = state.mem.allocValue();
Value * posV = state.ctx.mem.allocValue();
posV->mkInt(pos.id);
line.mkApp(&lineOfPos, posV);
column.mkApp(&columnOfPos, posV);
@ -1777,7 +1777,7 @@ static void prim_removeAttrs(EvalState & state, const PosIdx pos, Value * * args
/* Copy all attributes not in that set. Note that we don't need
to sort v.attrs because it's a subset of an already sorted
vector. */
auto attrs = state.buildBindings(args[0]->attrs->size());
auto attrs = state.ctx.buildBindings(args[0]->attrs->size());
std::set_difference(
args[0]->attrs->begin(), args[0]->attrs->end(),
names.begin(), names.end(),
@ -1794,7 +1794,7 @@ static void prim_listToAttrs(EvalState & state, const PosIdx pos, Value * * args
{
state.forceList(*args[0], pos, "while evaluating the argument passed to builtins.listToAttrs");
auto attrs = state.buildBindings(args[0]->listSize());
auto attrs = state.ctx.buildBindings(args[0]->listSize());
std::set<Symbol> seen;
@ -1823,7 +1823,7 @@ static void prim_intersectAttrs(EvalState & state, const PosIdx pos, Value * * a
Bindings &left = *args[0]->attrs;
Bindings &right = *args[1]->attrs;
auto attrs = state.buildBindings(std::min(left.size(), right.size()));
auto attrs = state.ctx.buildBindings(std::min(left.size(), right.size()));
// The current implementation has good asymptotic complexity and is reasonably
// simple. Further optimization may be possible, but does not seem productive,
@ -1896,7 +1896,7 @@ static void prim_catAttrs(EvalState & state, const PosIdx pos, Value * * args, V
res[found++] = i->value;
}
v = state.mem.newList(found);
v = state.ctx.mem.newList(found);
for (unsigned int n = 0; n < found; ++n)
v.listElems()[n] = res[n];
}
@ -1916,7 +1916,7 @@ static void prim_functionArgs(EvalState & state, const PosIdx pos, Value * * arg
return;
}
auto attrs = state.buildBindings(args[0]->lambda.fun->formals->formals.size());
auto attrs = state.ctx.buildBindings(args[0]->lambda.fun->formals->formals.size());
for (auto & i : args[0]->lambda.fun->formals->formals)
// !!! should optimise booleans (allocate only once)
attrs.alloc(i.name, i.pos).mkBool(i.def != nullptr);
@ -1928,11 +1928,11 @@ static void prim_mapAttrs(EvalState & state, const PosIdx pos, Value * * args, V
{
state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.mapAttrs");
auto attrs = state.buildBindings(args[1]->attrs->size());
auto attrs = state.ctx.buildBindings(args[1]->attrs->size());
for (auto & i : *args[1]->attrs) {
Value * vName = state.mem.allocValue();
Value * vFun2 = state.mem.allocValue();
Value * vName = state.ctx.mem.allocValue();
Value * vFun2 = state.ctx.mem.allocValue();
vName->mkString(state.ctx.symbols[i.name]);
vFun2->mkApp(args[0], vName);
attrs.alloc(i.name).mkApp(vFun2, i.value);
@ -1964,10 +1964,10 @@ static void prim_zipAttrsWith(EvalState & state, const PosIdx pos, Value * * arg
attrsSeen[attr.name].first++;
}
auto attrs = state.buildBindings(attrsSeen.size());
auto attrs = state.ctx.buildBindings(attrsSeen.size());
for (auto & [sym, elem] : attrsSeen) {
auto & list = attrs.alloc(sym);
list = state.mem.newList(elem.first);
list = state.ctx.mem.newList(elem.first);
elem.second = list.listElems();
}
v.mkAttrs(attrs.alreadySorted());
@ -1979,11 +1979,11 @@ static void prim_zipAttrsWith(EvalState & state, const PosIdx pos, Value * * arg
}
for (auto & attr : *v.attrs) {
auto name = state.mem.allocValue();
auto name = state.ctx.mem.allocValue();
name->mkString(state.ctx.symbols[attr.name]);
auto call1 = state.mem.allocValue();
auto call1 = state.ctx.mem.allocValue();
call1->mkApp(args[0], name);
auto call2 = state.mem.allocValue();
auto call2 = state.ctx.mem.allocValue();
call2->mkApp(call1, attr.value);
attr.value = call2;
}
@ -2036,7 +2036,7 @@ static void prim_tail(EvalState & state, const PosIdx pos, Value * * args, Value
if (args[0]->listSize() == 0)
state.ctx.errors.make<EvalError>("'tail' called on an empty list").atPos(pos).debugThrow();
v = state.mem.newList(args[0]->listSize() - 1);
v = state.ctx.mem.newList(args[0]->listSize() - 1);
for (unsigned int n = 0; n < v.listSize(); ++n)
v.listElems()[n] = args[0]->listElems()[n + 1];
}
@ -2053,9 +2053,9 @@ static void prim_map(EvalState & state, const PosIdx pos, Value * * args, Value
state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.map");
v = state.mem.newList(args[1]->listSize());
v = state.ctx.mem.newList(args[1]->listSize());
for (unsigned int n = 0; n < v.listSize(); ++n)
(v.listElems()[n] = state.mem.allocValue())->mkApp(
(v.listElems()[n] = state.ctx.mem.allocValue())->mkApp(
args[0], args[1]->listElems()[n]);
}
@ -2089,7 +2089,7 @@ static void prim_filter(EvalState & state, const PosIdx pos, Value * * args, Val
if (same)
v = *args[1];
else {
v = state.mem.newList(k);
v = state.ctx.mem.newList(k);
for (unsigned int n = 0; n < k; ++n) v.listElems()[n] = vs[n];
}
}
@ -2133,7 +2133,7 @@ static void prim_foldlStrict(EvalState & state, const PosIdx pos, Value * * args
for (auto [n, elem] : enumerate(args[2]->listItems())) {
Value * vs []{vCur, elem};
vCur = n == args[2]->listSize() - 1 ? &v : state.mem.allocValue();
vCur = n == args[2]->listSize() - 1 ? &v : state.ctx.mem.allocValue();
state.callFunction(*args[0], 2, vs, *vCur, pos);
}
state.forceValue(v, pos);
@ -2189,11 +2189,11 @@ static void prim_genList(EvalState & state, const PosIdx pos, Value * * args, Va
// as evaluating map without accessing any values makes little sense.
state.forceFunction(*args[0], noPos, "while evaluating the first argument passed to builtins.genList");
v = state.mem.newList(len);
v = state.ctx.mem.newList(len);
for (size_t n = 0; n < len; ++n) {
auto arg = state.mem.allocValue();
auto arg = state.ctx.mem.allocValue();
arg->mkInt(n);
(v.listElems()[n] = state.mem.allocValue())->mkApp(args[0], arg);
(v.listElems()[n] = state.ctx.mem.allocValue())->mkApp(args[0], arg);
}
}
@ -2212,7 +2212,7 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value
state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.sort");
v = state.mem.newList(len);
v = state.ctx.mem.newList(len);
for (unsigned int n = 0; n < len; ++n) {
state.forceValue(*args[1]->listElems()[n], pos);
v.listElems()[n] = args[1]->listElems()[n];
@ -2261,17 +2261,17 @@ static void prim_partition(EvalState & state, const PosIdx pos, Value * * args,
wrong.push_back(vElem);
}
auto attrs = state.buildBindings(2);
auto attrs = state.ctx.buildBindings(2);
auto & vRight = attrs.alloc(state.ctx.s.right);
auto rsize = right.size();
vRight = state.mem.newList(rsize);
vRight = state.ctx.mem.newList(rsize);
if (rsize)
memcpy(vRight.listElems(), right.data(), sizeof(Value *) * rsize);
auto & vWrong = attrs.alloc(state.ctx.s.wrong);
auto wsize = wrong.size();
vWrong = state.mem.newList(wsize);
vWrong = state.ctx.mem.newList(wsize);
if (wsize)
memcpy(vWrong.listElems(), wrong.data(), sizeof(Value *) * wsize);
@ -2294,12 +2294,12 @@ static void prim_groupBy(EvalState & state, const PosIdx pos, Value * * args, Va
vector->second.push_back(vElem);
}
auto attrs2 = state.buildBindings(attrs.size());
auto attrs2 = state.ctx.buildBindings(attrs.size());
for (auto & i : attrs) {
auto & list = attrs2.alloc(i.first);
auto size = i.second.size();
list = state.mem.newList(size);
list = state.ctx.mem.newList(size);
memcpy(list.listElems(), i.second.data(), sizeof(Value *) * size);
}
@ -2323,7 +2323,7 @@ static void prim_concatMap(EvalState & state, const PosIdx pos, Value * * args,
len += lists[n].listSize();
}
v = state.mem.newList(len);
v = state.ctx.mem.newList(len);
auto out = v.listElems();
for (unsigned int n = 0, pos = 0; n < nrLists; ++n) {
auto l = lists[n].listSize();
@ -2575,12 +2575,12 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
// the first match is the whole string
const size_t len = match.size() - 1;
v = state.mem.newList(len);
v = state.ctx.mem.newList(len);
for (size_t i = 0; i < len; ++i) {
if (!match[i+1].matched)
(v.listElems()[i] = state.mem.allocValue())->mkNull();
(v.listElems()[i] = state.ctx.mem.allocValue())->mkNull();
else
(v.listElems()[i] = state.mem.allocValue())->mkString(match[i + 1].str());
(v.listElems()[i] = state.ctx.mem.allocValue())->mkString(match[i + 1].str());
}
} catch (std::regex_error & e) {
@ -2614,7 +2614,7 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
// Any matches results are surrounded by non-matching results.
const size_t len = std::distance(begin, end);
v = state.mem.newList(2 * len + 1);
v = state.ctx.mem.newList(2 * len + 1);
size_t idx = 0;
if (len == 0) {
@ -2627,24 +2627,24 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
auto match = *i;
// Add a string for non-matched characters.
(v.listElems()[idx++] = state.mem.allocValue())->mkString(match.prefix().str());
(v.listElems()[idx++] = state.ctx.mem.allocValue())->mkString(match.prefix().str());
// Add a list for matched substrings.
const size_t slen = match.size() - 1;
auto elem = v.listElems()[idx++] = state.mem.allocValue();
auto elem = v.listElems()[idx++] = state.ctx.mem.allocValue();
// Start at 1, beacause the first match is the whole string.
*elem = state.mem.newList(slen);
*elem = state.ctx.mem.newList(slen);
for (size_t si = 0; si < slen; ++si) {
if (!match[si + 1].matched)
(elem->listElems()[si] = state.mem.allocValue())->mkNull();
(elem->listElems()[si] = state.ctx.mem.allocValue())->mkNull();
else
(elem->listElems()[si] = state.mem.allocValue())->mkString(match[si + 1].str());
(elem->listElems()[si] = state.ctx.mem.allocValue())->mkString(match[si + 1].str());
}
// Add a string for non-matched suffix characters.
if (idx == 2 * len)
(v.listElems()[idx++] = state.mem.allocValue())->mkString(match.suffix().str());
(v.listElems()[idx++] = state.ctx.mem.allocValue())->mkString(match.suffix().str());
}
assert(idx == 2 * len + 1);
@ -2749,7 +2749,7 @@ static void prim_parseDrvName(EvalState & state, const PosIdx pos, Value * * arg
{
auto name = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.parseDrvName");
DrvName parsed(name);
auto attrs = state.buildBindings(2);
auto attrs = state.ctx.buildBindings(2);
attrs.alloc(state.ctx.s.name).mkString(parsed.name);
attrs.alloc("version").mkString(parsed.version);
v.mkAttrs(attrs);
@ -2774,9 +2774,9 @@ static void prim_splitVersion(EvalState & state, const PosIdx pos, Value * * arg
break;
components.emplace_back(component);
}
v = state.mem.newList(components.size());
v = state.ctx.mem.newList(components.size());
for (const auto & [n, component] : enumerate(components))
(v.listElems()[n] = state.mem.allocValue())->mkString(std::move(component));
(v.listElems()[n] = state.ctx.mem.allocValue())->mkString(std::move(component));
}

View file

@ -138,20 +138,20 @@ void prim_getContext(EvalState & state, const PosIdx pos, Value * * args, Value
}, ((NixStringContextElem &&) i).raw);
}
auto attrs = state.buildBindings(contextInfos.size());
auto attrs = state.ctx.buildBindings(contextInfos.size());
auto sAllOutputs = state.ctx.symbols.create("allOutputs");
for (const auto & info : contextInfos) {
auto infoAttrs = state.buildBindings(3);
auto infoAttrs = state.ctx.buildBindings(3);
if (info.second.path)
infoAttrs.alloc(state.ctx.s.path).mkBool(true);
if (info.second.allOutputs)
infoAttrs.alloc(sAllOutputs).mkBool(true);
if (!info.second.outputs.empty()) {
auto & outputsVal = infoAttrs.alloc(state.ctx.s.outputs);
outputsVal = state.mem.newList(info.second.outputs.size());
outputsVal = state.ctx.mem.newList(info.second.outputs.size());
for (const auto & [i, output] : enumerate(info.second.outputs))
(outputsVal.listElems()[i] = state.mem.allocValue())->mkString(output);
(outputsVal.listElems()[i] = state.ctx.mem.allocValue())->mkString(output);
}
attrs.alloc(state.ctx.store->printStorePath(info.first)).mkAttrs(infoAttrs);
}

View file

@ -67,7 +67,7 @@ static void prim_fetchMercurial(EvalState & state, const PosIdx pos, Value * * a
// FIXME: use name
auto [tree, input2] = input.fetch(state.ctx.store);
auto attrs2 = state.buildBindings(8);
auto attrs2 = state.ctx.buildBindings(8);
state.paths.mkStorePathString(tree.storePath, attrs2.alloc(state.ctx.s.outPath));
if (input2.getRef())
attrs2.alloc("branch").mkString(*input2.getRef());

View file

@ -26,7 +26,7 @@ void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, Value &
size_t size = 0;
for (auto & i : table) { (void) i; size++; }
auto attrs = state.buildBindings(size);
auto attrs = state.ctx.buildBindings(size);
for(auto & elem : table)
visit(attrs.alloc(elem.first), elem.second);
@ -39,9 +39,9 @@ void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, Value &
auto array = toml::get<std::vector<toml::value>>(t);
size_t size = array.size();
v = state.mem.newList(size);
v = state.ctx.mem.newList(size);
for (size_t i = 0; i < size; ++i)
visit(*(v.listElems()[i] = state.mem.allocValue()), array[i]);
visit(*(v.listElems()[i] = state.ctx.mem.allocValue()), array[i]);
}
break;;
case toml::value_t::boolean:
@ -62,7 +62,7 @@ void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, Value &
case toml::value_t::local_time:
{
if (experimentalFeatureSettings.isEnabled(Xp::ParseTomlTimestamps)) {
auto attrs = state.buildBindings(2);
auto attrs = state.ctx.buildBindings(2);
attrs.alloc("_type").mkString("timestamp");
std::ostringstream s;
s << t;