gc_export.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. /* Defined type related operations */
  121. /**
  122. * Get number of defined types in the given wasm module
  123. *
  124. * @param module the wasm module
  125. *
  126. * @return defined type count
  127. */
  128. WASM_RUNTIME_API_EXTERN uint32_t
  129. wasm_get_defined_type_count(const wasm_module_t module);
  130. /**
  131. * Get defined type by type index
  132. *
  133. * @param module the wasm module
  134. * @param index the type index
  135. *
  136. * @return defined type
  137. */
  138. WASM_RUNTIME_API_EXTERN wasm_defined_type_t
  139. wasm_get_defined_type(const wasm_module_t module, uint32_t index);
  140. /**
  141. * Get defined type of the GC managed object, the object must be struct,
  142. * array or func.
  143. *
  144. * @param obj the object
  145. *
  146. * @return defined type of the object.
  147. */
  148. WASM_RUNTIME_API_EXTERN wasm_defined_type_t
  149. wasm_obj_get_defined_type(const wasm_obj_t obj);
  150. /**
  151. * Get defined type index of the GC managed object, the object must be struct,
  152. * array or func.
  153. *
  154. * @param obj the object
  155. *
  156. * @return defined type index of the object.
  157. */
  158. WASM_RUNTIME_API_EXTERN int32_t
  159. wasm_obj_get_defined_type_idx(const wasm_module_t module, const wasm_obj_t obj);
  160. /**
  161. * Check whether a defined type is a function type
  162. *
  163. * @param def_type the defined type to be checked
  164. *
  165. * @return true if the defined type is function type, false otherwise
  166. */
  167. WASM_RUNTIME_API_EXTERN bool
  168. wasm_defined_type_is_func_type(const wasm_defined_type_t def_type);
  169. /**
  170. * Check whether a defined type is a struct type
  171. *
  172. * @param def_type the defined type to be checked
  173. *
  174. * @return true if the defined type is struct type, false otherwise
  175. */
  176. WASM_RUNTIME_API_EXTERN bool
  177. wasm_defined_type_is_struct_type(const wasm_defined_type_t def_type);
  178. /**
  179. * Check whether a defined type is an array type
  180. *
  181. * @param def_type the defined type to be checked
  182. *
  183. * @return true if the defined type is array type, false otherwise
  184. */
  185. WASM_RUNTIME_API_EXTERN bool
  186. wasm_defined_type_is_array_type(const wasm_defined_type_t def_type);
  187. /**
  188. * Get parameter count of a function type
  189. *
  190. * @param func_type the specified function type
  191. *
  192. * @return the param count of the specified function type
  193. */
  194. WASM_RUNTIME_API_EXTERN uint32_t
  195. wasm_func_type_get_param_count(const wasm_func_type_t func_type);
  196. /**
  197. * Get type of a specified parameter of a function type
  198. *
  199. * @param func_type the specified function type
  200. * @param param_idx the specified param index
  201. *
  202. * @return the param type at the specified param index of the specified func
  203. * type
  204. */
  205. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  206. wasm_func_type_get_param_type(const wasm_func_type_t func_type,
  207. uint32_t param_idx);
  208. /**
  209. * Get result count of a function type
  210. *
  211. * @param func_type the specified function type
  212. *
  213. * @return the result count of the specified function type
  214. */
  215. WASM_RUNTIME_API_EXTERN uint32_t
  216. wasm_func_type_get_result_count(const wasm_func_type_t func_type);
  217. /**
  218. * Get type of a specified result of a function type
  219. *
  220. * @param func_type the specified function type
  221. * @param param_idx the specified result index
  222. *
  223. * @return the result type at the specified result index of the specified func
  224. * type
  225. */
  226. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  227. wasm_func_type_get_result_type(const wasm_func_type_t func_type,
  228. uint32_t result_idx);
  229. /**
  230. * Get field count of a struct type
  231. *
  232. * @param struct_type the specified struct type
  233. *
  234. * @return the field count of the specified struct type
  235. */
  236. WASM_RUNTIME_API_EXTERN uint32_t
  237. wasm_struct_type_get_field_count(const wasm_struct_type_t struct_type);
  238. /**
  239. * Get type of a specified field of a struct type
  240. *
  241. * @param struct_type the specified struct type
  242. * @param field_idx index of the specified field
  243. * @param p_is_mutable if not NULL, output the mutability of the field
  244. *
  245. * @return the result type at the specified field index of the specified struct
  246. */
  247. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  248. wasm_struct_type_get_field_type(const wasm_struct_type_t struct_type,
  249. uint32_t field_idx, bool *p_is_mutable);
  250. /**
  251. * Get element type of an array type
  252. *
  253. * @param array_type the specified array type
  254. * @param p_is_mutable if not NULL, output the mutability of the element type
  255. *
  256. * @return the ref type of array's elem type
  257. */
  258. WASM_RUNTIME_API_EXTERN wasm_ref_type_t
  259. wasm_array_type_get_elem_type(const wasm_array_type_t array_type,
  260. bool *p_is_mutable);
  261. /**
  262. * Check whether two defined types are equal
  263. *
  264. * @param def_type1 the specified defined type1
  265. * @param def_type2 the specified defined type2
  266. * @param module current wasm module
  267. *
  268. * @return true if the defined type1 is equal to the defined type2,
  269. * false otherwise
  270. */
  271. WASM_RUNTIME_API_EXTERN bool
  272. wasm_defined_type_equal(const wasm_defined_type_t def_type1,
  273. const wasm_defined_type_t def_type2,
  274. const wasm_module_t module);
  275. /**
  276. * Check whether def_type1 is subtype of def_type2
  277. *
  278. * @param def_type1 the specified defined type1
  279. * @param def_type2 the specified defined type2
  280. * @param module current wasm module
  281. *
  282. * @return true if the defined type1 is subtype of the defined type2,
  283. * false otherwise
  284. */
  285. WASM_RUNTIME_API_EXTERN bool
  286. wasm_defined_type_is_subtype_of(const wasm_defined_type_t def_type1,
  287. const wasm_defined_type_t def_type2,
  288. const wasm_module_t module);
  289. /* ref type related operations */
  290. /**
  291. * Set the ref_type to be (ref null? type_idx)
  292. *
  293. * @param ref_type the ref_type to be set
  294. * @param nullable whether the ref_type is nullable
  295. * @param type_idx the type index
  296. */
  297. WASM_RUNTIME_API_EXTERN void
  298. wasm_ref_type_set_type_idx(wasm_ref_type_t *ref_type, bool nullable,
  299. int32_t type_idx);
  300. /**
  301. * Set the ref_type to be (ref null? func/extern/any/eq/i31/struct/array/..)
  302. *
  303. * @param ref_type the ref_type to be set
  304. * @param nullable whether the ref_type is nullable
  305. * @param heap_type the heap type
  306. */
  307. WASM_RUNTIME_API_EXTERN void
  308. wasm_ref_type_set_heap_type(wasm_ref_type_t *ref_type, bool nullable,
  309. int32_t heap_type);
  310. /**
  311. * Check whether two ref types are equal
  312. *
  313. * @param ref_type1 the specified ref type1
  314. * @param ref_type2 the specified ref type2
  315. * @param module current wasm module
  316. *
  317. * @return true if the ref type1 is equal to the ref type2,
  318. * false otherwise
  319. */
  320. WASM_RUNTIME_API_EXTERN bool
  321. wasm_ref_type_equal(const wasm_ref_type_t *ref_type1,
  322. const wasm_ref_type_t *ref_type2,
  323. const wasm_module_t module);
  324. /**
  325. * Check whether ref_type1 is subtype of ref_type2
  326. *
  327. * @param ref_type1 the specified ref type1
  328. * @param ref_type2 the specified ref type2
  329. * @param module current wasm module
  330. *
  331. * @return true if the ref type1 is subtype of the ref type2,
  332. * false otherwise
  333. */
  334. WASM_RUNTIME_API_EXTERN bool
  335. wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
  336. const wasm_ref_type_t *ref_type2,
  337. const wasm_module_t module);
  338. /* wasm object related operations */
  339. /**
  340. * Create a struct object with the index of defined type
  341. *
  342. * @param exec_env the execution environment
  343. * @param type_idx index of the struct type
  344. *
  345. * @return wasm_struct_obj_t if create success, NULL otherwise
  346. */
  347. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  348. wasm_struct_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);
  349. /**
  350. * Create a struct object with the struct type
  351. *
  352. * @param exec_env the execution environment
  353. * @param type defined struct type
  354. *
  355. * @return wasm_struct_obj_t if create success, NULL otherwise
  356. */
  357. WASM_RUNTIME_API_EXTERN wasm_struct_obj_t
  358. wasm_struct_obj_new_with_type(wasm_exec_env_t exec_env,
  359. const wasm_struct_type_t type);
  360. /**
  361. * Set the field value of a struct object
  362. *
  363. * @param obj the struct object to set field
  364. * @param field_idx the specified field index
  365. * @param value wasm value to be set
  366. */
  367. WASM_RUNTIME_API_EXTERN void
  368. wasm_struct_obj_set_field(wasm_struct_obj_t obj, uint32_t field_idx,
  369. const wasm_value_t *value);
  370. /**
  371. * Get the field value of a struct object
  372. *
  373. * @param obj the struct object to get field
  374. * @param field_idx the specified field index
  375. * @param sign_extend whether to sign extend for i8 and i16 element types
  376. * @param value output the wasm value
  377. */
  378. WASM_RUNTIME_API_EXTERN void
  379. wasm_struct_obj_get_field(const wasm_struct_obj_t obj, uint32_t field_idx,
  380. bool sign_extend, wasm_value_t *value);
  381. /**
  382. * Create an array object with the index of defined type, the obj's length is
  383. * length, init value is init_value
  384. *
  385. * @param exec_env the execution environment
  386. * @param type_idx the index of the specified type
  387. * @param length the array's length
  388. * @param init_value the array's init value
  389. *
  390. * @return the created array object
  391. */
  392. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  393. wasm_array_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx,
  394. uint32_t length, wasm_value_t *init_value);
  395. /**
  396. * Create an array object with the array type, the obj's length is length, init
  397. * value is init_value
  398. *
  399. * @param exec_env the execution environment
  400. * @param type the array's specified type
  401. * @param length the array's length
  402. * @param init_value the array's init value
  403. *
  404. * @return the created array object
  405. */
  406. WASM_RUNTIME_API_EXTERN wasm_array_obj_t
  407. wasm_array_obj_new_with_type(wasm_exec_env_t exec_env,
  408. const wasm_array_type_t type, uint32_t length,
  409. wasm_value_t *init_value);
  410. /**
  411. * Set the specified element's value of an array object
  412. *
  413. * @param array_obj the array object to set element value
  414. * @param elem_idx the specified element index
  415. * @param value wasm value to be set
  416. */
  417. WASM_RUNTIME_API_EXTERN void
  418. wasm_array_obj_set_elem(wasm_array_obj_t array_obj, uint32_t elem_idx,
  419. const wasm_value_t *value);
  420. /**
  421. * Get the specified element's value of an array object
  422. *
  423. * @param array_obj the array object to get element value
  424. * @param elem_idx the specified element index
  425. * @param sign_extend whether to sign extend for i8 and i16 element types
  426. * @param value output the wasm value
  427. */
  428. WASM_RUNTIME_API_EXTERN void
  429. wasm_array_obj_get_elem(const wasm_array_obj_t array_obj, uint32_t elem_idx,
  430. bool sign_extend, wasm_value_t *value);
  431. /**
  432. * Copy elements from one array to another
  433. *
  434. * @param dst_obj destination array object
  435. * @param dst_idx target index in destination
  436. * @param src_obj source array object
  437. * @param src_idx start index in source
  438. * @param len length of elements to copy
  439. */
  440. WASM_RUNTIME_API_EXTERN void
  441. wasm_array_obj_copy(wasm_array_obj_t dst_obj, uint32_t dst_idx,
  442. const wasm_array_obj_t src_obj, uint32_t src_idx,
  443. uint32_t len);
  444. /**
  445. * Return the length of an array object
  446. *
  447. * @param array_obj the array object to get length
  448. *
  449. * @return length of the array object
  450. */
  451. WASM_RUNTIME_API_EXTERN uint32_t
  452. wasm_array_obj_length(const wasm_array_obj_t array_obj);
  453. /**
  454. * Get the address of the first element of an array object
  455. *
  456. * @param array_obj the array object to get element address
  457. *
  458. * @return address of the first element
  459. */
  460. WASM_RUNTIME_API_EXTERN void *
  461. wasm_array_obj_first_elem_addr(const wasm_array_obj_t array_obj);
  462. /**
  463. * Get the address of the i-th element of an array object
  464. *
  465. * @param array_obj the array object to get element address
  466. * @param elem_idx the specified element index
  467. *
  468. * @return address of the specified element
  469. */
  470. WASM_RUNTIME_API_EXTERN void *
  471. wasm_array_obj_elem_addr(const wasm_array_obj_t array_obj, uint32_t elem_idx);
  472. /**
  473. * Create a function object with the index of defined type and the index of the
  474. * function
  475. *
  476. * @param exec_env the execution environment
  477. * @param type_idx the index of the specified type
  478. * @param func_idx_bound the index of the function
  479. *
  480. * @return the created function object
  481. */
  482. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  483. wasm_func_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx,
  484. uint32_t func_idx_bound);
  485. /**
  486. * Create a function object with the function type and the index of the function
  487. *
  488. * @param exec_env the execution environment
  489. * @param type the specified type
  490. * @param func_idx_bound the index of the function
  491. *
  492. * @return the created function object
  493. */
  494. WASM_RUNTIME_API_EXTERN wasm_func_obj_t
  495. wasm_func_obj_new_with_type(wasm_exec_env_t exec_env, wasm_func_type_t type,
  496. uint32_t func_idx_bound);
  497. /**
  498. * Get the function index bound of a function object
  499. *
  500. * @param func_obj the function object
  501. *
  502. * @return the bound function index
  503. */
  504. WASM_RUNTIME_API_EXTERN uint32_t
  505. wasm_func_obj_get_func_idx_bound(const wasm_func_obj_t func_obj);
  506. /**
  507. * Get the function type of a function object
  508. *
  509. * @param func_obj the function object
  510. *
  511. * @return defined function type
  512. */
  513. WASM_RUNTIME_API_EXTERN wasm_func_type_t
  514. wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);
  515. /**
  516. * Call the given WASM function object with arguments (bytecode and AoT).
  517. *
  518. * @param exec_env the execution environment to call the function,
  519. * which must be created from wasm_create_exec_env()
  520. * @param func_obj the function object to call
  521. * @param argc total cell number that the function parameters occupy,
  522. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  523. * occupies one cell, i64/f64 argument occupies two cells, note that
  524. * it might be different from the parameter number of the function
  525. * @param argv the arguments. If the function has return value,
  526. * the first (or first two in case 64-bit return value) element of
  527. * argv stores the return value of the called WASM function after this
  528. * function returns.
  529. *
  530. * @return true if success, false otherwise and exception will be thrown,
  531. * the caller can call wasm_runtime_get_exception to get the exception
  532. * info.
  533. */
  534. WASM_RUNTIME_API_EXTERN bool
  535. wasm_runtime_call_func_ref(wasm_exec_env_t exec_env,
  536. const wasm_func_obj_t func_obj, uint32_t argc,
  537. uint32_t argv[]);
  538. /**
  539. * Call the given WASM function object with provided results space
  540. * and arguments (bytecode and AoT).
  541. *
  542. * @param exec_env the execution environment to call the function,
  543. * which must be created from wasm_create_exec_env()
  544. * @param func_obj the function object to call
  545. * @param num_results the number of results
  546. * @param results the pre-alloced pointer to get the results
  547. * @param num_args the number of arguments
  548. * @param args the arguments
  549. *
  550. * @return true if success, false otherwise and exception will be thrown,
  551. * the caller can call wasm_runtime_get_exception to get the exception
  552. * info.
  553. */
  554. WASM_RUNTIME_API_EXTERN bool
  555. wasm_runtime_call_func_ref_a(wasm_exec_env_t exec_env,
  556. const wasm_func_obj_t func_obj,
  557. uint32_t num_results, wasm_val_t results[],
  558. uint32_t num_args, wasm_val_t *args);
  559. /**
  560. * Call the given WASM function object with provided results space and
  561. * variant arguments (bytecode and AoT).
  562. *
  563. * @param exec_env the execution environment to call the function,
  564. * which must be created from wasm_create_exec_env()
  565. * @param func_obj the function object to call
  566. * @param num_results the number of results
  567. * @param results the pre-alloced pointer to get the results
  568. * @param num_args the number of arguments
  569. * @param ... the variant arguments
  570. *
  571. * @return true if success, false otherwise and exception will be thrown,
  572. * the caller can call wasm_runtime_get_exception to get the exception
  573. * info.
  574. */
  575. WASM_RUNTIME_API_EXTERN bool
  576. wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
  577. const wasm_func_obj_t func_obj,
  578. uint32_t num_results, wasm_val_t results[],
  579. uint32_t num_args, ...);
  580. /**
  581. * Create an externref object with host object
  582. *
  583. * @param exec_env the execution environment
  584. * @param host_obj host object pointer
  585. *
  586. * @return wasm_externref_obj_t if success, NULL otherwise
  587. */
  588. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  589. wasm_externref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  590. /**
  591. * Get the host value of an externref object
  592. *
  593. * @param externref_obj the externref object
  594. *
  595. * @return the stored host object pointer
  596. */
  597. WASM_RUNTIME_API_EXTERN const void *
  598. wasm_externref_obj_get_value(const wasm_externref_obj_t externref_obj);
  599. /**
  600. * Create an anyref object with host object
  601. *
  602. * @param exec_env the execution environment
  603. * @param host_obj host object pointer
  604. *
  605. * @return wasm_anyref_obj_t if success, NULL otherwise
  606. */
  607. WASM_RUNTIME_API_EXTERN wasm_anyref_obj_t
  608. wasm_anyref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);
  609. /**
  610. * Get the host object value of an anyref object
  611. *
  612. * @param anyref_obj the anyref object
  613. *
  614. * @return the stored host object pointer
  615. */
  616. WASM_RUNTIME_API_EXTERN const void *
  617. wasm_anyref_obj_get_value(const wasm_anyref_obj_t anyref_obj);
  618. /**
  619. * Get the internal object inside the externref object, same as
  620. * the operation of opcode extern.internalize
  621. *
  622. * @param externref_obj the externref object
  623. *
  624. * @return internalized wasm_obj_t
  625. */
  626. WASM_RUNTIME_API_EXTERN wasm_obj_t
  627. wasm_externref_obj_to_internal_obj(const wasm_externref_obj_t externref_obj);
  628. /**
  629. * Create an externref object from an internal object, same as
  630. * the operation of opcode extern.externalize
  631. *
  632. * @param exec_env the execution environment
  633. * @param internal_obj the internal object
  634. *
  635. * @return wasm_externref_obj_t if create success, NULL othersise
  636. */
  637. WASM_RUNTIME_API_EXTERN wasm_externref_obj_t
  638. wasm_internal_obj_to_externref_obj(wasm_exec_env_t exec_env,
  639. const wasm_obj_t internal_obj);
  640. /**
  641. * Create an i31 object
  642. *
  643. * @param i31_value the scalar value
  644. *
  645. * @return wasm_i31_obj_t
  646. */
  647. WASM_RUNTIME_API_EXTERN wasm_i31_obj_t
  648. wasm_i31_obj_new(uint32_t i31_value);
  649. /**
  650. * Get value from an i31 object
  651. *
  652. * @param i31_obj the i31 object
  653. * @param sign_extend whether to sign extend the value
  654. *
  655. * @return wasm_i31_obj_t
  656. */
  657. WASM_RUNTIME_API_EXTERN uint32_t
  658. wasm_i31_obj_get_value(wasm_i31_obj_t i31_obj, bool sign_extend);
  659. /**
  660. * Pin an object to make it traced during GC
  661. *
  662. * @param exec_env the execution environment
  663. * @param obj the object to pin
  664. *
  665. * @return true if success, false otherwise
  666. */
  667. WASM_RUNTIME_API_EXTERN bool
  668. wasm_runtime_pin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  669. /**
  670. * Unpin an object
  671. *
  672. * @param exec_env the execution environment
  673. * @param obj the object to unpin
  674. *
  675. * @return true if success, false otherwise
  676. */
  677. WASM_RUNTIME_API_EXTERN bool
  678. wasm_runtime_unpin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);
  679. /**
  680. * Check whether an object is a struct objectc
  681. *
  682. * @param obj the object to check
  683. *
  684. * @return true if the object is a struct, false otherwise
  685. */
  686. WASM_RUNTIME_API_EXTERN bool
  687. wasm_obj_is_struct_obj(const wasm_obj_t obj);
  688. /**
  689. * Check whether an object is an array object
  690. *
  691. * @param obj the object to check
  692. *
  693. * @return true if the object is a array, false otherwise
  694. */
  695. WASM_RUNTIME_API_EXTERN bool
  696. wasm_obj_is_array_obj(const wasm_obj_t obj);
  697. /**
  698. * Check whether an object is a function object
  699. *
  700. * @param obj the object to check
  701. *
  702. * @return true if the object is a function, false otherwise
  703. */
  704. WASM_RUNTIME_API_EXTERN bool
  705. wasm_obj_is_func_obj(const wasm_obj_t obj);
  706. /**
  707. * Check whether an object is an i31 object
  708. *
  709. * @param obj the object to check
  710. *
  711. * @return true if the object is an i32, false otherwise
  712. */
  713. WASM_RUNTIME_API_EXTERN bool
  714. wasm_obj_is_i31_obj(const wasm_obj_t obj);
  715. /**
  716. * Check whether an object is an externref object
  717. *
  718. * @param obj the object to check
  719. *
  720. * @return true if the object is an externref, false otherwise
  721. */
  722. WASM_RUNTIME_API_EXTERN bool
  723. wasm_obj_is_externref_obj(const wasm_obj_t obj);
  724. /**
  725. * Check whether an object is an anyref object
  726. *
  727. * @param obj the object to check
  728. *
  729. * @return true if the object is an anyref, false otherwise
  730. */
  731. WASM_RUNTIME_API_EXTERN bool
  732. wasm_obj_is_anyref_obj(const wasm_obj_t obj);
  733. /**
  734. * Check whether an object is a struct object, or, an i31/struct/array object
  735. *
  736. * @param obj the object to check
  737. *
  738. * @return true if the object is an internal object, false otherwise
  739. */
  740. WASM_RUNTIME_API_EXTERN bool
  741. wasm_obj_is_internal_obj(const wasm_obj_t obj);
  742. /**
  743. * Check whether an object is an eq object
  744. *
  745. * @param obj the object to check
  746. *
  747. * @return true if the object is an eq object, false otherwise
  748. */
  749. WASM_RUNTIME_API_EXTERN bool
  750. wasm_obj_is_eq_obj(const wasm_obj_t obj);
  751. /**
  752. * Check whether an object is an instance of a defined type
  753. *
  754. * @param obj the object to check
  755. * @param defined_type the defined type
  756. * @param module current wasm module
  757. *
  758. * @return true if the object is instance of the defined type, false otherwise
  759. */
  760. WASM_RUNTIME_API_EXTERN bool
  761. wasm_obj_is_instance_of_defined_type(const wasm_obj_t obj,
  762. const wasm_defined_type_t defined_type,
  763. const wasm_module_t module);
  764. /**
  765. * Check whether an object is an instance of a defined type with
  766. * index type_idx
  767. *
  768. * @param obj the object to check
  769. * @param type_idx the type index
  770. * @param module current wasm module
  771. *
  772. * @return true if the object is instance of the defined type specified by
  773. * type_idx, false otherwise
  774. */
  775. WASM_RUNTIME_API_EXTERN bool
  776. wasm_obj_is_instance_of_type_idx(const wasm_obj_t obj, uint32_t type_idx,
  777. const wasm_module_t module);
  778. /**
  779. * Check whether an object is an instance of a ref type
  780. *
  781. * @param obj the object to check
  782. * @param ref_type the ref type
  783. *
  784. * @return true if the object is instance of the ref type, false otherwise
  785. */
  786. WASM_RUNTIME_API_EXTERN bool
  787. wasm_obj_is_instance_of_ref_type(const wasm_obj_t obj,
  788. const wasm_ref_type_t *ref_type);
  789. /**
  790. * Push a local object ref into stack, note that we should set its value
  791. * after pushing to retain it during GC, and should pop it from stack
  792. * before returning from the current function
  793. *
  794. * @param exec_env the execution environment
  795. * @param local_obj_ref the local object ref to push
  796. */
  797. WASM_RUNTIME_API_EXTERN void
  798. wasm_runtime_push_local_object_ref(wasm_exec_env_t exec_env,
  799. wasm_local_obj_ref_t *local_obj_ref);
  800. /**
  801. * Pop a local object ref from stack
  802. *
  803. * @param exec_env the execution environment
  804. *
  805. * @return the popped wasm_local_obj_ref_t
  806. */
  807. WASM_RUNTIME_API_EXTERN wasm_local_obj_ref_t *
  808. wasm_runtime_pop_local_object_ref(wasm_exec_env_t exec_env);
  809. /**
  810. * Pop n local object refs from stack
  811. *
  812. * @param exec_env the execution environment
  813. * @param n number to pop
  814. */
  815. WASM_RUNTIME_API_EXTERN void
  816. wasm_runtime_pop_local_object_refs(wasm_exec_env_t exec_env, uint32_t n);
  817. #ifdef __cplusplus
  818. }
  819. #endif
  820. #endif /* end of _GC_EXPORT_H */