Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/libexpr-c/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,34 @@ PrimOp * nix_alloc_primop(
PrimOpFun fun,
int arity,
const char * name,
const char ** args,
const char * const * args,
const char * doc,
void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
try {
std::vector<std::string> argNames;
if (args)
for (size_t i = 0; args[i]; i++)
argNames.emplace_back(args[i]);
// `RegisterPrimOp` raises the arity to `args.size()`, which would
// desync it from the arity bound into `nix_c_primop_wrapper`.
if (!argNames.empty() && argNames.size() != (size_t) arity)
throw nix::Error(
"primop '%s' has arity %d, but %d argument names were given", name, arity, argNames.size());

using namespace std::placeholders;
auto p = new
#if NIX_USE_BOEHMGC
(GC)
#endif
nix::PrimOp{
.name = name,
.args = {},
.args = std::move(argNames),
.arity = (size_t) arity,
.doc = doc ? std::optional<std::string>{doc} : std::nullopt,
.impl = std::bind(nix_c_primop_wrapper, fun, user_data, arity, _1, _2, _3, _4)};
if (args)
for (size_t i = 0; args[i]; i++)
p->args.emplace_back(*args);
nix_gc_incref(nullptr, p);
return (PrimOp *) p;
}
Expand Down
5 changes: 3 additions & 2 deletions src/libexpr-c/nix_api_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ typedef void (*PrimOpFun)(
* @param[in] fun callback
* @param[in] arity expected number of function arguments
* @param[in] name function name
* @param[in] args array of argument names, NULL-terminated
* @param[in] args optional, NULL-terminated array of argument names, used for documentation.
* If non-empty, it must contain exactly `arity` names.
* @param[in] doc optional, documentation for this primop
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called
* @return primop, or null in case of errors
Expand All @@ -207,7 +208,7 @@ PrimOp * nix_alloc_primop(
PrimOpFun fun,
int arity,
const char * name,
const char ** args,
const char * const * args,
const char * doc,
void * user_data);

Expand Down
40 changes: 40 additions & 0 deletions src/libexpr-tests/nix_api_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "nix_api_expr.h"
#include "nix_api_value.h"

#include "nix/expr/eval.hh"
#include "nix/expr/tests/nix_api_expr.hh"
#include "nix/util/tests/string_callback.hh"
#include "nix/util/file-system.hh"
Expand Down Expand Up @@ -350,6 +351,45 @@ TEST_F(nix_api_expr_test, nix_alloc_primop_without_doc)
assert_ctx_ok();
}

TEST_F(nix_api_expr_test, nix_alloc_primop_arg_names)
{
const char * const args[] = {"s", "n", nullptr};
PrimOp * primop = nix_alloc_primop(ctx, primop_square, 2, "named", args, nullptr, nullptr);
assert_ctx_ok();
ASSERT_NE(nullptr, primop);
EXPECT_THAT(((nix::PrimOp *) primop)->args, testing::ElementsAre("s", "n"));

nix_gc_decref(ctx, primop);
assert_ctx_ok();
}

TEST_F(nix_api_expr_test, nix_alloc_primop_empty_arg_names)
{
const char * const args[] = {nullptr};
PrimOp * primop = nix_alloc_primop(ctx, primop_square, 1, "unnamed", args, nullptr, nullptr);
assert_ctx_ok();
ASSERT_NE(nullptr, primop);
EXPECT_THAT(((nix::PrimOp *) primop)->args, testing::IsEmpty());

nix_gc_decref(ctx, primop);
assert_ctx_ok();
}

TEST_F(nix_api_expr_test, nix_alloc_primop_arg_names_arity_mismatch)
{
const char * const tooMany[] = {"a", "b", nullptr};
ASSERT_EQ(nullptr, nix_alloc_primop(ctx, primop_square, 1, "tooMany", tooMany, nullptr, nullptr));
ASSERT_EQ(nix_err_code(ctx), NIX_ERR_NIX_ERROR);
ASSERT_THAT(nix_err_msg(nullptr, ctx, nullptr), testing::HasSubstr("argument names were given"));
ASSERT_THAT(nix_err_msg(nullptr, ctx, nullptr), testing::HasSubstr("tooMany"));

const char * const tooFew[] = {"a", nullptr};
ASSERT_EQ(nullptr, nix_alloc_primop(ctx, primop_square, 2, "tooFew", tooFew, nullptr, nullptr));
ASSERT_EQ(nix_err_code(ctx), NIX_ERR_NIX_ERROR);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never quite understood what use the error codes have an how they are converted (or rather how they design is supposed to work...). At least my understanding was that NIX_ERROR was supposed to represent evaluation errors (and this is more akin EINVAL which we don't have).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, because that part of the C API seems very undercooked in general. But I'm just wondering if I'm misunderstanding or if we really don't have any way to represent "invalid argument" errors.

ASSERT_THAT(nix_err_msg(nullptr, ctx, nullptr), testing::HasSubstr("argument names were given"));
ASSERT_THAT(nix_err_msg(nullptr, ctx, nullptr), testing::HasSubstr("tooFew"));
}

TEST_F(nix_api_expr_test, nix_expr_primop)
{
PrimOp * primop = nix_alloc_primop(
Expand Down
Loading