1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 00:11:17 +02:00

C API: add user_data argument to nix_alloc_primop

Also add a helper function for primops, that converts to C argument
types
(and eventually handles errors)
This commit is contained in:
Yorick van Pelt 2023-08-28 18:20:52 +02:00 committed by José Luis Lafuente
parent 48aa57549d
commit 3d79f38709
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
2 changed files with 38 additions and 8 deletions

View file

@ -34,18 +34,39 @@ static nix::Value & check_value_not_null(Value * value)
return *((nix::Value *) value);
}
/**
* Helper function to convert calls from nix into C API.
*
* Deals with errors and converts arguments from C++ into C types.
*/
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);
}
PrimOp * nix_alloc_primop(
nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc)
nix_c_context * context,
PrimOpFun fun,
int arity,
const char * name,
const char ** args,
const char * doc,
void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto fun2 = (nix::PrimOpFun) fun;
auto p = new
#ifdef HAVE_BOEHMGC
(GC)
#endif
nix::PrimOp{.name = name, .args = {}, .arity = (size_t) arity, .doc = doc, .fun = fun2};
nix::PrimOp{
.name = name,
.args = {},
.arity = (size_t) arity,
.doc = doc,
.fun = std::bind_front(nix_c_primop_wrapper, fun, user_data)};
if (args)
for (size_t i = 0; args[i]; i++)
p->args.emplace_back(*args);