wasm_c_api.h 29 KB

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