gc_export.h 28 KB

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