mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-12 12:41:25 +00:00

Introduce module instance context APIs which can set one or more contexts created by the embedder for a wasm module instance: ```C wasm_runtime_create_context_key wasm_runtime_destroy_context_key wasm_runtime_set_context wasm_runtime_set_context_spread wasm_runtime_get_context ``` And make libc-wasi use it and set wasi context as the first context bound to the wasm module instance. Also add samples. Refer to https://github.com/bytecodealliance/wasm-micro-runtime/issues/2460.
33 lines
857 B
C
33 lines
857 B
C
/*
|
|
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#include "wasm_export.h"
|
|
#include "my_context.h"
|
|
|
|
void
|
|
set_context(wasm_exec_env_t exec_env, int32_t n)
|
|
{
|
|
wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
|
|
printf("%s called on module inst %p\n", __func__, inst);
|
|
struct my_context *ctx = &my_context;
|
|
ctx->x = n;
|
|
wasm_runtime_set_context_spread(inst, my_context_key, ctx);
|
|
}
|
|
|
|
int32_t
|
|
get_context(wasm_exec_env_t exec_env)
|
|
{
|
|
wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
|
|
printf("%s called on module inst %p\n", __func__, inst);
|
|
struct my_context *ctx = wasm_runtime_get_context(inst, my_context_key);
|
|
if (ctx == NULL) {
|
|
return -1;
|
|
}
|
|
return ctx->x;
|
|
}
|