wasm_c_api.h 29 KB

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