1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

More extern "C" for FFI

This allows us to catch the header and file getting out of sync, because
we are not doing overloading by mistake.
This commit is contained in:
John Ericson 2025-09-02 01:54:27 -04:00 committed by Jörg Thalheim
parent 1935c19705
commit 7e4608a3f8
11 changed files with 48 additions and 0 deletions

View file

@ -40,6 +40,8 @@ static T * unsafe_new_with_self(F && init)
return new (p) T(init(static_cast<T *>(p))); return new (p) T(init(static_cast<T *>(p)));
} }
extern "C" {
nix_err nix_libexpr_init(nix_c_context * context) nix_err nix_libexpr_init(nix_c_context * context)
{ {
if (context) if (context)
@ -287,3 +289,5 @@ void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * o
GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0);
#endif #endif
} }
} // extern "C"

View file

@ -8,6 +8,8 @@
#include "nix_api_value.h" #include "nix_api_value.h"
#include "nix/expr/search-path.hh" #include "nix/expr/search-path.hh"
extern "C" {
struct nix_eval_state_builder struct nix_eval_state_builder
{ {
nix::ref<nix::Store> store; nix::ref<nix::Store> store;
@ -61,4 +63,6 @@ struct nix_realised_string
std::vector<StorePath> storePaths; std::vector<StorePath> storePaths;
}; };
} // extern "C"
#endif // NIX_API_EXPR_INTERNAL_H #endif // NIX_API_EXPR_INTERNAL_H

View file

@ -14,6 +14,8 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
extern "C" {
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;
@ -40,6 +42,8 @@ nix_err nix_external_add_string_context(nix_c_context * context, nix_string_cont
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
} // extern "C"
class NixCExternalValue : public nix::ExternalValueBase class NixCExternalValue : public nix::ExternalValueBase
{ {
NixCExternalValueDesc & desc; NixCExternalValueDesc & desc;
@ -170,6 +174,8 @@ public:
virtual ~NixCExternalValue() override {}; virtual ~NixCExternalValue() override {};
}; };
extern "C" {
ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v) ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v)
{ {
if (context) if (context)
@ -198,3 +204,5 @@ void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
} // extern "C"

View file

@ -111,6 +111,8 @@ static void nix_c_primop_wrapper(
v = vTmp; v = vTmp;
} }
extern "C" {
PrimOp * nix_alloc_primop( PrimOp * nix_alloc_primop(
nix_c_context * context, nix_c_context * context,
PrimOpFun fun, PrimOpFun fun,
@ -651,3 +653,5 @@ const StorePath * nix_realised_string_get_store_path(nix_realised_string * s, si
{ {
return &s->storePaths[i]; return &s->storePaths[i];
} }
} // extern "C"

View file

@ -2,6 +2,8 @@
#include "nix_api_fetchers_internal.hh" #include "nix_api_fetchers_internal.hh"
#include "nix_api_util_internal.h" #include "nix_api_util_internal.h"
extern "C" {
nix_fetchers_settings * nix_fetchers_settings_new(nix_c_context * context) nix_fetchers_settings * nix_fetchers_settings_new(nix_c_context * context)
{ {
try { try {
@ -17,3 +19,5 @@ void nix_fetchers_settings_free(nix_fetchers_settings * settings)
{ {
delete settings; delete settings;
} }
} // extern "C"

View file

@ -10,6 +10,8 @@
#include "nix/flake/flake.hh" #include "nix/flake/flake.hh"
extern "C" {
nix_flake_settings * nix_flake_settings_new(nix_c_context * context) nix_flake_settings * nix_flake_settings_new(nix_c_context * context)
{ {
nix_clear_err(context); nix_clear_err(context);
@ -203,3 +205,5 @@ nix_value * nix_locked_flake_get_output_attrs(
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
} // extern "C"

View file

@ -5,6 +5,8 @@
#include "nix/main/plugin.hh" #include "nix/main/plugin.hh"
extern "C" {
nix_err nix_init_plugins(nix_c_context * context) nix_err nix_init_plugins(nix_c_context * context)
{ {
if (context) if (context)
@ -14,3 +16,5 @@ nix_err nix_init_plugins(nix_c_context * context)
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
} // extern "C"

View file

@ -10,6 +10,8 @@
#include "nix/store/globals.hh" #include "nix/store/globals.hh"
extern "C" {
nix_err nix_libstore_init(nix_c_context * context) nix_err nix_libstore_init(nix_c_context * context)
{ {
if (context) if (context)
@ -180,3 +182,5 @@ nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
} // extern "C"

View file

@ -2,6 +2,8 @@
#define NIX_API_STORE_INTERNAL_H #define NIX_API_STORE_INTERNAL_H
#include "nix/store/store-api.hh" #include "nix/store/store-api.hh"
extern "C" {
struct Store struct Store
{ {
nix::ref<nix::Store> ptr; nix::ref<nix::Store> ptr;
@ -12,4 +14,6 @@ struct StorePath
nix::StorePath path; nix::StorePath path;
}; };
} // extern "C"
#endif #endif

View file

@ -9,6 +9,8 @@
#include "nix_api_util_config.h" #include "nix_api_util_config.h"
extern "C" {
nix_c_context * nix_c_context_create() nix_c_context * nix_c_context_create()
{ {
return new nix_c_context(); return new nix_c_context();
@ -156,3 +158,5 @@ nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callb
callback(str.c_str(), str.size(), user_data); callback(str.c_str(), str.size(), user_data);
return NIX_OK; return NIX_OK;
} }
} // extern "C"

View file

@ -7,6 +7,8 @@
#include "nix/util/error.hh" #include "nix/util/error.hh"
#include "nix_api_util.h" #include "nix_api_util.h"
extern "C" {
struct nix_c_context struct nix_c_context
{ {
nix_err last_err_code = NIX_OK; nix_err last_err_code = NIX_OK;
@ -47,4 +49,6 @@ nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callb
} }
#define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr) #define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr)
} // extern "C"
#endif // NIX_API_UTIL_INTERNAL_H #endif // NIX_API_UTIL_INTERNAL_H