wasm_c_api.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. uint8_t __paddings[7];
  319. union {
  320. int32_t i32;
  321. int64_t i64;
  322. float32_t f32;
  323. float64_t f64;
  324. struct wasm_ref_t* ref;
  325. } of;
  326. } wasm_val_t;
  327. #endif
  328. WASM_API_EXTERN void wasm_val_delete(own wasm_val_t* v);
  329. WASM_API_EXTERN void wasm_val_copy(own wasm_val_t* out, const wasm_val_t*);
  330. WASM_DECLARE_VEC(val, )
  331. // References
  332. #define WASM_DECLARE_REF_BASE(name) \
  333. WASM_DECLARE_OWN(name) \
  334. \
  335. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
  336. WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
  337. \
  338. WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
  339. WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
  340. WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
  341. wasm_##name##_t*, void*, void (*)(void*));
  342. #define WASM_DECLARE_REF(name) \
  343. WASM_DECLARE_REF_BASE(name) \
  344. \
  345. WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
  346. WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
  347. WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
  348. WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
  349. #define WASM_DECLARE_SHARABLE_REF(name) \
  350. WASM_DECLARE_REF(name) \
  351. WASM_DECLARE_OWN(shared_##name) \
  352. \
  353. WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
  354. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
  355. WASM_DECLARE_REF_BASE(ref)
  356. // Frames
  357. WASM_DECLARE_OWN(frame)
  358. WASM_DECLARE_VEC(frame, *)
  359. WASM_API_EXTERN own wasm_frame_t* wasm_frame_copy(const wasm_frame_t*);
  360. WASM_API_EXTERN struct wasm_instance_t* wasm_frame_instance(const wasm_frame_t*);
  361. WASM_API_EXTERN uint32_t wasm_frame_func_index(const wasm_frame_t*);
  362. WASM_API_EXTERN size_t wasm_frame_func_offset(const wasm_frame_t*);
  363. WASM_API_EXTERN size_t wasm_frame_module_offset(const wasm_frame_t*);
  364. // Traps
  365. typedef wasm_name_t wasm_message_t; // null terminated
  366. WASM_DECLARE_REF(trap)
  367. WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*);
  368. WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out);
  369. WASM_API_EXTERN own wasm_frame_t* wasm_trap_origin(const wasm_trap_t*);
  370. WASM_API_EXTERN void wasm_trap_trace(const wasm_trap_t*, own wasm_frame_vec_t* out);
  371. // Foreign Objects
  372. WASM_DECLARE_REF(foreign)
  373. WASM_API_EXTERN own wasm_foreign_t* wasm_foreign_new(wasm_store_t*);
  374. // Modules
  375. // WASM_DECLARE_SHARABLE_REF(module)
  376. #ifndef WASM_MODULE_T_DEFINED
  377. #define WASM_MODULE_T_DEFINED
  378. struct WASMModuleCommon;
  379. typedef struct WASMModuleCommon *wasm_module_t;
  380. #endif
  381. WASM_API_EXTERN own wasm_module_t* wasm_module_new(
  382. wasm_store_t*, const wasm_byte_vec_t* binary);
  383. WASM_API_EXTERN void wasm_module_delete(own wasm_module_t*);
  384. WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
  385. WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out);
  386. WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out);
  387. WASM_API_EXTERN void wasm_module_serialize(wasm_module_t*, own wasm_byte_vec_t* out);
  388. WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const wasm_byte_vec_t*);
  389. typedef wasm_module_t wasm_shared_module_t;
  390. WASM_API_EXTERN own wasm_shared_module_t* wasm_module_share(wasm_module_t*);
  391. WASM_API_EXTERN own wasm_module_t* wasm_module_obtain(wasm_store_t*, wasm_shared_module_t*);
  392. WASM_API_EXTERN void wasm_shared_module_delete(own wasm_shared_module_t*);
  393. // Function Instances
  394. WASM_DECLARE_REF(func)
  395. typedef own wasm_trap_t* (*wasm_func_callback_t)(
  396. const wasm_val_vec_t* args, own wasm_val_vec_t *results);
  397. typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
  398. void* env, const wasm_val_vec_t *args, wasm_val_vec_t *results);
  399. WASM_API_EXTERN own wasm_func_t* wasm_func_new(
  400. wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
  401. WASM_API_EXTERN own wasm_func_t* wasm_func_new_with_env(
  402. wasm_store_t*, const wasm_functype_t* type, wasm_func_callback_with_env_t,
  403. void* env, void (*finalizer)(void*));
  404. WASM_API_EXTERN own wasm_functype_t* wasm_func_type(const wasm_func_t*);
  405. WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*);
  406. WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*);
  407. WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
  408. const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results);
  409. // Global Instances
  410. WASM_DECLARE_REF(global)
  411. WASM_API_EXTERN own wasm_global_t* wasm_global_new(
  412. wasm_store_t*, const wasm_globaltype_t*, const wasm_val_t*);
  413. WASM_API_EXTERN own wasm_globaltype_t* wasm_global_type(const wasm_global_t*);
  414. WASM_API_EXTERN void wasm_global_get(const wasm_global_t*, own wasm_val_t* out);
  415. WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);
  416. // Table Instances
  417. WASM_DECLARE_REF(table)
  418. typedef uint32_t wasm_table_size_t;
  419. WASM_API_EXTERN own wasm_table_t* wasm_table_new(
  420. wasm_store_t*, const wasm_tabletype_t*, wasm_ref_t* init);
  421. WASM_API_EXTERN own wasm_tabletype_t* wasm_table_type(const wasm_table_t*);
  422. WASM_API_EXTERN own wasm_ref_t* wasm_table_get(const wasm_table_t*, wasm_table_size_t index);
  423. WASM_API_EXTERN bool wasm_table_set(wasm_table_t*, wasm_table_size_t index, wasm_ref_t*);
  424. WASM_API_EXTERN wasm_table_size_t wasm_table_size(const wasm_table_t*);
  425. WASM_API_EXTERN bool wasm_table_grow(wasm_table_t*, wasm_table_size_t delta, wasm_ref_t* init);
  426. // Memory Instances
  427. WASM_DECLARE_REF(memory)
  428. typedef uint32_t wasm_memory_pages_t;
  429. static const size_t MEMORY_PAGE_SIZE = 0x10000;
  430. WASM_API_EXTERN own wasm_memory_t* wasm_memory_new(wasm_store_t*, const wasm_memorytype_t*);
  431. WASM_API_EXTERN own wasm_memorytype_t* wasm_memory_type(const wasm_memory_t*);
  432. WASM_API_EXTERN byte_t* wasm_memory_data(wasm_memory_t*);
  433. WASM_API_EXTERN size_t wasm_memory_data_size(const wasm_memory_t*);
  434. WASM_API_EXTERN wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*);
  435. WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta);
  436. // Externals
  437. WASM_DECLARE_REF(extern)
  438. WASM_DECLARE_VEC(extern, *)
  439. WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*);
  440. WASM_API_EXTERN own wasm_externtype_t* wasm_extern_type(const wasm_extern_t*);
  441. WASM_API_EXTERN wasm_extern_t* wasm_func_as_extern(wasm_func_t*);
  442. WASM_API_EXTERN wasm_extern_t* wasm_global_as_extern(wasm_global_t*);
  443. WASM_API_EXTERN wasm_extern_t* wasm_table_as_extern(wasm_table_t*);
  444. WASM_API_EXTERN wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*);
  445. WASM_API_EXTERN wasm_func_t* wasm_extern_as_func(wasm_extern_t*);
  446. WASM_API_EXTERN wasm_global_t* wasm_extern_as_global(wasm_extern_t*);
  447. WASM_API_EXTERN wasm_table_t* wasm_extern_as_table(wasm_extern_t*);
  448. WASM_API_EXTERN wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*);
  449. WASM_API_EXTERN const wasm_extern_t* wasm_func_as_extern_const(const wasm_func_t*);
  450. WASM_API_EXTERN const wasm_extern_t* wasm_global_as_extern_const(const wasm_global_t*);
  451. WASM_API_EXTERN const wasm_extern_t* wasm_table_as_extern_const(const wasm_table_t*);
  452. WASM_API_EXTERN const wasm_extern_t* wasm_memory_as_extern_const(const wasm_memory_t*);
  453. WASM_API_EXTERN const wasm_func_t* wasm_extern_as_func_const(const wasm_extern_t*);
  454. WASM_API_EXTERN const wasm_global_t* wasm_extern_as_global_const(const wasm_extern_t*);
  455. WASM_API_EXTERN const wasm_table_t* wasm_extern_as_table_const(const wasm_extern_t*);
  456. WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_extern_t*);
  457. // Module Instances
  458. WASM_DECLARE_REF(instance)
  459. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
  460. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  461. own wasm_trap_t** trap
  462. );
  463. // please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
  464. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
  465. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  466. own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size
  467. );
  468. WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);
  469. ///////////////////////////////////////////////////////////////////////////////
  470. // Convenience
  471. // Vectors
  472. #define WASM_EMPTY_VEC {0, NULL, 0, 0, NULL}
  473. #define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array, sizeof(array)/sizeof(*(array)), sizeof(*(array)), NULL}
  474. // Value Type construction short-hands
  475. static inline own wasm_valtype_t* wasm_valtype_new_i32(void) {
  476. return wasm_valtype_new(WASM_I32);
  477. }
  478. static inline own wasm_valtype_t* wasm_valtype_new_i64(void) {
  479. return wasm_valtype_new(WASM_I64);
  480. }
  481. static inline own wasm_valtype_t* wasm_valtype_new_f32(void) {
  482. return wasm_valtype_new(WASM_F32);
  483. }
  484. static inline own wasm_valtype_t* wasm_valtype_new_f64(void) {
  485. return wasm_valtype_new(WASM_F64);
  486. }
  487. static inline own wasm_valtype_t* wasm_valtype_new_anyref(void) {
  488. return wasm_valtype_new(WASM_ANYREF);
  489. }
  490. static inline own wasm_valtype_t* wasm_valtype_new_funcref(void) {
  491. return wasm_valtype_new(WASM_FUNCREF);
  492. }
  493. // Function Types construction short-hands
  494. static inline own wasm_functype_t* wasm_functype_new_0_0(void) {
  495. wasm_valtype_vec_t params, results;
  496. wasm_valtype_vec_new_empty(&params);
  497. wasm_valtype_vec_new_empty(&results);
  498. return wasm_functype_new(&params, &results);
  499. }
  500. static inline own wasm_functype_t* wasm_functype_new_1_0(
  501. own wasm_valtype_t* p
  502. ) {
  503. wasm_valtype_t* ps[1] = {p};
  504. wasm_valtype_vec_t params, results;
  505. wasm_valtype_vec_new(&params, 1, ps);
  506. wasm_valtype_vec_new_empty(&results);
  507. return wasm_functype_new(&params, &results);
  508. }
  509. static inline own wasm_functype_t* wasm_functype_new_2_0(
  510. own wasm_valtype_t* p1, own wasm_valtype_t* p2
  511. ) {
  512. wasm_valtype_t* ps[2] = {p1, p2};
  513. wasm_valtype_vec_t params, results;
  514. wasm_valtype_vec_new(&params, 2, ps);
  515. wasm_valtype_vec_new_empty(&results);
  516. return wasm_functype_new(&params, &results);
  517. }
  518. static inline own wasm_functype_t* wasm_functype_new_3_0(
  519. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3
  520. ) {
  521. wasm_valtype_t* ps[3] = {p1, p2, p3};
  522. wasm_valtype_vec_t params, results;
  523. wasm_valtype_vec_new(&params, 3, ps);
  524. wasm_valtype_vec_new_empty(&results);
  525. return wasm_functype_new(&params, &results);
  526. }
  527. static inline own wasm_functype_t* wasm_functype_new_0_1(
  528. own wasm_valtype_t* r
  529. ) {
  530. wasm_valtype_t* rs[1] = {r};
  531. wasm_valtype_vec_t params, results;
  532. wasm_valtype_vec_new_empty(&params);
  533. wasm_valtype_vec_new(&results, 1, rs);
  534. return wasm_functype_new(&params, &results);
  535. }
  536. static inline own wasm_functype_t* wasm_functype_new_1_1(
  537. own wasm_valtype_t* p, own wasm_valtype_t* r
  538. ) {
  539. wasm_valtype_t* ps[1] = {p};
  540. wasm_valtype_t* rs[1] = {r};
  541. wasm_valtype_vec_t params, results;
  542. wasm_valtype_vec_new(&params, 1, ps);
  543. wasm_valtype_vec_new(&results, 1, rs);
  544. return wasm_functype_new(&params, &results);
  545. }
  546. static inline own wasm_functype_t* wasm_functype_new_2_1(
  547. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* r
  548. ) {
  549. wasm_valtype_t* ps[2] = {p1, p2};
  550. wasm_valtype_t* rs[1] = {r};
  551. wasm_valtype_vec_t params, results;
  552. wasm_valtype_vec_new(&params, 2, ps);
  553. wasm_valtype_vec_new(&results, 1, rs);
  554. return wasm_functype_new(&params, &results);
  555. }
  556. static inline own wasm_functype_t* wasm_functype_new_3_1(
  557. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  558. own wasm_valtype_t* r
  559. ) {
  560. wasm_valtype_t* ps[3] = {p1, p2, p3};
  561. wasm_valtype_t* rs[1] = {r};
  562. wasm_valtype_vec_t params, results;
  563. wasm_valtype_vec_new(&params, 3, ps);
  564. wasm_valtype_vec_new(&results, 1, rs);
  565. return wasm_functype_new(&params, &results);
  566. }
  567. static inline own wasm_functype_t* wasm_functype_new_0_2(
  568. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  569. ) {
  570. wasm_valtype_t* rs[2] = {r1, r2};
  571. wasm_valtype_vec_t params, results;
  572. wasm_valtype_vec_new_empty(&params);
  573. wasm_valtype_vec_new(&results, 2, rs);
  574. return wasm_functype_new(&params, &results);
  575. }
  576. static inline own wasm_functype_t* wasm_functype_new_1_2(
  577. own wasm_valtype_t* p, own wasm_valtype_t* r1, own wasm_valtype_t* r2
  578. ) {
  579. wasm_valtype_t* ps[1] = {p};
  580. wasm_valtype_t* rs[2] = {r1, r2};
  581. wasm_valtype_vec_t params, results;
  582. wasm_valtype_vec_new(&params, 1, ps);
  583. wasm_valtype_vec_new(&results, 2, rs);
  584. return wasm_functype_new(&params, &results);
  585. }
  586. static inline own wasm_functype_t* wasm_functype_new_2_2(
  587. own wasm_valtype_t* p1, own wasm_valtype_t* p2,
  588. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  589. ) {
  590. wasm_valtype_t* ps[2] = {p1, p2};
  591. wasm_valtype_t* rs[2] = {r1, r2};
  592. wasm_valtype_vec_t params, results;
  593. wasm_valtype_vec_new(&params, 2, ps);
  594. wasm_valtype_vec_new(&results, 2, rs);
  595. return wasm_functype_new(&params, &results);
  596. }
  597. static inline own wasm_functype_t* wasm_functype_new_3_2(
  598. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  599. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  600. ) {
  601. wasm_valtype_t* ps[3] = {p1, p2, p3};
  602. wasm_valtype_t* rs[2] = {r1, r2};
  603. wasm_valtype_vec_t params, results;
  604. wasm_valtype_vec_new(&params, 3, ps);
  605. wasm_valtype_vec_new(&results, 2, rs);
  606. return wasm_functype_new(&params, &results);
  607. }
  608. // Value construction short-hands
  609. static inline void wasm_val_init_ptr(own wasm_val_t* out, void* p) {
  610. #if UINTPTR_MAX == UINT32_MAX
  611. out->kind = WASM_I32;
  612. out->of.i32 = (intptr_t)p;
  613. #elif UINTPTR_MAX == UINT64_MAX
  614. out->kind = WASM_I64;
  615. out->of.i64 = (intptr_t)p;
  616. #endif
  617. }
  618. static inline void* wasm_val_ptr(const wasm_val_t* val) {
  619. #if UINTPTR_MAX == UINT32_MAX
  620. return (void*)(intptr_t)val->of.i32;
  621. #elif UINTPTR_MAX == UINT64_MAX
  622. return (void*)(intptr_t)val->of.i64;
  623. #endif
  624. }
  625. #define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}}
  626. #define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}}
  627. #define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}}
  628. #define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}}
  629. #define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}}
  630. #define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}}
  631. #define KILOBYTE(n) ((n) * 1024)
  632. // Create placeholders filled in `wasm_externvec_t* imports` for `wasm_instance_new()`
  633. WASM_API_EXTERN wasm_extern_t *wasm_extern_new_empty(wasm_store_t *, wasm_externkind_t);
  634. ///////////////////////////////////////////////////////////////////////////////
  635. #undef own
  636. /* clang-format on */
  637. #ifdef __cplusplus
  638. } // extern "C"
  639. #endif
  640. #endif // #ifdef _WASM_C_API_H_