fx.reflection: function: update invocation to support new value-types

This commit is contained in:
2026-05-17 17:11:02 +01:00
parent f1258489d1
commit 61182e0153
4 changed files with 196 additions and 23 deletions
+43 -7
View File
@@ -1,6 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
@@ -8,8 +9,8 @@ struct fx_function_p {
char *func_name;
fx_function_flags func_flags;
fx_function_impl func_impl;
fx_value_type func_return_type;
fx_value_type *func_arg_types;
fx_type_id func_return_type;
fx_type_id *func_arg_types;
/* number of explicit arguments that the function takes.
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
* variable number of arguments, which may be handled differently by the
@@ -61,6 +62,28 @@ static fx_status function_bind(
return FX_SUCCESS;
}
static void push_arg(
const fx_value *arg,
fx_type_id param_type,
struct callvm *vm)
{
if (!param_type) {
callvm_push(vm, arg);
return;
}
unsigned int param_value_type = __fx_type_get_value_type(param_type);
if (param_value_type == __FX_VALUE_TYPE_CSTR
&& fx_value_is_type(arg, FX_TYPE_STRING)) {
fx_value cstr = FX_CSTR(
fx_string_get_cstr(fx_value_get_object_c(arg)));
callvm_push(vm, &cstr);
return;
}
callvm_push(vm, arg);
}
static fx_status function_invoke(
const struct fx_function_p *func,
const fx_value *args,
@@ -84,14 +107,27 @@ static fx_status function_invoke(
struct callvm vm = {0};
callvm_reset(&vm, nr_fixed_args);
size_t param_index = 0;
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
const fx_value *arg = &func->func_bound_args[i];
callvm_push(&vm, arg);
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
for (size_t i = 0; i < nr_args; i++) {
const fx_value *arg = &args[i];
callvm_push(&vm, arg);
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
*return_value = callvm_invoke(
@@ -109,9 +145,9 @@ fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
const fx_value_type *args,
const fx_type_id *args,
size_t nr_args,
fx_value_type return_type)
fx_type_id return_type)
{
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
if (!func) {
@@ -122,7 +158,7 @@ fx_function *fx_function_create(
func,
FX_REFLECTION_TYPE_FUNCTION);
if (nr_args == 1 && args[0] == FX_VALUE_TYPE_NONE) {
if (nr_args == 1 && args[0] == NULL) {
nr_args = 0;
}