12#ifndef WASM_API_EXTERN
14#define WASM_API_EXTERN __declspec(dllimport)
16#define WASM_API_EXTERN
29inline void assertions(
void) {
30 static_assert(
sizeof(float) ==
sizeof(uint32_t),
"incompatible float type");
31 static_assert(
sizeof(double) ==
sizeof(uint64_t),
"incompatible double type");
32 static_assert(
sizeof(intptr_t) ==
sizeof(uint32_t) ||
33 sizeof(intptr_t) ==
sizeof(uint64_t),
34 "incompatible pointer type");
68#define WASM_DECLARE_OWN(name) \
69 typedef struct wasm_##name##_t wasm_##name##_t; \
71 WASM_API_EXTERN void wasm_##name##_delete(own wasm_##name##_t*);
76#define WASM_DECLARE_VEC(name, ptr_or_none) \
77 typedef struct wasm_##name##_vec_t { \
79 wasm_##name##_t ptr_or_none* data; \
80 } wasm_##name##_vec_t; \
82 WASM_API_EXTERN void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t* out); \
83 WASM_API_EXTERN void wasm_##name##_vec_new_uninitialized( \
84 own wasm_##name##_vec_t* out, size_t); \
85 WASM_API_EXTERN void wasm_##name##_vec_new( \
86 own wasm_##name##_vec_t* out, \
87 size_t, own wasm_##name##_t ptr_or_none const[]); \
88 WASM_API_EXTERN void wasm_##name##_vec_copy( \
89 own wasm_##name##_vec_t* out, const wasm_##name##_vec_t*); \
90 WASM_API_EXTERN void wasm_##name##_vec_delete(own wasm_##name##_vec_t*);
96WASM_DECLARE_VEC(
byte, )
100#define wasm_name wasm_byte_vec
101#define wasm_name_new wasm_byte_vec_new
102#define wasm_name_new_empty wasm_byte_vec_new_empty
103#define wasm_name_new_new_uninitialized wasm_byte_vec_new_uninitialized
104#define wasm_name_copy wasm_byte_vec_copy
105#define wasm_name_delete wasm_byte_vec_delete
107static inline void wasm_name_new_from_string(
113static inline void wasm_name_new_from_string_nt(
125WASM_DECLARE_OWN(config)
134WASM_DECLARE_OWN(engine)
142WASM_DECLARE_OWN(store)
153enum wasm_mutability_enum {
163static const uint32_t wasm_limits_max_default = 0xffffffff;
168#define WASM_DECLARE_TYPE(name) \
169 WASM_DECLARE_OWN(name) \
170 WASM_DECLARE_VEC(name, *) \
172 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*);
177WASM_DECLARE_TYPE(valtype)
180enum wasm_valkind_enum {
194 return k < WASM_ANYREF;
197 return k >= WASM_ANYREF;
210WASM_DECLARE_TYPE(functype)
221WASM_DECLARE_TYPE(globaltype)
232WASM_DECLARE_TYPE(tabletype)
243WASM_DECLARE_TYPE(memorytype)
252WASM_DECLARE_TYPE(externtype)
255enum wasm_externkind_enum {
287WASM_DECLARE_TYPE(importtype)
299WASM_DECLARE_TYPE(exporttype)
329WASM_DECLARE_VEC(val, )
334#define WASM_DECLARE_REF_BASE(name) \
335 WASM_DECLARE_OWN(name) \
337 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
338 WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
340 WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
341 WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
342 WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
343 wasm_##name##_t*, void*, void (*)(void*));
345#define WASM_DECLARE_REF(name) \
346 WASM_DECLARE_REF_BASE(name) \
348 WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
349 WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
350 WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
351 WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
353#define WASM_DECLARE_SHARABLE_REF(name) \
354 WASM_DECLARE_REF(name) \
355 WASM_DECLARE_OWN(shared_##name) \
357 WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
358 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
361WASM_DECLARE_REF_BASE(ref)
366WASM_DECLARE_OWN(frame)
367WASM_DECLARE_VEC(frame, *)
380WASM_DECLARE_REF(trap)
391WASM_DECLARE_REF(foreign)
398WASM_DECLARE_SHARABLE_REF(module)
414WASM_DECLARE_REF(func)
425 void* env,
void (*finalizer)(
void*));
437WASM_DECLARE_REF(global)
450WASM_DECLARE_REF(table)
468WASM_DECLARE_REF(memory)
472static const
size_t MEMORY_PAGE_SIZE = 0x10000;
487WASM_DECLARE_REF(extern)
488WASM_DECLARE_VEC(extern, *)
516WASM_DECLARE_REF(instance)
531#define WASM_EMPTY_VEC {0, NULL}
532#define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array}
689static inline void wasm_val_init_ptr(own
wasm_val_t* out,
void* p) {
690#if UINTPTR_MAX == UINT32_MAX
691 out->kind = WASM_I32;
692 out->of.i32 = (intptr_t)p;
693#elif UINTPTR_MAX == UINT64_MAX
694 out->kind = WASM_I64;
695 out->of.i64 = (intptr_t)p;
699static inline void* wasm_val_ptr(
const wasm_val_t* val) {
700#if UINTPTR_MAX == UINT32_MAX
701 return (
void*)(intptr_t)val->
of.i32;
702#elif UINTPTR_MAX == UINT64_MAX
703 return (
void*)(intptr_t)val->
of.i64;
707#define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}}
708#define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}}
709#define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}}
710#define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}}
711#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}}
712#define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}}
A list of bytes.
Definition: wasm.h:96
Global engine configuration.
Compilation environment and configuration.
An opaque object representing the type of an export.
A list of wasm_exporttype_t values.
Definition: wasm.h:299
Opaque struct representing a wasm external value.
A list of wasm_extern_t values.
Definition: wasm.h:488
An opaque object representing the type of a external value. Can be seen as a superclass of wasm_funct...
Unimplemented in Wasmtime.
Opaque struct representing a frame of a wasm stack trace.
A list of wasm_frame_t frameues.
Definition: wasm.h:367
Opaque struct representing a compiled wasm function.
An opaque object representing the type of a function.
Opaque struct representing a wasm global.
An opaque object representing the type of a global.
An opaque object representing the type of an import.
A list of wasm_importtype_t values.
Definition: wasm.h:287
Opaque struct representing a wasm instance.
Limits for tables/memories in wasm modules.
Definition: wasm.h:158
uint32_t min
Definition: wasm.h:159
uint32_t max
Definition: wasm.h:160
Opaque struct representing a wasm memory.
An opaque object representing the type of a memory.
Opaque struct representing a compiled wasm module.
A reference type: either a funcref or an externref.
A collection of instances and wasm global items.
Opaque struct representing a wasm table.
An opaque object representing the type of a table.
Opaque struct representing a wasm trap.
Representation of a WebAssembly value.
Definition: wasm.h:315
wasm_valkind_t kind
The kind of this value, or which of the fields in the of payload contains the actual value.
Definition: wasm.h:316
union wasm_val_t::@0 of
The actual value of this wasm_val_t. Only one field of this anonymous union is valid,...
A list of wasm_val_t values.
Definition: wasm.h:329
An object representing the type of a value.
A list of wasm_valtype_t values.
Definition: wasm.h:177
const wasm_externtype_t * wasm_importtype_type(const wasm_importtype_t *)
Returns the type of item this import is importing.
const wasm_functype_t * wasm_externtype_as_functype_const(const wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_functype_t.
struct wasm_val_t wasm_val_t
Convenience alias for wasm_val_t.
uint32_t wasm_frame_func_index(const wasm_frame_t *)
Returns the function index in the original wasm module that this frame corresponds to.
const wasm_valtype_t * wasm_tabletype_element(const wasm_tabletype_t *)
Returns the element type of this table.
wasm_global_t * wasm_global_new(wasm_store_t *, const wasm_globaltype_t *, const wasm_val_t *)
Creates a new WebAssembly global.
struct wasm_instance_t * wasm_frame_instance(const wasm_frame_t *)
Unimplemented in Wasmtime, aborts the process if called.
const wasm_globaltype_t * wasm_externtype_as_globaltype_const(const wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_globaltype_t.
void wasm_global_get(const wasm_global_t *, wasm_val_t *out)
Gets the value of this global.
wasm_tabletype_t * wasm_tabletype_new(wasm_valtype_t *, const wasm_limits_t *)
Creates a new table type.
void wasm_module_exports(const wasm_module_t *, wasm_exporttype_vec_t *out)
Returns the list of exports that this module provides.
const wasm_extern_t * wasm_memory_as_extern_const(const wasm_memory_t *)
Converts a wasm_memory_t to wasm_extern_t.
wasm_tabletype_t * wasm_externtype_as_tabletype(wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_tabletype_t.
void wasm_trap_message(const wasm_trap_t *, wasm_message_t *out)
Retrieves the message associated with this trap.
wasm_extern_t * wasm_func_as_extern(wasm_func_t *)
Converts a wasm_func_t to wasm_extern_t.
wasm_config_t * wasm_config_new(void)
Creates a new empty configuration object.
const wasm_name_t * wasm_exporttype_name(const wasm_exporttype_t *)
Returns the name of this export.
uint8_t wasm_externkind_t
Classifier for wasm_externtype_t.
Definition: wasm.h:254
wasm_table_t * wasm_extern_as_table(wasm_extern_t *)
Converts a wasm_extern_t to wasm_table_t.
void wasm_trap_trace(const wasm_trap_t *, wasm_frame_vec_t *out)
Returns the trace of wasm frames for this trap.
wasm_func_t * wasm_func_new_with_env(wasm_store_t *, const wasm_functype_t *type, wasm_func_callback_with_env_t, void *env, void(*finalizer)(void *))
Creates a new WebAssembly function with host functionality.
wasm_module_t * wasm_module_new(wasm_store_t *, const wasm_byte_vec_t *binary)
Compiles a raw WebAssembly binary to a wasm_module_t.
wasm_externkind_t wasm_extern_kind(const wasm_extern_t *)
Returns the kind of this extern, indicating what it will downcast as.
void wasm_instance_exports(const wasm_instance_t *, wasm_extern_vec_t *out)
Returns the exports of an instance.
wasm_trap_t *(* wasm_func_callback_t)(const wasm_val_vec_t *args, wasm_val_vec_t *results)
Type definition for functions passed to wasm_func_new.
Definition: wasm.h:416
size_t wasm_frame_module_offset(const wasm_frame_t *)
Returns the byte offset from the beginning of the original wasm file to the instruction this frame po...
wasm_memorytype_t * wasm_memory_type(const wasm_memory_t *)
Returns the type of this memory.
void wasm_valtype_vec_new(wasm_valtype_vec_t *out, size_t, wasm_valtype_t *const[])
Creates a vector with the provided contents.
const wasm_externtype_t * wasm_exporttype_type(const wasm_exporttype_t *)
Returns the type of this export.
const wasm_tabletype_t * wasm_externtype_as_tabletype_const(const wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_tabletype_t.
uint32_t wasm_table_size_t
Typedef for indices and sizes of wasm tables.
Definition: wasm.h:452
const wasm_externtype_t * wasm_tabletype_as_externtype_const(const wasm_tabletype_t *)
Converts a wasm_tabletype_t to a wasm_externtype_t.
wasm_memory_t * wasm_memory_new(wasm_store_t *, const wasm_memorytype_t *)
Creates a new WebAssembly memory.
wasm_extern_t * wasm_memory_as_extern(wasm_memory_t *)
Converts a wasm_memory_t to wasm_extern_t.
wasm_engine_t * wasm_engine_new(void)
Creates a new engine with the default configuration.
wasm_table_size_t wasm_table_size(const wasm_table_t *)
Gets the current size, in elements, of this table.
uint8_t wasm_valkind_t
Different kinds of types supported in wasm.
Definition: wasm.h:179
const wasm_valtype_vec_t * wasm_functype_results(const wasm_functype_t *)
Returns the list of results of this function type.
void wasm_global_set(wasm_global_t *, const wasm_val_t *)
Sets the value of this global.
const wasm_limits_t * wasm_memorytype_limits(const wasm_memorytype_t *)
Returns the limits of this memory.
const wasm_valtype_vec_t * wasm_functype_params(const wasm_functype_t *)
Returns the list of parameters of this function type.
char byte_t
A type definition for a number that occupies a single byte of data.
Definition: wasm.h:37
const wasm_valtype_t * wasm_globaltype_content(const wasm_globaltype_t *)
Returns the type of value contained in a global.
const wasm_externtype_t * wasm_functype_as_externtype_const(const wasm_functype_t *)
Converts a wasm_functype_t to a wasm_externtype_t.
const wasm_name_t * wasm_importtype_name(const wasm_importtype_t *)
Returns the name this import is importing from.
size_t wasm_func_param_arity(const wasm_func_t *)
Returns the number of arguments expected by this function.
const wasm_global_t * wasm_extern_as_global_const(const wasm_extern_t *)
Converts a wasm_extern_t to wasm_global_t.
wasm_frame_t * wasm_trap_origin(const wasm_trap_t *)
Returns the top frame of the wasm stack responsible for this trap.
size_t wasm_memory_data_size(const wasm_memory_t *)
Returns the size, in bytes, of this memory.
const wasm_memory_t * wasm_extern_as_memory_const(const wasm_extern_t *)
Converts a wasm_extern_t to wasm_memory_t.
bool wasm_table_grow(wasm_table_t *, wasm_table_size_t delta, wasm_ref_t *init)
Attempts to grow this table by delta elements.
wasm_trap_t * wasm_trap_new(wasm_store_t *store, const wasm_message_t *)
Creates a new wasm_trap_t with the provided message.
struct wasm_limits_t wasm_limits_t
A convenience typedef to wasm_limits_t.
wasm_exporttype_t * wasm_exporttype_new(wasm_name_t *, wasm_externtype_t *)
Creates a new export type.
wasm_instance_t * wasm_instance_new(wasm_store_t *, const wasm_module_t *, const wasm_extern_vec_t *imports, wasm_trap_t **)
Instantiates a module with the provided imports.
void wasm_val_copy(wasm_val_t *out, const wasm_val_t *)
Copies a wasm_val_t to a new one.
uint32_t wasm_memory_pages_t
Unsigned integer to hold the number of pages a memory has.
Definition: wasm.h:470
wasm_func_t * wasm_extern_as_func(wasm_extern_t *)
Converts a wasm_extern_t to wasm_func_t.
wasm_globaltype_t * wasm_globaltype_new(wasm_valtype_t *, wasm_mutability_t)
Creates a new global type.
wasm_trap_t *(* wasm_func_callback_with_env_t)(void *env, const wasm_val_vec_t *args, wasm_val_vec_t *results)
Type definition for functions passed to wasm_func_new_with_env.
Definition: wasm.h:418
wasm_externtype_t * wasm_memorytype_as_externtype(wasm_memorytype_t *)
Converts a wasm_memorytype_t to a wasm_externtype_t.
bool wasm_table_set(wasm_table_t *, wasm_table_size_t index, wasm_ref_t *)
Sets an element in this table.
float float32_t
A type definition for a 32-bit float.
Definition: wasm.h:38
bool wasm_memory_grow(wasm_memory_t *, wasm_memory_pages_t delta)
Attempts to grow this memory by delta wasm pages.
wasm_ref_t * wasm_table_get(const wasm_table_t *, wasm_table_size_t index)
Gets an element from this table.
wasm_store_t * wasm_store_new(wasm_engine_t *)
Creates a new store within the specified engine.
wasm_globaltype_t * wasm_global_type(const wasm_global_t *)
Returns the type of this global.
const wasm_name_t * wasm_importtype_module(const wasm_importtype_t *)
Returns the module this import is importing from.
wasm_tabletype_t * wasm_table_type(const wasm_table_t *)
Returns the type of this table.
bool wasm_module_validate(wasm_store_t *, const wasm_byte_vec_t *binary)
Validates whether a provided byte sequence is a valid wasm binary.
wasm_func_t * wasm_func_new(wasm_store_t *, const wasm_functype_t *, wasm_func_callback_t)
Creates a new WebAssembly function with host functionality.
const wasm_table_t * wasm_extern_as_table_const(const wasm_extern_t *)
Converts a wasm_extern_t to wasm_table_t.
wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t *)
Returns the kind of external item this type represents.
wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t *)
Returns whether or not a global is mutable.
wasm_extern_t * wasm_global_as_extern(wasm_global_t *)
Converts a wasm_global_t to wasm_extern_t.
wasm_functype_t * wasm_externtype_as_functype(wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_functype_t.
wasm_memory_pages_t wasm_memory_size(const wasm_memory_t *)
Returns the size, in wasm pages, of this memory.
void wasm_module_imports(const wasm_module_t *, wasm_importtype_vec_t *out)
Returns the list of imports that this module expects.
wasm_externtype_t * wasm_globaltype_as_externtype(wasm_globaltype_t *)
Converts a wasm_globaltype_t to a wasm_externtype_t.
const wasm_extern_t * wasm_global_as_extern_const(const wasm_global_t *)
Converts a wasm_global_t to wasm_extern_t.
const wasm_memorytype_t * wasm_externtype_as_memorytype_const(const wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_memorytype_t.
#define wasm_name_new
Convenience alias.
Definition: wasm.h:101
const wasm_extern_t * wasm_func_as_extern_const(const wasm_func_t *)
Converts a wasm_func_t to wasm_extern_t.
wasm_memorytype_t * wasm_externtype_as_memorytype(wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_memorytype_t.
wasm_trap_t * wasm_func_call(const wasm_func_t *, const wasm_val_vec_t *args, wasm_val_vec_t *results)
Calls the provided function with the arguments given.
wasm_module_t * wasm_module_deserialize(wasm_store_t *, const wasm_byte_vec_t *)
Deserializes a previously-serialized module.
wasm_importtype_t * wasm_importtype_new(wasm_name_t *module, wasm_name_t *name, wasm_externtype_t *)
Creates a new import type.
wasm_global_t * wasm_extern_as_global(wasm_extern_t *)
Converts a wasm_extern_t to wasm_global_t.
wasm_foreign_t * wasm_foreign_new(wasm_store_t *)
Unimplemented in Wasmtime, aborts the process if called.
const wasm_externtype_t * wasm_globaltype_as_externtype_const(const wasm_globaltype_t *)
Converts a wasm_globaltype_t to a wasm_externtype_t.
wasm_engine_t * wasm_engine_new_with_config(wasm_config_t *)
Creates a new engine with the specified configuration.
size_t wasm_func_result_arity(const wasm_func_t *)
Returns the number of results returned by this function.
wasm_externtype_t * wasm_functype_as_externtype(wasm_functype_t *)
Converts a wasm_functype_t to a wasm_externtype_t.
wasm_functype_t * wasm_functype_new(wasm_valtype_vec_t *params, wasm_valtype_vec_t *results)
Creates a new function type with the provided parameter and result types.
wasm_globaltype_t * wasm_externtype_as_globaltype(wasm_externtype_t *)
Attempts to convert a wasm_externtype_t to a wasm_globaltype_t.
void wasm_val_delete(wasm_val_t *v)
Deletes a type.
wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t *)
Returns the associated kind for this value type.
wasm_memorytype_t * wasm_memorytype_new(const wasm_limits_t *)
Creates a new memory type.
double float64_t
A type definition for a 64-bit float.
Definition: wasm.h:39
const wasm_limits_t * wasm_tabletype_limits(const wasm_tabletype_t *)
Returns the limits of this table.
wasm_externtype_t * wasm_tabletype_as_externtype(wasm_tabletype_t *)
Converts a wasm_tabletype_t to a wasm_externtype_t.
uint8_t wasm_mutability_t
Boolean flag for whether a global is mutable or not.
Definition: wasm.h:152
wasm_valtype_t * wasm_valtype_new(wasm_valkind_t)
Creates a new value type from the specified kind.
wasm_frame_t * wasm_frame_copy(const wasm_frame_t *)
Returns a copy of the provided frame.
wasm_memory_t * wasm_extern_as_memory(wasm_extern_t *)
Converts a wasm_extern_t to wasm_memory_t.
byte_t wasm_byte_t
A type definition for a number that occupies a single byte of data.
Definition: wasm.h:95
void wasm_module_serialize(const wasm_module_t *, wasm_byte_vec_t *out)
Serializes the provided module to a byte vector.
wasm_extern_t * wasm_table_as_extern(wasm_table_t *)
Converts a wasm_table_t to wasm_extern_t.
const wasm_extern_t * wasm_table_as_extern_const(const wasm_table_t *)
Converts a wasm_table_t to wasm_extern_t.
wasm_externtype_t * wasm_extern_type(const wasm_extern_t *)
Returns the type of this extern.
wasm_functype_t * wasm_func_type(const wasm_func_t *)
Returns the type of this function.
byte_t * wasm_memory_data(wasm_memory_t *)
Returns the base address, in memory, where this memory is located.
const wasm_func_t * wasm_extern_as_func_const(const wasm_extern_t *)
Converts a wasm_extern_t to wasm_func_t.
const wasm_externtype_t * wasm_memorytype_as_externtype_const(const wasm_memorytype_t *)
Converts a wasm_memorytype_t to a wasm_externtype_t.
void wasm_valtype_vec_new_empty(wasm_valtype_vec_t *out)
Creates an empty vector.
size_t wasm_frame_func_offset(const wasm_frame_t *)
Returns the byte offset from the beginning of the function in the original wasm file to the instructi...
wasm_table_t * wasm_table_new(wasm_store_t *, const wasm_tabletype_t *, wasm_ref_t *init)
Creates a new WebAssembly table.