mirror of
https://github.com/NixOS/nix
synced 2025-06-25 14:51:16 +02:00
C API: add (un)initialized value checks
This commit is contained in:
parent
9d7dee4a8f
commit
ccad6e94e2
3 changed files with 128 additions and 24 deletions
|
@ -20,7 +20,7 @@
|
|||
# include "gc_cpp.h"
|
||||
#endif
|
||||
|
||||
// Helper function to throw an exception if value is null
|
||||
// Helper function to throw an exception if value is null or in an invalid state
|
||||
static const nix::Value & check_value_not_null(const Value * value)
|
||||
{
|
||||
if (!value) {
|
||||
|
@ -37,6 +37,20 @@ static nix::Value & check_value_not_null(Value * value)
|
|||
return *((nix::Value *) value);
|
||||
}
|
||||
|
||||
static void check_value_initialized(const nix::Value & value)
|
||||
{
|
||||
if (!value.isValid()) {
|
||||
throw std::runtime_error("Uninitialized Value");
|
||||
}
|
||||
}
|
||||
|
||||
static void check_value_uninitialized(const nix::Value & value)
|
||||
{
|
||||
if (value.isValid()) {
|
||||
throw std::runtime_error("Value already initialized. Variables are immutable");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to convert calls from nix into C API.
|
||||
*
|
||||
|
@ -112,6 +126,7 @@ ValueType nix_get_type(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
using namespace nix;
|
||||
switch (v.type()) {
|
||||
case nThunk:
|
||||
|
@ -148,6 +163,7 @@ const char * nix_get_typename(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
auto s = nix::showType(v);
|
||||
return strdup(s.c_str());
|
||||
}
|
||||
|
@ -160,6 +176,7 @@ bool nix_get_bool(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nBool);
|
||||
return v.boolean();
|
||||
}
|
||||
|
@ -172,6 +189,7 @@ nix_err nix_get_string(nix_c_context * context, const Value * value, nix_get_str
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nString);
|
||||
call_nix_get_string_callback(v.c_str(), callback, user_data);
|
||||
}
|
||||
|
@ -184,6 +202,7 @@ const char * nix_get_path_string(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nPath);
|
||||
// NOTE (from @yorickvP)
|
||||
// v._path.path should work but may not be how Eelco intended it.
|
||||
|
@ -203,6 +222,7 @@ unsigned int nix_get_list_size(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nList);
|
||||
return v.listSize();
|
||||
}
|
||||
|
@ -215,6 +235,7 @@ unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nAttrs);
|
||||
return v.attrs()->size();
|
||||
}
|
||||
|
@ -227,6 +248,7 @@ double nix_get_float(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nFloat);
|
||||
return v.fpoint();
|
||||
}
|
||||
|
@ -239,6 +261,7 @@ int64_t nix_get_int(nix_c_context * context, const Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nInt);
|
||||
return v.integer();
|
||||
}
|
||||
|
@ -251,6 +274,7 @@ ExternalValue * nix_get_external(nix_c_context * context, Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nExternal);
|
||||
return (ExternalValue *) v.external();
|
||||
}
|
||||
|
@ -263,6 +287,7 @@ Value * nix_get_list_byidx(nix_c_context * context, const Value * value, EvalSta
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nList);
|
||||
auto * p = v.listElems()[ix];
|
||||
nix_gc_incref(nullptr, p);
|
||||
|
@ -279,6 +304,7 @@ Value * nix_get_attr_byname(nix_c_context * context, const Value * value, EvalSt
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nAttrs);
|
||||
nix::Symbol s = state->state.symbols.create(name);
|
||||
auto attr = v.attrs()->get(s);
|
||||
|
@ -299,6 +325,7 @@ bool nix_has_attr_byname(nix_c_context * context, const Value * value, EvalState
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
assert(v.type() == nix::nAttrs);
|
||||
nix::Symbol s = state->state.symbols.create(name);
|
||||
auto attr = v.attrs()->get(s);
|
||||
|
@ -316,6 +343,7 @@ nix_get_attr_byidx(nix_c_context * context, const Value * value, EvalState * sta
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
const nix::Attr & a = (*v.attrs())[i];
|
||||
*name = ((const std::string &) (state->state.symbols[a.name])).c_str();
|
||||
nix_gc_incref(nullptr, a.value);
|
||||
|
@ -331,6 +359,7 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * valu
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
const nix::Attr & a = (*v.attrs())[i];
|
||||
return ((const std::string &) (state->state.symbols[a.name])).c_str();
|
||||
}
|
||||
|
@ -343,6 +372,7 @@ nix_err nix_init_bool(nix_c_context * context, Value * value, bool b)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkBool(b);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -355,6 +385,7 @@ nix_err nix_init_string(nix_c_context * context, Value * value, const char * str
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkString(std::string_view(str));
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -366,6 +397,7 @@ nix_err nix_init_path_string(nix_c_context * context, EvalState * s, Value * val
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkPath(s->state.rootPath(nix::CanonPath(str)));
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -377,6 +409,7 @@ nix_err nix_init_float(nix_c_context * context, Value * value, double d)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkFloat(d);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -388,6 +421,7 @@ nix_err nix_init_int(nix_c_context * context, Value * value, int64_t i)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkInt(i);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -399,6 +433,7 @@ nix_err nix_init_null(nix_c_context * context, Value * value)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkNull();
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -423,6 +458,7 @@ nix_err nix_init_external(nix_c_context * context, Value * value, ExternalValue
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
auto r = (nix::ExternalValueBase *) val;
|
||||
v.mkExternal(r);
|
||||
}
|
||||
|
@ -450,6 +486,7 @@ nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_buil
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & e = check_value_not_null(value);
|
||||
check_value_initialized(e);
|
||||
list_builder->builder[index] = &e;
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -470,6 +507,7 @@ nix_err nix_make_list(nix_c_context * context, ListBuilder * list_builder, Value
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkList(list_builder->builder);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -481,6 +519,7 @@ nix_err nix_init_primop(nix_c_context * context, Value * value, PrimOp * p)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkPrimOp((nix::PrimOp *) p);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -492,7 +531,9 @@ nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source)
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
auto & s = check_value_not_null(source);
|
||||
check_value_initialized(s);
|
||||
v = s;
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -504,6 +545,7 @@ nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder *
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_uninitialized(v);
|
||||
v.mkAttrs(b->builder);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
|
@ -530,6 +572,7 @@ nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * b
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
nix::Symbol s = bb->builder.state.symbols.create(name);
|
||||
bb->builder.insert(s, &v);
|
||||
}
|
||||
|
@ -551,6 +594,7 @@ nix_realised_string * nix_string_realise(nix_c_context * context, EvalState * st
|
|||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto & v = check_value_not_null(value);
|
||||
check_value_initialized(v);
|
||||
nix::NixStringContext stringContext;
|
||||
auto rawStr = state->state.coerceToString(nix::noPos, v, stringContext, "while realising a string").toOwned();
|
||||
nix::StorePathSet storePaths;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue