From e0b5a8152311769c559c9f2eb484db605b36f108 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 2 Nov 2023 20:47:12 +0900 Subject: [PATCH] examples/hostfunc: implement a host func version of load_call --- examples/hostfunc/hostfunc.c | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/examples/hostfunc/hostfunc.c b/examples/hostfunc/hostfunc.c index d086876f..d4388d1b 100644 --- a/examples/hostfunc/hostfunc.c +++ b/examples/hostfunc/hostfunc.c @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include static int @@ -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 =