mirror of
https://github.com/NixOS/nix
synced 2025-06-26 11:41:15 +02:00
Merge pull request #11866 from DeterminateSystems/callFunction-span
callFunction: Use std::span
This commit is contained in:
commit
61d075840f
5 changed files with 23 additions and 28 deletions
|
@ -67,7 +67,7 @@ nix_err nix_value_call_multi(nix_c_context * context, EvalState * state, nix_val
|
||||||
if (context)
|
if (context)
|
||||||
context->last_err_code = NIX_OK;
|
context->last_err_code = NIX_OK;
|
||||||
try {
|
try {
|
||||||
state->state.callFunction(fn->value, nargs, (nix::Value * *)args, value->value, nix::noPos);
|
state->state.callFunction(fn->value, {(nix::Value * *) args, nargs}, value->value, nix::noPos);
|
||||||
state->state.forceValue(value->value, nix::noPos);
|
state->state.forceValue(value->value, nix::noPos);
|
||||||
}
|
}
|
||||||
NIXC_CATCH_ERRS
|
NIXC_CATCH_ERRS
|
||||||
|
|
|
@ -588,14 +588,14 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
|
||||||
if (isFunctor(v)) {
|
if (isFunctor(v)) {
|
||||||
try {
|
try {
|
||||||
Value & functor = *v.attrs()->find(sFunctor)->value;
|
Value & functor = *v.attrs()->find(sFunctor)->value;
|
||||||
Value * vp = &v;
|
Value * vp[] = {&v};
|
||||||
Value partiallyApplied;
|
Value partiallyApplied;
|
||||||
// The first paramater is not user-provided, and may be
|
// The first paramater is not user-provided, and may be
|
||||||
// handled by code that is opaque to the user, like lib.const = x: y: y;
|
// handled by code that is opaque to the user, like lib.const = x: y: y;
|
||||||
// So preferably we show docs that are relevant to the
|
// So preferably we show docs that are relevant to the
|
||||||
// "partially applied" function returned by e.g. `const`.
|
// "partially applied" function returned by e.g. `const`.
|
||||||
// We apply the first argument:
|
// We apply the first argument:
|
||||||
callFunction(functor, 1, &vp, partiallyApplied, noPos);
|
callFunction(functor, vp, partiallyApplied, noPos);
|
||||||
auto _level = addCallDepth(noPos);
|
auto _level = addCallDepth(noPos);
|
||||||
return getDoc(partiallyApplied);
|
return getDoc(partiallyApplied);
|
||||||
}
|
}
|
||||||
|
@ -1460,7 +1460,7 @@ void ExprLambda::eval(EvalState & state, Env & env, Value & v)
|
||||||
v.mkLambda(&env, this);
|
v.mkLambda(&env, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value & vRes, const PosIdx pos)
|
void EvalState::callFunction(Value & fun, std::span<Value *> args, Value & vRes, const PosIdx pos)
|
||||||
{
|
{
|
||||||
auto _level = addCallDepth(pos);
|
auto _level = addCallDepth(pos);
|
||||||
|
|
||||||
|
@ -1475,16 +1475,16 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
auto makeAppChain = [&]()
|
auto makeAppChain = [&]()
|
||||||
{
|
{
|
||||||
vRes = vCur;
|
vRes = vCur;
|
||||||
for (size_t i = 0; i < nrArgs; ++i) {
|
for (auto arg : args) {
|
||||||
auto fun2 = allocValue();
|
auto fun2 = allocValue();
|
||||||
*fun2 = vRes;
|
*fun2 = vRes;
|
||||||
vRes.mkPrimOpApp(fun2, args[i]);
|
vRes.mkPrimOpApp(fun2, arg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const Attr * functor;
|
const Attr * functor;
|
||||||
|
|
||||||
while (nrArgs > 0) {
|
while (args.size() > 0) {
|
||||||
|
|
||||||
if (vCur.isLambda()) {
|
if (vCur.isLambda()) {
|
||||||
|
|
||||||
|
@ -1587,15 +1587,14 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
nrArgs--;
|
args = args.subspan(1);
|
||||||
args += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (vCur.isPrimOp()) {
|
else if (vCur.isPrimOp()) {
|
||||||
|
|
||||||
size_t argsLeft = vCur.primOp()->arity;
|
size_t argsLeft = vCur.primOp()->arity;
|
||||||
|
|
||||||
if (nrArgs < argsLeft) {
|
if (args.size() < argsLeft) {
|
||||||
/* We don't have enough arguments, so create a tPrimOpApp chain. */
|
/* We don't have enough arguments, so create a tPrimOpApp chain. */
|
||||||
makeAppChain();
|
makeAppChain();
|
||||||
return;
|
return;
|
||||||
|
@ -1607,15 +1606,14 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
if (countCalls) primOpCalls[fn->name]++;
|
if (countCalls) primOpCalls[fn->name]++;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fn->fun(*this, vCur.determinePos(noPos), args, vCur);
|
fn->fun(*this, vCur.determinePos(noPos), args.data(), vCur);
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
if (fn->addTrace)
|
if (fn->addTrace)
|
||||||
addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name);
|
addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
nrArgs -= argsLeft;
|
args = args.subspan(argsLeft);
|
||||||
args += argsLeft;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1631,7 +1629,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
auto arity = primOp->primOp()->arity;
|
auto arity = primOp->primOp()->arity;
|
||||||
auto argsLeft = arity - argsDone;
|
auto argsLeft = arity - argsDone;
|
||||||
|
|
||||||
if (nrArgs < argsLeft) {
|
if (args.size() < argsLeft) {
|
||||||
/* We still don't have enough arguments, so extend the tPrimOpApp chain. */
|
/* We still don't have enough arguments, so extend the tPrimOpApp chain. */
|
||||||
makeAppChain();
|
makeAppChain();
|
||||||
return;
|
return;
|
||||||
|
@ -1663,8 +1661,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
nrArgs -= argsLeft;
|
args = args.subspan(argsLeft);
|
||||||
args += argsLeft;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1675,13 +1672,12 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
|
||||||
Value * args2[] = {allocValue(), args[0]};
|
Value * args2[] = {allocValue(), args[0]};
|
||||||
*args2[0] = vCur;
|
*args2[0] = vCur;
|
||||||
try {
|
try {
|
||||||
callFunction(*functor->value, 2, args2, vCur, functor->pos);
|
callFunction(*functor->value, args2, vCur, functor->pos);
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace(positions[pos], "while calling a functor (an attribute set with a '__functor' attribute)");
|
e.addTrace(positions[pos], "while calling a functor (an attribute set with a '__functor' attribute)");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
nrArgs--;
|
args = args.subspan(1);
|
||||||
args++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
@ -1724,7 +1720,7 @@ void ExprCall::eval(EvalState & state, Env & env, Value & v)
|
||||||
for (size_t i = 0; i < args.size(); ++i)
|
for (size_t i = 0; i < args.size(); ++i)
|
||||||
vArgs[i] = args[i]->maybeThunk(state, env);
|
vArgs[i] = args[i]->maybeThunk(state, env);
|
||||||
|
|
||||||
state.callFunction(vFun, args.size(), vArgs.data(), v, pos);
|
state.callFunction(vFun, vArgs, v, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -690,13 +690,12 @@ public:
|
||||||
|
|
||||||
bool isFunctor(Value & fun);
|
bool isFunctor(Value & fun);
|
||||||
|
|
||||||
// FIXME: use std::span
|
void callFunction(Value & fun, std::span<Value *> args, Value & vRes, const PosIdx pos);
|
||||||
void callFunction(Value & fun, size_t nrArgs, Value * * args, Value & vRes, const PosIdx pos);
|
|
||||||
|
|
||||||
void callFunction(Value & fun, Value & arg, Value & vRes, const PosIdx pos)
|
void callFunction(Value & fun, Value & arg, Value & vRes, const PosIdx pos)
|
||||||
{
|
{
|
||||||
Value * args[] = {&arg};
|
Value * args[] = {&arg};
|
||||||
callFunction(fun, 1, args, vRes, pos);
|
callFunction(fun, args, vRes, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -724,7 +724,7 @@ static void prim_genericClosure(EvalState & state, const PosIdx pos, Value * * a
|
||||||
|
|
||||||
/* Call the `operator' function with `e' as argument. */
|
/* Call the `operator' function with `e' as argument. */
|
||||||
Value newElements;
|
Value newElements;
|
||||||
state.callFunction(*op->value, 1, &e, newElements, noPos);
|
state.callFunction(*op->value, {&e, 1}, newElements, noPos);
|
||||||
state.forceList(newElements, noPos, "while evaluating the return value of the `operator` passed to builtins.genericClosure");
|
state.forceList(newElements, noPos, "while evaluating the return value of the `operator` passed to builtins.genericClosure");
|
||||||
|
|
||||||
/* Add the values returned by the operator to the work set. */
|
/* Add the values returned by the operator to the work set. */
|
||||||
|
@ -2450,7 +2450,7 @@ bool EvalState::callPathFilter(
|
||||||
// assert that type is not "unknown"
|
// assert that type is not "unknown"
|
||||||
Value * args []{&arg1, fileTypeToString(*this, st.type)};
|
Value * args []{&arg1, fileTypeToString(*this, st.type)};
|
||||||
Value res;
|
Value res;
|
||||||
callFunction(*filterFun, 2, args, res, pos);
|
callFunction(*filterFun, args, res, pos);
|
||||||
|
|
||||||
return forceBool(res, pos, "while evaluating the return value of the path filter function");
|
return forceBool(res, pos, "while evaluating the return value of the path filter function");
|
||||||
}
|
}
|
||||||
|
@ -3487,7 +3487,7 @@ static void prim_foldlStrict(EvalState & state, const PosIdx pos, Value * * args
|
||||||
for (auto [n, elem] : enumerate(args[2]->listItems())) {
|
for (auto [n, elem] : enumerate(args[2]->listItems())) {
|
||||||
Value * vs []{vCur, elem};
|
Value * vs []{vCur, elem};
|
||||||
vCur = n == args[2]->listSize() - 1 ? &v : state.allocValue();
|
vCur = n == args[2]->listSize() - 1 ? &v : state.allocValue();
|
||||||
state.callFunction(*args[0], 2, vs, *vCur, pos);
|
state.callFunction(*args[0], vs, *vCur, pos);
|
||||||
}
|
}
|
||||||
state.forceValue(v, pos);
|
state.forceValue(v, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -3637,7 +3637,7 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value
|
||||||
|
|
||||||
Value * vs[] = {a, b};
|
Value * vs[] = {a, b};
|
||||||
Value vBool;
|
Value vBool;
|
||||||
state.callFunction(*args[0], 2, vs, vBool, noPos);
|
state.callFunction(*args[0], vs, vBool, noPos);
|
||||||
return state.forceBool(vBool, pos, "while evaluating the return value of the sorting function passed to builtins.sort");
|
return state.forceBool(vBool, pos, "while evaluating the return value of the sorting function passed to builtins.sort");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -816,7 +816,7 @@ void callFlake(EvalState & state,
|
||||||
assert(vFetchFinalTree);
|
assert(vFetchFinalTree);
|
||||||
|
|
||||||
Value * args[] = {vLocks, &vOverrides, *vFetchFinalTree};
|
Value * args[] = {vLocks, &vOverrides, *vFetchFinalTree};
|
||||||
state.callFunction(*vCallFlake, 3, args, vRes, noPos);
|
state.callFunction(*vCallFlake, args, vRes, noPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initLib(const Settings & settings)
|
void initLib(const Settings & settings)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue