1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-26 15:51:15 +02:00

C API: reformat according to proposed clang-format file

This commit is contained in:
Yorick van Pelt 2023-08-28 16:45:02 +02:00 committed by José Luis Lafuente
parent 91e53de7d3
commit e1bb799da9
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
13 changed files with 1115 additions and 1078 deletions

View file

@ -21,147 +21,158 @@
#include "gc_cpp.h" #include "gc_cpp.h"
#endif #endif
nix_err nix_libexpr_init(nix_c_context *context) { nix_err nix_libexpr_init(nix_c_context * context)
if (context) {
context->last_err_code = NIX_OK; if (context)
{ context->last_err_code = NIX_OK;
auto ret = nix_libutil_init(context); {
if (ret != NIX_OK) auto ret = nix_libutil_init(context);
return ret; if (ret != NIX_OK)
} return ret;
{ }
auto ret = nix_libstore_init(context); {
if (ret != NIX_OK) auto ret = nix_libstore_init(context);
return ret; if (ret != NIX_OK)
} return ret;
try { }
nix::initGC(); try {
} nix::initGC();
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, nix_err
const char *expr, const char *path, nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value)
Value *value) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
nix::Expr *parsedExpr = state->state.parseExprFromString( nix::Expr * parsedExpr = state->state.parseExprFromString(expr, state->state.rootPath(nix::CanonPath(path)));
expr, state->state.rootPath(nix::CanonPath(path))); state->state.eval(parsedExpr, *(nix::Value *) value);
state->state.eval(parsedExpr, *(nix::Value *)value); state->state.forceValue(*(nix::Value *) value, nix::noPos);
state->state.forceValue(*(nix::Value *)value, nix::noPos); }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value)
Value *arg, Value *value) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
state->state.callFunction(*(nix::Value *)fn, *(nix::Value *)arg, state->state.callFunction(*(nix::Value *) fn, *(nix::Value *) arg, *(nix::Value *) value, nix::noPos);
*(nix::Value *)value, nix::noPos); state->state.forceValue(*(nix::Value *) value, nix::noPos);
state->state.forceValue(*(nix::Value *)value, nix::noPos); }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
nix_err nix_value_force(nix_c_context *context, State *state, Value *value) { nix_err nix_value_force(nix_c_context * context, State * state, Value * value)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
state->state.forceValue(*(nix::Value *)value, nix::noPos); try {
} state->state.forceValue(*(nix::Value *) value, nix::noPos);
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_value_force_deep(nix_c_context *context, State *state, nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value)
Value *value) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
state->state.forceValueDeep(*(nix::Value *)value); state->state.forceValueDeep(*(nix::Value *) value);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
State *nix_state_create(nix_c_context *context, const char **searchPath_c, State * nix_state_create(nix_c_context * context, const char ** searchPath_c, Store * store)
Store *store) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
nix::Strings searchPath; nix::Strings searchPath;
if (searchPath_c != nullptr) if (searchPath_c != nullptr)
for (size_t i = 0; searchPath_c[i] != nullptr; i++) for (size_t i = 0; searchPath_c[i] != nullptr; i++)
searchPath.push_back(searchPath_c[i]); searchPath.push_back(searchPath_c[i]);
return new State{ return new State{nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)};
nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; }
} NIXC_CATCH_ERRS_NULL
NIXC_CATCH_ERRS_NULL
} }
void nix_state_free(State *state) { delete state; } void nix_state_free(State * state)
{
delete state;
}
#ifdef HAVE_BOEHMGC #ifdef HAVE_BOEHMGC
std::unordered_map< std::unordered_map<
const void *, unsigned int, std::hash<const void *>, const void *,
unsigned int,
std::hash<const void *>,
std::equal_to<const void *>, std::equal_to<const void *>,
traceable_allocator<std::pair<const void *const, unsigned int>>> traceable_allocator<std::pair<const void * const, unsigned int>>>
nix_refcounts; nix_refcounts;
std::mutex nix_refcount_lock; std::mutex nix_refcount_lock;
nix_err nix_gc_incref(nix_c_context *context, const void *p) { nix_err nix_gc_incref(nix_c_context * context, const void * p)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
std::scoped_lock lock(nix_refcount_lock); try {
auto f = nix_refcounts.find(p); std::scoped_lock lock(nix_refcount_lock);
if (f != nix_refcounts.end()) { auto f = nix_refcounts.find(p);
f->second++; if (f != nix_refcounts.end()) {
} else { f->second++;
nix_refcounts[p] = 1; } else {
nix_refcounts[p] = 1;
}
} }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
nix_err nix_gc_decref(nix_c_context *context, const void *p) { nix_err nix_gc_decref(nix_c_context * context, const void * p)
{
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
std::scoped_lock lock(nix_refcount_lock); std::scoped_lock lock(nix_refcount_lock);
auto f = nix_refcounts.find(p); auto f = nix_refcounts.find(p);
if (f != nix_refcounts.end()) { if (f != nix_refcounts.end()) {
if (--f->second == 0) if (--f->second == 0)
nix_refcounts.erase(f); nix_refcounts.erase(f);
} else } else
throw std::runtime_error("nix_gc_decref: object was not referenced"); throw std::runtime_error("nix_gc_decref: object was not referenced");
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
void nix_gc_now() { GC_gcollect(); } void nix_gc_now()
{
GC_gcollect();
}
#else #else
void nix_gc_incref(nix_c_context *context, const void *) { void nix_gc_incref(nix_c_context * context, const void *)
if (context) {
context->last_err_code = NIX_OK; if (context)
return NIX_OK; context->last_err_code = NIX_OK;
return NIX_OK;
} }
void nix_gc_decref(nix_c_context *context, const void *) { void nix_gc_decref(nix_c_context * context, const void *)
if (context) {
context->last_err_code = NIX_OK; if (context)
return NIX_OK; context->last_err_code = NIX_OK;
return NIX_OK;
} }
void nix_gc_now() {} void nix_gc_now() {}
#endif #endif
void nix_gc_register_finalizer(void *obj, void *cd, void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd))
void (*finalizer)(void *obj, void *cd)) { {
#ifdef HAVE_BOEHMGC #ifdef HAVE_BOEHMGC
GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0);
#endif #endif
} }

View file

@ -66,7 +66,7 @@ typedef void Value; // nix::Value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @return NIX_OK if the initialization was successful, an error code otherwise. * @return NIX_OK if the initialization was successful, an error code otherwise.
*/ */
nix_err nix_libexpr_init(nix_c_context *context); nix_err nix_libexpr_init(nix_c_context * context);
/** /**
* @brief Parses and evaluates a Nix expression from a string. * @brief Parses and evaluates a Nix expression from a string.
@ -75,14 +75,14 @@ nix_err nix_libexpr_init(nix_c_context *context);
* @param[in] state The state of the evaluation. * @param[in] state The state of the evaluation.
* @param[in] expr The Nix expression to parse. * @param[in] expr The Nix expression to parse.
* @param[in] path The file path to associate with the expression. * @param[in] path The file path to associate with the expression.
* This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given directory. * This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given
* directory.
* @param[out] value The result of the evaluation. You should allocate this * @param[out] value The result of the evaluation. You should allocate this
* yourself. * yourself.
* @return NIX_OK if the evaluation was successful, an error code otherwise. * @return NIX_OK if the evaluation was successful, an error code otherwise.
*/ */
nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, nix_err
const char *expr, const char *path, nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value);
Value *value);
/** /**
* @brief Calls a Nix function with an argument. * @brief Calls a Nix function with an argument.
@ -94,8 +94,7 @@ nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
* @param[out] value The result of the function call. * @param[out] value The result of the function call.
* @return NIX_OK if the function call was successful, an error code otherwise. * @return NIX_OK if the function call was successful, an error code otherwise.
*/ */
nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value);
Value *arg, Value *value);
/** /**
* @brief Forces the evaluation of a Nix value. * @brief Forces the evaluation of a Nix value.
@ -117,7 +116,7 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn,
* @return NIX_OK if the force operation was successful, an error code * @return NIX_OK if the force operation was successful, an error code
* otherwise. * otherwise.
*/ */
nix_err nix_value_force(nix_c_context *context, State *state, Value *value); nix_err nix_value_force(nix_c_context * context, State * state, Value * value);
/** /**
* @brief Forces the deep evaluation of a Nix value. * @brief Forces the deep evaluation of a Nix value.
@ -133,8 +132,7 @@ nix_err nix_value_force(nix_c_context *context, State *state, Value *value);
* @return NIX_OK if the deep force operation was successful, an error code * @return NIX_OK if the deep force operation was successful, an error code
* otherwise. * otherwise.
*/ */
nix_err nix_value_force_deep(nix_c_context *context, State *state, nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value);
Value *value);
/** /**
* @brief Create a new Nix language evaluator state. * @brief Create a new Nix language evaluator state.
@ -144,8 +142,7 @@ nix_err nix_value_force_deep(nix_c_context *context, State *state,
* @param[in] store The Nix store to use. * @param[in] store The Nix store to use.
* @return A new Nix state or NULL on failure. * @return A new Nix state or NULL on failure.
*/ */
State *nix_state_create(nix_c_context *context, const char **searchPath, State * nix_state_create(nix_c_context * context, const char ** searchPath, Store * store);
Store *store);
/** /**
* @brief Frees a Nix state. * @brief Frees a Nix state.
@ -154,7 +151,7 @@ State *nix_state_create(nix_c_context *context, const char **searchPath,
* *
* @param[in] state The state to free. * @param[in] state The state to free.
*/ */
void nix_state_free(State *state); void nix_state_free(State * state);
/** @addtogroup GC /** @addtogroup GC
* @brief Reference counting and garbage collector operations * @brief Reference counting and garbage collector operations
@ -178,14 +175,14 @@ void nix_state_free(State *state);
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] object The object to keep alive * @param[in] object The object to keep alive
*/ */
nix_err nix_gc_incref(nix_c_context *context, const void *object); nix_err nix_gc_incref(nix_c_context * context, const void * object);
/** /**
* @brief Decrement the garbage collector reference counter for the given object * @brief Decrement the garbage collector reference counter for the given object
* *
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] object The object to stop referencing * @param[in] object The object to stop referencing
*/ */
nix_err nix_gc_decref(nix_c_context *context, const void *object); nix_err nix_gc_decref(nix_c_context * context, const void * object);
/** /**
* @brief Trigger the garbage collector manually * @brief Trigger the garbage collector manually
@ -203,8 +200,7 @@ void nix_gc_now();
* @param[in] cd the data to pass to the finalizer * @param[in] cd the data to pass to the finalizer
* @param[in] finalizer the callback function, called with obj and cd * @param[in] finalizer the callback function, called with obj and cd
*/ */
void nix_gc_register_finalizer(void *obj, void *cd, void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd));
void (*finalizer)(void *obj, void *cd));
/** @} */ /** @} */
// cffi end // cffi end

