wasm_c_api.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // WebAssembly C API
  2. #ifndef _WASM_C_API_H_
  3. #define _WASM_C_API_H_
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #ifndef WASM_API_EXTERN
  10. #if defined(_MSC_BUILD)
  11. #if defined(COMPILING_WASM_RUNTIME_API)
  12. #define WASM_API_EXTERN __declspec(dllexport)
  13. #else
  14. #define WASM_API_EXTERN __declspec(dllimport)
  15. #endif
  16. #else
  17. #define WASM_API_EXTERN
  18. #endif
  19. #endif
  20. #if defined(__GNUC__) || defined(__clang__)
  21. #define DEPRECATED __attribute__((deprecated))
  22. #elif defined(_MSC_VER)
  23. #define DEPRECATED __declspec(deprecated)
  24. #else
  25. #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
  26. #define DEPRECATED
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* clang-format off */
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // Auxiliaries
  34. // Machine types
  35. #if (__STDC_VERSION__) > 199901L
  36. inline void assertions(void) {
  37. static_assert(sizeof(float) == sizeof(uint32_t), "incompatible float type");
  38. static_assert(sizeof(double) == sizeof(uint64_t), "incompatible double type");
  39. static_assert(sizeof(intptr_t) == sizeof(uint32_t) ||
  40. sizeof(intptr_t) == sizeof(uint64_t),
  41. "incompatible pointer type");
  42. }
  43. #endif
  44. typedef char byte_t;
  45. typedef float float32_t;
  46. typedef double float64_t;
  47. // Ownership
  48. #define own
  49. // The qualifier `own` is used to indicate ownership of data in this API.
  50. // It is intended to be interpreted similar to a `const` qualifier:
  51. //
  52. // - `own wasm_xxx_t*` owns the pointed-to data
  53. // - `own wasm_xxx_t` distributes to all fields of a struct or union `xxx`
  54. // - `own wasm_xxx_vec_t` owns the vector as well as its elements(!)
  55. // - an `own` function parameter passes ownership from caller to callee
  56. // - an `own` function result passes ownership from callee to caller
  57. // - an exception are `own` pointer parameters named `out`, which are copy-back
  58. // output parameters passing back ownership from callee to caller
  59. //
  60. // Own data is created by `wasm_xxx_new` functions and some others.
  61. // It must be released with the corresponding `wasm_xxx_delete` function.
  62. //
  63. // Deleting a reference does not necessarily delete the underlying object,
  64. // it merely indicates that this owner no longer uses it.
  65. //
  66. // For vectors, `const wasm_xxx_vec_t` is used informally to indicate that
  67. // neither the vector nor its elements should be modified.
  68. // TODO: introduce proper `wasm_xxx_const_vec_t`?
  69. #define WASM_DECLARE_OWN(name) \
  70. typedef struct wasm_##name##_t wasm_##name##_t; \
  71. \
  72. WASM_API_EXTERN void wasm_##name##_delete(own wasm_##name##_t*);
  73. // Vectors
  74. // size: capacity
  75. // num_elems: current number of elements
  76. // size_of_elem: size of one elemen
  77. #define WASM_DECLARE_VEC(name, ptr_or_none) \
  78. typedef struct wasm_##name##_vec_t { \
  79. size_t size; \
  80. wasm_##name##_t ptr_or_none* data; \
  81. size_t num_elems; \
  82. size_t size_of_elem; \
  83. void *lock; \
  84. } wasm_##name##_vec_t; \
  85. \
  86. WASM_API_EXTERN void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t* out); \
  87. WASM_API_EXTERN void wasm_##name##_vec_new_uninitialized( \
  88. own wasm_##name##_vec_t* out, size_t); \
  89. WASM_API_EXTERN void wasm_##name##_vec_new( \
  90. own wasm_##name##_vec_t* out, \
  91. size_t, own wasm_##name##_t ptr_or_none const[]); \
  92. WASM_API_EXTERN void wasm_##name##_vec_copy( \
  93. own wasm_##name##_vec_t* out, const wasm_##name##_vec_t*); \
  94. WASM_API_EXTERN void wasm_##name##_vec_delete(own wasm_##name##_vec_t*);
  95. // Byte vectors
  96. typedef byte_t wasm_byte_t;
  97. WASM_DECLARE_VEC(byte, )
  98. typedef wasm_byte_vec_t wasm_name_t;
  99. #define wasm_name wasm_byte_vec
  100. #define wasm_name_new wasm_byte_vec_new
  101. #define wasm_name_new_empty wasm_byte_vec_new_empty
  102. #define wasm_name_new_new_uninitialized wasm_byte_vec_new_uninitialized
  103. #define wasm_name_copy wasm_byte_vec_copy
  104. #define wasm_name_delete wasm_byte_vec_delete
  105. static inline void wasm_name_new_from_string(
  106. own wasm_name_t* out, const char* s
  107. ) {
  108. wasm_name_new(out, strlen(s), s);
  109. }
  110. static inline void wasm_name_new_from_string_nt(
  111. own wasm_name_t* out, const char* s
  112. ) {
  113. wasm_name_new(out, strlen(s) + 1, s);
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////
  116. // Runtime Environment
  117. // Configuration
  118. WASM_DECLARE_OWN(config)
  119. #ifndef MEM_ALLOC_OPTION_DEFINED
  120. #define MEM_ALLOC_OPTION_DEFINED
  121. /* same definition from wasm_export.h */
  122. /* Memory allocator type */
  123. typedef enum {
  124. /* pool mode, allocate memory from user defined heap buffer */
  125. Alloc_With_Pool = 0,
  126. /* user allocator mode, allocate memory from user defined
  127. malloc function */
  128. Alloc_With_Allocator,
  129. /* system allocator mode, allocate memory from system allocator,
  130. or, platform's os_malloc function */
  131. Alloc_With_System_Allocator,
  132. } mem_alloc_type_t;
  133. /* Memory allocator option */
  134. typedef union MemAllocOption {
  135. struct {
  136. void *heap_buf;
  137. uint32_t heap_size;
  138. } pool;
  139. struct {
  140. void *malloc_func;
  141. void *realloc_func;
  142. void *free_func;
  143. /* allocator user data, only used when
  144. WASM_MEM_ALLOC_WITH_USER_DATA is defined */
  145. void *user_data;
  146. } allocator;
  147. } MemAllocOption;
  148. #endif /* MEM_ALLOC_OPTION_DEFINED */
  149. /* Runtime configration */
  150. struct wasm_config_t {
  151. mem_alloc_type_t mem_alloc_type;
  152. MemAllocOption mem_alloc_option;
  153. uint32_t segue_flags;
  154. bool enable_linux_perf;
  155. /*TODO: wasi args*/
  156. };
  157. /*
  158. * by default:
  159. * - mem_alloc_type is Alloc_With_System_Allocator
  160. * - mem_alloc_option is all 0
  161. * - enable_linux_perf is false
  162. */
  163. WASM_API_EXTERN own wasm_config_t* wasm_config_new(void);
  164. // Embedders may provide custom functions for manipulating configs.
  165. WASM_API_EXTERN own wasm_config_t*
  166. wasm_config_set_mem_alloc_opt(wasm_config_t *, mem_alloc_type_t, MemAllocOption *);
  167. WASM_API_EXTERN own wasm_config_t*
  168. wasm_config_set_linux_perf_opt(wasm_config_t *, bool);
  169. /**
  170. * Enable using GS register as the base address of linear memory in linux x86_64,
  171. * which may speedup the linear memory access for LLVM AOT/JIT:
  172. * bit0 to bit4 denotes i32.load, i64.load, f32.load, f64.load, v128.load
  173. * bit8 to bit12 denotes i32.store, i64.store, f32.store, f64.store, v128.store
  174. * For example, 0x01 enables i32.load, 0x0100 enables i32.store.
  175. * To enable all load/store operations, use 0x1F1F
  176. */
  177. WASM_API_EXTERN wasm_config_t*
  178. wasm_config_set_segue_flags(wasm_config_t *config, uint32_t segue_flags);
  179. // Engine
  180. WASM_DECLARE_OWN(engine)
  181. /**
  182. * Create a new engine
  183. *
  184. * Note: for the engine new/delete operations, including this,
  185. * wasm_engine_new_with_config, wasm_engine_new_with_args, and
  186. * wasm_engine_delete, if the platform has mutex initializer,
  187. * then they are thread-safe: we use a global lock to lock the
  188. * operations of the engine. Otherwise they are not thread-safe:
  189. * when there are engine new/delete operations happening
  190. * simultaneously in multiple threads, developer must create
  191. * the lock by himself, and add the lock when calling these
  192. * functions.
  193. */
  194. WASM_API_EXTERN own wasm_engine_t* wasm_engine_new(void);
  195. WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(wasm_config_t*);
  196. DEPRECATED WASM_API_EXTERN own wasm_engine_t *
  197. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts);
  198. // Store
  199. WASM_DECLARE_OWN(store)
  200. WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
  201. ///////////////////////////////////////////////////////////////////////////////
  202. // Type Representations
  203. // Type attributes
  204. typedef uint8_t wasm_mutability_t;
  205. enum wasm_mutability_enum {
  206. WASM_CONST,
  207. WASM_VAR,
  208. };
  209. typedef struct wasm_limits_t {
  210. uint32_t min;
  211. uint32_t max;
  212. } wasm_limits_t;
  213. static const uint32_t wasm_limits_max_default = 0xffffffff;
  214. // Generic
  215. #define WASM_DECLARE_TYPE(name) \
  216. WASM_DECLARE_OWN(name) \
  217. WASM_DECLARE_VEC(name, *) \
  218. \
  219. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*);
  220. // Value Types
  221. WASM_DECLARE_TYPE(valtype)
  222. #ifndef WASM_VALKIND_T_DEFINED
  223. #define WASM_VALKIND_T_DEFINED
  224. typedef uint8_t wasm_valkind_t;
  225. enum wasm_valkind_enum {
  226. WASM_I32,
  227. WASM_I64,
  228. WASM_F32,
  229. WASM_F64,
  230. WASM_ANYREF = 128,
  231. WASM_FUNCREF,
  232. };
  233. #endif
  234. WASM_API_EXTERN own wasm_valtype_t* wasm_valtype_new(wasm_valkind_t);
  235. WASM_API_EXTERN wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t*);
  236. static inline bool wasm_valkind_is_num(wasm_valkind_t k) {
  237. return k < WASM_ANYREF;
  238. }
  239. static inline bool wasm_valkind_is_ref(wasm_valkind_t k) {
  240. return k >= WASM_ANYREF;
  241. }
  242. static inline bool wasm_valtype_is_num(const wasm_valtype_t* t) {
  243. return wasm_valkind_is_num(wasm_valtype_kind(t));
  244. }
  245. static inline bool wasm_valtype_is_ref(const wasm_valtype_t* t) {
  246. return wasm_valkind_is_ref(wasm_valtype_kind(t));
  247. }
  248. // Function Types
  249. WASM_DECLARE_TYPE(functype)
  250. WASM_API_EXTERN own wasm_functype_t* wasm_functype_new(
  251. own wasm_valtype_vec_t* params, own wasm_valtype_vec_t* results);
  252. WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t*);
  253. WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_results(const wasm_functype_t*);
  254. // Global Types
  255. WASM_DECLARE_TYPE(globaltype)
  256. WASM_API_EXTERN own wasm_globaltype_t* wasm_globaltype_new(
  257. own wasm_valtype_t*, wasm_mutability_t);
  258. WASM_API_EXTERN const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t*);
  259. WASM_API_EXTERN wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t*);
  260. // Table Types
  261. WASM_DECLARE_TYPE(tabletype)
  262. WASM_API_EXTERN own wasm_tabletype_t* wasm_tabletype_new(
  263. own wasm_valtype_t*, const wasm_limits_t*);
  264. WASM_API_EXTERN const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t*);
  265. WASM_API_EXTERN const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t*);
  266. // Memory Types
  267. WASM_DECLARE_TYPE(memorytype)
  268. WASM_API_EXTERN own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*);
  269. WASM_API_EXTERN const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t*);
  270. // Extern Types
  271. WASM_DECLARE_TYPE(externtype)
  272. typedef uint8_t wasm_externkind_t;
  273. enum wasm_externkind_enum {
  274. WASM_EXTERN_FUNC,
  275. WASM_EXTERN_GLOBAL,
  276. WASM_EXTERN_TABLE,
  277. WASM_EXTERN_MEMORY,
  278. };
  279. WASM_API_EXTERN wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*);
  280. WASM_API_EXTERN wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t*);
  281. WASM_API_EXTERN wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t*);
  282. WASM_API_EXTERN wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t*);
  283. WASM_API_EXTERN wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t*);
  284. WASM_API_EXTERN wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*);
  285. WASM_API_EXTERN wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t*);
  286. WASM_API_EXTERN wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t*);
  287. WASM_API_EXTERN wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t*);
  288. WASM_API_EXTERN const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t*);
  289. WASM_API_EXTERN const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t*);
  290. WASM_API_EXTERN const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t*);
  291. WASM_API_EXTERN const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t*);
  292. WASM_API_EXTERN const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t*);
  293. WASM_API_EXTERN const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t*);
  294. WASM_API_EXTERN const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t*);
  295. WASM_API_EXTERN const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t*);
  296. // Import Types
  297. WASM_DECLARE_TYPE(importtype)
  298. WASM_API_EXTERN own wasm_importtype_t* wasm_importtype_new(
  299. own wasm_name_t* module, own wasm_name_t* name, own wasm_externtype_t*);
  300. WASM_API_EXTERN const wasm_name_t* wasm_importtype_module(const wasm_importtype_t*);
  301. WASM_API_EXTERN const wasm_name_t* wasm_importtype_name(const wasm_importtype_t*);
  302. WASM_API_EXTERN const wasm_externtype_t* wasm_importtype_type(const wasm_importtype_t*);
  303. WASM_API_EXTERN bool wasm_importtype_is_linked(const wasm_importtype_t*);
  304. // Export Types
  305. WASM_DECLARE_TYPE(exporttype)
  306. WASM_API_EXTERN own wasm_exporttype_t* wasm_exporttype_new(
  307. own wasm_name_t*, own wasm_externtype_t*);
  308. WASM_API_EXTERN const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t*);
  309. WASM_API_EXTERN const wasm_externtype_t* wasm_exporttype_type(const wasm_exporttype_t*);
  310. ///////////////////////////////////////////////////////////////////////////////
  311. // Runtime Objects
  312. // Values
  313. #ifndef WASM_VAL_T_DEFINED
  314. #define WASM_VAL_T_DEFINED
  315. struct wasm_ref_t;
  316. typedef struct wasm_val_t {
  317. wasm_valkind_t kind;
  318. union {
  319. int32_t i32;
  320. int64_t i64;
  321. float32_t f32;
  322. float64_t f64;
  323. struct wasm_ref_t* ref;
  324. } of;
  325. } wasm_val_t;
  326. #endif
  327. WASM_API_EXTERN void wasm_val_delete(own wasm_val_t* v);
  328. WASM_API_EXTERN void wasm_val_copy(own wasm_val_t* out, const wasm_val_t*);
  329. WASM_DECLARE_VEC(val, )
  330. // References
  331. #define WASM_DECLARE_REF_BASE(name) \
  332. WASM_DECLARE_OWN(name) \
  333. \
  334. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
  335. WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
  336. \
  337. WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
  338. WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
  339. WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
  340. wasm_##name##_t*, void*, void (*)(void*));
  341. #define WASM_DECLARE_REF(name) \
  342. WASM_DECLARE_REF_BASE(name) \
  343. \
  344. WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
  345. WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
  346. WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
  347. WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
  348. #define WASM_DECLARE_SHARABLE_REF(name) \
  349. WASM_DECLARE_REF(name) \
  350. WASM_DECLARE_OWN(shared_##name) \
  351. \
  352. WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
  353. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
  354. WASM_DECLARE_REF_BASE(ref)
  355. // Frames
  356. WASM_DECLARE_OWN(frame)
  357. WASM_DECLARE_VEC(frame, *)
  358. WASM_API_EXTERN own wasm_frame_t* wasm_frame_copy(const wasm_frame_t*);
  359. WASM_API_EXTERN struct wasm_instance_t* wasm_frame_instance(const wasm_frame_t*);
  360. WASM_API_EXTERN uint32_t wasm_frame_func_index(const wasm_frame_t*);
  361. WASM_API_EXTERN size_t wasm_frame_func_offset(const wasm_frame_t*);
  362. WASM_API_EXTERN size_t wasm_frame_module_offset(const wasm_frame_t*);
  363. // Traps
  364. typedef wasm_name_t wasm_message_t; // null terminated
  365. WASM_DECLARE_REF(trap)
  366. WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*);
  367. WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out);
  368. WASM_API_EXTERN own wasm_frame_t* wasm_trap_origin(const wasm_trap_t*);
  369. WASM_API_EXTERN void wasm_trap_trace(const wasm_trap_t*, own wasm_frame_vec_t* out);
  370. // Foreign Objects
  371. WASM_DECLARE_REF(foreign)
  372. WASM_API_EXTERN own wasm_foreign_t* wasm_foreign_new(wasm_store_t*);
  373. // Modules
  374. // WASM_DECLARE_SHARABLE_REF(module)
  375. #ifndef WASM_MODULE_T_DEFINED
  376. #define WASM_MODULE_T_DEFINED
  377. struct WASMModuleCommon;
  378. typedef struct WASMModuleCommon *wasm_module_t;
  379. #endif
  380. WASM_API_EXTERN own wasm_module_t* wasm_module_new(
  381. wasm_store_t*, const wasm_byte_vec_t* binary);
  382. WASM_API_EXTERN void wasm_module_delete(own wasm_module_t*);
  383. WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
  384. WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out);
  385. WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out);
  386. WASM_API_EXTERN void wasm_module_serialize(wasm_module_t*, own wasm_byte_vec_t* out);
  387. WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const wasm_byte_vec_t*);
  388. typedef wasm_module_t wasm_shared_module_t;
  389. WASM_API_EXTERN own wasm_shared_module_t* wasm_module_share(wasm_module_t*);
  390. WASM_API_EXTERN own wasm_module_t* wasm_module_obtain(wasm_store_t*, wasm_shared_module_t*);
  391. WASM_API_EXTERN void wasm_shared_module_delete(own wasm_shared_module_t*);
  392. // Function Instances
  393. WASM_DECLARE_REF(func)
  394. typedef own wasm_trap_t* (*wasm_func_callback_t)(
  395. const wasm_val_vec_t* args, own wasm_val_vec_t *results);
  396. typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
  397. void* env, const wasm_val_vec_t *args, wasm_val_vec_t *results);
  398. WASM_API_EXTERN own wasm_func_t* wasm_func_new(
  399. wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
  400. WASM_API_EXTERN own wasm_func_t* wasm_func_new_with_env(
  401. wasm_store_t*, const wasm_functype_t* type, wasm_func_callback_with_env_t,
  402. void* env, void (*finalizer)(void*));
  403. WASM_API_EXTERN own wasm_functype_t* wasm_func_type(const wasm_func_t*);
  404. WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*);
  405. WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*);
  406. WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
  407. const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results);
  408. // Global Instances
  409. WASM_DECLARE_REF(global)
  410. WASM_API_EXTERN own wasm_global_t* wasm_global_new(
  411. wasm_store_t*, const wasm_globaltype_t*, const wasm_val_t*);
  412. WASM_API_EXTERN own wasm_globaltype_t* wasm_global_type(const wasm_global_t*);
  413. WASM_API_EXTERN void wasm_global_get(const wasm_global_t*, own wasm_val_t* out);
  414. WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);
  415. // Table Instances
  416. WASM_DECLARE_REF(table)
  417. typedef uint32_t wasm_table_size_t;
  418. WASM_API_EXTERN own wasm_table_t* wasm_table_new(
  419. wasm_store_t*, const wasm_tabletype_t*, wasm_ref_t* init);
  420. WASM_API_EXTERN own wasm_tabletype_t* wasm_table_type(const wasm_table_t*);
  421. WASM_API_EXTERN own wasm_ref_t* wasm_table_get(const wasm_table_t*, wasm_table_size_t index);
  422. WASM_API_EXTERN bool wasm_table_set(wasm_table_t*, wasm_table_size_t index, wasm_ref_t*);
  423. WASM_API_EXTERN wasm_table_size_t wasm_table_size(const wasm_table_t*);
  424. WASM_API_EXTERN bool wasm_table_grow(wasm_table_t*, wasm_table_size_t delta, wasm_ref_t* init);
  425. // Memory Instances
  426. WASM_DECLARE_REF(memory)
  427. typedef uint32_t wasm_memory_pages_t;
  428. static const size_t MEMORY_PAGE_SIZE = 0x10000;
  429. WASM_API_EXTERN own wasm_memory_t* wasm_memory_new(wasm_store_t*, const wasm_memorytype_t*);
  430. WASM_API_EXTERN own wasm_memorytype_t* wasm_memory_type(const wasm_memory_t*);
  431. WASM_API_EXTERN byte_t* wasm_memory_data(wasm_memory_t*);
  432. WASM_API_EXTERN size_t wasm_memory_data_size(const wasm_memory_t*);
  433. WASM_API_EXTERN wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*);
  434. WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta);
  435. // Externals
  436. WASM_DECLARE_REF(extern)
  437. WASM_DECLARE_VEC(extern, *)
  438. WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*);
  439. WASM_API_EXTERN own wasm_externtype_t* wasm_extern_type(const wasm_extern_t*);
  440. WASM_API_EXTERN wasm_extern_t* wasm_func_as_extern(wasm_func_t*);
  441. WASM_API_EXTERN wasm_extern_t* wasm_global_as_extern(wasm_global_t*);
  442. WASM_API_EXTERN wasm_extern_t* wasm_table_as_extern(wasm_table_t*);
  443. WASM_API_EXTERN wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*);
  444. WASM_API_EXTERN wasm_func_t* wasm_extern_as_func(wasm_extern_t*);
  445. WASM_API_EXTERN wasm_global_t* wasm_extern_as_global(wasm_extern_t*);
  446. WASM_API_EXTERN wasm_table_t* wasm_extern_as_table(wasm_extern_t*);
  447. WASM_API_EXTERN wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*);
  448. WASM_API_EXTERN const wasm_extern_t* wasm_func_as_extern_const(const wasm_func_t*);
  449. WASM_API_EXTERN const wasm_extern_t* wasm_global_as_extern_const(const wasm_global_t*);
  450. WASM_API_EXTERN const wasm_extern_t* wasm_table_as_extern_const(const wasm_table_t*);
  451. WASM_API_EXTERN const wasm_extern_t* wasm_memory_as_extern_const(const wasm_memory_t*);
  452. WASM_API_EXTERN const wasm_func_t* wasm_extern_as_func_const(const wasm_extern_t*);
  453. WASM_API_EXTERN const wasm_global_t* wasm_extern_as_global_const(const wasm_extern_t*);
  454. WASM_API_EXTERN const wasm_table_t* wasm_extern_as_table_const(const wasm_extern_t*);
  455. WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_extern_t*);
  456. // Module Instances
  457. WASM_DECLARE_REF(instance)
  458. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
  459. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  460. own wasm_trap_t** trap
  461. );
  462. // please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
  463. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
  464. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  465. own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size
  466. );
  467. WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);
  468. ///////////////////////////////////////////////////////////////////////////////
  469. // Convenience
  470. // Vectors
  471. #define WASM_EMPTY_VEC {0, NULL, 0, 0, NULL}
  472. #define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array, sizeof(array)/sizeof(*(array)), sizeof(*(array)), NULL}
  473. // Value Type construction short-hands
  474. static inline own wasm_valtype_t* wasm_valtype_new_i32(void) {
  475. return wasm_valtype_new(WASM_I32);
  476. }
  477. static inline own wasm_valtype_t* wasm_valtype_new_i64(void) {
  478. return wasm_valtype_new(WASM_I64);
  479. }
  480. static inline own wasm_valtype_t* wasm_valtype_new_f32(void) {
  481. return wasm_valtype_new(WASM_F32);
  482. }
  483. static inline own wasm_valtype_t* wasm_valtype_new_f64(void) {
  484. return wasm_valtype_new(WASM_F64);
  485. }
  486. static inline own wasm_valtype_t* wasm_valtype_new_anyref(void) {
  487. return wasm_valtype_new(WASM_ANYREF);
  488. }
  489. static inline own wasm_valtype_t* wasm_valtype_new_funcref(void) {
  490. return wasm_valtype_new(WASM_FUNCREF);
  491. }
  492. // Function Types construction short-hands
  493. static inline own wasm_functype_t* wasm_functype_new_0_0(void) {
  494. wasm_valtype_vec_t params, results;
  495. wasm_valtype_vec_new_empty(&params);
  496. wasm_valtype_vec_new_empty(&results);
  497. return wasm_functype_new(&params, &results);
  498. }
  499. static inline own wasm_functype_t* wasm_functype_new_1_0(
  500. own wasm_valtype_t* p
  501. ) {
  502. wasm_valtype_t* ps[1] = {p};
  503. wasm_valtype_vec_t params, results;
  504. wasm_valtype_vec_new(&params, 1, ps);
  505. wasm_valtype_vec_new_empty(&results);
  506. return wasm_functype_new(&params, &results);
  507. }
  508. static inline own wasm_functype_t* wasm_functype_new_2_0(
  509. own wasm_valtype_t* p1, own wasm_valtype_t* p2
  510. ) {
  511. wasm_valtype_t* ps[2] = {p1, p2};
  512. wasm_valtype_vec_t params, results;
  513. wasm_valtype_vec_new(&params, 2, ps);
  514. wasm_valtype_vec_new_empty(&results);
  515. return wasm_functype_new(&params, &results);
  516. }
  517. static inline own wasm_functype_t* wasm_functype_new_3_0(
  518. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3
  519. ) {
  520. wasm_valtype_t* ps[3] = {p1, p2, p3};
  521. wasm_valtype_vec_t params, results;
  522. wasm_valtype_vec_new(&params, 3, ps);
  523. wasm_valtype_vec_new_empty(&results);
  524. return wasm_functype_new(&params, &results);
  525. }
  526. static inline own wasm_functype_t* wasm_functype_new_0_1(
  527. own wasm_valtype_t* r
  528. ) {
  529. wasm_valtype_t* rs[1] = {r};
  530. wasm_valtype_vec_t params, results;
  531. wasm_valtype_vec_new_empty(&params);
  532. wasm_valtype_vec_new(&results, 1, rs);
  533. return wasm_functype_new(&params, &results);
  534. }
  535. static inline own wasm_functype_t* wasm_functype_new_1_1(
  536. own wasm_valtype_t* p, own wasm_valtype_t* r
  537. ) {
  538. wasm_valtype_t* ps[1] = {p};
  539. wasm_valtype_t* rs[1] = {r};
  540. wasm_valtype_vec_t params, results;
  541. wasm_valtype_vec_new(&params, 1, ps);
  542. wasm_valtype_vec_new(&results, 1, rs);
  543. return wasm_functype_new(&params, &results);
  544. }
  545. static inline own wasm_functype_t* wasm_functype_new_2_1(
  546. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* r
  547. ) {
  548. wasm_valtype_t* ps[2] = {p1, p2};
  549. wasm_valtype_t* rs[1] = {r};
  550. wasm_valtype_vec_t params, results;
  551. wasm_valtype_vec_new(&params, 2, ps);
  552. wasm_valtype_vec_new(&results, 1, rs);
  553. return wasm_functype_new(&params, &results);
  554. }
  555. static inline own wasm_functype_t* wasm_functype_new_3_1(
  556. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  557. own wasm_valtype_t* r
  558. ) {
  559. wasm_valtype_t* ps[3] = {p1, p2, p3};
  560. wasm_valtype_t* rs[1] = {r};
  561. wasm_valtype_vec_t params, results;
  562. wasm_valtype_vec_new(&params, 3, ps);
  563. wasm_valtype_vec_new(&results, 1, rs);
  564. return wasm_functype_new(&params, &results);
  565. }
  566. static inline own wasm_functype_t* wasm_functype_new_0_2(
  567. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  568. ) {
  569. wasm_valtype_t* rs[2] = {r1, r2};
  570. wasm_valtype_vec_t params, results;
  571. wasm_valtype_vec_new_empty(&params);
  572. wasm_valtype_vec_new(&results, 2, rs);
  573. return wasm_functype_new(&params, &results);
  574. }
  575. static inline own wasm_functype_t* wasm_functype_new_1_2(
  576. own wasm_valtype_t* p, own wasm_valtype_t* r1, own wasm_valtype_t* r2
  577. ) {
  578. wasm_valtype_t* ps[1] = {p};
  579. wasm_valtype_t* rs[2] = {r1, r2};
  580. wasm_valtype_vec_t params, results;
  581. wasm_valtype_vec_new(&params, 1, ps);
  582. wasm_valtype_vec_new(&results, 2, rs);
  583. return wasm_functype_new(&params, &results);
  584. }
  585. static inline own wasm_functype_t* wasm_functype_new_2_2(
  586. own wasm_valtype_t* p1, own wasm_valtype_t* p2,
  587. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  588. ) {
  589. wasm_valtype_t* ps[2] = {p1, p2};
  590. wasm_valtype_t* rs[2] = {r1, r2};
  591. wasm_valtype_vec_t params, results;
  592. wasm_valtype_vec_new(&params, 2, ps);
  593. wasm_valtype_vec_new(&results, 2, rs);
  594. return wasm_functype_new(&params, &results);
  595. }
  596. static inline own wasm_functype_t* wasm_functype_new_3_2(
  597. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  598. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  599. ) {
  600. wasm_valtype_t* ps[3] = {p1, p2, p3};
  601. wasm_valtype_t* rs[2] = {r1, r2};
  602. wasm_valtype_vec_t params, results;
  603. wasm_valtype_vec_new(&params, 3, ps);
  604. wasm_valtype_vec_new(&results, 2, rs);
  605. return wasm_functype_new(&params, &results);
  606. }
  607. // Value construction short-hands
  608. static inline void wasm_val_init_ptr(own wasm_val_t* out, void* p) {
  609. #if UINTPTR_MAX == UINT32_MAX
  610. out->kind = WASM_I32;
  611. out->of.i32 = (intptr_t)p;
  612. #elif UINTPTR_MAX == UINT64_MAX
  613. out->kind = WASM_I64;
  614. out->of.i64 = (intptr_t)p;
  615. #endif
  616. }
  617. static inline void* wasm_val_ptr(const wasm_val_t* val) {
  618. #if UINTPTR_MAX == UINT32_MAX
  619. return (void*)(intptr_t)val->of.i32;
  620. #elif UINTPTR_MAX == UINT64_MAX
  621. return (void*)(intptr_t)val->of.i64;
  622. #endif
  623. }
  624. #define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}}
  625. #define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}}
  626. #define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}}
  627. #define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}}
  628. #define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}}
  629. #define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}}
  630. #define KILOBYTE(n) ((n) * 1024)
  631. // Create placeholders filled in `wasm_externvec_t* imports` for `wasm_instance_new()`
  632. WASM_API_EXTERN wasm_extern_t *wasm_extern_new_empty(wasm_store_t *, wasm_externkind_t);
  633. ///////////////////////////////////////////////////////////////////////////////
  634. #undef own
  635. /* clang-format on */
  636. #ifdef __cplusplus
  637. } // extern "C"
  638. #endif
  639. #endif // #ifdef _WASM_C_API_H_