gc_export.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _GC_EXPORT_H
  6. #define _GC_EXPORT_H
  7. #include "wasm_export.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. typedef uint8_t wasm_value_type_t;
  12. typedef enum wasm_value_type_enum {
  13. VALUE_TYPE_I32 = 0x7F,
  14. VALUE_TYPE_I64 = 0x7E,
  15. VALUE_TYPE_F32 = 0x7D,
  16. VALUE_TYPE_F64 = 0x7C,
  17. VALUE_TYPE_V128 = 0x7B,
  18. VALUE_TYPE_I8 = 0x7A,
  19. VALUE_TYPE_I16 = 0x79,
  20. VALUE_TYPE_FUNCREF = 0x70,
  21. VALUE_TYPE_EXTERNREF = 0x6F,
  22. VALUE_TYPE_ANYREF = 0x6E,
  23. VALUE_TYPE_EQREF = 0x6D,
  24. VALUE_TYPE_HT_NULLABLE_REF = 0x6C,
  25. VALUE_TYPE_HT_NON_NULLABLE_REF = 0x6B,
  26. VALUE_TYPE_I31REF = 0x6A,
  27. VALUE_TYPE_NULLFUNCREF = 0x69,
  28. VALUE_TYPE_NULLEXTERNREF = 0x68,
  29. VALUE_TYPE_STRUCTREF = 0x67,
  30. VALUE_TYPE_ARRAYREF = 0x66,
  31. VALUE_TYPE_NULLREF = 0x65,
  32. } wasm_value_type_enum;
  33. typedef int32_t wasm_heap_type_t;
  34. typedef enum wasm_heap_type_enum {
  35. HEAP_TYPE_FUNC = -0x10,
  36. HEAP_TYPE_EXTERN = -0x11,
  37. HEAP_TYPE_ANY = -0x12,
  38. HEAP_TYPE_EQ = -0x13,
  39. HEAP_TYPE_I31 = -0x16,
  40. HEAP_TYPE_NOFUNC = -0x17,
  41. HEAP_TYPE_NOEXTERN = -0x18,
  42. HEAP_TYPE_STRUCT = -0x19,
  43. HEAP_TYPE_ARRAY = -0x1A,
  44. HEAP_TYPE_NONE = -0x1B
  45. } wasm_heap_type_enum;
  46. struct WASMObject;
  47. typedef struct WASMObject *wasm_obj_t;
  48. #ifndef WASM_VALUE_DEFINED
  49. #define WASM_VALUE_DEFINED
  50. typedef union V128 {
  51. int8_t i8x16[16];
  52. int16_t i16x8[8];
  53. int32_t i32x8[4];
  54. int64_t i64x2[2];
  55. float f32x4[4];
  56. double f64x2[2];
  57. } V128;
  58. typedef union WASMValue {
  59. int32_t i32;
  60. uint32_t u32;
  61. uint32_t global_index;
  62. uint32_t ref_index;
  63. int64_t i64;
  64. uint64_t u64;
  65. float f32;
  66. double f64;
  67. V128 v128;
  68. wasm_obj_t gc_obj;
  69. uint32_t type_index;
  70. struct {
  71. uint32_t type_index;
  72. uint32_t N;
  73. } array_new_canon_fixed;
  74. } WASMValue;
  75. #endif /* end of WASM_VALUE_DEFINED */
  76. typedef union WASMValue wasm_value_t;
  77. /* Reference type, the layout is same as WasmRefType in wasm.h
  78. * use wasm_ref_type_set_type_idx to initialize as concrete ref type
  79. * use wasm_ref_type_set_heap_type to initialize as abstract ref type
  80. */
  81. typedef struct wasm_ref_type_t {
  82. wasm_value_type_t value_type;
  83. bool nullable;
  84. int32_t heap_type;
  85. } wasm_ref_type_t;
  86. /**
  87. * Local object reference that can be traced when GC occurs. All
  88. * native functions that need to hold WASM objects which may not be
  89. * referenced from other elements of GC root set may be hold with
  90. * this type of variable so that they can be traced when GC occurs.
  91. * Before using such a variable, it must be pushed onto the stack
  92. * (implemented as a chain) of such variables, and before leaving the
  93. * frame of the variables, they must be popped from the stack.
  94. */
  95. typedef struct WASMLocalObjectRef {
  96. /* Previous local object reference variable on the stack */
  97. struct WASMLocalObjectRef *prev;
  98. /* The reference of WASM object hold by this variable */
  99. wasm_obj_t val;
  100. } WASMLocalObjectRef, wasm_local_obj_ref_t;
  101. struct WASMType;
  102. struct WASMFuncType;
  103. struct WASMStructType;
  104. struct WASMArrayType;
  105. typedef struct WASMType *wasm_defined_type_t;
  106. typedef struct WASMFuncType *wasm_func_type_t;
  107. typedef struct WASMStructType *wasm_struct_type_t;
  108. typedef struct WASMArrayType *wasm_array_type_t;
  109. struct WASMExternrefObject;
  110. struct WASMAnyrefObject;
  111. struct WASMStructObject;
  112. struct WASMArrayObject;
  113. struct WASMFuncObject;
  114. typedef struct WASMExternrefObject *wasm_externref_obj_t;
  115. typedef struct WASMAnyrefObject *wasm_anyref_obj_t;
  116. typedef struct WASMStructObject *wasm_struct_obj_t;
  117. typedef struct WASMArrayObject *wasm_array_obj_t;
  118. typedef struct WASMFuncObject *wasm_func_obj_t;
  119. typedef uintptr_t wasm_i31_obj_t;
  120. typedef void (*wasm_obj_finalizer_t)(const wasm_obj_t obj, void *data);
  121. /* Defined type related operations */
  122. /**
  123. * Get number of defined types in the given wasm module
  124. *
  125. * @param module the wasm module
  126. *
  127. * @return defined type count
  128. */
  129. WASM_RUNTIME_API_EXTERN uint32_t
  130. wasm_get_defined_type_count(const wasm_module_t module);
  131. /**
  132. * Get defined type by type index
  133. *
  134. * @param module the wasm module
  135. * @param index the type index
  136. *
  137. * @return defined type
  138. */
  139. WASM_RUNTIME_API_EXTERN wasm_defined_type_t
  140. wasm_get_defined_type(const wasm_module_t module, uint32_t index);
  141. /**
  142. * Get defined type of the GC managed object, the object must be struct,
  143. * array or func.
  144. *
  145. * @param obj the object
  146. *
  147. * @return defined type of the object.
  148. */
  149. WASM_RUNTIME_API_EXTERN wasm_defined_type_t
  150. wasm_obj_get_defined_type(const wasm_obj_t obj);
  151. /**
  152. * Get defined type index of the GC managed object, the object must be struct,
  153. * array or func.
  154. *
  155. * @param obj the object
  156. *
  157. * @return defined type index of the object.
  158. */
  159. WASM_RUNTIME_API_EXTERN int32_t
  160. wasm_obj_get_defined_type_idx(const wasm_module_t module, const wasm_obj_t obj);
  161. /**
  162. * Check whether a defined type is a function type
  163. *
  164. * @param def_type the defined type to be checked
  165. *
  166. * @return true if the defined type is function type, false otherwise
  167. */
  168. WASM_RUNTIME_API_EXTERN bool
  169. wasm_defined_type_is_func_type(const wasm_defined_type_t def_type);
  170. /**
  171. * Check whether a defined type is a struct type
  172. *
  173. * @param def_type the defined type to be checked
  174. *
  175. * @return true if the defined type is struct type, false otherwise
  176. */
  177. WASM_RUNTIME_API_EXTERN bool
  178. wasm_defined_type_is_struct_type(const wasm_defined_type_t def_type);
  179. /**
  180. * Check whether a defined type is an array type
  181. *
  182. * @param def_type the defined type to be checked
  183. *
  184. * @return true if the defined type is array type, false otherwise
  185. */
  186. WASM_RUNTIME_API_EXTERN bool
  187. wasm_defined_type_is_array_type(const wasm_defined_type_t def_type);
  188. /**
  189. * Get parameter count of a function type
  190. *
  191. * @param func_type the specified function type
  192. *
  193. * @return the param count of the specified function type
  194. */
  195. WASM_RUNTIME_API_EXTERN uint32_t
  196. wasm_func_type_get_param_count(const wasm_func_type_t func_type);
  197. /**
  198. * Get type of a specified parameter of a function type
  199. *
  200. * @param func_type the specified function type
  201. * @param param_idx the specified param index
  202. *
  203. * @return the param type at the specified param index of the specified func
  204. * type
  205. */
  206. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  207. wasm_func_type_get_param_type(const wasm_func_type_t func_type,
  208. uint32_t param_idx);
  209. /**
  210. * Get result count of a function type
  211. *
  212. * @param func_type the specified function type
  213. *
  214. * @return the result count of the specified function type
  215. */
  216. WASM_RUNTIME_API_EXTERN uint32_t
  217. wasm_func_type_get_result_count(const wasm_func_type_t func_type);
  218. /**
  219. * Get type of a specified result of a function type
  220. *
  221. * @param func_type the specified function type
  222. * @param param_idx the specified result index
  223. *
  224. * @return the result type at the specified result index of the specified func
  225. * type
  226. */
  227. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  228. wasm_func_type_get_result_type(const wasm_func_type_t func_type,
  229. uint32_t result_idx);
  230. /**
  231. * Get field count of a struct type
  232. *
  233. * @param struct_type the specified struct type
  234. *
  235. * @return the field count of the specified struct type
  236. */
  237. WASM_RUNTIME_API_EXTERN uint32_t
  238. wasm_struct_type_get_field_count(const wasm_struct_type_t struct_type);
  239. /**
  240. * Get type of a specified field of a struct type
  241. *
  242. * @param struct_type the specified struct type
  243. * @param field_idx index of the specified field
  244. * @param p_is_mutable if not NULL, output the mutability of the field
  245. *
  246. * @return the result type at the specified field index of the specified struct
  247. */
  248. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  249. wasm_struct_type_get_field_type(const wasm_struct_type_t struct_type,
  250. uint32_t field_idx, bool *p_is_mutable);
  251. /**
  252. * Get element type of an array type
  253. *
  254. * @param array_type the specified array type
  255. * @param p_is_mutable if not NULL, output the mutability of the element type
  256. *
  257. * @return the ref type of array's elem type
  258. */
  259. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  260. wasm_array_type_get_elem_type(const wasm_array_type_t array_type,
  261. bool *p_is_mutable);
  262. /**
  263. * Check whether two defined types are equal
  264. *
  265. * @param def_type1 the specified defined type1
  266. * @param def_type2 the specified defined type2
  267. * @param module current wasm module
  268. *
  269. * @return true if the defined type1 is equal to the defined type2,
  270. * false otherwise
  271. */
  272. WASM_RUNTIME_API_EXTERN bool
  273. wasm_defined_type_equal(const wasm_defined_type_t def_type1,
  274. const wasm_defined_type_t def_type2,
  275. const wasm_module_t module);
  276. /**
  277. * Check whether def_type1 is subtype of def_type2
  278. *
  279. * @param def_type1 the specified defined type1
  280. * @param def_type2 the specified defined type2
  281. * @param module current wasm module
  282. *
  283. * @return true if the defined type1 is subtype of the defined type2,
  284. * false otherwise
  285. */
  286. WASM_RUNTIME_API_EXTERN bool
  287. wasm_defined_type_is_subtype_of(const wasm_defined_type_t def_type1,
  288. const wasm_defined_type_t def_type2,
  289. const wasm_module_t module);
  290. /* ref type related operations */
  291. /**
  292. * Set the ref_type to be (ref null? type_idx)
  293. *
  294. * @param ref_type the ref_type to be set
  295. * @param nullable whether the ref_type is nullable
  296. * @param type_idx the type index
  297. */
  298. WASM_RUNTIME_API_EXTERN void
  299. wasm_ref_type_set_type_idx(wasm_ref_type_t *ref_type, bool nullable,
  300. int32_t type_idx);
  301. /**
  302. * Set the ref_type to be (ref null? func/extern/any/eq/i31/struct/array/..)
  303. *
  304. * @param ref_type the ref_type to be set
  305. * @param nullable whether the ref_type is nullable
  306. * @param heap_type the heap type
  307. */
  308. WASM_RUNTIME_API_EXTERN void
  309. wasm_ref_type_set_heap_type(wasm_ref_type_t *ref_type, bool nullable,
  310. int32_t heap_type);
  311. /**
  312. * Check whether two ref types are equal
  313. *
  314. * @param ref_type1 the specified ref type1
  315. * @param ref_type2 the specified ref type2
  316. * @param module current wasm module
  317. *
  318. * @return true if the ref type1 is equal to the ref type2,
  319. * false otherwise
  320. */
  321. WASM_RUNTIME_API_EXTERN bool
  322. wasm_ref_type_equal(const wasm_ref_type_t *ref_type1,
  323. const wasm_ref_type_t *ref_type2,
  324. const wasm_module_t module);
  325. /**
  326. * Check whether ref_type1 is subtype of ref_type2
  327. *
  328. * @param ref_type1 the specified ref type1
  329. * @param ref_type2 the specified ref type2
  330. * @param module current wasm module
  331. *
  332. * @return true if the ref type1 is subtype of the ref type2,
  333. * false otherwise
  334. */
  335. WASM_RUNTIME_API_EXTERN bool
  336. wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
  337. const wasm_ref_type_t *ref_type2,
  338. const wasm_module_t module);
  339. /* wasm object related operations */
  340. /**
  341. * Create a struct object with the index of defined type
  342. *
  343. * @param exec_env the execution environment
  344. * @param type_idx index of the struct type
  345. *
  346. * @return wasm_struct_obj_t if create success, NULL otherwise
  347. */
  348. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  349. wasm_struct_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);
  350. /**
  351. * Create a struct object with the struct type
  352. *
  353. * @param exec_env the execution environment
  354. * @param type defined struct type
  355. *
  356. * @return wasm_struct_obj_t if create success, NULL otherwise
  357. */
  358. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  359. wasm_struct_obj_new_with_type(wasm_exec_env_t exec_env,
  360. const wasm_struct_type_t type);
  361. /**
  362. * Set the field value of a struct object
  363. *
  364. * @param obj the struct object to set field
  365. * @param field_idx the specified field index
  366. * @param value wasm value to be set
  367. */
  368. WASM_RUNTIME_API_EXTERN void
  369. wasm_struct_obj_set_field(wasm_struct_obj_t obj, uint32_t field_idx,
  370. const wasm_value_t *value);
  371. /**
  372. * Get the field value of a struct object
  373. *
  374. * @param obj the struct object to get field
  375. * @param field_idx the specified field index
  376. * @param sign_extend whether to sign extend for i8 and i16 element types
  377. * @param value output the wasm value
  378. */
  379. WASM_RUNTIME_API_EXTERN void
  380. wasm_struct_obj_get_field(const wasm_struct_obj_t obj, uint32_t field_idx,
  381. bool sign_extend, wasm_value_t *value);
  382. /**
  383. * Create an array object with the index of defined type, the obj's length is
  384. * length, init value is init_value
  385. *
  386. * @param exec_env the execution environment
  387. * @param type_idx the index of the specified type
  388. * @param length the array's length
  389. * @param init_value the array's init value
  390. *
  391. * @return the created array object
  392. */
  393. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  394. wasm_array_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx,
  395. uint32_t length, wasm_value_t *init_value);
  396. /**
  397. * Create an array object with the array type, the obj's length is length, init
  398. * value is init_value
  399. *
  400. * @param exec_env the execution environment
  401. * @param type the array's specified type
  402. * @param length the array's length
  403. * @param init_value the array's init value
  404. *
  405. * @return the created array object
  406. */
  407. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  408. wasm_array_obj_new_with_type(wasm_exec_env_t exec_env,
  409. const wasm_array_type_t type, uint32_t length,
  410. wasm_value_t *init_value);
  411. /**
  412. * Set the specified element's value of an array object
  413. *
  414. * @param array_obj the array object to set element value
  415. * @param elem_idx the specified element index
  416. * @param value wasm value to be set
  417. */
  418. WASM_RUNTIME_API_EXTERN void
  419. wasm_array_obj_set_elem(wasm_array_obj_t array_obj, uint32_t elem_idx,
  420. const wasm_value_t *value);
  421. /**
  422. * Get the specified element's value of an array object
  423. *
  424. * @param array_obj the array object to get element value
  425. * @param elem_idx the specified element index
  426. * @param sign_extend whether to sign extend for i8 and i16 element types
  427. * @param value output the wasm value
  428. */
  429. WASM_RUNTIME_API_EXTERN void
  430. wasm_array_obj_get_elem(const wasm_array_obj_t array_obj, uint32_t elem_idx,
  431. bool sign_extend, wasm_value_t *value);
  432. /**
  433. * Copy elements from one array to another
  434. *
  435. * @param dst_obj destination array object
  436. * @param dst_idx target index in destination
  437. * @param src_obj source array object
  438. * @param src_idx start index in source
  439. * @param len length of elements to copy
  440. */
  441. WASM_RUNTIME_API_EXTERN void
  442. wasm_array_obj_copy(wasm_array_obj_t dst_obj, uint32_t dst_idx,
  443. const wasm_array_obj_t src_obj, uint32_t src_idx,
  444. uint32_t len);
  445. /**
  446. * Return the length of an array object
  447. *
  448. * @param array_obj the array object to get length
  449. *
  450. * @return length of the array object
  451. */
  452. WASM_RUNTIME_API_EXTERN uint32_t
  453. wasm_array_obj_length(const wasm_array_obj_t array_obj);
  454. /**
  455. * Get the address of the first element of an array object
  456. *
  457. * @param array_obj the array object to get element address
  458. *
  459. * @return address of the first element
  460. */
  461. WASM_RUNTIME_API_EXTERN void *
  462. wasm_array_obj_first_elem_addr(const wasm_array_obj_t array_obj);
  463. /**
  464. * Get the address of the i-th element of an array object
  465. *
  466. * @param array_obj the array object to get element address
  467. * @param elem_idx the specified element index
  468. *
  469. * @return address of the specified element
  470. */
  471. WASM_RUNTIME_API_EXTERN void *
  472. wasm_array_obj_elem_addr(const wasm_array_obj_t array_obj, uint32_t elem_idx);
  473. /**
  474. * Create a function object with the index of defined type and the index of the
  475. * function
  476. *
  477. * @param exec_env the execution environment
  478. * @param type_idx the index of the specified type
  479. * @param func_idx_bound the index of the function
  480. *
  481. * @return the created function object
  482. */
  483. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  484. wasm_func_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx,
  485. uint32_t func_idx_bound);
  486. /**
  487. * Create a function object with the function type and the index of the function
  488. *
  489. * @param exec_env the execution environment
  490. * @param type the specified type
  491. * @param func_idx_bound the index of the function
  492. *
  493. * @return the created function object
  494. */
  495. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  496. wasm_func_obj_new_with_type(wasm_exec_env_t exec_env, wasm_func_type_t type,
  497. uint32_t func_idx_bound);
  498. /**
  499. * Get the function index bound of a function object
  500. *
  501. * @param func_obj the function object
  502. *
  503. * @return the bound function index
  504. */
  505. WASM_RUNTIME_API_EXTERN uint32_t
  506. wasm_func_obj_get_func_idx_bound(const wasm_func_obj_t func_obj);
  507. /**
  508. * Get the function type of a function object
  509. *
  510. * @param func_obj the function object
  511. *
  512. * @return defined function type
  513. */
  514. WASM_RUNTIME_API_EXTERN wasm_func_type_t
  515. wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);
  516. /**
  517. * Call the given WASM function object with arguments (bytecode and AoT).
  518. *
  519. * @param exec_env the execution environment to call the function,
  520. * which must be created from wasm_create_exec_env()
  521. * @param func_obj the function object to call
  522. * @param argc total cell number that the function parameters occupy,
  523. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  524. * occupies one cell, i64/f64 argument occupies two cells, note that
  525. * it might be different from the parameter number of the function
  526. * @param argv the arguments. If the function has return value,
  527. * the first (or first two in case 64-bit return value) element of
  528. * argv stores the return value of the called WASM function after this
  529. * function returns.
  530. *
  531. * @return true if success, false otherwise and exception will be thrown,
  532. * the caller can call wasm_runtime_get_exception to get the exception
  533. * info.
  534. */
  535. WASM_RUNTIME_API_EXTERN bool
  536. wasm_runtime_call_func_ref(wasm_exec_env_t exec_env,
  537. const wasm_func_obj_t func_obj, uint32_t argc,
  538. uint32_t argv[]);
  539. /**
  540. * Call the given WASM function object with provided results space
  541. * and arguments (bytecode and AoT).
  542. *
  543. * @param exec_env the execution environment to call the function,
  544. * which must be created from wasm_create_exec_env()
  545. * @param func_obj the function object to call
  546. * @param num_results the number of results
  547. * @param results the pre-alloced pointer to get the results
  548. * @param num_args the number of arguments
  549. * @param args the arguments
  550. *
  551. * @return true if success, false otherwise and exception will be thrown,
  552. * the caller can call wasm_runtime_get_exception to get the exception
  553. * info.
  554. */
  555. WASM_RUNTIME_API_EXTERN bool
  556. wasm_runtime_call_func_ref_a(wasm_exec_env_t exec_env,
  557. const wasm_func_obj_t func_obj,
  558. uint32_t num_results, wasm_val_t results[],
  559. uint32_t num_args, wasm_val_t *args);
  560. /**
  561. * Call the given WASM function object with provided results space and
  562. * variant arguments (bytecode and AoT).
  563. *
  564. * @param exec_env the execution environment to call the function,
  565. * which must be created from wasm_create_exec_env()
  566. * @param func_obj the function object to call
  567. * @param num_results the number of results
  568. * @param results the pre-alloced pointer to get the results
  569. * @param num_args the number of arguments
  570. * @param ... the variant arguments
  571. *
  572. * @return true if success, false otherwise and exception will be thrown,
  573. * the caller can call wasm_runtime_get_exception to get the exception
  574. * info.
  575. */
  576. WASM_RUNTIME_API_EXTERN bool
  577. wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
  578. const wasm_func_obj_t func_obj,
  579. uint32_t num_results, wasm_val_t results[],
  580. uint32_t num_args, ...);
  581. /**
  582. * Create an externref object with host object
  583. *
  584. * @param exec_env the execution environment
  585. * @param host_obj host object pointer
  586. *
  587. * @return wasm_externref_obj_t if success, NULL otherwise
  588. */
  589. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  590. wasm_externref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  591. /**
  592. * Get the host value of an externref object
  593. *
  594. * @param externref_obj the externref object
  595. *
  596. * @return the stored host object pointer
  597. */
  598. WASM_RUNTIME_API_EXTERN const void *
  599. wasm_externref_obj_get_value(const wasm_externref_obj_t externref_obj);
  600. /**
  601. * Create an anyref object with host object
  602. *
  603. * @param exec_env the execution environment
  604. * @param host_obj host object pointer
  605. *
  606. * @return wasm_anyref_obj_t if success, NULL otherwise
  607. */
  608. WASM_RUNTIME_API_EXTERN wasm_anyref_obj_t
  609. wasm_anyref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  610. /**
  611. * Get the host object value of an anyref object
  612. *
  613. * @param anyref_obj the anyref object
  614. *
  615. * @return the stored host object pointer
  616. */
  617. WASM_RUNTIME_API_EXTERN const void *
  618. wasm_anyref_obj_get_value(const wasm_anyref_obj_t anyref_obj);
  619. /**
  620. * Get the internal object inside the externref object, same as
  621. * the operation of opcode extern.internalize
  622. *
  623. * @param externref_obj the externref object
  624. *
  625. * @return internalized wasm_obj_t
  626. */
  627. WASM_RUNTIME_API_EXTERN wasm_obj_t
  628. wasm_externref_obj_to_internal_obj(const wasm_externref_obj_t externref_obj);
  629. /**
  630. * Create an externref object from an internal object, same as
  631. * the operation of opcode extern.externalize
  632. *
  633. * @param exec_env the execution environment
  634. * @param internal_obj the internal object
  635. *
  636. * @return wasm_externref_obj_t if create success, NULL othersise
  637. */
  638. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  639. wasm_internal_obj_to_externref_obj(wasm_exec_env_t exec_env,
  640. const wasm_obj_t internal_obj);
  641. /**
  642. * Create an i31 object
  643. *
  644. * @param i31_value the scalar value
  645. *
  646. * @return wasm_i31_obj_t
  647. */
  648. WASM_RUNTIME_API_EXTERN wasm_i31_obj_t
  649. wasm_i31_obj_new(uint32_t i31_value);
  650. /**
  651. * Get value from an i31 object
  652. *
  653. * @param i31_obj the i31 object
  654. * @param sign_extend whether to sign extend the value
  655. *
  656. * @return wasm_i31_obj_t
  657. */
  658. WASM_RUNTIME_API_EXTERN uint32_t
  659. wasm_i31_obj_get_value(wasm_i31_obj_t i31_obj, bool sign_extend);
  660. /**
  661. * Pin an object to make it traced during GC
  662. *
  663. * @param exec_env the execution environment
  664. * @param obj the object to pin
  665. *
  666. * @return true if success, false otherwise
  667. */
  668. WASM_RUNTIME_API_EXTERN bool
  669. wasm_runtime_pin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  670. /**
  671. * Unpin an object
  672. *
  673. * @param exec_env the execution environment
  674. * @param obj the object to unpin
  675. *
  676. * @return true if success, false otherwise
  677. */
  678. WASM_RUNTIME_API_EXTERN bool
  679. wasm_runtime_unpin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  680. /**
  681. * Check whether an object is a struct objectc
  682. *
  683. * @param obj the object to check
  684. *
  685. * @return true if the object is a struct, false otherwise
  686. */
  687. WASM_RUNTIME_API_EXTERN bool
  688. wasm_obj_is_struct_obj(const wasm_obj_t obj);
  689. /**
  690. * Check whether an object is an array object
  691. *
  692. * @param obj the object to check
  693. *
  694. * @return true if the object is a array, false otherwise
  695. */
  696. WASM_RUNTIME_API_EXTERN bool
  697. wasm_obj_is_array_obj(const wasm_obj_t obj);
  698. /**
  699. * Check whether an object is a function object
  700. *
  701. * @param obj the object to check
  702. *
  703. * @return true if the object is a function, false otherwise
  704. */
  705. WASM_RUNTIME_API_EXTERN bool
  706. wasm_obj_is_func_obj(const wasm_obj_t obj);
  707. /**
  708. * Check whether an object is an i31 object
  709. *
  710. * @param obj the object to check
  711. *
  712. * @return true if the object is an i32, false otherwise
  713. */
  714. WASM_RUNTIME_API_EXTERN bool
  715. wasm_obj_is_i31_obj(const wasm_obj_t obj);
  716. /**
  717. * Check whether an object is an externref object
  718. *
  719. * @param obj the object to check
  720. *
  721. * @return true if the object is an externref, false otherwise
  722. */
  723. WASM_RUNTIME_API_EXTERN bool
  724. wasm_obj_is_externref_obj(const wasm_obj_t obj);
  725. /**
  726. * Check whether an object is an anyref object
  727. *
  728. * @param obj the object to check
  729. *
  730. * @return true if the object is an anyref, false otherwise
  731. */
  732. WASM_RUNTIME_API_EXTERN bool
  733. wasm_obj_is_anyref_obj(const wasm_obj_t obj);
  734. /**
  735. * Check whether an object is a struct object, or, an i31/struct/array object
  736. *
  737. * @param obj the object to check
  738. *
  739. * @return true if the object is an internal object, false otherwise
  740. */
  741. WASM_RUNTIME_API_EXTERN bool
  742. wasm_obj_is_internal_obj(const wasm_obj_t obj);
  743. /**
  744. * Check whether an object is an eq object
  745. *
  746. * @param obj the object to check
  747. *
  748. * @return true if the object is an eq object, false otherwise
  749. */
  750. WASM_RUNTIME_API_EXTERN bool
  751. wasm_obj_is_eq_obj(const wasm_obj_t obj);
  752. /**
  753. * Check whether an object is an instance of a defined type
  754. *
  755. * @param obj the object to check
  756. * @param defined_type the defined type
  757. * @param module current wasm module
  758. *
  759. * @return true if the object is instance of the defined type, false otherwise
  760. */
  761. WASM_RUNTIME_API_EXTERN bool
  762. wasm_obj_is_instance_of_defined_type(const wasm_obj_t obj,
  763. const wasm_defined_type_t defined_type,
  764. const wasm_module_t module);
  765. /**
  766. * Check whether an object is an instance of a defined type with
  767. * index type_idx
  768. *
  769. * @param obj the object to check
  770. * @param type_idx the type index
  771. * @param module current wasm module
  772. *
  773. * @return true if the object is instance of the defined type specified by
  774. * type_idx, false otherwise
  775. */
  776. WASM_RUNTIME_API_EXTERN bool
  777. wasm_obj_is_instance_of_type_idx(const wasm_obj_t obj, uint32_t type_idx,
  778. const wasm_module_t module);
  779. /**
  780. * Check whether an object is an instance of a ref type
  781. *
  782. * @param obj the object to check
  783. * @param ref_type the ref type
  784. *
  785. * @return true if the object is instance of the ref type, false otherwise
  786. */
  787. WASM_RUNTIME_API_EXTERN bool
  788. wasm_obj_is_instance_of_ref_type(const wasm_obj_t obj,
  789. const wasm_ref_type_t *ref_type);
  790. /**
  791. * Push a local object ref into stack, note that we should set its value
  792. * after pushing to retain it during GC, and should pop it from stack
  793. * before returning from the current function
  794. *
  795. * @param exec_env the execution environment
  796. * @param local_obj_ref the local object ref to push
  797. */
  798. WASM_RUNTIME_API_EXTERN void
  799. wasm_runtime_push_local_object_ref(wasm_exec_env_t exec_env,
  800. wasm_local_obj_ref_t *local_obj_ref);
  801. /**
  802. * Pop a local object ref from stack
  803. *
  804. * @param exec_env the execution environment
  805. *
  806. * @return the popped wasm_local_obj_ref_t
  807. */
  808. WASM_RUNTIME_API_EXTERN wasm_local_obj_ref_t *
  809. wasm_runtime_pop_local_object_ref(wasm_exec_env_t exec_env);
  810. /**
  811. * Pop n local object refs from stack
  812. *
  813. * @param exec_env the execution environment
  814. * @param n number to pop
  815. */
  816. WASM_RUNTIME_API_EXTERN void
  817. wasm_runtime_pop_local_object_refs(wasm_exec_env_t exec_env, uint32_t n);
  818. /**
  819. * Set finalizer to the given object, if another finalizer is set to the same
  820. * object, the previous one will be cancelled
  821. *
  822. * @param exec_env the execution environment
  823. * @param obj object to set finalizer
  824. * @param cb finalizer function to be called before this object is freed
  825. * @param data custom data to be passed to finalizer function
  826. *
  827. * @return true if success, false otherwise
  828. */
  829. bool
  830. wasm_obj_set_gc_finalizer(wasm_exec_env_t exec_env, const wasm_obj_t obj,
  831. wasm_obj_finalizer_t cb, void *data);
  832. /**
  833. * Unset finalizer to the given object
  834. *
  835. * @param exec_env the execution environment
  836. * @param obj object to unset finalizer
  837. */
  838. void
  839. wasm_obj_unset_gc_finalizer(wasm_exec_env_t exec_env, void *obj);
  840. #ifdef __cplusplus
  841. }
  842. #endif
  843. #endif /* end of _GC_EXPORT_H */