View file

@ -4,12 +4,14 @@
#include "eval.hh" #include "eval.hh"
#include "attr-set.hh" #include "attr-set.hh"
struct State { struct State
nix::EvalState state; {
nix::EvalState state;
}; };
struct BindingsBuilder { struct BindingsBuilder
nix::BindingsBuilder builder; {
nix::BindingsBuilder builder;
}; };
#endif // NIX_API_EXPR_INTERNAL_H #endif // NIX_API_EXPR_INTERNAL_H

View file

@ -20,178 +20,189 @@
#include "gc_cpp.h" #include "gc_cpp.h"
#endif #endif
struct nix_string_return { struct nix_string_return
std::string str; {
std::string str;
}; };
struct nix_printer { struct nix_printer
std::ostream &s; {
std::ostream & s;
}; };
struct nix_string_context { struct nix_string_context
nix::NixStringContext &ctx; {
nix::NixStringContext & ctx;
}; };
void nix_set_string_return(nix_string_return *str, const char *c) { void nix_set_string_return(nix_string_return * str, const char * c)
str->str = c; {
str->str = c;
} }
nix_err nix_external_print(nix_c_context *context, nix_printer *printer, nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * c)
const char *c) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
printer->s << c; printer->s << c;
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_external_add_string_context(nix_c_context *context, nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * ctx, const char * c)
nix_string_context *ctx, {
const char *c) { if (context)
if (context) context->last_err_code = NIX_OK;
context->last_err_code = NIX_OK; try {
try { auto r = nix::NixStringContextElem::parse(c);
auto r = nix::NixStringContextElem::parse(c); ctx->ctx.insert(r);
ctx->ctx.insert(r); }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
class NixCExternalValue : public nix::ExternalValueBase { class NixCExternalValue : public nix::ExternalValueBase
NixCExternalValueDesc &desc; {
void *v; NixCExternalValueDesc & desc;
void * v;
public: public:
NixCExternalValue(NixCExternalValueDesc &desc, void *v) : desc(desc), v(v){}; NixCExternalValue(NixCExternalValueDesc & desc, void * v)
void *get_ptr() { return v; } : desc(desc)
/** , v(v){};
* Print out the value void * get_ptr()
*/ {
virtual std::ostream &print(std::ostream &str) const override { return v;
nix_printer p{str};
desc.print(v, &p);
return str;
}
/**
* Return a simple string describing the type
*/
virtual std::string showType() const override {
nix_string_return res;
desc.showType(v, &res);
return std::move(res.str);
}
/**
* Return a string to be used in builtins.typeOf
*/
virtual std::string typeOf() const override {
nix_string_return res;
desc.typeOf(v, &res);
return std::move(res.str);
}
/**
* Coerce the value to a string.
*/
virtual std::string coerceToString(const nix::Pos &pos,
nix::NixStringContext &context,
bool copyMore,
bool copyToStore) const override {
if (!desc.coerceToString) {
return nix::ExternalValueBase::coerceToString(pos, context, copyMore,
copyToStore);
} }
nix_string_context ctx{context}; /**
nix_string_return res{""}; * Print out the value
// todo: pos, errors */
desc.coerceToString(v, &ctx, copyMore, copyToStore, &res); virtual std::ostream & print(std::ostream & str) const override
if (res.str.empty()) { {
return nix::ExternalValueBase::coerceToString(pos, context, copyMore, nix_printer p{str};
copyToStore); desc.print(v, &p);
return str;
} }
return std::move(res.str);
}
/** /**
* Compare to another value of the same type. * Return a simple string describing the type
*/ */
virtual bool operator==(const ExternalValueBase &b) const override { virtual std::string showType() const override
if (!desc.equal) { {
return false; nix_string_return res;
desc.showType(v, &res);
return std::move(res.str);
} }
auto r = dynamic_cast<const NixCExternalValue *>(&b);
if (!r)
return false;
return desc.equal(v, r->v);
}
/** /**
* Print the value as JSON. * Return a string to be used in builtins.typeOf
*/ */
virtual nlohmann::json virtual std::string typeOf() const override
printValueAsJSON(nix::EvalState &state, bool strict, {
nix::NixStringContext &context, nix_string_return res;
bool copyToStore = true) const override { desc.typeOf(v, &res);
if (!desc.printValueAsJSON) { return std::move(res.str);
return nix::ExternalValueBase::printValueAsJSON(state, strict, context,
copyToStore);
} }
nix_string_context ctx{context};
nix_string_return res{""};
desc.printValueAsJSON(v, (State *)&state, strict, &ctx, copyToStore, &res);
if (res.str.empty()) {
return nix::ExternalValueBase::printValueAsJSON(state, strict, context,
copyToStore);
}
return nlohmann::json::parse(res.str);
}
/** /**
* Print the value as XML. * Coerce the value to a string.
*/ */
virtual void printValueAsXML(nix::EvalState &state, bool strict, virtual std::string coerceToString(
bool location, nix::XMLWriter &doc, const nix::Pos & pos, nix::NixStringContext & context, bool copyMore, bool copyToStore) const override
nix::NixStringContext &context, {
nix::PathSet &drvsSeen, if (!desc.coerceToString) {
const nix::PosIdx pos) const override { return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore);
if (!desc.printValueAsXML) { }
return nix::ExternalValueBase::printValueAsXML( nix_string_context ctx{context};
state, strict, location, doc, context, drvsSeen, pos); nix_string_return res{""};
// todo: pos, errors
desc.coerceToString(v, &ctx, copyMore, copyToStore, &res);
if (res.str.empty()) {
return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore);
}
return std::move(res.str);
} }
nix_string_context ctx{context};
desc.printValueAsXML(v, (State *)&state, strict, location, &doc, &ctx,
&drvsSeen, *reinterpret_cast<const uint32_t *>(&pos));
}
virtual ~NixCExternalValue() override{}; /**
* Compare to another value of the same type.
*/
virtual bool operator==(const ExternalValueBase & b) const override
{
if (!desc.equal) {
return false;
}
auto r = dynamic_cast<const NixCExternalValue *>(&b);
if (!r)
return false;
return desc.equal(v, r->v);
}
/**
* Print the value as JSON.
*/
virtual nlohmann::json printValueAsJSON(
nix::EvalState & state, bool strict, nix::NixStringContext & context, bool copyToStore = true) const override
{
if (!desc.printValueAsJSON) {
return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore);
}
nix_string_context ctx{context};
nix_string_return res{""};
desc.printValueAsJSON(v, (State *) &state, strict, &ctx, copyToStore, &res);
if (res.str.empty()) {
return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore);
}
return nlohmann::json::parse(res.str);
}
/**
* Print the value as XML.
*/
virtual void printValueAsXML(
nix::EvalState & state,
bool strict,
bool location,
nix::XMLWriter & doc,
nix::NixStringContext & context,
nix::PathSet & drvsSeen,
const nix::PosIdx pos) const override
{
if (!desc.printValueAsXML) {
return nix::ExternalValueBase::printValueAsXML(state, strict, location, doc, context, drvsSeen, pos);
}
nix_string_context ctx{context};
desc.printValueAsXML(
v, (State *) &state, strict, location, &doc, &ctx, &drvsSeen, *reinterpret_cast<const uint32_t *>(&pos));
}
virtual ~NixCExternalValue() override{};
}; };
ExternalValue *nix_create_external_value(nix_c_context *context, ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v)
NixCExternalValueDesc *desc, void *v) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto ret = new auto ret = new
#ifdef HAVE_BOEHMGC #ifdef HAVE_BOEHMGC
(GC) (GC)
#endif #endif
NixCExternalValue(*desc, v); NixCExternalValue(*desc, v);
nix_gc_incref(nullptr, ret); nix_gc_incref(nullptr, ret);
return (ExternalValue *)ret; return (ExternalValue *) ret;
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b) { void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto r = dynamic_cast<NixCExternalValue *>((nix::ExternalValueBase *)b); try {
if (r) auto r = dynamic_cast<NixCExternalValue *>((nix::ExternalValueBase *) b);
return r->get_ptr(); if (r)
return nullptr; return r->get_ptr();
} return nullptr;
NIXC_CATCH_ERRS_NULL }
NIXC_CATCH_ERRS_NULL
} }

