Implement most missing wasm-c-api APIs (#303) (#676)

Remove unnecessary functions and implement more APIs:
- wasm_##name##same
- wasm##name##as_ref
- wasm_ref_as##name##
- wasm_ref_delete
- wasm_module_validate
- wasm_table_get/set/size
- wasm_memory_size
- wasm_config_new
- wasm_foreign_new

And add more wasm-c-api samples, update the related documen, add more CI rules.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-07-28 21:53:37 +08:00
committed by GitHub
parent 4193949ef5
commit edb184f709
24 changed files with 1872 additions and 450 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@
#ifndef _WASM_C_API_INTERNAL_H
#define _WASM_C_API_INTERNAL_H
#include "wasm_c_api.h"
#include "../include/wasm_c_api.h"
#include "wasm_runtime_common.h"
#ifndef own
@ -19,9 +19,9 @@
/* caller needs to take care resource for the vector itself */
#define DEFAULT_VECTOR_INIT_LENGTH (64)
WASM_DECLARE_VEC(store, *)
WASM_DECLARE_VEC(module, *)
WASM_DECLARE_VEC(instance, *)
WASM_DECLARE_VEC(module, *)
WASM_DECLARE_VEC(store, *)
/* Runtime Environment */
struct wasm_engine_t {
@ -32,6 +32,7 @@ struct wasm_engine_t {
struct wasm_store_t {
wasm_module_vec_t *modules;
wasm_instance_vec_t *instances;
Vector *foreigns;
};
/* Type Representations */
@ -82,8 +83,25 @@ struct wasm_exporttype_t {
};
/* Runtime Objects */
enum wasm_reference_kind {
WASM_REF_foreign,
WASM_REF_func,
WASM_REF_global,
WASM_REF_memory,
WASM_REF_table,
};
struct wasm_host_info {
void *info;
void (*finalizer)(void *);
};
struct wasm_ref_t {
uint32 obj;
wasm_store_t *store;
enum wasm_reference_kind kind;
struct wasm_host_info host_info;
uint32 ref_idx_rt;
WASMModuleInstanceCommon *inst_comm_rt;
};
struct wasm_trap_t {
@ -91,11 +109,22 @@ struct wasm_trap_t {
Vector *frames;
};
struct wasm_foreign_t {
wasm_store_t *store;
enum wasm_reference_kind kind;
struct wasm_host_info host_info;
int32 ref_cnt;
uint32 foreign_idx_rt;
WASMModuleInstanceCommon *inst_comm_rt;
};
struct wasm_func_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_functype_t *type;
bool with_env;
@ -117,10 +146,12 @@ struct wasm_func_t {
};
struct wasm_global_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_globaltype_t *type;
wasm_val_t *init;
/*
@ -132,10 +163,12 @@ struct wasm_global_t {
};
struct wasm_memory_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_memorytype_t *type;
/*
* an index in both memory runtime instance lists
@ -146,10 +179,12 @@ struct wasm_memory_t {
};
struct wasm_table_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_tabletype_t *type;
/*
* an index in both table runtime instance lists
@ -160,16 +195,49 @@ struct wasm_table_t {
};
struct wasm_extern_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
wasm_externkind_t kind;
uint8 data[1];
uint8 data[4];
};
struct wasm_instance_t {
wasm_store_t *store;
wasm_extern_vec_t *imports;
wasm_extern_vec_t *exports;
struct wasm_host_info host_info;
WASMModuleInstanceCommon *inst_comm_rt;
};
wasm_ref_t *
wasm_ref_new_internal(wasm_store_t *store,
enum wasm_reference_kind kind,
uint32 obj_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_foreign_t *
wasm_foreign_new_internal(wasm_store_t *store,
uint32 foreign_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_func_t *
wasm_func_new_internal(wasm_store_t *store,
uint16 func_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_global_t *
wasm_global_new_internal(wasm_store_t *store,
uint16 global_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_memory_t *
wasm_memory_new_internal(wasm_store_t *store,
uint16 memory_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_table_t *
wasm_table_new_internal(wasm_store_t *store,
uint16 table_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
#endif /* _WASM_C_API_INTERNAL_H */

View File

@ -3977,57 +3977,6 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
return false;
}
uint8 *
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory_inst_idx];
return memory_inst->memory_data;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst_comm->module_type == Wasm_Module_AoT) {
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
AOTMemoryInstance *memory_inst =
((AOTMemoryInstance**)module_inst->memories.ptr)[memory_inst_idx];
return memory_inst->memory_data.ptr;
}
#endif
return NULL;
}
uint32
wasm_runtime_get_memory_data_size(
const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory_inst_idx];
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst_comm->module_type == Wasm_Module_AoT) {
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
AOTMemoryInstance *memory_inst =
((AOTMemoryInstance**)module_inst->memories.ptr)[memory_inst_idx];
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
}
#endif
return 0;
}
static inline bool
argv_to_params(wasm_val_t *out_params,
const uint32 *argv,
@ -4058,6 +4007,23 @@ argv_to_params(wasm_val_t *out_params,
u32[0] = *argv++;
u32[1] = *argv++;
break;
#if WASM_ENABLE_REF_TYPES != 0
case VALUE_TYPE_EXTERNREF:
param->kind = WASM_ANYREF;
if (NULL_REF == *argv) {
param->of.ref = NULL;
}
else {
if (!wasm_externref_ref2obj(*argv,
(void **)&param->of.ref)) {
return false;
}
}
argv++;
break;
#endif
default:
return false;
}
@ -4067,7 +4033,8 @@ argv_to_params(wasm_val_t *out_params,
}
static inline bool
results_to_argv(uint32 *out_argv,
results_to_argv(WASMModuleInstanceCommon *module_inst,
uint32 *out_argv,
const wasm_val_t *results,
WASMType *func_type)
{
@ -4087,6 +4054,15 @@ results_to_argv(uint32 *out_argv,
*argv++ = u32[0];
*argv++ = u32[1];
break;
#if WASM_ENABLE_REF_TYPES != 0
case VALUE_TYPE_EXTERNREF:
if (!wasm_externref_obj2ref(module_inst, result->of.ref,
argv)) {
return false;
}
argv++;
break;
#endif
default:
return false;
}
@ -4134,7 +4110,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
char trap_message[128] = { 0 };
bh_memcpy_s(
trap_message, 127, trap->message->data,
(trap->message->size < 127 ? trap->message->size : 127));
(trap->message->size < 127 ? (uint32)trap->message->size : 127));
wasm_runtime_set_exception(module_inst, trap_message);
}
else {
@ -4152,7 +4128,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
goto fail;
}
if (!results_to_argv(argv, results, func_type)) {
if (!results_to_argv(module_inst, argv, results, func_type)) {
wasm_runtime_set_exception(module_inst, "unsupported result type");
goto fail;
}

View File

@ -796,14 +796,6 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
uint32 *out_min_size,
uint32 *out_max_size);
uint8 *
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx);
uint32
wasm_runtime_get_memory_data_size(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx);
bool
wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
void *func_ptr, WASMType *func_type,