gc_export.h 28 KB

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