View file

@ -42,7 +42,7 @@ typedef struct nix_string_context nix_string_context;
* @param[out] str the nix_string_return to write to * @param[out] str the nix_string_return to write to
* @param[in] c The string to copy * @param[in] c The string to copy
*/ */
void nix_set_string_return(nix_string_return *str, const char *c); void nix_set_string_return(nix_string_return * str, const char * c);
/** /**
* Print to the nix_printer * Print to the nix_printer
@ -52,8 +52,7 @@ void nix_set_string_return(nix_string_return *str, const char *c);
* @param[in] str The string to print * @param[in] str The string to print
* @returns NIX_OK if everything worked * @returns NIX_OK if everything worked
*/ */
nix_err nix_external_print(nix_c_context *context, nix_printer *printer, nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * str);
const char *str);
/** /**
* Add string context to the nix_string_context object * Add string context to the nix_string_context object
@ -62,9 +61,7 @@ nix_err nix_external_print(nix_c_context *context, nix_printer *printer,
* @param[in] c The context string to add * @param[in] c The context string to add
* @returns NIX_OK if everything worked * @returns NIX_OK if everything worked
*/ */
nix_err nix_external_add_string_context(nix_c_context *context, nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * string_context, const char * c);
nix_string_context *string_context,
const char *c);
/** /**
* @brief Definition for a class of external values * @brief Definition for a class of external values
@ -76,89 +73,88 @@ nix_err nix_external_add_string_context(nix_c_context *context,
* *
* @see nix_create_external_value * @see nix_create_external_value
*/ */
typedef struct NixCExternalValueDesc { typedef struct NixCExternalValueDesc
/** {
* @brief Called when printing the external value /**
* * @brief Called when printing the external value
* @param[in] self the void* passed to nix_create_external_value *
* @param[out] printer The printer to print to, pass to nix_external_print * @param[in] self the void* passed to nix_create_external_value
*/ * @param[out] printer The printer to print to, pass to nix_external_print
void (*print)(void *self, nix_printer *printer); */
/** void (*print)(void * self, nix_printer * printer);
* @brief Called on :t /**
* @param[in] self the void* passed to nix_create_external_value * @brief Called on :t
* @param[out] res the return value * @param[in] self the void* passed to nix_create_external_value
*/ * @param[out] res the return value
void (*showType)(void *self, nix_string_return *res); */
/** void (*showType)(void * self, nix_string_return * res);
* @brief Called on `builtins.typeOf` /**
* @param self the void* passed to nix_create_external_value * @brief Called on `builtins.typeOf`
* @param[out] res the return value * @param self the void* passed to nix_create_external_value
*/ * @param[out] res the return value
void (*typeOf)(void *self, nix_string_return *res); */
/** void (*typeOf)(void * self, nix_string_return * res);
* @brief Called on "${str}" and builtins.toString. /**
* * @brief Called on "${str}" and builtins.toString.
* The latter with coerceMore=true *
* Optional, the default is to throw an error. * The latter with coerceMore=true
* @param[in] self the void* passed to nix_create_external_value * Optional, the default is to throw an error.
* @param[out] c writable string context for the resulting string * @param[in] self the void* passed to nix_create_external_value
* @param[in] coerceMore boolean, try to coerce to strings in more cases * @param[out] c writable string context for the resulting string
* instead of throwing an error * @param[in] coerceMore boolean, try to coerce to strings in more cases
* @param[in] copyToStore boolean, whether to copy referenced paths to store * instead of throwing an error
* or keep them as-is * @param[in] copyToStore boolean, whether to copy referenced paths to store
* @param[out] res the return value. Not touching this, or setting it to the * or keep them as-is
* empty string, will make the conversion throw an error. * @param[out] res the return value. Not touching this, or setting it to the
*/ * empty string, will make the conversion throw an error.
void (*coerceToString)(void *self, nix_string_context *c, int coerceMore, */
int copyToStore, nix_string_return *res); void (*coerceToString)(
/** void * self, nix_string_context * c, int coerceMore, int copyToStore, nix_string_return * res);
* @brief Try to compare two external values /**
* * @brief Try to compare two external values
* Optional, the default is always false. *
* If the other object was not a Nix C external value, this comparison will * Optional, the default is always false.
* also return false * If the other object was not a Nix C external value, this comparison will
* @param[in] self the void* passed to nix_create_external_value * also return false
* @param[in] other the void* passed to the other object's * @param[in] self the void* passed to nix_create_external_value
* nix_create_external_value * @param[in] other the void* passed to the other object's
* @returns true if the objects are deemed to be equal * nix_create_external_value
*/ * @returns true if the objects are deemed to be equal
int (*equal)(void *self, void *other); */
/** int (*equal)(void * self, void * other);
* @brief Convert the external value to json /**
* * @brief Convert the external value to json
* Optional, the default is to throw an error *
* @param[in] self the void* passed to nix_create_external_value * Optional, the default is to throw an error
* @param[in] state The evaluator state * @param[in] self the void* passed to nix_create_external_value
* @param[in] strict boolean Whether to force the value before printing * @param[in] state The evaluator state
* @param[out] c writable string context for the resulting string * @param[in] strict boolean Whether to force the value before printing
* @param[in] copyToStore whether to copy referenced paths to store or keep * @param[out] c writable string context for the resulting string
* them as-is * @param[in] copyToStore whether to copy referenced paths to store or keep
* @param[out] res the return value. Gets parsed as JSON. Not touching this, * them as-is
* or setting it to the empty string, will make the conversion throw an error. * @param[out] res the return value. Gets parsed as JSON. Not touching this,
*/ * or setting it to the empty string, will make the conversion throw an error.
void (*printValueAsJSON)(void *self, State *, int strict, */
nix_string_context *c, bool copyToStore, void (*printValueAsJSON)(
nix_string_return *res); void * self, State *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res);
/** /**
* @brief Convert the external value to XML * @brief Convert the external value to XML
* *
* Optional, the default is to throw an error * Optional, the default is to throw an error
* @todo The mechanisms for this call are incomplete. There are no C * @todo The mechanisms for this call are incomplete. There are no C
* bindings to work with XML, pathsets and positions. * bindings to work with XML, pathsets and positions.
* @param[in] self the void* passed to nix_create_external_value * @param[in] self the void* passed to nix_create_external_value
* @param[in] state The evaluator state * @param[in] state The evaluator state
* @param[in] strict boolean Whether to force the value before printing * @param[in] strict boolean Whether to force the value before printing
* @param[in] location boolean Whether to include position information in the * @param[in] location boolean Whether to include position information in the
* xml * xml
* @param[out] doc XML document to output to * @param[out] doc XML document to output to
* @param[out] c writable string context for the resulting string * @param[out] c writable string context for the resulting string
* @param[in,out] drvsSeen a path set to avoid duplicating derivations * @param[in,out] drvsSeen a path set to avoid duplicating derivations
* @param[in] pos The position of the call. * @param[in] pos The position of the call.
*/ */
void (*printValueAsXML)(void *self, State *, int strict, int location, void (*printValueAsXML)(
void *doc, nix_string_context *c, void *drvsSeen, void * self, State *, int strict, int location, void * doc, nix_string_context * c, void * drvsSeen, int pos);
int pos);
} NixCExternalValueDesc; } NixCExternalValueDesc;
/** /**
@ -173,8 +169,7 @@ typedef struct NixCExternalValueDesc {
* @returns external value, owned by the garbage collector * @returns external value, owned by the garbage collector
* @see nix_set_external * @see nix_set_external
*/ */
ExternalValue *nix_create_external_value(nix_c_context *context, ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v);
NixCExternalValueDesc *desc, void *v);
/** /**
* @brief Extract the pointer from a nix c external value. * @brief Extract the pointer from a nix c external value.
@ -183,7 +178,7 @@ ExternalValue *nix_create_external_value(nix_c_context *context,
* @returns The pointer, or null if the external value was not from nix c. * @returns The pointer, or null if the external value was not from nix c.
* @see nix_get_external * @see nix_get_external
*/ */
void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b); void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b);
// cffi end // cffi end
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -18,437 +18,457 @@
#endif #endif
// Helper function to throw an exception if value is null // Helper function to throw an exception if value is null
static const nix::Value &check_value_not_null(const Value *value) { static const nix::Value & check_value_not_null(const Value * value)
if (!value) { {
throw std::runtime_error("Value is null"); if (!value) {
} throw std::runtime_error("Value is null");
return *((const nix::Value *)value); }
return *((const nix::Value *) value);
} }
static nix::Value &check_value_not_null(Value *value) { static nix::Value & check_value_not_null(Value * value)
if (!value) { {
throw std::runtime_error("Value is null"); if (!value) {
} throw std::runtime_error("Value is null");
return *((nix::Value *)value); }
return *((nix::Value *) value);
} }
PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, PrimOp * nix_alloc_primop(
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)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto fun2 = (nix::PrimOpFun)fun; try {
auto p = new auto fun2 = (nix::PrimOpFun) fun;
auto p = new
#ifdef HAVE_BOEHMGC #ifdef HAVE_BOEHMGC
(GC) (GC)
#endif #endif
nix::PrimOp{.name = name, nix::PrimOp{.name = name, .args = {}, .arity = (size_t) arity, .doc = doc, .fun = fun2};
.args = {}, if (args)
.arity = (size_t)arity, for (size_t i = 0; args[i]; i++)
.doc = doc, p->args.emplace_back(*args);
.fun = fun2}; nix_gc_incref(nullptr, p);
if (args) return (PrimOp *) p;
for (size_t i = 0; args[i]; i++)
p->args.emplace_back(*args);
nix_gc_incref(nullptr, p);
return (PrimOp *)p;
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp) {
if (context)
context->last_err_code = NIX_OK;
try {
nix::RegisterPrimOp r(std::move(*((nix::PrimOp *)primOp)));
}
NIXC_CATCH_ERRS
}
Value *nix_alloc_value(nix_c_context *context, State *state) {
if (context)
context->last_err_code = NIX_OK;
try {
Value *res = state->state.allocValue();
nix_gc_incref(nullptr, res);
return res;
}
NIXC_CATCH_ERRS_NULL
}
ValueType nix_get_type(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
using namespace nix;
switch (v.type()) {
case nThunk:
return NIX_TYPE_THUNK;
case nInt:
return NIX_TYPE_INT;
case nFloat:
return NIX_TYPE_FLOAT;
case nBool:
return NIX_TYPE_BOOL;
case nString:
return NIX_TYPE_STRING;
case nPath:
return NIX_TYPE_PATH;
case nNull:
return NIX_TYPE_NULL;
case nAttrs:
return NIX_TYPE_ATTRS;
case nList:
return NIX_TYPE_LIST;
case nFunction:
return NIX_TYPE_FUNCTION;
case nExternal:
return NIX_TYPE_EXTERNAL;
} }
return NIX_TYPE_NULL; NIXC_CATCH_ERRS_NULL
}
NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL);
} }
const char *nix_get_typename(nix_c_context *context, const Value *value) { nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
auto s = nix::showType(v); nix::RegisterPrimOp r(std::move(*((nix::PrimOp *) primOp)));
return strdup(s.c_str());
}
NIXC_CATCH_ERRS_NULL
}
bool nix_get_bool(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nBool);
return v.boolean;
}
NIXC_CATCH_ERRS_RES(false);
}
const char *nix_get_string(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nString);
return v.string.s;
}
NIXC_CATCH_ERRS_NULL
}
const char *nix_get_path_string(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nPath);
return v._path;
}
NIXC_CATCH_ERRS_NULL
}
unsigned int nix_get_list_size(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nList);
return v.listSize();
}
NIXC_CATCH_ERRS_RES(0);
}
unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nAttrs);
return v.attrs->size();
}
NIXC_CATCH_ERRS_RES(0);
}
double nix_get_float(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nFloat);
return v.fpoint;
}
NIXC_CATCH_ERRS_RES(NAN);
}
int64_t nix_get_int(nix_c_context *context, const Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nInt);
return v.integer;
}
NIXC_CATCH_ERRS_RES(0);
}
ExternalValue *nix_get_external(nix_c_context *context, Value *value) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nExternal);
return (ExternalValue *)v.external;
}
NIXC_CATCH_ERRS_NULL;
}
Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int ix) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nList);
auto *p = v.listElems()[ix];
nix_gc_incref(nullptr, p);
state->state.forceValue(*p, nix::noPos);
return (Value *)p;
}
NIXC_CATCH_ERRS_NULL
}
Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
assert(v.type() == nix::nAttrs);
nix::Symbol s = state->state.symbols.create(name);
auto attr = v.attrs->get(s);
if (attr) {
nix_gc_incref(nullptr, attr->value);
state->state.forceValue(*attr->value, nix::noPos);
return attr->value;
} }
nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); NIXC_CATCH_ERRS
return nullptr;
}
NIXC_CATCH_ERRS_NULL
} }
bool nix_has_attr_byname(nix_c_context *context, const Value *value, Value * nix_alloc_value(nix_c_context * context, State * state)
State *state, const char *name) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); Value * res = state->state.allocValue();
assert(v.type() == nix::nAttrs); nix_gc_incref(nullptr, res);
nix::Symbol s = state->state.symbols.create(name); return res;
auto attr = v.attrs->get(s); }
if (attr) NIXC_CATCH_ERRS_NULL
return true;
return false;
}
NIXC_CATCH_ERRS_RES(false);
} }
Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, ValueType nix_get_type(nix_c_context * context, const Value * value)
State *state, unsigned int i, const char **name) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
const nix::Attr &a = (*v.attrs)[i]; using namespace nix;
*name = ((const std::string &)(state->state.symbols[a.name])).c_str(); switch (v.type()) {
nix_gc_incref(nullptr, a.value); case nThunk:
state->state.forceValue(*a.value, nix::noPos); return NIX_TYPE_THUNK;
return a.value; case nInt:
} return NIX_TYPE_INT;
NIXC_CATCH_ERRS_NULL case nFloat:
return NIX_TYPE_FLOAT;
case nBool:
return NIX_TYPE_BOOL;
case nString:
return NIX_TYPE_STRING;
case nPath:
return NIX_TYPE_PATH;
case nNull:
return NIX_TYPE_NULL;
case nAttrs:
return NIX_TYPE_ATTRS;
case nList:
return NIX_TYPE_LIST;
case nFunction:
return NIX_TYPE_FUNCTION;
case nExternal:
return NIX_TYPE_EXTERNAL;
}
return NIX_TYPE_NULL;
}
NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL);
} }
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, const char * nix_get_typename(nix_c_context * context, const Value * value)
State *state, unsigned int i) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
const nix::Attr &a = (*v.attrs)[i]; auto s = nix::showType(v);
return ((const std::string &)(state->state.symbols[a.name])).c_str(); return strdup(s.c_str());
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { bool nix_get_bool(nix_c_context * context, const Value * value)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkBool(b); auto & v = check_value_not_null(value);
} assert(v.type() == nix::nBool);
NIXC_CATCH_ERRS return v.boolean;
}
NIXC_CATCH_ERRS_RES(false);
}
const char * nix_get_string(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nString);
return v.string.s;
}
NIXC_CATCH_ERRS_NULL
}
const char * nix_get_path_string(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nPath);
return v._path;
}
NIXC_CATCH_ERRS_NULL
}
unsigned int nix_get_list_size(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nList);
return v.listSize();
}
NIXC_CATCH_ERRS_RES(0);
}
unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nAttrs);
return v.attrs->size();
}
NIXC_CATCH_ERRS_RES(0);
}
double nix_get_float(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nFloat);
return v.fpoint;
}
NIXC_CATCH_ERRS_RES(NAN);
}
int64_t nix_get_int(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nInt);
return v.integer;
}
NIXC_CATCH_ERRS_RES(0);
}
ExternalValue * nix_get_external(nix_c_context * context, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nExternal);
return (ExternalValue *) v.external;
}
NIXC_CATCH_ERRS_NULL;
}
Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nList);
auto * p = v.listElems()[ix];
nix_gc_incref(nullptr, p);
state->state.forceValue(*p, nix::noPos);
return (Value *) p;
}
NIXC_CATCH_ERRS_NULL
}
Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nAttrs);
nix::Symbol s = state->state.symbols.create(name);
auto attr = v.attrs->get(s);
if (attr) {
nix_gc_incref(nullptr, attr->value);
state->state.forceValue(*attr->value, nix::noPos);
return attr->value;
}
nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute");
return nullptr;
}
NIXC_CATCH_ERRS_NULL
}
bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
assert(v.type() == nix::nAttrs);
nix::Symbol s = state->state.symbols.create(name);
auto attr = v.attrs->get(s);
if (attr)
return true;
return false;
}
NIXC_CATCH_ERRS_RES(false);
}
Value *
nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
const nix::Attr & a = (*v.attrs)[i];
*name = ((const std::string &) (state->state.symbols[a.name])).c_str();
nix_gc_incref(nullptr, a.value);
state->state.forceValue(*a.value, nix::noPos);
return a.value;
}
NIXC_CATCH_ERRS_NULL
}
const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
const nix::Attr & a = (*v.attrs)[i];
return ((const std::string &) (state->state.symbols[a.name])).c_str();
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_set_bool(nix_c_context * context, Value * value, bool b)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_not_null(value);
v.mkBool(b);
}
NIXC_CATCH_ERRS
} }
// todo string context // todo string context
nix_err nix_set_string(nix_c_context *context, Value *value, const char *str) { nix_err nix_set_string(nix_c_context * context, Value * value, const char * str)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkString(std::string_view(str)); auto & v = check_value_not_null(value);
} v.mkString(std::string_view(str));
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_set_path_string(nix_c_context *context, Value *value, nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str)
const char *str) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
v.mkPath(std::string_view(str)); v.mkPath(std::string_view(str));
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_set_float(nix_c_context *context, Value *value, double d) { nix_err nix_set_float(nix_c_context * context, Value * value, double d)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkFloat(d); auto & v = check_value_not_null(value);
} v.mkFloat(d);
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i) { nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkInt(i); auto & v = check_value_not_null(value);
} v.mkInt(i);
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_set_null(nix_c_context *context, Value *value) { nix_err nix_set_null(nix_c_context * context, Value * value)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkNull(); auto & v = check_value_not_null(value);
} v.mkNull();
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_set_external(nix_c_context *context, Value *value, nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val)
ExternalValue *val) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
auto r = (nix::ExternalValueBase *)val; auto r = (nix::ExternalValueBase *) val;
v.mkExternal(r); v.mkExternal(r);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_make_list(nix_c_context *context, State *s, Value *value, nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size)
unsigned int size) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
s->state.mkList(v, size); s->state.mkList(v, size);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_set_list_byidx(nix_c_context *context, Value *value, nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem)
unsigned int ix, Value *elem) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
// todo: assert that this is a list // todo: assert that this is a list
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
auto &e = check_value_not_null(elem); auto & e = check_value_not_null(elem);
v.listElems()[ix] = &e; v.listElems()[ix] = &e;
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *p) { nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * p)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
v.mkPrimOp((nix::PrimOp *)p); auto & v = check_value_not_null(value);
} v.mkPrimOp((nix::PrimOp *) p);
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
auto &v = check_value_not_null(value); try {
auto &s = check_value_not_null(source); auto & v = check_value_not_null(value);
v = s; auto & s = check_value_not_null(source);
} v = s;
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
nix_err nix_make_attrs(nix_c_context *context, Value *value, nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b)
BindingsBuilder *b) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
v.mkAttrs(b->builder); v.mkAttrs(b->builder);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity)
size_t capacity) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto bb = state->state.buildBindings(capacity); auto bb = state->state.buildBindings(capacity);
return new return new
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
(NoGC) (NoGC)
#endif #endif
BindingsBuilder{std::move(bb)}; BindingsBuilder{std::move(bb)};
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b, nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * b, const char * name, Value * value)
const char *name, Value *value) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto &v = check_value_not_null(value); auto & v = check_value_not_null(value);
nix::Symbol s = b->builder.state.symbols.create(name); nix::Symbol s = b->builder.state.symbols.create(name);
b->builder.insert(s, &v); b->builder.insert(s, &v);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
void nix_bindings_builder_free(BindingsBuilder *bb) { void nix_bindings_builder_free(BindingsBuilder * bb)
{
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
GC_FREE((nix::BindingsBuilder *)bb); GC_FREE((nix::BindingsBuilder *) bb);
#else #else
delete (nix::BindingsBuilder *)bb; delete (nix::BindingsBuilder *) bb;
#endif #endif
} }

