C API: Value -> nix_value

This commit is contained in:
Robert Hensing 2024-06-13 18:18:36 +02:00
parent afdd12be5e
commit 0b56c98b1c
5 changed files with 29 additions and 5 deletions

View file

@ -36,7 +36,8 @@ typedef struct EvalState EvalState; // nix::EvalState
* @struct Value * @struct Value
* @see value_manip * @see value_manip
*/ */
typedef void Value; // nix::Value typedef struct nix_value nix_value;
[[deprecated("use nix_value instead")]] typedef nix_value Value;
// Function prototypes // Function prototypes
/** /**

View file

@ -20,6 +20,11 @@ struct ListBuilder
nix::ListBuilder builder; nix::ListBuilder builder;
}; };
struct nix_value
{
nix::Value value;
};
struct nix_string_return struct nix_string_return
{ {
std::string str; std::string str;

View file

@ -64,6 +64,11 @@ static nix::Value & check_value_out(Value * value)
return v; return v;
} }
static inline nix_value * as_nix_value_ptr(nix::Value * v)
{
return reinterpret_cast<nix_value *>(v);
}
/** /**
* Helper function to convert calls from nix into C API. * Helper function to convert calls from nix into C API.
* *
@ -159,7 +164,7 @@ Value * nix_alloc_value(nix_c_context * context, EvalState * state)
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
Value * res = state->state.allocValue(); nix_value * res = as_nix_value_ptr(state->state.allocValue());
nix_gc_incref(nullptr, res); nix_gc_incref(nullptr, res);
return res; return res;
} }
@ -345,7 +350,7 @@ Value * nix_get_attr_byname(nix_c_context * context, const Value * value, EvalSt
if (attr) { if (attr) {
nix_gc_incref(nullptr, attr->value); nix_gc_incref(nullptr, attr->value);
state->state.forceValue(*attr->value, nix::noPos); state->state.forceValue(*attr->value, nix::noPos);
return attr->value; return as_nix_value_ptr(attr->value);
} }
nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute");
return nullptr; return nullptr;
@ -380,7 +385,7 @@ nix_get_attr_byidx(nix_c_context * context, const Value * value, EvalState * sta
*name = ((const std::string &) (state->state.symbols[a.name])).c_str(); *name = ((const std::string &) (state->state.symbols[a.name])).c_str();
nix_gc_incref(nullptr, a.value); nix_gc_incref(nullptr, a.value);
state->state.forceValue(*a.value, nix::noPos); state->state.forceValue(*a.value, nix::noPos);
return a.value; return as_nix_value_ptr(a.value);
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }

View file

@ -35,8 +35,11 @@ typedef enum {
} ValueType; } ValueType;
// forward declarations // forward declarations
typedef void Value; typedef struct nix_value nix_value;
typedef struct EvalState EvalState; typedef struct EvalState EvalState;
[[deprecated("use nix_value instead")]] typedef nix_value Value;
// type defs // type defs
/** @brief Stores an under-construction set of bindings /** @brief Stores an under-construction set of bindings
* @ingroup value_manip * @ingroup value_manip

View file

@ -4,16 +4,26 @@
#include "nix_api_util_internal.h" #include "nix_api_util_internal.h"
#include "nix_api_expr.h" #include "nix_api_expr.h"
#include "nix_api_value.h" #include "nix_api_value.h"
#include "nix_api_expr_internal.h"
#include "tests/nix_api_expr.hh" #include "tests/nix_api_expr.hh"
#include "tests/string_callback.hh" #include "tests/string_callback.hh"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
TEST_F(nix_api_expr_test, as_nix_value_ptr)
{
// nix_alloc_value casts nix::Value to nix_value
// It should be obvious from the decl that that works, but if it doesn't,
// the whole implementation would be utterly broken.
ASSERT_EQ(sizeof(nix::Value), sizeof(nix_value));
}
TEST_F(nix_api_expr_test, nix_value_get_int_invalid) TEST_F(nix_api_expr_test, nix_value_get_int_invalid)
{ {
ASSERT_EQ(0, nix_get_int(ctx, nullptr)); ASSERT_EQ(0, nix_get_int(ctx, nullptr));