Skip to content

Commit

Permalink
examples/hostfunc: implement a host func version of load_call
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Nov 2, 2023
1 parent 189da21 commit e0b5a81
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/hostfunc/hostfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <errno.h>
#include <stdlib.h>

#include <toywasm/cconv.h>
#include <toywasm/endian.h>
#include <toywasm/exec_context.h>
#include <toywasm/host_instance.h>

static int
Expand Down Expand Up @@ -39,8 +41,50 @@ my_host_inst_load(struct exec_context *ctx, struct host_instance *hi,
return host_ret;
}

static int
my_host_inst_load_call(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft, const struct cell *params,
struct cell *results)
{
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t pp = HOST_FUNC_PARAM(ft, params, 0, i32);
int host_ret;
uint32_t le32;

host_ret = host_func_copyin(ctx, &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t p = le32_to_host(le32);
host_ret = host_func_copyin(ctx, &le32, p, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t funcptr = le32_to_host(le32);
p += 4;
le32 = host_to_le32(p);
host_ret = host_func_copyout(ctx, &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
const struct funcinst *func;
host_ret =
cconv_deref_func_ptr(ctx, ctx->instance, funcptr, ft, &func);
if (host_ret != 0) {
goto fail;
}
/* tail call with the same argument */
ctx->event_u.call.func = func;
ctx->event = EXEC_EVENT_CALL;
host_ret = ETOYWASMRESTART;
fail:
HOST_FUNC_FREE_CONVERTED_PARAMS();
return host_ret;
}

static const struct host_func my_host_inst_funcs[] = {
HOST_FUNC(my_host_inst_, load, "(i)i"),
HOST_FUNC(my_host_inst_, load_call, "(i)i"),
};

static const struct name name_my_host_inst =
Expand Down

0 comments on commit e0b5a81

Please sign in to comment.