View file

@ -20,17 +20,17 @@ extern "C" {
// Type definitions // Type definitions
typedef enum { typedef enum {
NIX_TYPE_THUNK, NIX_TYPE_THUNK,
NIX_TYPE_INT, NIX_TYPE_INT,
NIX_TYPE_FLOAT, NIX_TYPE_FLOAT,
NIX_TYPE_BOOL, NIX_TYPE_BOOL,
NIX_TYPE_STRING, NIX_TYPE_STRING,
NIX_TYPE_PATH, NIX_TYPE_PATH,
NIX_TYPE_NULL, NIX_TYPE_NULL,
NIX_TYPE_ATTRS, NIX_TYPE_ATTRS,
NIX_TYPE_LIST, NIX_TYPE_LIST,
NIX_TYPE_FUNCTION, NIX_TYPE_FUNCTION,
NIX_TYPE_EXTERNAL NIX_TYPE_EXTERNAL
} ValueType; } ValueType;
// forward declarations // forward declarations
@ -67,11 +67,12 @@ typedef struct ExternalValue ExternalValue;
/** @brief Function pointer for primops /** @brief Function pointer for primops
* @param[in] state Evaluator state * @param[in] state Evaluator state
* @param[in] pos Call position, opaque index into the state's position table. * @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[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 * @param[out] ret return value
* @see nix_alloc_primop, nix_set_primop * @see nix_alloc_primop, nix_set_primop
*/ */
typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret); typedef void (*PrimOpFun)(State * state, int pos, Value ** args, Value * ret);
/** @brief Allocate a PrimOp /** @brief Allocate a PrimOp
* *
@ -87,8 +88,8 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret);
* @return primop, or null in case of errors * @return primop, or null in case of errors
* @see nix_set_primop * @see nix_set_primop
*/ */
PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, PrimOp * nix_alloc_primop(
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);
/** @brief add a primop to the `builtins` attribute set /** @brief add a primop to the `builtins` attribute set
* *
@ -102,7 +103,7 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity,
* @return primop, or null in case of errors * @return primop, or null in case of errors
* *
*/ */
nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp);
/** @} */ /** @} */
// Function prototypes // Function prototypes
@ -115,7 +116,7 @@ nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp);
* @return value, or null in case of errors * @return value, or null in case of errors
* *
*/ */
Value *nix_alloc_value(nix_c_context *context, State *state); Value * nix_alloc_value(nix_c_context * context, State * state);
/** @addtogroup value_manip Manipulating values /** @addtogroup value_manip Manipulating values
* @brief Functions to inspect and change Nix language values, represented by Value. * @brief Functions to inspect and change Nix language values, represented by Value.
* @{ * @{
@ -128,65 +129,65 @@ Value *nix_alloc_value(nix_c_context *context, State *state);
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return type of nix value * @return type of nix value
*/ */
ValueType nix_get_type(nix_c_context *context, const Value *value); ValueType nix_get_type(nix_c_context * context, const Value * value);
/** @brief Get type name of value as defined in the evaluator /** @brief Get type name of value as defined in the evaluator
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return type name, owned string * @return type name, owned string
* @todo way to free the result * @todo way to free the result
*/ */
const char *nix_get_typename(nix_c_context *context, const Value *value); const char * nix_get_typename(nix_c_context * context, const Value * value);
/** @brief Get boolean value /** @brief Get boolean value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return true or false, error info via context * @return true or false, error info via context
*/ */
bool nix_get_bool(nix_c_context *context, const Value *value); bool nix_get_bool(nix_c_context * context, const Value * value);
/** @brief Get string /** @brief Get string
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return string * @return string
* @return NULL in case of error. * @return NULL in case of error.
*/ */
const char *nix_get_string(nix_c_context *context, const Value *value); const char * nix_get_string(nix_c_context * context, const Value * value);
/** @brief Get path as string /** @brief Get path as string
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return string * @return string
* @return NULL in case of error. * @return NULL in case of error.
*/ */
const char *nix_get_path_string(nix_c_context *context, const Value *value); const char * nix_get_path_string(nix_c_context * context, const Value * value);
/** @brief Get the length of a list /** @brief Get the length of a list
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return length of list, error info via context * @return length of list, error info via context
*/ */
unsigned int nix_get_list_size(nix_c_context *context, const Value *value); unsigned int nix_get_list_size(nix_c_context * context, const Value * value);
/** @brief Get the element count of an attrset /** @brief Get the element count of an attrset
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return attrset element count, error info via context * @return attrset element count, error info via context
*/ */
unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value); unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value);
/** @brief Get float value in 64 bits /** @brief Get float value in 64 bits
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return float contents, error info via context * @return float contents, error info via context
*/ */
double nix_get_float(nix_c_context *context, const Value *value); double nix_get_float(nix_c_context * context, const Value * value);
/** @brief Get int value /** @brief Get int value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return int contents, error info via context * @return int contents, error info via context
*/ */
int64_t nix_get_int(nix_c_context *context, const Value *value); int64_t nix_get_int(nix_c_context * context, const Value * value);
/** @brief Get external reference /** @brief Get external reference
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return reference to external, NULL in case of error * @return reference to external, NULL in case of error
*/ */
ExternalValue *nix_get_external(nix_c_context *context, Value *); ExternalValue * nix_get_external(nix_c_context * context, Value *);
/** @brief Get the ix'th element of a list /** @brief Get the ix'th element of a list
* *
@ -197,8 +198,7 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *);
* @param[in] ix list element to get * @param[in] ix list element to get
* @return value, NULL in case of errors * @return value, NULL in case of errors
*/ */
Value *nix_get_list_byidx(nix_c_context *context, const Value *value, Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix);
State *state, unsigned int ix);
/** @brief Get an attr by name /** @brief Get an attr by name
* *
* Owned by the GC. Use nix_gc_decref when you're done with the pointer * Owned by the GC. Use nix_gc_decref when you're done with the pointer
@ -208,8 +208,7 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
* @param[in] name attribute name * @param[in] name attribute name
* @return value, NULL in case of errors * @return value, NULL in case of errors
*/ */
Value *nix_get_attr_byname(nix_c_context *context, const Value *value, Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name);
State *state, const char *name);
/** @brief Check if an attribute name exists on a value /** @brief Check if an attribute name exists on a value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
@ -218,8 +217,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
* @param[in] name attribute name * @param[in] name attribute name
* @return value, error info via context * @return value, error info via context
*/ */
bool nix_has_attr_byname(nix_c_context *context, const Value *value, bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name);
State *state, const char *name);
/** @brief Get an attribute by index in the sorted bindings /** @brief Get an attribute by index in the sorted bindings
* *
@ -233,8 +231,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value,
* @param[out] name will store a pointer to the attribute name * @param[out] name will store a pointer to the attribute name
* @return value, NULL in case of errors * @return value, NULL in case of errors
*/ */
Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, Value *
State *state, unsigned int i, const char **name); nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name);
/** @brief Get an attribute name by index in the sorted bindings /** @brief Get an attribute name by index in the sorted bindings
* *
@ -247,8 +245,7 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
* @param[in] i attribute index * @param[in] i attribute index
* @return name, NULL in case of errors * @return name, NULL in case of errors
*/ */
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i);
State *state, unsigned int i);
/**@}*/ /**@}*/
/** @name Setters /** @name Setters
*/ */
@ -259,58 +256,55 @@ const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
* @param[in] b the boolean value * @param[in] b the boolean value
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_bool(nix_c_context *context, Value *value, bool b); nix_err nix_set_bool(nix_c_context * context, Value * value, bool b);
/** @brief Set a string /** @brief Set a string
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] str the string, copied * @param[in] str the string, copied
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_string(nix_c_context *context, Value *value, const char *str); nix_err nix_set_string(nix_c_context * context, Value * value, const char * str);
/** @brief Set a path /** @brief Set a path
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] str the path string, copied * @param[in] str the path string, copied
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_path_string(nix_c_context *context, Value *value, nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str);
const char *str);
/** @brief Set a float /** @brief Set a float
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] d the float, 64-bits * @param[in] d the float, 64-bits
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_float(nix_c_context *context, Value *value, double d); nix_err nix_set_float(nix_c_context * context, Value * value, double d);
/** @brief Set an int /** @brief Set an int
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] i the int * @param[in] i the int
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i); nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i);
/** @brief Set null /** @brief Set null
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_null(nix_c_context *context, Value *value); nix_err nix_set_null(nix_c_context * context, Value * value);
/** @brief Set an external value /** @brief Set an external value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] val the external value to set. Will be GC-referenced by the value. * @param[in] val the external value to set. Will be GC-referenced by the value.
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_external(nix_c_context *context, Value *value, nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val);
ExternalValue *val);
/** @brief Allocate a list /** @brief Allocate a list
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] size size of list * @param[in] size size of list
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_make_list(nix_c_context *context, State *s, Value *value, nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size);
unsigned int size);
/** @brief Manipulate a list by index /** @brief Manipulate a list by index
* *
* Don't do this mid-computation. * Don't do this mid-computation.
@ -321,16 +315,14 @@ nix_err nix_make_list(nix_c_context *context, State *s, Value *value,
* @param[in] elem the value to set, will be gc-referenced by the value * @param[in] elem the value to set, will be gc-referenced by the value
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_list_byidx(nix_c_context *context, Value *value, nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem);
unsigned int ix, Value *elem);
/** @brief Create an attribute set from a bindings builder /** @brief Create an attribute set from a bindings builder
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] b bindings builder to use. Make sure to unref this afterwards. * @param[in] b bindings builder to use. Make sure to unref this afterwards.
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_make_attrs(nix_c_context *context, Value *value, nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b);
BindingsBuilder *b);
/** @brief Set primop /** @brief Set primop
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
@ -338,14 +330,14 @@ nix_err nix_make_attrs(nix_c_context *context, Value *value,
* @see nix_alloc_primop * @see nix_alloc_primop
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *op); nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * op);
/** @brief Copy from another value /** @brief Copy from another value
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[out] value Nix value to modify * @param[out] value Nix value to modify
* @param[in] source value to copy from * @param[in] source value to copy from
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source);
/**@}*/ /**@}*/
/** @brief Create a bindings builder /** @brief Create a bindings builder
@ -356,8 +348,7 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source);
* @return owned reference to a bindings builder. Make sure to unref when you're * @return owned reference to a bindings builder. Make sure to unref when you're
done. done.
*/ */
BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity);
size_t capacity);
/** @brief Insert bindings into a builder /** @brief Insert bindings into a builder
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] builder BindingsBuilder to insert into * @param[in] builder BindingsBuilder to insert into
@ -365,15 +356,14 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
* @param[in] value value to give the binding * @param[in] value value to give the binding
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_bindings_builder_insert(nix_c_context *context, nix_err
BindingsBuilder *builder, const char *name, nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * builder, const char * name, Value * value);
Value *value);
/** @brief Free a bindings builder /** @brief Free a bindings builder
* *
* Does not fail. * Does not fail.
* @param[in] builder the builder to free * @param[in] builder the builder to free
*/ */
void nix_bindings_builder_free(BindingsBuilder *builder); void nix_bindings_builder_free(BindingsBuilder * builder);
/**@}*/ /**@}*/
// cffi end // cffi end

