fx: support for registering callable functions with types

This commit is contained in:
2026-05-05 21:16:31 +01:00
parent 703155affe
commit f3062222cb
16 changed files with 182 additions and 25 deletions
+19 -3
View File
@@ -22,6 +22,11 @@ struct fx_function_p {
/*** PRIVATE FUNCTIONS ********************************************************/
static const char *function_get_name(const struct fx_function_p *func)
{
return func->func_name;
}
static fx_status function_bind(
struct fx_function_p *func,
fx_value *args,
@@ -98,10 +103,9 @@ static fx_status function_invoke(
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS
* *********************************************************/
/*** PUBLIC FUNCTIONS *********************************************************/
FX_API fx_function *fx_function_create(
fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
@@ -118,6 +122,10 @@ FX_API fx_function *fx_function_create(
func,
FX_REFLECTION_TYPE_FUNCTION);
if (nr_args == 1 && args[0] == FX_VALUE_TYPE_NONE) {
nr_args = 0;
}
p->func_name = fx_strdup(name);
p->func_flags = flags;
p->func_impl = impl;
@@ -131,6 +139,14 @@ FX_API fx_function *fx_function_create(
return func;
}
const char *fx_function_get_name(const fx_function *func)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_FUNCTION,
function_get_name,
func);
}
fx_status fx_function_bind(fx_function *func, fx_value *args, size_t nr_args)
{
FX_CLASS_DISPATCH_STATIC(
@@ -14,6 +14,7 @@ typedef enum fx_function_flags {
FX_FUNCTION_F_STATIC = 0x01u,
FX_FUNCTION_F_VIRTUAL = 0x02u,
FX_FUNCTION_F_VARARG = 0x04u,
FX_FUNCTION_F_CONSTRUCTOR = 0x08u,
} fx_function_flags;
typedef void (*fx_function_impl)();
@@ -25,6 +26,8 @@ FX_TYPE_CLASS_DECLARATION_END(fx_function)
FX_API fx_type_id fx_function_get_type();
FX_API const char *fx_function_get_name(const fx_function *func);
FX_API fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
@@ -2,6 +2,7 @@
#define FX_REFLECTION_TYPE_H_
#include <fx/macros.h>
#include <fx/reflection/function.h>
FX_DECLS_BEGIN;
@@ -17,6 +18,9 @@ FX_TYPE_CLASS_DECLARATION_END(fx_type)
FX_API fx_type_id fx_type_get_type();
FX_API const char *fx_type_get_name(const fx_type *ty);
FX_API const fx_function *fx_type_get_function(
const fx_type *ty,
const char *name);
FX_API const fx_type *fx_type_get_by_id(fx_type_id id);
FX_API const fx_type *fx_type_get_by_name(const char *name);
+19
View File
@@ -12,12 +12,22 @@ struct fx_type_p {
extern struct fx_type_info *fx_type_info_get_by_id(const union fx_type_id *key);
extern struct fx_type_info *fx_type_info_get_by_name(const char *name);
extern fx_function *fx_type_info_get_function_by_name(
const struct fx_type_info *ty,
const char *name);
static const char *type_get_name(const struct fx_type_p *ty)
{
return ty->ty_info->ty_name;
}
const fx_function *type_get_function(
const struct fx_type_p *ty,
const char *name)
{
return fx_type_info_get_function_by_name(ty->ty_info, name);
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_type *__fx_type_create(struct fx_type_info *type_info)
@@ -40,6 +50,15 @@ const char *fx_type_get_name(const fx_type *ty)
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_name, ty);
}
const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_TYPE,
type_get_function,
ty,
name);
}
const fx_type *fx_type_get_by_id(fx_type_id id)
{
struct fx_type_info *ty = fx_type_info_get_by_id(id);