wasm_c_api.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 WASM_API_DEPRECATED __attribute__((deprecated))
  22. #elif defined(_MSC_VER)
  23. #define WASM_API_DEPRECATED __declspec(deprecated)
  24. #else
  25. #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
  26. #define WASM_API_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. #ifndef INSTANTIATION_ARGS_OPTION_DEFINED
  158. #define INSTANTIATION_ARGS_OPTION_DEFINED
  159. /* WASM module instantiation arguments */
  160. typedef struct InstantiationArgs {
  161. uint32_t default_stack_size;
  162. uint32_t host_managed_heap_size;
  163. uint32_t max_memory_pages;
  164. } InstantiationArgs;
  165. #endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
  166. /*
  167. * by default:
  168. * - mem_alloc_type is Alloc_With_System_Allocator
  169. * - mem_alloc_option is all 0
  170. * - enable_linux_perf is false
  171. */
  172. WASM_API_EXTERN own wasm_config_t* wasm_config_new(void);
  173. // Embedders may provide custom functions for manipulating configs.
  174. WASM_API_EXTERN own wasm_config_t*
  175. wasm_config_set_mem_alloc_opt(wasm_config_t *, mem_alloc_type_t, MemAllocOption *);
  176. WASM_API_EXTERN own wasm_config_t*
  177. wasm_config_set_linux_perf_opt(wasm_config_t *, bool);
  178. /**
  179. * Enable using GS register as the base address of linear memory in linux x86_64,
  180. * which may speedup the linear memory access for LLVM AOT/JIT:
  181. * bit0 to bit4 denotes i32.load, i64.load, f32.load, f64.load, v128.load
  182. * bit8 to bit12 denotes i32.store, i64.store, f32.store, f64.store, v128.store
  183. * For example, 0x01 enables i32.load, 0x0100 enables i32.store.
  184. * To enable all load/store operations, use 0x1F1F
  185. */
  186. WASM_API_EXTERN wasm_config_t*
  187. wasm_config_set_segue_flags(wasm_config_t *config, uint32_t segue_flags);
  188. // Engine
  189. WASM_DECLARE_OWN(engine)
  190. /**
  191. * Create a new engine
  192. *
  193. * Note: for the engine new/delete operations, including this,
  194. * wasm_engine_new_with_config, wasm_engine_new_with_args, and
  195. * wasm_engine_delete, if the platform has mutex initializer,
  196. * then they are thread-safe: we use a global lock to lock the
  197. * operations of the engine. Otherwise they are not thread-safe:
  198. * when there are engine new/delete operations happening
  199. * simultaneously in multiple threads, developer must create
  200. * the lock by himself, and add the lock when calling these
  201. * functions.
  202. */
  203. WASM_API_EXTERN own wasm_engine_t* wasm_engine_new(void);
  204. WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(wasm_config_t*);
  205. WASM_API_DEPRECATED WASM_API_EXTERN own wasm_engine_t *
  206. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts);
  207. // Store
  208. WASM_DECLARE_OWN(store)
  209. WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
  210. ///////////////////////////////////////////////////////////////////////////////
  211. // Type Representations
  212. // Type attributes
  213. typedef uint8_t wasm_mutability_t;
  214. enum wasm_mutability_enum {
  215. WASM_CONST,
  216. WASM_VAR,
  217. };
  218. typedef struct wasm_limits_t {
  219. uint32_t min;
  220. uint32_t max;
  221. } wasm_limits_t;
  222. static const uint32_t wasm_limits_max_default = 0xffffffff;
  223. // Generic
  224. #define WASM_DECLARE_TYPE(name) \
  225. WASM_DECLARE_OWN(name) \
  226. WASM_DECLARE_VEC(name, *) \
  227. \
  228. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*);
  229. // Value Types
  230. WASM_DECLARE_TYPE(valtype)
  231. #ifndef WASM_VALKIND_T_DEFINED
  232. #define WASM_VALKIND_T_DEFINED
  233. typedef uint8_t wasm_valkind_t;
  234. enum wasm_valkind_enum {
  235. WASM_I32,
  236. WASM_I64,
  237. WASM_F32,
  238. WASM_F64,
  239. WASM_ANYREF = 128,
  240. WASM_FUNCREF,
  241. };
  242. #endif
  243. WASM_API_EXTERN own wasm_valtype_t* wasm_valtype_new(wasm_valkind_t);
  244. WASM_API_EXTERN wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t*);
  245. static inline bool wasm_valkind_is_num(wasm_valkind_t k) {
  246. return k < WASM_ANYREF;
  247. }
  248. static inline bool wasm_valkind_is_ref(wasm_valkind_t k) {
  249. return k >= WASM_ANYREF;
  250. }
  251. static inline bool wasm_valtype_is_num(const wasm_valtype_t* t) {
  252. return wasm_valkind_is_num(wasm_valtype_kind(t));
  253. }
  254. static inline bool wasm_valtype_is_ref(const wasm_valtype_t* t) {
  255. return wasm_valkind_is_ref(wasm_valtype_kind(t));
  256. }
  257. // Function Types
  258. WASM_DECLARE_TYPE(functype)
  259. WASM_API_EXTERN own wasm_functype_t* wasm_functype_new(
  260. own wasm_valtype_vec_t* params, own wasm_valtype_vec_t* results);
  261. WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t*);
  262. WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_results(const wasm_functype_t*);
  263. // Global Types
  264. WASM_DECLARE_TYPE(globaltype)
  265. WASM_API_EXTERN own wasm_globaltype_t* wasm_globaltype_new(
  266. own wasm_valtype_t*, wasm_mutability_t);
  267. WASM_API_EXTERN const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t*);
  268. WASM_API_EXTERN wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t*);
  269. // Table Types
  270. WASM_DECLARE_TYPE(tabletype)
  271. WASM_API_EXTERN own wasm_tabletype_t* wasm_tabletype_new(
  272. own wasm_valtype_t*, const wasm_limits_t*);
  273. WASM_API_EXTERN const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t*);
  274. WASM_API_EXTERN const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t*);
  275. // Memory Types
  276. WASM_DECLARE_TYPE(memorytype)
  277. WASM_API_EXTERN own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*);
  278. WASM_API_EXTERN const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t*);
  279. // Extern Types
  280. WASM_DECLARE_TYPE(externtype)
  281. typedef uint8_t wasm_externkind_t;
  282. enum wasm_externkind_enum {
  283. WASM_EXTERN_FUNC,
  284. WASM_EXTERN_GLOBAL,
  285. WASM_EXTERN_TABLE,
  286. WASM_EXTERN_MEMORY,
  287. };
  288. WASM_API_EXTERN wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*);
  289. WASM_API_EXTERN wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t*);
  290. WASM_API_EXTERN wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t*);
  291. WASM_API_EXTERN wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t*);
  292. WASM_API_EXTERN wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t*);
  293. WASM_API_EXTERN wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*);
  294. WASM_API_EXTERN wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t*);
  295. WASM_API_EXTERN wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t*);
  296. WASM_API_EXTERN wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t*);
  297. WASM_API_EXTERN const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t*);
  298. WASM_API_EXTERN const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t*);
  299. WASM_API_EXTERN const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t*);
  300. WASM_API_EXTERN const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t*);
  301. WASM_API_EXTERN const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t*);
  302. WASM_API_EXTERN const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t*);
  303. WASM_API_EXTERN const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t*);
  304. WASM_API_EXTERN const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t*);
  305. // Import Types
  306. WASM_DECLARE_TYPE(importtype)
  307. WASM_API_EXTERN own wasm_importtype_t* wasm_importtype_new(
  308. own wasm_name_t* module, own wasm_name_t* name, own wasm_externtype_t*);
  309. WASM_API_EXTERN const wasm_name_t* wasm_importtype_module(const wasm_importtype_t*);
  310. WASM_API_EXTERN const wasm_name_t* wasm_importtype_name(const wasm_importtype_t*);
  311. WASM_API_EXTERN const wasm_externtype_t* wasm_importtype_type(const wasm_importtype_t*);
  312. WASM_API_EXTERN bool wasm_importtype_is_linked(const wasm_importtype_t*);
  313. // Export Types
  314. WASM_DECLARE_TYPE(exporttype)
  315. WASM_API_EXTERN own wasm_exporttype_t* wasm_exporttype_new(
  316. own wasm_name_t*, own wasm_externtype_t*);
  317. WASM_API_EXTERN const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t*);
  318. WASM_API_EXTERN const wasm_externtype_t* wasm_exporttype_type(const wasm_exporttype_t*);
  319. ///////////////////////////////////////////////////////////////////////////////
  320. // Runtime Objects
  321. // Values
  322. #ifndef WASM_VAL_T_DEFINED
  323. #define WASM_VAL_T_DEFINED
  324. struct wasm_ref_t;
  325. typedef struct wasm_val_t {
  326. wasm_valkind_t kind;
  327. uint8_t __paddings[7];
  328. union {
  329. int32_t i32;
  330. int64_t i64;
  331. float32_t f32;
  332. float64_t f64;
  333. struct wasm_ref_t* ref;
  334. } of;
  335. } wasm_val_t;
  336. #endif
  337. WASM_API_EXTERN void wasm_val_delete(own wasm_val_t* v);
  338. WASM_API_EXTERN void wasm_val_copy(own wasm_val_t* out, const wasm_val_t*);
  339. WASM_DECLARE_VEC(val, )
  340. // References
  341. #define WASM_DECLARE_REF_BASE(name) \
  342. WASM_DECLARE_OWN(name) \
  343. \
  344. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
  345. WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
  346. \
  347. WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
  348. WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
  349. WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
  350. wasm_##name##_t*, void*, void (*)(void*));
  351. #define WASM_DECLARE_REF(name) \
  352. WASM_DECLARE_REF_BASE(name) \
  353. \
  354. WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
  355. WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
  356. WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
  357. WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
  358. #define WASM_DECLARE_SHARABLE_REF(name) \
  359. WASM_DECLARE_REF(name) \
  360. WASM_DECLARE_OWN(shared_##name) \
  361. \
  362. WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
  363. WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
  364. WASM_DECLARE_REF_BASE(ref)
  365. // Frames
  366. WASM_DECLARE_OWN(frame)
  367. WASM_DECLARE_VEC(frame, *)
  368. WASM_API_EXTERN own wasm_frame_t* wasm_frame_copy(const wasm_frame_t*);
  369. WASM_API_EXTERN struct wasm_instance_t* wasm_frame_instance(const wasm_frame_t*);
  370. WASM_API_EXTERN uint32_t wasm_frame_func_index(const wasm_frame_t*);
  371. WASM_API_EXTERN size_t wasm_frame_func_offset(const wasm_frame_t*);
  372. WASM_API_EXTERN size_t wasm_frame_module_offset(const wasm_frame_t*);
  373. // Traps
  374. typedef wasm_name_t wasm_message_t; // null terminated
  375. WASM_DECLARE_REF(trap)
  376. WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*);
  377. WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out);
  378. WASM_API_EXTERN own wasm_frame_t* wasm_trap_origin(const wasm_trap_t*);
  379. WASM_API_EXTERN void wasm_trap_trace(const wasm_trap_t*, own wasm_frame_vec_t* out);
  380. // Foreign Objects
  381. WASM_DECLARE_REF(foreign)
  382. WASM_API_EXTERN own wasm_foreign_t* wasm_foreign_new(wasm_store_t*);
  383. // Modules
  384. // WASM_DECLARE_SHARABLE_REF(module)
  385. #ifndef WASM_MODULE_T_DEFINED
  386. #define WASM_MODULE_T_DEFINED
  387. struct WASMModuleCommon;
  388. typedef struct WASMModuleCommon *wasm_module_t;
  389. #endif
  390. #ifndef LOAD_ARGS_OPTION_DEFINED
  391. #define LOAD_ARGS_OPTION_DEFINED
  392. typedef struct LoadArgs {
  393. char *name;
  394. /* TODO: more fields? */
  395. } LoadArgs;
  396. #endif /* LOAD_ARGS_OPTION_DEFINED */
  397. WASM_API_EXTERN own wasm_module_t* wasm_module_new(
  398. wasm_store_t*, const wasm_byte_vec_t* binary);
  399. // please refer to wasm_runtime_load_ex(...) in core/iwasm/include/wasm_export.h
  400. WASM_API_EXTERN own wasm_module_t* wasm_module_new_ex(
  401. wasm_store_t*, const wasm_byte_vec_t* binary, const LoadArgs *args);
  402. WASM_API_EXTERN void wasm_module_delete(own wasm_module_t*);
  403. WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
  404. WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out);
  405. WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out);
  406. WASM_API_EXTERN void wasm_module_serialize(wasm_module_t*, own wasm_byte_vec_t* out);
  407. WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const wasm_byte_vec_t*);
  408. typedef wasm_module_t wasm_shared_module_t;
  409. WASM_API_EXTERN own wasm_shared_module_t* wasm_module_share(wasm_module_t*);
  410. WASM_API_EXTERN own wasm_module_t* wasm_module_obtain(wasm_store_t*, wasm_shared_module_t*);
  411. WASM_API_EXTERN void wasm_shared_module_delete(own wasm_shared_module_t*);
  412. WASM_API_EXTERN bool wasm_module_set_name(wasm_module_t*, const char* name);
  413. WASM_API_EXTERN const char *wasm_module_get_name(wasm_module_t*);
  414. // Function Instances
  415. WASM_DECLARE_REF(func)
  416. typedef own wasm_trap_t* (*wasm_func_callback_t)(
  417. const wasm_val_vec_t* args, own wasm_val_vec_t *results);
  418. typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
  419. void* env, const wasm_val_vec_t *args, wasm_val_vec_t *results);
  420. WASM_API_EXTERN own wasm_func_t* wasm_func_new(
  421. wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
  422. WASM_API_EXTERN own wasm_func_t* wasm_func_new_with_env(
  423. wasm_store_t*, const wasm_functype_t* type, wasm_func_callback_with_env_t,
  424. void* env, void (*finalizer)(void*));
  425. WASM_API_EXTERN own wasm_functype_t* wasm_func_type(const wasm_func_t*);
  426. WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*);
  427. WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*);
  428. WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
  429. const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results);
  430. // Global Instances
  431. WASM_DECLARE_REF(global)
  432. WASM_API_EXTERN own wasm_global_t* wasm_global_new(
  433. wasm_store_t*, const wasm_globaltype_t*, const wasm_val_t*);
  434. WASM_API_EXTERN own wasm_globaltype_t* wasm_global_type(const wasm_global_t*);
  435. WASM_API_EXTERN void wasm_global_get(const wasm_global_t*, own wasm_val_t* out);
  436. WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);
  437. // Table Instances
  438. WASM_DECLARE_REF(table)
  439. typedef uint32_t wasm_table_size_t;
  440. WASM_API_EXTERN own wasm_table_t* wasm_table_new(
  441. wasm_store_t*, const wasm_tabletype_t*, wasm_ref_t* init);
  442. WASM_API_EXTERN own wasm_tabletype_t* wasm_table_type(const wasm_table_t*);
  443. WASM_API_EXTERN own wasm_ref_t* wasm_table_get(const wasm_table_t*, wasm_table_size_t index);
  444. WASM_API_EXTERN bool wasm_table_set(wasm_table_t*, wasm_table_size_t index, wasm_ref_t*);
  445. WASM_API_EXTERN wasm_table_size_t wasm_table_size(const wasm_table_t*);
  446. WASM_API_EXTERN bool wasm_table_grow(wasm_table_t*, wasm_table_size_t delta, wasm_ref_t* init);
  447. // Memory Instances
  448. WASM_DECLARE_REF(memory)
  449. typedef uint32_t wasm_memory_pages_t;
  450. static const size_t MEMORY_PAGE_SIZE = 0x10000;
  451. WASM_API_EXTERN own wasm_memory_t* wasm_memory_new(wasm_store_t*, const wasm_memorytype_t*);
  452. WASM_API_EXTERN own wasm_memorytype_t* wasm_memory_type(const wasm_memory_t*);
  453. WASM_API_EXTERN byte_t* wasm_memory_data(wasm_memory_t*);
  454. WASM_API_EXTERN size_t wasm_memory_data_size(const wasm_memory_t*);
  455. WASM_API_EXTERN wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*);
  456. WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta);
  457. // Externals
  458. WASM_DECLARE_REF(extern)
  459. WASM_DECLARE_VEC(extern, *)
  460. WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*);
  461. WASM_API_EXTERN own wasm_externtype_t* wasm_extern_type(const wasm_extern_t*);
  462. WASM_API_EXTERN wasm_extern_t* wasm_func_as_extern(wasm_func_t*);
  463. WASM_API_EXTERN wasm_extern_t* wasm_global_as_extern(wasm_global_t*);
  464. WASM_API_EXTERN wasm_extern_t* wasm_table_as_extern(wasm_table_t*);
  465. WASM_API_EXTERN wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*);
  466. WASM_API_EXTERN wasm_func_t* wasm_extern_as_func(wasm_extern_t*);
  467. WASM_API_EXTERN wasm_global_t* wasm_extern_as_global(wasm_extern_t*);
  468. WASM_API_EXTERN wasm_table_t* wasm_extern_as_table(wasm_extern_t*);
  469. WASM_API_EXTERN wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*);
  470. WASM_API_EXTERN const wasm_extern_t* wasm_func_as_extern_const(const wasm_func_t*);
  471. WASM_API_EXTERN const wasm_extern_t* wasm_global_as_extern_const(const wasm_global_t*);
  472. WASM_API_EXTERN const wasm_extern_t* wasm_table_as_extern_const(const wasm_table_t*);
  473. WASM_API_EXTERN const wasm_extern_t* wasm_memory_as_extern_const(const wasm_memory_t*);
  474. WASM_API_EXTERN const wasm_func_t* wasm_extern_as_func_const(const wasm_extern_t*);
  475. WASM_API_EXTERN const wasm_global_t* wasm_extern_as_global_const(const wasm_extern_t*);
  476. WASM_API_EXTERN const wasm_table_t* wasm_extern_as_table_const(const wasm_extern_t*);
  477. WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_extern_t*);
  478. // Module Instances
  479. WASM_DECLARE_REF(instance)
  480. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
  481. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  482. own wasm_trap_t** trap
  483. );
  484. // please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
  485. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
  486. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  487. own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size
  488. );
  489. // please refer to wasm_runtime_instantiate_ex(...) in core/iwasm/include/wasm_export.h
  490. WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args_ex(
  491. wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
  492. own wasm_trap_t** trap, const InstantiationArgs *inst_args
  493. );
  494. WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);
  495. ///////////////////////////////////////////////////////////////////////////////
  496. // Convenience
  497. // Vectors
  498. #define WASM_EMPTY_VEC {0, NULL, 0, 0, NULL}
  499. #define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array, sizeof(array)/sizeof(*(array)), sizeof(*(array)), NULL}
  500. // Value Type construction short-hands
  501. static inline own wasm_valtype_t* wasm_valtype_new_i32(void) {
  502. return wasm_valtype_new(WASM_I32);
  503. }
  504. static inline own wasm_valtype_t* wasm_valtype_new_i64(void) {
  505. return wasm_valtype_new(WASM_I64);
  506. }
  507. static inline own wasm_valtype_t* wasm_valtype_new_f32(void) {
  508. return wasm_valtype_new(WASM_F32);
  509. }
  510. static inline own wasm_valtype_t* wasm_valtype_new_f64(void) {
  511. return wasm_valtype_new(WASM_F64);
  512. }
  513. static inline own wasm_valtype_t* wasm_valtype_new_anyref(void) {
  514. return wasm_valtype_new(WASM_ANYREF);
  515. }
  516. static inline own wasm_valtype_t* wasm_valtype_new_funcref(void) {
  517. return wasm_valtype_new(WASM_FUNCREF);
  518. }
  519. // Function Types construction short-hands
  520. static inline own wasm_functype_t* wasm_functype_new_0_0(void) {
  521. wasm_valtype_vec_t params, results;
  522. wasm_valtype_vec_new_empty(&params);
  523. wasm_valtype_vec_new_empty(&results);
  524. return wasm_functype_new(&params, &results);
  525. }
  526. static inline own wasm_functype_t* wasm_functype_new_1_0(
  527. own wasm_valtype_t* p
  528. ) {
  529. wasm_valtype_t* ps[1] = {p};
  530. wasm_valtype_vec_t params, results;
  531. wasm_valtype_vec_new(&params, 1, ps);
  532. wasm_valtype_vec_new_empty(&results);
  533. return wasm_functype_new(&params, &results);
  534. }
  535. static inline own wasm_functype_t* wasm_functype_new_2_0(
  536. own wasm_valtype_t* p1, own wasm_valtype_t* p2
  537. ) {
  538. wasm_valtype_t* ps[2] = {p1, p2};
  539. wasm_valtype_vec_t params, results;
  540. wasm_valtype_vec_new(&params, 2, ps);
  541. wasm_valtype_vec_new_empty(&results);
  542. return wasm_functype_new(&params, &results);
  543. }
  544. static inline own wasm_functype_t* wasm_functype_new_3_0(
  545. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3
  546. ) {
  547. wasm_valtype_t* ps[3] = {p1, p2, p3};
  548. wasm_valtype_vec_t params, results;
  549. wasm_valtype_vec_new(&params, 3, ps);
  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_0_1(
  554. own wasm_valtype_t* r
  555. ) {
  556. wasm_valtype_t* rs[1] = {r};
  557. wasm_valtype_vec_t params, results;
  558. wasm_valtype_vec_new_empty(&params);
  559. wasm_valtype_vec_new(&results, 1, rs);
  560. return wasm_functype_new(&params, &results);
  561. }
  562. static inline own wasm_functype_t* wasm_functype_new_1_1(
  563. own wasm_valtype_t* p, own wasm_valtype_t* r
  564. ) {
  565. wasm_valtype_t* ps[1] = {p};
  566. wasm_valtype_t* rs[1] = {r};
  567. wasm_valtype_vec_t params, results;
  568. wasm_valtype_vec_new(&params, 1, ps);
  569. wasm_valtype_vec_new(&results, 1, rs);
  570. return wasm_functype_new(&params, &results);
  571. }
  572. static inline own wasm_functype_t* wasm_functype_new_2_1(
  573. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* r
  574. ) {
  575. wasm_valtype_t* ps[2] = {p1, p2};
  576. wasm_valtype_t* rs[1] = {r};
  577. wasm_valtype_vec_t params, results;
  578. wasm_valtype_vec_new(&params, 2, ps);
  579. wasm_valtype_vec_new(&results, 1, rs);
  580. return wasm_functype_new(&params, &results);
  581. }
  582. static inline own wasm_functype_t* wasm_functype_new_3_1(
  583. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  584. own wasm_valtype_t* r
  585. ) {
  586. wasm_valtype_t* ps[3] = {p1, p2, p3};
  587. wasm_valtype_t* rs[1] = {r};
  588. wasm_valtype_vec_t params, results;
  589. wasm_valtype_vec_new(&params, 3, 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_0_2(
  594. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  595. ) {
  596. wasm_valtype_t* rs[2] = {r1, r2};
  597. wasm_valtype_vec_t params, results;
  598. wasm_valtype_vec_new_empty(&params);
  599. wasm_valtype_vec_new(&results, 2, rs);
  600. return wasm_functype_new(&params, &results);
  601. }
  602. static inline own wasm_functype_t* wasm_functype_new_1_2(
  603. own wasm_valtype_t* p, own wasm_valtype_t* r1, own wasm_valtype_t* r2
  604. ) {
  605. wasm_valtype_t* ps[1] = {p};
  606. wasm_valtype_t* rs[2] = {r1, r2};
  607. wasm_valtype_vec_t params, results;
  608. wasm_valtype_vec_new(&params, 1, ps);
  609. wasm_valtype_vec_new(&results, 2, rs);
  610. return wasm_functype_new(&params, &results);
  611. }
  612. static inline own wasm_functype_t* wasm_functype_new_2_2(
  613. own wasm_valtype_t* p1, own wasm_valtype_t* p2,
  614. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  615. ) {
  616. wasm_valtype_t* ps[2] = {p1, p2};
  617. wasm_valtype_t* rs[2] = {r1, r2};
  618. wasm_valtype_vec_t params, results;
  619. wasm_valtype_vec_new(&params, 2, ps);
  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_3_2(
  624. own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
  625. own wasm_valtype_t* r1, own wasm_valtype_t* r2
  626. ) {
  627. wasm_valtype_t* ps[3] = {p1, p2, p3};
  628. wasm_valtype_t* rs[2] = {r1, r2};
  629. wasm_valtype_vec_t params, results;
  630. wasm_valtype_vec_new(&params, 3, ps);
  631. wasm_valtype_vec_new(&results, 2, rs);
  632. return wasm_functype_new(&params, &results);
  633. }
  634. // Value construction short-hands
  635. static inline void wasm_val_init_ptr(own wasm_val_t* out, void* p) {
  636. #if UINTPTR_MAX == UINT32_MAX
  637. out->kind = WASM_I32;
  638. out->of.i32 = (intptr_t)p;
  639. #elif UINTPTR_MAX == UINT64_MAX
  640. out->kind = WASM_I64;
  641. out->of.i64 = (intptr_t)p;
  642. #endif
  643. }
  644. static inline void* wasm_val_ptr(const wasm_val_t* val) {
  645. #if UINTPTR_MAX == UINT32_MAX
  646. return (void*)(intptr_t)val->of.i32;
  647. #elif UINTPTR_MAX == UINT64_MAX
  648. return (void*)(intptr_t)val->of.i64;
  649. #endif
  650. }
  651. #define WASM_I32_VAL(i) {.kind = WASM_I32, .__paddings = {0}, .of = {.i32 = i}}
  652. #define WASM_I64_VAL(i) {.kind = WASM_I64, .__paddings = {0}, .of = {.i64 = i}}
  653. #define WASM_F32_VAL(z) {.kind = WASM_F32, .__paddings = {0}, .of = {.f32 = z}}
  654. #define WASM_F64_VAL(z) {.kind = WASM_F64, .__paddings = {0}, .of = {.f64 = z}}
  655. #define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .__paddings = {0}, .of = {.ref = r}}
  656. #define WASM_INIT_VAL {.kind = WASM_ANYREF, .__paddings = {0}, .of = {.ref = NULL}}
  657. #define KILOBYTE(n) ((n) * 1024)
  658. // Create placeholders filled in `wasm_externvec_t* imports` for `wasm_instance_new()`
  659. WASM_API_EXTERN wasm_extern_t *wasm_extern_new_empty(wasm_store_t *, wasm_externkind_t);
  660. ///////////////////////////////////////////////////////////////////////////////
  661. #undef own
  662. /* clang-format on */
  663. #ifdef __cplusplus
  664. } // extern "C"
  665. #endif
  666. #endif // #ifdef _WASM_C_API_H_