gc_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "../wasm_runtime_common.h"
  6. #include "gc_export.h"
  7. #if WASM_ENABLE_INTERP != 0
  8. #include "../interpreter/wasm_runtime.h"
  9. #endif
  10. #if WASM_ENABLE_AOT != 0
  11. #include "../aot/aot_runtime.h"
  12. #endif
  13. static bool
  14. wasm_ref_type_normalize(wasm_ref_type_t *ref_type)
  15. {
  16. wasm_value_type_t value_type = ref_type->value_type;
  17. int32 heap_type = ref_type->heap_type;
  18. if (!((value_type >= VALUE_TYPE_I16 && value_type <= VALUE_TYPE_I32)
  19. || ((value_type >= (uint8)REF_TYPE_ARRAYREF
  20. && value_type <= (uint8)REF_TYPE_NULLFUNCREF)
  21. || (value_type >= (uint8)REF_TYPE_HT_NULLABLE
  22. && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE)
  23. #if WASM_ENABLE_STRINGREF != 0
  24. || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8
  25. && value_type <= (uint8)REF_TYPE_STRINGREF)
  26. || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER
  27. && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16)
  28. #endif
  29. ))) {
  30. return false;
  31. }
  32. if (value_type == VALUE_TYPE_HT_NULLABLE_REF
  33. || value_type == VALUE_TYPE_HT_NON_NULLABLE_REF) {
  34. if (heap_type < 0 && !wasm_is_valid_heap_type(heap_type)) {
  35. return false;
  36. }
  37. }
  38. if (value_type != REF_TYPE_HT_NULLABLE) {
  39. ref_type->nullable = false;
  40. }
  41. else {
  42. if (wasm_is_valid_heap_type(heap_type)) {
  43. ref_type->value_type =
  44. #if WASM_ENABLE_STRINGREF != 0
  45. (uint8)(REF_TYPE_STRINGVIEWITER + heap_type
  46. - HEAP_TYPE_STRINGVIEWITER);
  47. #else
  48. (uint8)(REF_TYPE_ARRAYREF + heap_type - HEAP_TYPE_ARRAY);
  49. #endif
  50. ref_type->nullable = false;
  51. ref_type->heap_type = 0;
  52. }
  53. else {
  54. ref_type->nullable = true;
  55. }
  56. }
  57. return true;
  58. }
  59. uint32
  60. wasm_get_defined_type_count(WASMModuleCommon *const module)
  61. {
  62. uint32 type_count = 0;
  63. #if WASM_ENABLE_INTERP != 0
  64. if (module->module_type == Wasm_Module_Bytecode) {
  65. WASMModule *wasm_module = (WASMModule *)module;
  66. type_count = wasm_module->type_count;
  67. }
  68. #endif
  69. #if WASM_ENABLE_AOT != 0
  70. if (module->module_type == Wasm_Module_AoT) {
  71. AOTModule *aot_module = (AOTModule *)module;
  72. type_count = aot_module->type_count;
  73. }
  74. #endif
  75. return type_count;
  76. }
  77. WASMType *
  78. wasm_get_defined_type(WASMModuleCommon *const module, uint32 index)
  79. {
  80. WASMType *type = NULL;
  81. #if WASM_ENABLE_INTERP != 0
  82. if (module->module_type == Wasm_Module_Bytecode) {
  83. WASMModule *wasm_module = (WASMModule *)module;
  84. bh_assert(index < wasm_module->type_count);
  85. type = wasm_module->types[index];
  86. }
  87. #endif
  88. #if WASM_ENABLE_AOT != 0
  89. if (module->module_type == Wasm_Module_AoT) {
  90. AOTModule *aot_module = (AOTModule *)module;
  91. bh_assert(index < aot_module->type_count);
  92. type = aot_module->types[index];
  93. }
  94. #endif
  95. return type;
  96. }
  97. WASMType *
  98. wasm_obj_get_defined_type(const WASMObjectRef obj)
  99. {
  100. if ((!wasm_obj_is_struct_obj(obj)) && (!wasm_obj_is_array_obj(obj))
  101. && (!wasm_obj_is_func_obj(obj))) {
  102. bh_assert(false);
  103. }
  104. return ((WASMRttTypeRef)(obj->header))->defined_type;
  105. }
  106. int32
  107. wasm_obj_get_defined_type_idx(WASMModuleCommon *const module,
  108. const WASMObjectRef obj)
  109. {
  110. WASMType *type = wasm_obj_get_defined_type(obj);
  111. uint32 i, type_idx = (uint32)-1;
  112. #if WASM_ENABLE_INTERP != 0
  113. if (module->module_type == Wasm_Module_Bytecode) {
  114. WASMModule *wasm_module = (WASMModule *)module;
  115. uint32 type_count = wasm_module->type_count;
  116. for (i = 0; i < type_count; i++) {
  117. if (wasm_module->types[i] == type) {
  118. type_idx = i;
  119. break;
  120. }
  121. }
  122. bh_assert(type_idx < type_count);
  123. }
  124. #endif
  125. #if WASM_ENABLE_AOT != 0
  126. if (module->module_type == Wasm_Module_AoT) {
  127. AOTModule *aot_module = (AOTModule *)module;
  128. uint32 type_count = aot_module->type_count;
  129. for (i = 0; i < type_count; i++) {
  130. if (aot_module->types[i] == type) {
  131. type_idx = i;
  132. break;
  133. }
  134. }
  135. bh_assert(type_idx < type_count);
  136. }
  137. #endif
  138. return type_idx;
  139. }
  140. bool
  141. wasm_defined_type_is_func_type(WASMType *const def_type)
  142. {
  143. return wasm_type_is_func_type(def_type);
  144. }
  145. bool
  146. wasm_defined_type_is_struct_type(WASMType *const def_type)
  147. {
  148. return wasm_type_is_struct_type(def_type);
  149. }
  150. bool
  151. wasm_defined_type_is_array_type(WASMType *const def_type)
  152. {
  153. return wasm_type_is_array_type(def_type);
  154. }
  155. wasm_ref_type_t
  156. wasm_func_type_get_param_type(WASMFuncType *const func_type, uint32 param_idx)
  157. {
  158. wasm_ref_type_t ref_type = { 0 };
  159. bh_assert(param_idx < func_type->param_count);
  160. ref_type.value_type = func_type->types[param_idx];
  161. if (wasm_is_type_multi_byte_type(func_type->types[param_idx])) {
  162. WASMRefType *param_ref_type = wasm_reftype_map_find(
  163. func_type->ref_type_maps, func_type->ref_type_map_count, param_idx);
  164. bh_assert(param_ref_type);
  165. ref_type.nullable = param_ref_type->ref_ht_common.nullable;
  166. ref_type.heap_type = param_ref_type->ref_ht_common.heap_type;
  167. }
  168. return ref_type;
  169. }
  170. wasm_ref_type_t
  171. wasm_func_type_get_result_type(WASMFuncType *const func_type, uint32 result_idx)
  172. {
  173. wasm_ref_type_t ref_type = { 0 };
  174. uint32 result_idx_with_param;
  175. result_idx_with_param = func_type->param_count + result_idx;
  176. bh_assert(result_idx < func_type->result_count);
  177. ref_type.value_type = func_type->types[result_idx_with_param];
  178. if (wasm_is_type_multi_byte_type(func_type->types[result_idx_with_param])) {
  179. WASMRefType *result_ref_type = wasm_reftype_map_find(
  180. func_type->ref_type_maps, func_type->ref_type_map_count,
  181. result_idx_with_param);
  182. bh_assert(result_ref_type);
  183. ref_type.nullable = result_ref_type->ref_ht_common.nullable;
  184. ref_type.heap_type = result_ref_type->ref_ht_common.heap_type;
  185. }
  186. return ref_type;
  187. }
  188. uint32
  189. wasm_struct_type_get_field_count(WASMStructType *const struct_type)
  190. {
  191. bh_assert(struct_type->base_type.type_flag == WASM_TYPE_STRUCT);
  192. return struct_type->field_count;
  193. }
  194. wasm_ref_type_t
  195. wasm_struct_type_get_field_type(WASMStructType *const struct_type,
  196. uint32 field_idx, bool *p_is_mutable)
  197. {
  198. wasm_ref_type_t ref_type = { 0 };
  199. WASMStructFieldType field;
  200. bh_assert(struct_type->base_type.type_flag == WASM_TYPE_STRUCT);
  201. bh_assert(field_idx < struct_type->field_count);
  202. field = struct_type->fields[field_idx];
  203. ref_type.value_type = field.field_type;
  204. if (wasm_is_type_multi_byte_type(field.field_type)) {
  205. WASMRefType *field_ref_type =
  206. wasm_reftype_map_find(struct_type->ref_type_maps,
  207. struct_type->ref_type_map_count, field_idx);
  208. bh_assert(field_ref_type);
  209. ref_type.nullable = field_ref_type->ref_ht_common.nullable;
  210. ref_type.heap_type = field_ref_type->ref_ht_common.heap_type;
  211. }
  212. if (p_is_mutable) {
  213. *p_is_mutable = field.field_flags & 1;
  214. }
  215. return ref_type;
  216. }
  217. wasm_ref_type_t
  218. wasm_array_type_get_elem_type(WASMArrayType *const array_type,
  219. bool *p_is_mutable)
  220. {
  221. wasm_ref_type_t ref_type = { 0 };
  222. ref_type.value_type = array_type->elem_type;
  223. if (wasm_is_type_multi_byte_type(array_type->elem_type)) {
  224. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  225. ref_type.nullable = elem_ref_type->ref_ht_common.nullable;
  226. ref_type.heap_type = elem_ref_type->ref_ht_common.heap_type;
  227. }
  228. if (p_is_mutable) {
  229. *p_is_mutable = array_type->elem_flags & 1;
  230. }
  231. return ref_type;
  232. }
  233. bool
  234. wasm_defined_type_equal(WASMType *const def_type1, WASMType *const def_type2,
  235. WASMModuleCommon *const module)
  236. {
  237. WASMTypePtr *types = NULL;
  238. uint32 type_count = 0;
  239. #if WASM_ENABLE_INTERP != 0
  240. if (module->module_type == Wasm_Module_Bytecode) {
  241. WASMModule *wasm_module = (WASMModule *)module;
  242. types = wasm_module->types;
  243. type_count = wasm_module->type_count;
  244. }
  245. #endif
  246. #if WASM_ENABLE_AOT != 0
  247. if (module->module_type == Wasm_Module_AoT) {
  248. AOTModule *aot_module = (AOTModule *)module;
  249. types = aot_module->types;
  250. type_count = aot_module->type_count;
  251. }
  252. #endif
  253. bh_assert(types);
  254. return wasm_type_equal(def_type1, def_type2, types, type_count);
  255. }
  256. bool
  257. wasm_defined_type_is_subtype_of(WASMType *const def_type1,
  258. WASMType *const def_type2,
  259. WASMModuleCommon *const module)
  260. {
  261. WASMTypePtr *types = NULL;
  262. uint32 type_count = 0;
  263. #if WASM_ENABLE_INTERP != 0
  264. if (module->module_type == Wasm_Module_Bytecode) {
  265. WASMModule *wasm_module = (WASMModule *)module;
  266. types = wasm_module->types;
  267. type_count = wasm_module->type_count;
  268. }
  269. #endif
  270. #if WASM_ENABLE_AOT != 0
  271. if (module->module_type == Wasm_Module_AoT) {
  272. AOTModule *aot_module = (AOTModule *)module;
  273. types = aot_module->types;
  274. type_count = aot_module->type_count;
  275. }
  276. #endif
  277. bh_assert(types);
  278. return wasm_type_is_subtype_of(def_type1, def_type2, types, type_count);
  279. }
  280. void
  281. wasm_ref_type_set_type_idx(wasm_ref_type_t *ref_type, bool nullable,
  282. int32 type_idx)
  283. {
  284. bh_assert(type_idx >= 0);
  285. ref_type->value_type =
  286. nullable ? VALUE_TYPE_HT_NULLABLE_REF : VALUE_TYPE_HT_NON_NULLABLE_REF;
  287. ref_type->nullable = nullable;
  288. ref_type->heap_type = type_idx;
  289. }
  290. void
  291. wasm_ref_type_set_heap_type(wasm_ref_type_t *ref_type, bool nullable,
  292. int32 heap_type)
  293. {
  294. bool ret;
  295. bh_assert((heap_type >= HEAP_TYPE_ARRAY && heap_type <= HEAP_TYPE_NOFUNC)
  296. #if WASM_ENABLE_STRINGREF != 0
  297. || heap_type == HEAP_TYPE_STRINGREF
  298. || heap_type == HEAP_TYPE_STRINGVIEWWTF8
  299. || heap_type == HEAP_TYPE_STRINGVIEWWTF16
  300. || heap_type == HEAP_TYPE_STRINGVIEWITER
  301. #endif
  302. );
  303. ref_type->value_type =
  304. nullable ? VALUE_TYPE_HT_NULLABLE_REF : VALUE_TYPE_HT_NON_NULLABLE_REF;
  305. ref_type->nullable = nullable;
  306. ref_type->heap_type = heap_type;
  307. ret = wasm_ref_type_normalize(ref_type);
  308. bh_assert(ret);
  309. (void)ret;
  310. }
  311. bool
  312. wasm_ref_type_equal(const wasm_ref_type_t *ref_type1,
  313. const wasm_ref_type_t *ref_type2,
  314. WASMModuleCommon *const module)
  315. {
  316. wasm_ref_type_t ref_type1_norm = { 0 };
  317. wasm_ref_type_t ref_type2_norm = { 0 };
  318. uint32 type_count = 0;
  319. WASMTypePtr *types = NULL;
  320. uint8 type1;
  321. uint8 type2;
  322. bh_memcpy_s(&ref_type1_norm, (uint32)sizeof(wasm_ref_type_t), ref_type1,
  323. (uint32)sizeof(wasm_ref_type_t));
  324. bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
  325. (uint32)sizeof(wasm_ref_type_t));
  326. if (!wasm_ref_type_normalize(&ref_type1_norm)) {
  327. return false;
  328. }
  329. if (!wasm_ref_type_normalize(&ref_type2_norm)) {
  330. return false;
  331. }
  332. type1 = ref_type1_norm.value_type;
  333. type2 = ref_type2_norm.value_type;
  334. #if WASM_ENABLE_INTERP != 0
  335. if (module->module_type == Wasm_Module_Bytecode) {
  336. types = ((WASMModule *)module)->types;
  337. type_count = wasm_get_defined_type_count(module);
  338. }
  339. #endif
  340. #if WASM_ENABLE_AOT != 0
  341. if (module->module_type == Wasm_Module_AoT) {
  342. types = ((AOTModule *)module)->types;
  343. type_count = wasm_get_defined_type_count(module);
  344. }
  345. #endif
  346. return wasm_reftype_equal(type1, (WASMRefType *)&ref_type1_norm, type2,
  347. (WASMRefType *)&ref_type2_norm, types,
  348. type_count);
  349. }
  350. bool
  351. wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
  352. const wasm_ref_type_t *ref_type2,
  353. WASMModuleCommon *const module)
  354. {
  355. wasm_ref_type_t ref_type1_norm = { 0 };
  356. wasm_ref_type_t ref_type2_norm = { 0 };
  357. uint8 type1;
  358. uint8 type2;
  359. WASMTypePtr *types = NULL;
  360. uint32 type_count = 0;
  361. bh_memcpy_s(&ref_type1_norm, (uint32)sizeof(wasm_ref_type_t), ref_type1,
  362. (uint32)sizeof(wasm_ref_type_t));
  363. bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
  364. (uint32)sizeof(wasm_ref_type_t));
  365. if (!wasm_ref_type_normalize(&ref_type1_norm)) {
  366. return false;
  367. }
  368. if (!wasm_ref_type_normalize(&ref_type2_norm)) {
  369. return false;
  370. }
  371. type1 = ref_type1_norm.value_type;
  372. type2 = ref_type2_norm.value_type;
  373. #if WASM_ENABLE_INTERP != 0
  374. if (module->module_type == Wasm_Module_Bytecode) {
  375. types = ((WASMModule *)module)->types;
  376. type_count = wasm_get_defined_type_count(module);
  377. }
  378. #endif
  379. #if WASM_ENABLE_AOT != 0
  380. if (module->module_type == Wasm_Module_AoT) {
  381. types = ((AOTModule *)module)->types;
  382. type_count = wasm_get_defined_type_count(module);
  383. }
  384. #endif
  385. bh_assert(types);
  386. return wasm_reftype_is_subtype_of(type1, (WASMRefType *)&ref_type1_norm,
  387. type2, (WASMRefType *)&ref_type2_norm,
  388. types, type_count);
  389. }
  390. WASMStructObjectRef
  391. wasm_struct_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx)
  392. {
  393. WASMStructObjectRef struct_obj;
  394. WASMModuleInstanceCommon *module_inst =
  395. wasm_runtime_get_module_inst(exec_env);
  396. WASMType *type = NULL;
  397. WASMRttTypeRef rtt_type = NULL;
  398. #if WASM_ENABLE_INTERP != 0
  399. if (module_inst->module_type == Wasm_Module_Bytecode) {
  400. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  401. bh_assert(type_idx < module->type_count);
  402. type = module->types[type_idx];
  403. bh_assert(wasm_defined_type_is_struct_type(type));
  404. rtt_type =
  405. wasm_rtt_type_new(type, type_idx, module->rtt_types,
  406. module->type_count, &module->rtt_type_lock);
  407. }
  408. #endif
  409. #if WASM_ENABLE_AOT != 0
  410. if (module_inst->module_type == Wasm_Module_AoT) {
  411. AOTModule *module =
  412. (AOTModule *)((AOTModuleInstance *)module_inst)->module;
  413. bh_assert(type_idx < module->type_count);
  414. type = module->types[type_idx];
  415. bh_assert(wasm_defined_type_is_struct_type(type));
  416. rtt_type =
  417. wasm_rtt_type_new(type, type_idx, module->rtt_types,
  418. module->type_count, &module->rtt_type_lock);
  419. }
  420. #endif
  421. if (!rtt_type) {
  422. return NULL;
  423. }
  424. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  425. return struct_obj;
  426. }
  427. WASMStructObjectRef
  428. wasm_struct_obj_new_with_type(WASMExecEnv *exec_env, WASMStructType *type)
  429. {
  430. WASMStructObjectRef struct_obj;
  431. WASMModuleInstanceCommon *module_inst =
  432. wasm_runtime_get_module_inst(exec_env);
  433. WASMRttTypeRef rtt_type = NULL;
  434. uint32 i = 0;
  435. uint32 type_count = 0;
  436. bh_assert(type->base_type.type_flag == WASM_TYPE_STRUCT);
  437. #if WASM_ENABLE_INTERP != 0
  438. if (module_inst->module_type == Wasm_Module_Bytecode) {
  439. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  440. type_count = module->type_count;
  441. for (i = 0; i < type_count; i++) {
  442. if (module->types[i] == (WASMType *)type) {
  443. break;
  444. }
  445. }
  446. bh_assert(i < type_count);
  447. rtt_type =
  448. wasm_rtt_type_new((WASMType *)type, i, module->rtt_types,
  449. module->type_count, &module->rtt_type_lock);
  450. }
  451. #endif
  452. #if WASM_ENABLE_AOT != 0
  453. if (module_inst->module_type == Wasm_Module_AoT) {
  454. AOTModule *module =
  455. (AOTModule *)((AOTModuleInstance *)module_inst)->module;
  456. type_count = module->type_count;
  457. for (i = 0; i < type_count; i++) {
  458. if (module->types[i] == (AOTType *)type) {
  459. break;
  460. }
  461. }
  462. bh_assert(i < type_count);
  463. rtt_type =
  464. wasm_rtt_type_new((AOTType *)type, i, module->rtt_types,
  465. module->type_count, &module->rtt_type_lock);
  466. }
  467. #endif
  468. if (!rtt_type) {
  469. return NULL;
  470. }
  471. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  472. return struct_obj;
  473. }
  474. WASMArrayObjectRef
  475. wasm_array_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx,
  476. uint32 length, wasm_value_t *init_value)
  477. {
  478. WASMArrayObjectRef array_obj;
  479. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  480. WASMType *defined_type = wasm_get_defined_type(module, type_idx);
  481. WASMRttTypeRef rtt_type = NULL;
  482. bh_assert(wasm_defined_type_is_array_type(defined_type));
  483. #if WASM_ENABLE_INTERP != 0
  484. if (module->module_type == Wasm_Module_Bytecode) {
  485. WASMModule *wasm_module = (WASMModule *)module;
  486. rtt_type = wasm_rtt_type_new(
  487. defined_type, type_idx, wasm_module->rtt_types,
  488. wasm_module->type_count, &wasm_module->rtt_type_lock);
  489. }
  490. #endif
  491. #if WASM_ENABLE_AOT != 0
  492. if (module->module_type == Wasm_Module_AoT) {
  493. AOTModule *aot_module = (AOTModule *)module;
  494. rtt_type = wasm_rtt_type_new(
  495. defined_type, type_idx, aot_module->rtt_types,
  496. aot_module->type_count, &aot_module->rtt_type_lock);
  497. }
  498. #endif
  499. if (!rtt_type) {
  500. return NULL;
  501. }
  502. array_obj = wasm_array_obj_new(exec_env, rtt_type, length, init_value);
  503. return array_obj;
  504. }
  505. WASMArrayObjectRef
  506. wasm_array_obj_new_with_type(WASMExecEnv *exec_env, WASMArrayType *type,
  507. uint32 length, wasm_value_t *init_value)
  508. {
  509. WASMArrayObjectRef array_obj;
  510. uint32 i, type_count, type_idx = 0;
  511. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  512. bh_assert(type->base_type.type_flag == WASM_TYPE_ARRAY);
  513. #if WASM_ENABLE_INTERP != 0
  514. if (module->module_type == Wasm_Module_Bytecode) {
  515. WASMModule *wasm_module = (WASMModule *)module;
  516. type_count = wasm_module->type_count;
  517. for (i = 0; i < type_count; i++) {
  518. if (wasm_module->types[i] == (WASMType *)type) {
  519. break;
  520. }
  521. }
  522. bh_assert(i < wasm_module->type_count);
  523. type_idx = i;
  524. }
  525. #endif
  526. #if WASM_ENABLE_AOT != 0
  527. if (module->module_type == Wasm_Module_AoT) {
  528. AOTModule *aot_module = (AOTModule *)module;
  529. type_count = aot_module->type_count;
  530. for (i = 0; i < type_count; i++) {
  531. if (aot_module->types[i] == (AOTType *)type) {
  532. break;
  533. }
  534. }
  535. bh_assert(i < aot_module->type_count);
  536. type_idx = i;
  537. }
  538. #endif
  539. array_obj =
  540. wasm_array_obj_new_with_typeidx(exec_env, type_idx, length, init_value);
  541. return array_obj;
  542. }
  543. WASMFuncObjectRef
  544. wasm_func_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx,
  545. uint32 func_idx_bound)
  546. {
  547. WASMFuncObjectRef func_obj;
  548. WASMRttTypeRef rtt_type = NULL;
  549. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  550. WASMType *defined_type = wasm_get_defined_type(module, type_idx);
  551. #if WASM_ENABLE_INTERP != 0
  552. if (module->module_type == Wasm_Module_Bytecode) {
  553. WASMModule *wasm_module = (WASMModule *)module;
  554. rtt_type = wasm_rtt_type_new(
  555. defined_type, type_idx, wasm_module->rtt_types,
  556. wasm_module->type_count, &wasm_module->rtt_type_lock);
  557. }
  558. #endif
  559. #if WASM_ENABLE_AOT != 0
  560. if (module->module_type == Wasm_Module_AoT) {
  561. AOTModule *aot_module = (AOTModule *)module;
  562. rtt_type = wasm_rtt_type_new(
  563. defined_type, type_idx, aot_module->rtt_types,
  564. aot_module->type_count, &aot_module->rtt_type_lock);
  565. }
  566. #endif
  567. if (!rtt_type) {
  568. return NULL;
  569. }
  570. func_obj = wasm_func_obj_new(exec_env, rtt_type, func_idx_bound);
  571. return func_obj;
  572. }
  573. WASMFuncObjectRef
  574. wasm_func_obj_new_with_type(WASMExecEnv *exec_env, WASMFuncType *type,
  575. uint32 func_idx_bound)
  576. {
  577. WASMFuncObjectRef func_obj;
  578. uint32 i, type_count, type_idx = 0;
  579. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  580. bh_assert(type->base_type.type_flag == WASM_TYPE_FUNC);
  581. #if WASM_ENABLE_INTERP != 0
  582. if (module->module_type == Wasm_Module_Bytecode) {
  583. WASMModule *wasm_module = (WASMModule *)module;
  584. type_count = wasm_module->type_count;
  585. for (i = 0; i < type_count; i++) {
  586. if (wasm_module->types[i] == (WASMType *)type) {
  587. break;
  588. }
  589. }
  590. bh_assert(i < wasm_module->type_count);
  591. type_idx = i;
  592. }
  593. #endif
  594. #if WASM_ENABLE_AOT != 0
  595. if (module->module_type == Wasm_Module_AoT) {
  596. AOTModule *aot_module = (AOTModule *)module;
  597. type_count = aot_module->type_count;
  598. for (i = 0; i < type_count; i++) {
  599. if (aot_module->types[i] == (AOTType *)type) {
  600. break;
  601. }
  602. }
  603. bh_assert(i < aot_module->type_count);
  604. type_idx = i;
  605. }
  606. #endif
  607. func_obj =
  608. wasm_func_obj_new_with_typeidx(exec_env, type_idx, func_idx_bound);
  609. return func_obj;
  610. }
  611. bool
  612. wasm_runtime_call_func_ref(WASMExecEnv *exec_env,
  613. const WASMFuncObjectRef func_obj, uint32 argc,
  614. uint32 argv[])
  615. {
  616. WASMFunctionInstanceCommon *func_inst = NULL;
  617. uint32 func_idx = wasm_func_obj_get_func_idx_bound(func_obj);
  618. #if WASM_ENABLE_AOT != 0
  619. AOTFunctionInstance aot_func_inst = { 0 };
  620. #endif
  621. #if WASM_ENABLE_INTERP != 0
  622. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  623. WASMFunctionInstance *wasm_func_inst;
  624. WASMModuleInstance *module_inst =
  625. (WASMModuleInstance *)exec_env->module_inst;
  626. bh_assert(func_idx < module_inst->module->import_function_count
  627. + module_inst->module->function_count);
  628. wasm_func_inst = module_inst->e->functions + func_idx;
  629. func_inst = (WASMFunctionInstanceCommon *)wasm_func_inst;
  630. }
  631. #endif
  632. #if WASM_ENABLE_AOT != 0
  633. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  634. uint32 func_type_idx;
  635. AOTModuleInstance *module_inst =
  636. (AOTModuleInstance *)exec_env->module_inst;
  637. AOTModule *module = (AOTModule *)module_inst->module;
  638. (void)module_inst;
  639. bh_assert(func_idx < module->import_func_count + module->func_count);
  640. aot_func_inst.func_name = "";
  641. aot_func_inst.func_index = func_idx;
  642. aot_func_inst.is_import_func = false;
  643. func_type_idx =
  644. module->func_type_indexes[func_idx - module->import_func_count];
  645. aot_func_inst.u.func.func_type =
  646. (AOTFuncType *)module->types[func_type_idx];
  647. aot_func_inst.u.func.func_ptr =
  648. module->func_ptrs[func_idx - module->import_func_count];
  649. func_inst = (WASMFunctionInstanceCommon *)(&aot_func_inst);
  650. }
  651. #endif
  652. bh_assert(func_inst);
  653. return wasm_runtime_call_wasm(exec_env, func_inst, argc, argv);
  654. }
  655. bool
  656. wasm_runtime_call_func_ref_a(WASMExecEnv *exec_env,
  657. const WASMFuncObjectRef func_obj,
  658. uint32 num_results, wasm_val_t results[],
  659. uint32 num_args, wasm_val_t *args)
  660. {
  661. /* TODO */
  662. return false;
  663. }
  664. bool
  665. wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
  666. const WASMFuncObjectRef func_obj,
  667. uint32 num_results, wasm_val_t results[],
  668. uint32 num_args, ...)
  669. {
  670. /* TODO */
  671. return false;
  672. }
  673. bool
  674. wasm_obj_is_instance_of_defined_type(WASMObjectRef obj, WASMType *defined_type,
  675. WASMModuleCommon *const module)
  676. {
  677. WASMType **types = NULL;
  678. uint32 type_count = 0;
  679. uint32 type_idx = 0;
  680. #if WASM_ENABLE_INTERP != 0
  681. if (module->module_type == Wasm_Module_Bytecode) {
  682. WASMModule *wasm_module = (WASMModule *)module;
  683. type_count = wasm_module->type_count;
  684. types = wasm_module->types;
  685. }
  686. #endif
  687. #if WASM_ENABLE_AOT != 0
  688. if (module->module_type == Wasm_Module_AoT) {
  689. AOTModule *aot_module = (AOTModule *)module;
  690. type_count = aot_module->type_count;
  691. types = (WASMType **)aot_module->types;
  692. }
  693. #endif
  694. for (type_idx = 0; type_idx < type_count; type_idx++) {
  695. if (types[type_idx] == defined_type) {
  696. break;
  697. }
  698. }
  699. bh_assert(type_idx < type_count);
  700. return wasm_obj_is_instance_of(obj, type_idx, types, type_count);
  701. }
  702. bool
  703. wasm_obj_is_instance_of_type_idx(WASMObjectRef obj, uint32 type_idx,
  704. WASMModuleCommon *const module)
  705. {
  706. WASMType **types = NULL;
  707. uint32 type_count = 0;
  708. #if WASM_ENABLE_INTERP != 0
  709. if (module->module_type == Wasm_Module_Bytecode) {
  710. WASMModule *wasm_module = (WASMModule *)module;
  711. types = wasm_module->types;
  712. }
  713. #endif
  714. #if WASM_ENABLE_AOT != 0
  715. if (module->module_type == Wasm_Module_AoT) {
  716. AOTModule *aot_module = (AOTModule *)module;
  717. types = (WASMType **)aot_module->types;
  718. }
  719. #endif
  720. bh_assert(types);
  721. return wasm_obj_is_instance_of(obj, type_idx, types, type_count);
  722. }
  723. bool
  724. wasm_obj_is_instance_of_ref_type(const WASMObjectRef obj,
  725. const wasm_ref_type_t *ref_type)
  726. {
  727. int32 heap_type = ref_type->heap_type;
  728. return wasm_obj_is_type_of(obj, heap_type);
  729. }
  730. void
  731. wasm_runtime_push_local_obj_ref(WASMExecEnv *exec_env, WASMLocalObjectRef *ref)
  732. {
  733. ref->val = NULL;
  734. ref->prev = exec_env->cur_local_object_ref;
  735. exec_env->cur_local_object_ref = ref;
  736. }
  737. WASMLocalObjectRef *
  738. wasm_runtime_pop_local_obj_ref(WASMExecEnv *exec_env)
  739. {
  740. WASMLocalObjectRef *local_ref = exec_env->cur_local_object_ref;
  741. exec_env->cur_local_object_ref = exec_env->cur_local_object_ref->prev;
  742. return local_ref;
  743. }
  744. void
  745. wasm_runtime_pop_local_obj_refs(WASMExecEnv *exec_env, uint32 n)
  746. {
  747. bh_assert(n > 0);
  748. do {
  749. exec_env->cur_local_object_ref = exec_env->cur_local_object_ref->prev;
  750. } while (--n > 0);
  751. }
  752. WASMLocalObjectRef *
  753. wasm_runtime_get_cur_local_obj_ref(WASMExecEnv *exec_env)
  754. {
  755. WASMLocalObjectRef *local_ref = exec_env->cur_local_object_ref;
  756. bh_assert(local_ref);
  757. return local_ref;
  758. }
  759. void
  760. wasm_runtime_gc_prepare(WASMExecEnv *exec_env)
  761. {
  762. #if 0
  763. /* TODO: implement wasm_runtime_gc_prepare for multi-thread */
  764. exec_env->is_gc_reclaiming = false;
  765. wasm_thread_suspend_all();
  766. exec_env->is_gc_reclaim = 1;
  767. exec_env->requesting_suspend = 0;
  768. #endif
  769. }
  770. void
  771. wasm_runtime_gc_finalize(WASMExecEnv *exec_env)
  772. {
  773. #if 0
  774. /* TODO: implement wasm_runtime_gc_finalize for multi-thread */
  775. wasm_thread_resume_all();
  776. exec_env->doing_gc_reclaim = 0;
  777. #endif
  778. }
  779. bool
  780. wasm_runtime_get_wasm_object_ref_list(WASMObjectRef obj,
  781. bool *p_is_compact_mode,
  782. uint32 *p_ref_num, uint16 **p_ref_list,
  783. uint32 *p_ref_start_offset)
  784. {
  785. return wasm_object_get_ref_list(obj, p_is_compact_mode, p_ref_num,
  786. p_ref_list, p_ref_start_offset);
  787. }
  788. bool
  789. wasm_runtime_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  790. {
  791. #if WASM_ENABLE_INTERP != 0
  792. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  793. return wasm_traverse_gc_rootset(exec_env, heap);
  794. }
  795. #endif
  796. #if WASM_ENABLE_AOT != 0
  797. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  798. return aot_traverse_gc_rootset(exec_env, heap);
  799. }
  800. #endif
  801. return false;
  802. }
  803. void
  804. wasm_runtime_set_gc_heap_handle(WASMModuleInstanceCommon *module_inst,
  805. void *gc_heap_handle)
  806. {
  807. #if WASM_ENABLE_INTERP != 0
  808. if (module_inst->module_type == Wasm_Module_Bytecode)
  809. ((WASMModuleInstance *)module_inst)->e->common.gc_heap_handle =
  810. gc_heap_handle;
  811. #endif
  812. #if WASM_ENABLE_AOT != 0
  813. if (module_inst->module_type == Wasm_Module_AoT) {
  814. AOTModuleInstanceExtra *e =
  815. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  816. e->common.gc_heap_handle = gc_heap_handle;
  817. }
  818. #endif
  819. }
  820. void *
  821. wasm_runtime_get_gc_heap_handle(WASMModuleInstanceCommon *module_inst)
  822. {
  823. #if WASM_ENABLE_INTERP != 0
  824. if (module_inst->module_type == Wasm_Module_Bytecode)
  825. return ((WASMModuleInstance *)module_inst)->e->common.gc_heap_handle;
  826. #endif
  827. #if WASM_ENABLE_AOT != 0
  828. if (module_inst->module_type == Wasm_Module_AoT) {
  829. AOTModuleInstanceExtra *e =
  830. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  831. return e->common.gc_heap_handle;
  832. }
  833. #endif
  834. return NULL;
  835. }
  836. bool
  837. wasm_runtime_get_wasm_object_extra_info_flag(WASMObjectRef obj)
  838. {
  839. return obj->header & WASM_OBJ_EXTRA_INFO_FLAG;
  840. }
  841. void
  842. wasm_runtime_set_wasm_object_extra_info_flag(WASMObjectRef obj, bool set)
  843. {
  844. if (set) {
  845. obj->header |= WASM_OBJ_EXTRA_INFO_FLAG;
  846. }
  847. else {
  848. obj->header &= ~WASM_OBJ_EXTRA_INFO_FLAG;
  849. }
  850. }