1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

C API: add a way to throw errors from primops

This commit is contained in:
Yorick van Pelt 2023-08-28 22:19:28 +02:00 committed by José Luis Lafuente
parent 3d79f38709
commit ab9250286a
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
5 changed files with 30 additions and 21 deletions

View file

@ -42,7 +42,12 @@ static nix::Value & check_value_not_null(Value * value)
static void nix_c_primop_wrapper(
PrimOpFun f, void * userdata, nix::EvalState & state, const nix::PosIdx pos, nix::Value ** args, nix::Value & v)
{
f(userdata, (State *) &state, *reinterpret_cast<const int *>(&pos), (Value **) args, (Value *) &v);
nix_c_context ctx;
f(userdata, &ctx, (State *) &state, (Value **) args, (Value *) &v);
/* TODO: In the future, this should throw different errors depending on the error code */
if (ctx.last_err_code != NIX_OK)
state.debugThrowLastTrace(nix::Error(
{.msg = nix::hintfmt("Error from builtin function: %s", *ctx.last_err), .errPos = state.positions[pos]}));
}
PrimOp * nix_alloc_primop(

View file

@ -65,15 +65,17 @@ typedef struct ExternalValue ExternalValue;
* @{
*/
/** @brief Function pointer for primops
* @param[in] user_data Arbitrary data, passed to nix_alloc_primop and stored.
* When you want to return an error, call nix_set_err_msg(context, NIX_ERR_UNKNOWN, "your error message here").
*
* @param[in] user_data Arbitrary data that was initially supplied to nix_alloc_primop
* @param[out] context Stores error information.
* @param[in] state Evaluator state
* @param[in] pos Call position, opaque index into the state's position table.
* @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before
* use.
* @param[out] ret return value
* @see nix_alloc_primop, nix_set_primop
*/
typedef void (*PrimOpFun)(void * user_data, State * state, int pos, Value ** args, Value * ret);
typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, State * state, Value ** args, Value * ret);
/** @brief Allocate a PrimOp
*
@ -86,7 +88,7 @@ typedef void (*PrimOpFun)(void * user_data, State * state, int pos, Value ** arg
* @param[in] name function name
* @param[in] args array of argument names, NULL-terminated
* @param[in] doc optional, documentation for this primop
* @param[in] user_data optional, arbitrary data, passed to the function when it's called
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called
* @return primop, or null in case of errors
* @see nix_set_primop
*/