test: dynamic function call tests

This commit is contained in:
2026-05-05 21:16:56 +01:00
parent f3062222cb
commit 4b20f8ca0e
3 changed files with 96 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
#include <fx/reflection/assembly.h>
#include <fx/reflection/function.h>
#include <fx/reflection/type.h>
#include <fx/string.h>
#include <stdio.h>
extern fx_assembly *fx_runtime_assembly_get(void);
static fx_string *create_string(const fx_type *ty)
{
const fx_function *string_create = fx_type_get_function(
ty,
"create_from_cstr");
if (!string_create) {
printf("cannot find create_from_cstr()\n");
return NULL;
}
printf("found fx.string.create_from_cstr()\n");
fx_value args[] = {FX_VALUE_CSTR("Hello, world!")};
fx_value result = FX_VALUE_EMPTY;
fx_status status = fx_function_invoke(
string_create,
args,
sizeof args / sizeof args[0],
&result);
if (!FX_OK(status)) {
printf("call to fx.string.create_from_cstr() failed (%s)\n",
fx_status_description(status));
return NULL;
}
fx_string *str = result.v_pointer;
return str;
}
static const char *get_cstr(const fx_type *ty, fx_string *str)
{
const fx_function *get_cstr = fx_type_get_function(ty, "get_cstr");
if (!get_cstr) {
printf("cannot find get_cstr()\n");
return NULL;
}
printf("found fx.string.get_cstr()\n");
fx_value args[] = {FX_VALUE_POINTER(str)};
fx_value result = FX_VALUE_EMPTY;
fx_status status = fx_function_invoke(
get_cstr,
args,
sizeof args / sizeof args[0],
&result);
if (!FX_OK(status)) {
printf("call to fx.string.get_cstr() failed (%s)\n",
fx_status_description(status));
return NULL;
}
const char *cstr = result.v_cstr;
return cstr;
}
int main(int argc, const char **argv)
{
fx_assembly *fx_runtime = fx_runtime_assembly_get();
fx_assembly_dump(fx_runtime);
const fx_type *ty = fx_type_get_by_name("fx.string");
if (!ty) {
printf("cannot find type\n");
return -1;
}
printf("found type '%s'\n", fx_type_get_name(ty));
fx_string *str = create_string(ty);
if (!str) {
return -1;
}
const char *cstr = get_cstr(ty, str);
if (!cstr) {
return -1;
}
printf("created string '%s'\n", cstr);
return 0;
}
+1
View File
@@ -1,4 +1,5 @@
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value.h>
#include <inttypes.h>
#include <stdio.h>
+8
View File
@@ -0,0 +1,8 @@
#include <fx/reflection/function.h>
#include <fx/value.h>
#include <stdio.h>
int main(int argc, const char **argv)
{
return 0;
}