View file

@ -7,122 +7,132 @@
#include "globals.hh" #include "globals.hh"
struct StorePath { struct StorePath
nix::StorePath path; {
nix::StorePath path;
}; };
nix_err nix_libstore_init(nix_c_context *context) { nix_err nix_libstore_init(nix_c_context * context)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
nix::initLibStore(); try {
} nix::initLibStore();
NIXC_CATCH_ERRS
}
nix_err nix_init_plugins(nix_c_context *context) {
if (context)
context->last_err_code = NIX_OK;
try {
nix::initPlugins();
}
NIXC_CATCH_ERRS
}
Store *nix_store_open(nix_c_context *context, const char *uri,
const char ***params) {
if (context)
context->last_err_code = NIX_OK;
try {
if (!uri) {
return new Store{nix::openStore()};
} else {
std::string uri_str = uri;
if (!params)
return new Store{nix::openStore(uri_str)};
nix::Store::Params params_map;
for (size_t i = 0; params[i] != nullptr; i++) {
params_map[params[i][0]] = params[i][1];
}
return new Store{nix::openStore(uri_str, params_map)};
} }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS_NULL
} }
void nix_store_unref(Store *store) { delete store; } nix_err nix_init_plugins(nix_c_context * context)
{
nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, if (context)
unsigned int n) { context->last_err_code = NIX_OK;
if (context) try {
context->last_err_code = NIX_OK; nix::initPlugins();
try {
auto res = store->ptr->getUri();
return nix_export_std_string(context, res, dest, n);
}
NIXC_CATCH_ERRS
}
nix_err nix_store_get_version(nix_c_context *context, Store *store, char *dest,
unsigned int n) {
if (context)
context->last_err_code = NIX_OK;
try {
auto res = store->ptr->getVersion();
if (res) {
return nix_export_std_string(context, *res, dest, n);
} else {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
"store does not have a version");
} }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
bool nix_store_is_valid_path(nix_c_context *context, Store *store, Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params)
StorePath *path) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
return store->ptr->isValidPath(path->path); if (!uri) {
} return new Store{nix::openStore()};
NIXC_CATCH_ERRS_RES(false); } else {
} std::string uri_str = uri;
if (!params)
return new Store{nix::openStore(uri_str)};
StorePath *nix_store_parse_path(nix_c_context *context, Store *store, nix::Store::Params params_map;
const char *path) { for (size_t i = 0; params[i] != nullptr; i++) {
if (context) params_map[params[i][0]] = params[i][1];
context->last_err_code = NIX_OK; }
try { return new Store{nix::openStore(uri_str, params_map)};
nix::StorePath s = store->ptr->parseStorePath(path); }
return new StorePath{std::move(s)};
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
void *userdata,
void (*callback)(void *userdata, const char *,
const char *)) {
if (context)
context->last_err_code = NIX_OK;
try {
store->ptr->buildPaths({
nix::DerivedPath::Built{
.drvPath = path->path,
.outputs = nix::OutputsSpec::All{},
},
});
if (callback) {
for (auto &[outputName, outputPath] :
store->ptr->queryDerivationOutputMap(path->path)) {
auto op = store->ptr->printStorePath(outputPath);
callback(userdata, outputName.c_str(), op.c_str());
}
} }
} NIXC_CATCH_ERRS_NULL
NIXC_CATCH_ERRS
} }
void nix_store_path_free(StorePath *sp) { delete sp; } void nix_store_unref(Store * store)
{
delete store;
}
nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto res = store->ptr->getUri();
return nix_export_std_string(context, res, dest, n);
}
NIXC_CATCH_ERRS
}
nix_err nix_store_get_version(nix_c_context * context, Store * store, char * dest, unsigned int n)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto res = store->ptr->getVersion();
if (res) {
return nix_export_std_string(context, *res, dest, n);
} else {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "store does not have a version");
}
}
NIXC_CATCH_ERRS
}
bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path)
{
if (context)
context->last_err_code = NIX_OK;
try {
return store->ptr->isValidPath(path->path);
}
NIXC_CATCH_ERRS_RES(false);
}
StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path)
{
if (context)
context->last_err_code = NIX_OK;
try {
nix::StorePath s = store->ptr->parseStorePath(path);
return new StorePath{std::move(s)};
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_store_build(
nix_c_context * context,
Store * store,
StorePath * path,
void * userdata,
void (*callback)(void * userdata, const char *, const char *))
{
if (context)
context->last_err_code = NIX_OK;
try {
store->ptr->buildPaths({
nix::DerivedPath::Built{
.drvPath = path->path,
.outputs = nix::OutputsSpec::All{},
},
});
if (callback) {
for (auto & [outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) {
auto op = store->ptr->printStorePath(outputPath);
callback(userdata, outputName.c_str(), op.c_str());
}
}
}
NIXC_CATCH_ERRS
}
void nix_store_path_free(StorePath * sp)
{
delete sp;
}

View file

@ -33,7 +33,7 @@ typedef struct StorePath StorePath;
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @return NIX_OK if the initialization was successful, an error code otherwise. * @return NIX_OK if the initialization was successful, an error code otherwise.
*/ */
nix_err nix_libstore_init(nix_c_context *context); nix_err nix_libstore_init(nix_c_context * context);
/** /**
* @brief Loads the plugins specified in Nix's plugin-files setting. * @brief Loads the plugins specified in Nix's plugin-files setting.
@ -44,7 +44,7 @@ nix_err nix_libstore_init(nix_c_context *context);
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @return NIX_OK if the initialization was successful, an error code otherwise. * @return NIX_OK if the initialization was successful, an error code otherwise.
*/ */
nix_err nix_init_plugins(nix_c_context *context); nix_err nix_init_plugins(nix_c_context * context);
/** /**
* @brief Open a nix store * @brief Open a nix store
@ -55,7 +55,7 @@ nix_err nix_init_plugins(nix_c_context *context);
* @return ref-counted Store pointer, NULL in case of errors * @return ref-counted Store pointer, NULL in case of errors
* @see nix_store_unref * @see nix_store_unref
*/ */
Store *nix_store_open(nix_c_context *, const char *uri, const char ***params); Store * nix_store_open(nix_c_context *, const char * uri, const char *** params);
/** /**
* @brief Unref a nix store * @brief Unref a nix store
@ -64,7 +64,7 @@ Store *nix_store_open(nix_c_context *, const char *uri, const char ***params);
* It'll be closed and deallocated when all references are gone. * It'll be closed and deallocated when all references are gone.
* @param[in] builder the store to unref * @param[in] builder the store to unref
*/ */
void nix_store_unref(Store *store); void nix_store_unref(Store * store);
/** /**
* @brief get the URI of a nix store * @brief get the URI of a nix store
@ -74,8 +74,7 @@ void nix_store_unref(Store *store);
* @param[in] n Maximum size of the returned string. * @param[in] n Maximum size of the returned string.
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n);
unsigned int n);
// returns: owned StorePath* // returns: owned StorePath*
/** /**
@ -87,25 +86,24 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest,
* @param[in] path Path string to parse, copied * @param[in] path Path string to parse, copied
* @return owned store path, NULL on error * @return owned store path, NULL on error
*/ */
StorePath *nix_store_parse_path(nix_c_context *context, Store *store, StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path);
const char *path);
/** @brief Deallocate a StorePath /** @brief Deallocate a StorePath
* *
* Does not fail. * Does not fail.
* @param[in] p the path to free * @param[in] p the path to free
*/ */
void nix_store_path_free(StorePath *p); void nix_store_path_free(StorePath * p);
/** /**
* @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in the store) * @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in
* the store)
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] store Nix Store reference * @param[in] store Nix Store reference
* @param[in] path Path to check * @param[in] path Path to check
* @return true or false, error info in context * @return true or false, error info in context
*/ */
bool nix_store_is_valid_path(nix_c_context *context, Store *store, bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path);
StorePath *path);
// nix_err nix_store_ensure(Store*, const char*); // nix_err nix_store_ensure(Store*, const char*);
// nix_err nix_store_build_paths(Store*); // nix_err nix_store_build_paths(Store*);
/** /**
@ -119,10 +117,12 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store,
* @param[in] userdata data to pass to every callback invocation * @param[in] userdata data to pass to every callback invocation
* @param[in] callback called for every realised output * @param[in] callback called for every realised output
*/ */
nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, nix_err nix_store_build(
void *userdata, nix_c_context * context,
void (*callback)(void *userdata, const char *outname, Store * store,
const char *out)); StorePath * path,
void * userdata,
void (*callback)(void * userdata, const char * outname, const char * out));
/** /**
* @brief get the version of a nix store * @brief get the version of a nix store
@ -132,8 +132,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
* @param[in] n Maximum size of the returned string. * @param[in] n Maximum size of the returned string.
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_store_get_version(nix_c_context *, Store *store, char *dest, nix_err nix_store_get_version(nix_c_context *, Store * store, char * dest, unsigned int n);
unsigned int n);
// cffi end // cffi end
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -2,7 +2,8 @@
#define NIX_API_STORE_INTERNAL_H #define NIX_API_STORE_INTERNAL_H
#include "store-api.hh" #include "store-api.hh"
struct Store { struct Store
nix::ref<nix::Store> ptr; {
nix::ref<nix::Store> ptr;
}; };
#endif #endif

View file

@ -7,139 +7,145 @@
#include <cxxabi.h> #include <cxxabi.h>
#include <typeinfo> #include <typeinfo>
nix_c_context *nix_c_context_create() { return new nix_c_context(); } nix_c_context * nix_c_context_create()
{
return new nix_c_context();
}
void nix_c_context_free(nix_c_context *context) { delete context; } void nix_c_context_free(nix_c_context * context)
{
delete context;
}
nix_err nix_context_error(nix_c_context *context) { nix_err nix_context_error(nix_c_context * context)
if (context == nullptr) { {
throw; if (context == nullptr) {
} throw;
try {
throw;
} catch (nix::Error &e) {
/* Storing this exception is annoying, take what we need here */
context->last_err = e.what();
context->info = e.info();
int status;
const char *demangled =
abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
if (demangled) {
context->name = demangled;
// todo: free(demangled);
} else {
context->name = typeid(e).name();
} }
context->last_err_code = NIX_ERR_NIX_ERROR; try {
return context->last_err_code; throw;
} catch (const std::exception &e) { } catch (nix::Error & e) {
context->last_err = e.what(); /* Storing this exception is annoying, take what we need here */
context->last_err_code = NIX_ERR_UNKNOWN; context->last_err = e.what();
return context->last_err_code; context->info = e.info();
} int status;
// unreachable const char * demangled = abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
if (demangled) {
context->name = demangled;
// todo: free(demangled);
} else {
context->name = typeid(e).name();
}
context->last_err_code = NIX_ERR_NIX_ERROR;
return context->last_err_code;
} catch (const std::exception & e) {
context->last_err = e.what();
context->last_err_code = NIX_ERR_UNKNOWN;
return context->last_err_code;
}
// unreachable
} }
nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) { nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg)
if (context == nullptr) { {
// todo last_err_code if (context == nullptr) {
throw nix::Error("Nix C api error: %s", msg); // todo last_err_code
} throw nix::Error("Nix C api error: %s", msg);
context->last_err_code = err; }
context->last_err = msg; context->last_err_code = err;
return err; context->last_err = msg;
return err;
} }
const char *nix_version_get() { return PACKAGE_VERSION; } const char * nix_version_get()
{
return PACKAGE_VERSION;
}
// Implementations // Implementations
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n)
int n) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
std::map<std::string, nix::AbstractConfig::SettingInfo> settings; std::map<std::string, nix::AbstractConfig::SettingInfo> settings;
nix::globalConfig.getSettings(settings); nix::globalConfig.getSettings(settings);
if (settings.contains(key)) if (settings.contains(key))
return nix_export_std_string(context, settings[key].value, value, n); return nix_export_std_string(context, settings[key].value, value, n);
else { else {
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
}
} }
} NIXC_CATCH_ERRS
NIXC_CATCH_ERRS
} }
nix_err nix_setting_set(nix_c_context *context, const char *key, nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value)
const char *value) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
if (nix::globalConfig.set(key, value)) if (nix::globalConfig.set(key, value))
return NIX_OK; return NIX_OK;
else { else {
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
} }
} }
nix_err nix_libutil_init(nix_c_context *context) { nix_err nix_libutil_init(nix_c_context * context)
if (context) {
context->last_err_code = NIX_OK; if (context)
try { context->last_err_code = NIX_OK;
nix::initLibUtil(); try {
return NIX_OK; nix::initLibUtil();
} return NIX_OK;
NIXC_CATCH_ERRS }
NIXC_CATCH_ERRS
} }
const char *nix_err_msg(nix_c_context *context, const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_context, unsigned int * n)
const nix_c_context *read_context, unsigned int *n) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
if (read_context->last_err) { if (read_context->last_err) {
if (n) if (n)
*n = read_context->last_err->size(); *n = read_context->last_err->size();
return read_context->last_err->c_str(); return read_context->last_err->c_str();
} }
nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message"); nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message");
return nullptr; return nullptr;
} }
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
char *value, int n) { {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
"Last error was not a nix error"); }
} return nix_export_std_string(context, read_context->name, value, n);
return nix_export_std_string(context, read_context->name, value, n);
} }
nix_err nix_err_info_msg(nix_c_context *context, nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
const nix_c_context *read_context, char *value, {
int n) { if (context)
if (context) context->last_err_code = NIX_OK;
context->last_err_code = NIX_OK; if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, }
"Last error was not a nix error"); return nix_export_std_string(context, read_context->info->msg.str(), value, n);
}
return nix_export_std_string(context, read_context->info->msg.str(), value,
n);
} }
nix_err nix_err_code(const nix_c_context *read_context) { nix_err nix_err_code(const nix_c_context * read_context)
return read_context->last_err_code; {
return read_context->last_err_code;
} }
// internal // internal
nix_err nix_export_std_string(nix_c_context *context, nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n)
const std::string_view str, char *dest, {
unsigned int n) { size_t i = str.copy(dest, n - 1);
size_t i = str.copy(dest, n - 1); dest[i] = 0;
dest[i] = 0; if (i == n - 1) {
if (i == n - 1) { return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short");
return nix_set_err_msg(context, NIX_ERR_OVERFLOW, } else
"Provided buffer too short"); return NIX_OK;
} else
return NIX_OK;
} }

