Implement more wasm-c-apis and enable Envoy integration (#622)
Implement more wasm-c-api APIs to support Envoy integration: - sync up with latest c-api definition - change CMakeLists to export necessary headers and install the static library of iwasm - enable to export tables and memories - support memorytype and tabletype APIs - update wasm-c-api sampels - enable to export importtype APIs And refine bazel scripts for sample XNNPACK workload, add license headers for sample simple. Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
@ -29,15 +29,15 @@ extern "C" {
|
||||
// Auxiliaries
|
||||
|
||||
// Machine types
|
||||
#if (__STDC_VERSION__ > 199901L)
|
||||
inline void assertions() {
|
||||
#if (__STDC_VERSION__) > 199901L
|
||||
inline void assertions(void) {
|
||||
static_assert(sizeof(float) == sizeof(uint32_t), "incompatible float type");
|
||||
static_assert(sizeof(double) == sizeof(uint64_t), "incompatible double type");
|
||||
static_assert(sizeof(intptr_t) == sizeof(uint32_t) ||
|
||||
sizeof(intptr_t) == sizeof(uint64_t),
|
||||
"incompatible pointer type");
|
||||
}
|
||||
#endif /* __STDC_VERSION__ > 199901L */
|
||||
#endif
|
||||
|
||||
typedef char byte_t;
|
||||
typedef float float32_t;
|
||||
@ -79,7 +79,7 @@ typedef double float64_t;
|
||||
// Vectors
|
||||
// size: capacity
|
||||
// num_elems: current number of elements
|
||||
// size_of_elem: size of one element
|
||||
// size_of_elem: size of one elemen
|
||||
#define WASM_DECLARE_VEC(name, ptr_or_none) \
|
||||
typedef struct wasm_##name##_vec_t { \
|
||||
size_t size; \
|
||||
@ -115,6 +115,12 @@ typedef wasm_byte_vec_t wasm_name_t;
|
||||
|
||||
static inline void wasm_name_new_from_string(
|
||||
own wasm_name_t* out, const char* s
|
||||
) {
|
||||
wasm_name_new(out, strlen(s), s);
|
||||
}
|
||||
|
||||
static inline void wasm_name_new_from_string_nt(
|
||||
own wasm_name_t* out, const char* s
|
||||
) {
|
||||
wasm_name_new(out, strlen(s) + 1, s);
|
||||
}
|
||||
@ -123,11 +129,21 @@ static inline void wasm_name_new_from_string(
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Runtime Environment
|
||||
|
||||
// Configuration
|
||||
|
||||
WASM_DECLARE_OWN(config)
|
||||
|
||||
WASM_API_EXTERN own wasm_config_t* wasm_config_new(void);
|
||||
|
||||
// Embedders may provide custom functions for manipulating configs.
|
||||
|
||||
|
||||
// Engine
|
||||
|
||||
WASM_DECLARE_OWN(engine)
|
||||
|
||||
WASM_API_EXTERN own wasm_engine_t* wasm_engine_new();
|
||||
WASM_API_EXTERN own wasm_engine_t* wasm_engine_new(void);
|
||||
WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(own wasm_config_t*);
|
||||
|
||||
|
||||
// Store
|
||||
@ -162,7 +178,7 @@ static const uint32_t wasm_limits_max_default = 0xffffffff;
|
||||
WASM_DECLARE_OWN(name) \
|
||||
WASM_DECLARE_VEC(name, *) \
|
||||
\
|
||||
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(wasm_##name##_t*);
|
||||
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*);
|
||||
|
||||
|
||||
// Value Types
|
||||
@ -334,7 +350,12 @@ WASM_DECLARE_VEC(val, )
|
||||
WASM_DECLARE_OWN(name) \
|
||||
\
|
||||
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
|
||||
WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*);
|
||||
WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
|
||||
\
|
||||
WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
|
||||
WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
|
||||
WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
|
||||
wasm_##name##_t*, void*, void (*)(void*));
|
||||
|
||||
#define WASM_DECLARE_REF(name) \
|
||||
WASM_DECLARE_REF_BASE(name) \
|
||||
@ -354,17 +375,42 @@ WASM_DECLARE_VEC(val, )
|
||||
|
||||
WASM_DECLARE_REF_BASE(ref)
|
||||
|
||||
|
||||
// Frames
|
||||
|
||||
WASM_DECLARE_OWN(frame)
|
||||
WASM_DECLARE_VEC(frame, *)
|
||||
WASM_API_EXTERN own wasm_frame_t* wasm_frame_copy(const wasm_frame_t*);
|
||||
|
||||
WASM_API_EXTERN struct wasm_instance_t* wasm_frame_instance(const wasm_frame_t*);
|
||||
WASM_API_EXTERN uint32_t wasm_frame_func_index(const wasm_frame_t*);
|
||||
WASM_API_EXTERN size_t wasm_frame_func_offset(const wasm_frame_t*);
|
||||
WASM_API_EXTERN size_t wasm_frame_module_offset(const wasm_frame_t*);
|
||||
|
||||
|
||||
// Traps
|
||||
|
||||
typedef wasm_name_t wasm_message_t; // null terminated
|
||||
|
||||
WASM_DECLARE_REF_BASE(trap)
|
||||
WASM_DECLARE_REF(trap)
|
||||
|
||||
WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*);
|
||||
|
||||
WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out);
|
||||
WASM_API_EXTERN own wasm_frame_t* wasm_trap_origin(const wasm_trap_t*);
|
||||
WASM_API_EXTERN void wasm_trap_trace(const wasm_trap_t*, own wasm_frame_vec_t* out);
|
||||
|
||||
|
||||
// Foreign Objects
|
||||
|
||||
WASM_DECLARE_REF(foreign)
|
||||
|
||||
WASM_API_EXTERN own wasm_foreign_t* wasm_foreign_new(wasm_store_t*);
|
||||
|
||||
|
||||
// Modules
|
||||
// WASM_DECLARE_SHARABLE_REF(module)
|
||||
|
||||
#ifndef WASM_MODULE_T_DEFINED
|
||||
#define WASM_MODULE_T_DEFINED
|
||||
struct WASMModuleCommon;
|
||||
@ -374,12 +420,9 @@ typedef struct WASMModuleCommon *wasm_module_t;
|
||||
WASM_API_EXTERN own wasm_module_t* wasm_module_new(
|
||||
wasm_store_t*, const wasm_byte_vec_t* binary);
|
||||
|
||||
WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
|
||||
|
||||
WASM_API_EXTERN void wasm_module_delete(own wasm_module_t*);
|
||||
|
||||
WASM_API_EXTERN own wasm_module_t* wasm_module_copy(const wasm_module_t*);
|
||||
WASM_API_EXTERN bool wasm_module_same(const wasm_module_t*, const wasm_module_t*);
|
||||
WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
|
||||
|
||||
WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out);
|
||||
WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out);
|
||||
@ -393,7 +436,7 @@ WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const
|
||||
WASM_DECLARE_REF(func)
|
||||
|
||||
typedef own wasm_trap_t* (*wasm_func_callback_t)(
|
||||
const wasm_val_t args[], wasm_val_t results[]);
|
||||
const wasm_val_t args[], own wasm_val_t results[]);
|
||||
typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
|
||||
void* env, const wasm_val_t args[], wasm_val_t results[]);
|
||||
|
||||
@ -413,7 +456,7 @@ WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
|
||||
|
||||
// Global Instances
|
||||
|
||||
WASM_DECLARE_REF_BASE(global)
|
||||
WASM_DECLARE_REF(global)
|
||||
|
||||
WASM_API_EXTERN own wasm_global_t* wasm_global_new(
|
||||
wasm_store_t*, const wasm_globaltype_t*, const wasm_val_t*);
|
||||
@ -426,7 +469,7 @@ WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);
|
||||
|
||||
// Table Instances
|
||||
|
||||
WASM_DECLARE_REF_BASE(table)
|
||||
WASM_DECLARE_REF(table)
|
||||
|
||||
typedef uint32_t wasm_table_size_t;
|
||||
|
||||
@ -444,7 +487,7 @@ WASM_API_EXTERN bool wasm_table_grow(wasm_table_t*, wasm_table_size_t delta, was
|
||||
|
||||
// Memory Instances
|
||||
|
||||
WASM_DECLARE_REF_BASE(memory)
|
||||
WASM_DECLARE_REF(memory)
|
||||
|
||||
typedef uint32_t wasm_memory_pages_t;
|
||||
|
||||
@ -463,7 +506,7 @@ WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta)
|
||||
|
||||
// Externals
|
||||
|
||||
WASM_DECLARE_REF_BASE(extern)
|
||||
WASM_DECLARE_REF(extern)
|
||||
WASM_DECLARE_VEC(extern, *)
|
||||
|
||||
WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*);
|
||||
@ -492,10 +535,10 @@ WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_exte
|
||||
|
||||
// Module Instances
|
||||
|
||||
WASM_DECLARE_REF_BASE(instance)
|
||||
WASM_DECLARE_REF(instance)
|
||||
|
||||
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_t* const imports[],
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_t *const imports[],
|
||||
own wasm_trap_t**
|
||||
);
|
||||
|
||||
@ -505,32 +548,38 @@ WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_exte
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Convenience
|
||||
|
||||
// Vectors
|
||||
|
||||
#define WASM_EMPTY_VEC {0, NULL, 0, 0}
|
||||
#define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array, sizeof(array)/sizeof(*(array)), sizeof(*(array))}
|
||||
|
||||
|
||||
// Value Type construction short-hands
|
||||
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_i32() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_i32(void) {
|
||||
return wasm_valtype_new(WASM_I32);
|
||||
}
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_i64() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_i64(void) {
|
||||
return wasm_valtype_new(WASM_I64);
|
||||
}
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_f32() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_f32(void) {
|
||||
return wasm_valtype_new(WASM_F32);
|
||||
}
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_f64() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_f64(void) {
|
||||
return wasm_valtype_new(WASM_F64);
|
||||
}
|
||||
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_anyref() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_anyref(void) {
|
||||
return wasm_valtype_new(WASM_ANYREF);
|
||||
}
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_funcref() {
|
||||
static inline own wasm_valtype_t* wasm_valtype_new_funcref(void) {
|
||||
return wasm_valtype_new(WASM_FUNCREF);
|
||||
}
|
||||
|
||||
|
||||
// Function Types construction short-hands
|
||||
|
||||
static inline own wasm_functype_t* wasm_functype_new_0_0() {
|
||||
static inline own wasm_functype_t* wasm_functype_new_0_0(void) {
|
||||
wasm_valtype_vec_t params, results;
|
||||
wasm_valtype_vec_new_empty(¶ms);
|
||||
wasm_valtype_vec_new_empty(&results);
|
||||
@ -677,6 +726,13 @@ static inline void* wasm_val_ptr(const wasm_val_t* val) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}}
|
||||
#define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}}
|
||||
#define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}}
|
||||
#define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}}
|
||||
#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}}
|
||||
#define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
Reference in New Issue
Block a user