18#ifndef WASM_API_EXTERN
19#if defined(_WIN32) && !defined(__MINGW32__) && !defined(LIBWASM_STATIC)
20#define WASM_API_EXTERN __declspec(dllimport)
22#define WASM_API_EXTERN
35inline void assertions(
void) {
36 static_assert(
sizeof(float) ==
sizeof(uint32_t),
"incompatible float type");
37 static_assert(
sizeof(double) ==
sizeof(uint64_t),
"incompatible double type");
38 static_assert(
sizeof(intptr_t) ==
sizeof(uint32_t) ||
39 sizeof(intptr_t) ==
sizeof(uint64_t),
40 "incompatible pointer type");
74#define WASM_DECLARE_OWN(name) \
75 typedef struct wasm_##name##_t wasm_##name##_t; \
77 WASM_API_EXTERN void wasm_##name##_delete(own wasm_##name##_t*);
82#define WASM_DECLARE_VEC(name, ptr_or_none) \
83 typedef struct wasm_##name##_vec_t { \
85 wasm_##name##_t ptr_or_none* data; \
86 } wasm_##name##_vec_t; \
88 WASM_API_EXTERN void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t* out); \
89 WASM_API_EXTERN void wasm_##name##_vec_new_uninitialized( \
90 own wasm_##name##_vec_t* out, size_t); \
91 WASM_API_EXTERN void wasm_##name##_vec_new( \
92 own wasm_##name##_vec_t* out, \
93 size_t, own wasm_##name##_t ptr_or_none const[]); \
94 WASM_API_EXTERN void wasm_##name##_vec_copy( \
95 own wasm_##name##_vec_t* out, const wasm_##name##_vec_t*); \
96 WASM_API_EXTERN void wasm_##name##_vec_delete(own wasm_##name##_vec_t*);
102WASM_DECLARE_VEC(
byte, )
106#define wasm_name wasm_byte_vec
107#define wasm_name_new wasm_byte_vec_new
108#define wasm_name_new_empty wasm_byte_vec_new_empty
109#define wasm_name_new_new_uninitialized wasm_byte_vec_new_uninitialized
110#define wasm_name_copy wasm_byte_vec_copy
111#define wasm_name_delete wasm_byte_vec_delete
113static inline void wasm_name_new_from_string(
119static inline void wasm_name_new_from_string_nt(
131WASM_DECLARE_OWN(config)
140WASM_DECLARE_OWN(engine)
148WASM_DECLARE_OWN(store)
159enum wasm_mutability_enum {
169static const uint32_t wasm_limits_max_default = 0xffffffff;
174#define WASM_DECLARE_TYPE(name) \
175 WASM_DECLARE_OWN(name) \
176 WASM_DECLARE_VEC(name, *) \
178 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*);
183WASM_DECLARE_TYPE(valtype)
186enum wasm_valkind_enum {
191 WASM_EXTERNREF = 128,
200 return k < WASM_EXTERNREF;
203 return k >= WASM_EXTERNREF;
216WASM_DECLARE_TYPE(functype)
227WASM_DECLARE_TYPE(globaltype)
238WASM_DECLARE_TYPE(tabletype)
249WASM_DECLARE_TYPE(memorytype)
258WASM_DECLARE_TYPE(externtype)
261enum wasm_externkind_enum {
293WASM_DECLARE_TYPE(importtype)
305WASM_DECLARE_TYPE(exporttype)
335WASM_DECLARE_VEC(val, )
340#define WASM_DECLARE_REF_BASE(name) \
341 WASM_DECLARE_OWN(name) \
343 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
344 WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
346 WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
347 WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
348 WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
349 wasm_##name##_t*, void*, void (*)(void*));
351#define WASM_DECLARE_REF(name) \
352 WASM_DECLARE_REF_BASE(name) \
354 WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
355 WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
356 WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
357 WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
359#define WASM_DECLARE_SHARABLE_REF(name) \
360 WASM_DECLARE_REF(name) \
361 WASM_DECLARE_OWN(shared_##name) \
363 WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
364 WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
367WASM_DECLARE_REF_BASE(ref)
372WASM_DECLARE_OWN(frame)
373WASM_DECLARE_VEC(frame, *)
386WASM_DECLARE_REF(trap)
397WASM_DECLARE_REF(foreign)
404WASM_DECLARE_SHARABLE_REF(module)
420WASM_DECLARE_REF(func)
431 void* env,
void (*finalizer)(
void*));
443WASM_DECLARE_REF(global)
456WASM_DECLARE_REF(table)
474WASM_DECLARE_REF(memory)
478static const
size_t MEMORY_PAGE_SIZE = 0x10000;
493WASM_DECLARE_REF(extern)
494WASM_DECLARE_VEC(extern, *)
522WASM_DECLARE_REF(instance)
537#define WASM_EMPTY_VEC {0, NULL}
538#define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array}
556static inline own
wasm_valtype_t* wasm_valtype_new_externref(
void) {
695static inline void wasm_val_init_ptr(own
wasm_val_t* out,
void* p) {
696#if UINTPTR_MAX == UINT32_MAX
697 out->kind = WASM_I32;
698 out->of.i32 = (intptr_t)p;
699#elif UINTPTR_MAX == UINT64_MAX
700 out->kind = WASM_I64;
701 out->of.i64 = (intptr_t)p;
705static inline void* wasm_val_ptr(
const wasm_val_t* val) {
706#if UINTPTR_MAX == UINT32_MAX
707 return (
void*)(intptr_t)val->
of.i32;
708#elif UINTPTR_MAX == UINT64_MAX
709 return (
void*)(intptr_t)val->
of.i64;
713#define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}}
714#define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}}
715#define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}}
716#define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}}
717#define WASM_REF_VAL(r) {.kind = WASM_EXTERNREF, .of = {.ref = r}}
718#define WASM_INIT_VAL {.kind = WASM_EXTERNREF, .of = {.ref = NULL}}
A list of bytes.
Definition: wasm.h:102
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:305
Opaque struct representing a wasm external value.
A list of wasm_extern_t values.
Definition: wasm.h:494
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:373
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:293
Opaque struct representing a wasm instance.
Limits for tables/memories in wasm modules.
Definition: wasm.h:164
uint32_t min
Definition: wasm.h:165
uint32_t max
Definition: wasm.h:166
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:321
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:322
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:335
An object representing the type of a value.
A list of wasm_valtype_t values.
Definition: wasm.h:183
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:260
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:422
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:458
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:185
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:43
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:476
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:424
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:44
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:107
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:45
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:158
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:101
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.