View file

@ -127,12 +127,12 @@ typedef struct nix_c_context nix_c_context;
* @return allocated nix_c_context, owned by the caller. Free using * @return allocated nix_c_context, owned by the caller. Free using
* `nix_c_context_free`. * `nix_c_context_free`.
*/ */
nix_c_context *nix_c_context_create(); nix_c_context * nix_c_context_create();
/** /**
* @brief Free a nix_c_context. Does not fail. * @brief Free a nix_c_context. Does not fail.
* @param[out] context The context to free, mandatory. * @param[out] context The context to free, mandatory.
*/ */
void nix_c_context_free(nix_c_context *context); void nix_c_context_free(nix_c_context * context);
/** /**
* @} * @}
*/ */
@ -147,7 +147,7 @@ void nix_c_context_free(nix_c_context *context);
* @return NIX_OK if the initialization is successful, or an error code * @return NIX_OK if the initialization is successful, or an error code
* otherwise. * otherwise.
*/ */
nix_err nix_libutil_init(nix_c_context *context); nix_err nix_libutil_init(nix_c_context * context);
/** @defgroup settings /** @defgroup settings
* @{ * @{
@ -167,8 +167,7 @@ nix_err nix_libutil_init(nix_c_context *context);
* provided buffer is too short, or NIX_OK if the setting was retrieved * provided buffer is too short, or NIX_OK if the setting was retrieved
* successfully. * successfully.
*/ */
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n);
int n);
/** /**
* @brief Sets a setting in the nix global configuration. * @brief Sets a setting in the nix global configuration.
@ -184,8 +183,7 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
* @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was
* set successfully. * set successfully.
*/ */
nix_err nix_setting_set(nix_c_context *context, const char *key, nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value);
const char *value);
/** /**
* @} * @}
@ -198,7 +196,7 @@ nix_err nix_setting_set(nix_c_context *context, const char *key,
* Does not fail. * Does not fail.
* @return A static string representing the version of the nix library. * @return A static string representing the version of the nix library.
*/ */
const char *nix_version_get(); const char * nix_version_get();
/** @addtogroup errors /** @addtogroup errors
* @{ * @{
@ -217,8 +215,7 @@ const char *nix_version_get();
* @return nullptr if no error message was ever set, * @return nullptr if no error message was ever set,
* a borrowed pointer to the error message otherwise. * a borrowed pointer to the error message otherwise.
*/ */
const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx, const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, unsigned int * n);
unsigned int *n);
/** /**
* @brief Retrieves the error message from errorInfo in a context. * @brief Retrieves the error message from errorInfo in a context.
@ -235,8 +232,7 @@ const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx,
* @param[in] n Maximum size of the returned string. * @param[in] n Maximum size of the returned string.
* @return NIX_OK if there were no errors, an error code otherwise. * @return NIX_OK if there were no errors, an error code otherwise.
*/ */
nix_err nix_err_info_msg(nix_c_context *context, nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
const nix_c_context *read_context, char *value, int n);
/** /**
* @brief Retrieves the error name from a context. * @brief Retrieves the error name from a context.
@ -253,8 +249,7 @@ nix_err nix_err_info_msg(nix_c_context *context,
* @param[in] n Maximum size of the returned string. * @param[in] n Maximum size of the returned string.
* @return NIX_OK if there were no errors, an error code otherwise. * @return NIX_OK if there were no errors, an error code otherwise.
*/ */
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
char *value, int n);
/** /**
* @brief Retrieves the most recent error code from a nix_c_context * @brief Retrieves the most recent error code from a nix_c_context
@ -266,7 +261,7 @@ nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
* @param[in] read_context the context to retrieve the error message from * @param[in] read_context the context to retrieve the error message from
* @return most recent error code stored in the context. * @return most recent error code stored in the context.
*/ */
nix_err nix_err_code(const nix_c_context *read_context); nix_err nix_err_code(const nix_c_context * read_context);
/** /**
* @} * @}

View file

@ -7,14 +7,15 @@
#include "error.hh" #include "error.hh"
#include "nix_api_util.h" #include "nix_api_util.h"
struct nix_c_context { struct nix_c_context
nix_err last_err_code = NIX_OK; {
std::optional<std::string> last_err = {}; nix_err last_err_code = NIX_OK;
std::optional<nix::ErrorInfo> info = {}; std::optional<std::string> last_err = {};
std::string name = ""; std::optional<nix::ErrorInfo> info = {};
std::string name = "";
}; };
nix_err nix_context_error(nix_c_context *context); nix_err nix_context_error(nix_c_context * context);
/** /**
* Internal use only. * Internal use only.
@ -26,7 +27,7 @@ nix_err nix_context_error(nix_c_context *context);
* @param msg The error message to set. * @param msg The error message to set.
* @returns the error code set * @returns the error code set
*/ */
nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg); nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg);
/** /**
* Internal use only. * Internal use only.
@ -40,21 +41,21 @@ nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg);
* @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length * @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length
* exceeds `n`. * exceeds `n`.
*/ */
nix_err nix_export_std_string(nix_c_context *context, nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n);
const std::string_view str, char *dest,
unsigned int n);
#define NIXC_CATCH_ERRS \ #define NIXC_CATCH_ERRS \
catch (...) { \ catch (...) \
return nix_context_error(context); \ { \
} \ return nix_context_error(context); \
return NIX_OK; } \
return NIX_OK;
#define NIXC_CATCH_ERRS_RES(def) \ #define NIXC_CATCH_ERRS_RES(def) \
catch (...) { \ catch (...) \
nix_context_error(context); \ { \
return def; \ nix_context_error(context); \
} return def; \
}
#define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr) #define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr)
#endif // NIX_API_UTIL_INTERNAL_H #endif // NIX_API_UTIL_INTERNAL_H