gc_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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_FUNC && heap_type >= HEAP_TYPE_NONE);
  296. ref_type->value_type =
  297. nullable ? VALUE_TYPE_HT_NULLABLE_REF : VALUE_TYPE_HT_NON_NULLABLE_REF;
  298. ref_type->nullable = nullable;
  299. ref_type->heap_type = heap_type;
  300. ret = wasm_ref_type_normalize(ref_type);
  301. bh_assert(ret);
  302. (void)ret;
  303. }
  304. bool
  305. wasm_ref_type_equal(const wasm_ref_type_t *ref_type1,
  306. const wasm_ref_type_t *ref_type2,
  307. WASMModuleCommon *const module)
  308. {
  309. wasm_ref_type_t ref_type1_norm = { 0 };
  310. wasm_ref_type_t ref_type2_norm = { 0 };
  311. uint32 type_count = 0;
  312. WASMTypePtr *types = NULL;
  313. uint8 type1;
  314. uint8 type2;
  315. bh_memcpy_s(&ref_type1_norm, (uint32)sizeof(wasm_ref_type_t), ref_type1,
  316. (uint32)sizeof(wasm_ref_type_t));
  317. bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
  318. (uint32)sizeof(wasm_ref_type_t));
  319. if (!wasm_ref_type_normalize(&ref_type1_norm)) {
  320. return false;
  321. }
  322. if (!wasm_ref_type_normalize(&ref_type2_norm)) {
  323. return false;
  324. }
  325. type1 = ref_type1_norm.value_type;
  326. type2 = ref_type2_norm.value_type;
  327. #if WASM_ENABLE_INTERP != 0
  328. if (module->module_type == Wasm_Module_Bytecode) {
  329. types = ((WASMModule *)module)->types;
  330. type_count = wasm_get_defined_type_count(module);
  331. }
  332. #endif
  333. #if WASM_ENABLE_AOT != 0
  334. if (module->module_type == Wasm_Module_AoT) {
  335. types = ((AOTModule *)module)->types;
  336. type_count = wasm_get_defined_type_count(module);
  337. }
  338. #endif
  339. return wasm_reftype_equal(type1, (WASMRefType *)&ref_type1_norm, type2,
  340. (WASMRefType *)&ref_type2_norm, types,
  341. type_count);
  342. }
  343. bool
  344. wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
  345. const wasm_ref_type_t *ref_type2,
  346. WASMModuleCommon *const module)
  347. {
  348. wasm_ref_type_t ref_type1_norm = { 0 };
  349. wasm_ref_type_t ref_type2_norm = { 0 };
  350. uint8 type1;
  351. uint8 type2;
  352. WASMTypePtr *types = NULL;
  353. uint32 type_count = 0;
  354. bh_memcpy_s(&ref_type1_norm, (uint32)sizeof(wasm_ref_type_t), ref_type1,
  355. (uint32)sizeof(wasm_ref_type_t));
  356. bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
  357. (uint32)sizeof(wasm_ref_type_t));
  358. if (!wasm_ref_type_normalize(&ref_type1_norm)) {
  359. return false;
  360. }
  361. if (!wasm_ref_type_normalize(&ref_type2_norm)) {
  362. return false;
  363. }
  364. type1 = ref_type1_norm.value_type;
  365. type2 = ref_type2_norm.value_type;
  366. #if WASM_ENABLE_INTERP != 0
  367. if (module->module_type == Wasm_Module_Bytecode) {
  368. types = ((WASMModule *)module)->types;
  369. type_count = wasm_get_defined_type_count(module);
  370. }
  371. #endif
  372. #if WASM_ENABLE_AOT != 0
  373. if (module->module_type == Wasm_Module_AoT) {
  374. types = ((AOTModule *)module)->types;
  375. type_count = wasm_get_defined_type_count(module);
  376. }
  377. #endif
  378. bh_assert(types);
  379. return wasm_reftype_is_subtype_of(type1, (WASMRefType *)&ref_type1_norm,
  380. type2, (WASMRefType *)&ref_type2_norm,
  381. types, type_count);
  382. }
  383. WASMStructObjectRef
  384. wasm_struct_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx)
  385. {
  386. WASMStructObjectRef struct_obj;
  387. WASMModuleInstanceCommon *module_inst =
  388. wasm_runtime_get_module_inst(exec_env);
  389. WASMType *type = NULL;
  390. WASMRttTypeRef rtt_type = NULL;
  391. #if WASM_ENABLE_INTERP != 0
  392. if (module_inst->module_type == Wasm_Module_Bytecode) {
  393. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  394. bh_assert(type_idx < module->type_count);
  395. type = module->types[type_idx];
  396. bh_assert(wasm_defined_type_is_struct_type(type));
  397. rtt_type =
  398. wasm_rtt_type_new(type, type_idx, module->rtt_types,
  399. module->type_count, &module->rtt_type_lock);
  400. }
  401. #endif
  402. #if WASM_ENABLE_AOT != 0
  403. if (module_inst->module_type == Wasm_Module_AoT) {
  404. AOTModule *module =
  405. (AOTModule *)((AOTModuleInstance *)module_inst)->module;
  406. bh_assert(type_idx < module->type_count);
  407. type = module->types[type_idx];
  408. bh_assert(wasm_defined_type_is_struct_type(type));
  409. rtt_type =
  410. wasm_rtt_type_new(type, type_idx, module->rtt_types,
  411. module->type_count, &module->rtt_type_lock);
  412. }
  413. #endif
  414. if (!rtt_type) {
  415. return NULL;
  416. }
  417. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  418. return struct_obj;
  419. }
  420. WASMStructObjectRef
  421. wasm_struct_obj_new_with_type(WASMExecEnv *exec_env, WASMStructType *type)
  422. {
  423. WASMStructObjectRef struct_obj;
  424. WASMModuleInstanceCommon *module_inst =
  425. wasm_runtime_get_module_inst(exec_env);
  426. WASMRttTypeRef rtt_type = NULL;
  427. uint32 i = 0;
  428. uint32 type_count = 0;
  429. bh_assert(type->base_type.type_flag == WASM_TYPE_STRUCT);
  430. #if WASM_ENABLE_INTERP != 0
  431. if (module_inst->module_type == Wasm_Module_Bytecode) {
  432. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  433. type_count = module->type_count;
  434. for (i = 0; i < type_count; i++) {
  435. if (module->types[i] == (WASMType *)type) {
  436. break;
  437. }
  438. }
  439. bh_assert(i < type_count);
  440. rtt_type =
  441. wasm_rtt_type_new((WASMType *)type, i, module->rtt_types,
  442. module->type_count, &module->rtt_type_lock);
  443. }
  444. #endif
  445. #if WASM_ENABLE_AOT != 0
  446. if (module_inst->module_type == Wasm_Module_AoT) {
  447. AOTModule *module =
  448. (AOTModule *)((AOTModuleInstance *)module_inst)->module;
  449. type_count = module->type_count;
  450. for (i = 0; i < type_count; i++) {
  451. if (module->types[i] == (AOTType *)type) {
  452. break;
  453. }
  454. }
  455. bh_assert(i < type_count);
  456. rtt_type =
  457. wasm_rtt_type_new((AOTType *)type, i, module->rtt_types,
  458. module->type_count, &module->rtt_type_lock);
  459. }
  460. #endif
  461. if (!rtt_type) {
  462. return NULL;
  463. }
  464. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  465. return struct_obj;
  466. }
  467. WASMArrayObjectRef
  468. wasm_array_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx,
  469. uint32 length, wasm_value_t *init_value)
  470. {
  471. WASMArrayObjectRef array_obj;
  472. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  473. WASMType *defined_type = wasm_get_defined_type(module, type_idx);
  474. WASMRttTypeRef rtt_type = NULL;
  475. bh_assert(wasm_defined_type_is_array_type(defined_type));
  476. #if WASM_ENABLE_INTERP != 0
  477. if (module->module_type == Wasm_Module_Bytecode) {
  478. WASMModule *wasm_module = (WASMModule *)module;
  479. rtt_type = wasm_rtt_type_new(
  480. defined_type, type_idx, wasm_module->rtt_types,
  481. wasm_module->type_count, &wasm_module->rtt_type_lock);
  482. }
  483. #endif
  484. #if WASM_ENABLE_AOT != 0
  485. if (module->module_type == Wasm_Module_AoT) {
  486. AOTModule *aot_module = (AOTModule *)module;
  487. rtt_type = wasm_rtt_type_new(
  488. defined_type, type_idx, aot_module->rtt_types,
  489. aot_module->type_count, &aot_module->rtt_type_lock);
  490. }
  491. #endif
  492. if (!rtt_type) {
  493. return NULL;
  494. }
  495. array_obj = wasm_array_obj_new(exec_env, rtt_type, length, init_value);
  496. return array_obj;
  497. }
  498. WASMArrayObjectRef
  499. wasm_array_obj_new_with_type(WASMExecEnv *exec_env, WASMArrayType *type,
  500. uint32 length, wasm_value_t *init_value)
  501. {
  502. WASMArrayObjectRef array_obj;
  503. uint32 i, type_count, type_idx = 0;
  504. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  505. bh_assert(type->base_type.type_flag == WASM_TYPE_ARRAY);
  506. #if WASM_ENABLE_INTERP != 0
  507. if (module->module_type == Wasm_Module_Bytecode) {
  508. WASMModule *wasm_module = (WASMModule *)module;
  509. type_count = wasm_module->type_count;
  510. for (i = 0; i < type_count; i++) {
  511. if (wasm_module->types[i] == (WASMType *)type) {
  512. break;
  513. }
  514. }
  515. bh_assert(i < wasm_module->type_count);
  516. type_idx = i;
  517. }
  518. #endif
  519. #if WASM_ENABLE_AOT != 0
  520. if (module->module_type == Wasm_Module_AoT) {
  521. AOTModule *aot_module = (AOTModule *)module;
  522. type_count = aot_module->type_count;
  523. for (i = 0; i < type_count; i++) {
  524. if (aot_module->types[i] == (AOTType *)type) {
  525. break;
  526. }
  527. }
  528. bh_assert(i < aot_module->type_count);
  529. type_idx = i;
  530. }
  531. #endif
  532. array_obj =
  533. wasm_array_obj_new_with_typeidx(exec_env, type_idx, length, init_value);
  534. return array_obj;
  535. }
  536. WASMFuncObjectRef
  537. wasm_func_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx,
  538. uint32 func_idx_bound)
  539. {
  540. WASMFuncObjectRef func_obj;
  541. WASMRttTypeRef rtt_type = NULL;
  542. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  543. WASMType *defined_type = wasm_get_defined_type(module, type_idx);
  544. #if WASM_ENABLE_INTERP != 0
  545. if (module->module_type == Wasm_Module_Bytecode) {
  546. WASMModule *wasm_module = (WASMModule *)module;
  547. rtt_type = wasm_rtt_type_new(
  548. defined_type, type_idx, wasm_module->rtt_types,
  549. wasm_module->type_count, &wasm_module->rtt_type_lock);
  550. }
  551. #endif
  552. #if WASM_ENABLE_AOT != 0
  553. if (module->module_type == Wasm_Module_AoT) {
  554. AOTModule *aot_module = (AOTModule *)module;
  555. rtt_type = wasm_rtt_type_new(
  556. defined_type, type_idx, aot_module->rtt_types,
  557. aot_module->type_count, &aot_module->rtt_type_lock);
  558. }
  559. #endif
  560. if (!rtt_type) {
  561. return NULL;
  562. }
  563. func_obj = wasm_func_obj_new(exec_env, rtt_type, func_idx_bound);
  564. return func_obj;
  565. }
  566. WASMFuncObjectRef
  567. wasm_func_obj_new_with_type(WASMExecEnv *exec_env, WASMFuncType *type,
  568. uint32 func_idx_bound)
  569. {
  570. WASMFuncObjectRef func_obj;
  571. uint32 i, type_count, type_idx = 0;
  572. WASMModuleCommon *module = wasm_exec_env_get_module(exec_env);
  573. bh_assert(type->base_type.type_flag == WASM_TYPE_FUNC);
  574. #if WASM_ENABLE_INTERP != 0
  575. if (module->module_type == Wasm_Module_Bytecode) {
  576. WASMModule *wasm_module = (WASMModule *)module;
  577. type_count = wasm_module->type_count;
  578. for (i = 0; i < type_count; i++) {
  579. if (wasm_module->types[i] == (WASMType *)type) {
  580. break;
  581. }
  582. }
  583. bh_assert(i < wasm_module->type_count);
  584. type_idx = i;
  585. }
  586. #endif
  587. #if WASM_ENABLE_AOT != 0
  588. if (module->module_type == Wasm_Module_AoT) {
  589. AOTModule *aot_module = (AOTModule *)module;
  590. type_count = aot_module->type_count;
  591. for (i = 0; i < type_count; i++) {
  592. if (aot_module->types[i] == (AOTType *)type) {
  593. break;
  594. }
  595. }
  596. bh_assert(i < aot_module->type_count);
  597. type_idx = i;
  598. }
  599. #endif
  600. func_obj =
  601. wasm_func_obj_new_with_typeidx(exec_env, type_idx, func_idx_bound);
  602. return func_obj;
  603. }
  604. bool
  605. wasm_runtime_call_func_ref(WASMExecEnv *exec_env,
  606. const WASMFuncObjectRef func_obj, uint32 argc,
  607. uint32 argv[])
  608. {
  609. WASMFunctionInstanceCommon *func_inst = NULL;
  610. uint32 func_idx = wasm_func_obj_get_func_idx_bound(func_obj);
  611. #if WASM_ENABLE_AOT != 0
  612. AOTFunctionInstance aot_func_inst = { 0 };
  613. #endif
  614. #if WASM_ENABLE_INTERP != 0
  615. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  616. WASMFunctionInstance *wasm_func_inst;
  617. WASMModuleInstance *module_inst =
  618. (WASMModuleInstance *)exec_env->module_inst;
  619. bh_assert(func_idx < module_inst->module->import_function_count
  620. + module_inst->module->function_count);
  621. wasm_func_inst = module_inst->e->functions + func_idx;
  622. func_inst = (WASMFunctionInstanceCommon *)wasm_func_inst;
  623. }
  624. #endif
  625. #if WASM_ENABLE_AOT != 0
  626. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  627. uint32 func_type_idx;
  628. AOTModuleInstance *module_inst =
  629. (AOTModuleInstance *)exec_env->module_inst;
  630. AOTModule *module = (AOTModule *)module_inst->module;
  631. (void)module_inst;
  632. bh_assert(func_idx < module->import_func_count + module->func_count);
  633. aot_func_inst.func_name = "";
  634. aot_func_inst.func_index = func_idx;
  635. aot_func_inst.is_import_func = false;
  636. func_type_idx =
  637. module->func_type_indexes[func_idx - module->import_func_count];
  638. aot_func_inst.u.func.func_type =
  639. (AOTFuncType *)module->types[func_type_idx];
  640. aot_func_inst.u.func.func_ptr =
  641. module->func_ptrs[func_idx - module->import_func_count];
  642. func_inst = (WASMFunctionInstanceCommon *)(&aot_func_inst);
  643. }
  644. #endif
  645. bh_assert(func_inst);
  646. return wasm_runtime_call_wasm(exec_env, func_inst, argc, argv);
  647. }
  648. bool
  649. wasm_runtime_call_func_ref_a(WASMExecEnv *exec_env,
  650. const WASMFuncObjectRef func_obj,
  651. uint32 num_results, wasm_val_t results[],
  652. uint32 num_args, wasm_val_t *args)
  653. {
  654. /* TODO */
  655. return false;
  656. }
  657. bool
  658. wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
  659. const WASMFuncObjectRef func_obj,
  660. uint32 num_results, wasm_val_t results[],
  661. uint32 num_args, ...)
  662. {
  663. /* TODO */
  664. return false;
  665. }
  666. bool
  667. wasm_obj_is_instance_of_defined_type(WASMObjectRef obj, WASMType *defined_type,
  668. WASMModuleCommon *const module)
  669. {
  670. WASMType **types = NULL;
  671. uint32 type_count = 0;
  672. uint32 type_idx = 0;
  673. #if WASM_ENABLE_INTERP != 0
  674. if (module->module_type == Wasm_Module_Bytecode) {
  675. WASMModule *wasm_module = (WASMModule *)module;
  676. type_count = wasm_module->type_count;
  677. types = wasm_module->types;
  678. }
  679. #endif
  680. #if WASM_ENABLE_AOT != 0
  681. if (module->module_type == Wasm_Module_AoT) {
  682. AOTModule *aot_module = (AOTModule *)module;
  683. type_count = aot_module->type_count;
  684. types = (WASMType **)aot_module->types;
  685. }
  686. #endif
  687. for (type_idx = 0; type_idx < type_count; type_idx++) {
  688. if (types[type_idx] == defined_type) {
  689. break;
  690. }
  691. }
  692. bh_assert(type_idx < type_count);
  693. return wasm_obj_is_instance_of(obj, type_idx, types, type_count);
  694. }
  695. bool
  696. wasm_obj_is_instance_of_type_idx(WASMObjectRef obj, uint32 type_idx,
  697. WASMModuleCommon *const module)
  698. {
  699. WASMType **types = NULL;
  700. uint32 type_count = 0;
  701. #if WASM_ENABLE_INTERP != 0
  702. if (module->module_type == Wasm_Module_Bytecode) {
  703. WASMModule *wasm_module = (WASMModule *)module;
  704. types = wasm_module->types;
  705. }
  706. #endif
  707. #if WASM_ENABLE_AOT != 0
  708. if (module->module_type == Wasm_Module_AoT) {
  709. AOTModule *aot_module = (AOTModule *)module;
  710. types = (WASMType **)aot_module->types;
  711. }
  712. #endif
  713. bh_assert(types);
  714. return wasm_obj_is_instance_of(obj, type_idx, types, type_count);
  715. }
  716. bool
  717. wasm_obj_is_instance_of_ref_type(const WASMObjectRef obj,
  718. const wasm_ref_type_t *ref_type)
  719. {
  720. int32 heap_type = ref_type->heap_type;
  721. return wasm_obj_is_type_of(obj, heap_type);
  722. }
  723. void
  724. wasm_runtime_push_local_obj_ref(WASMExecEnv *exec_env, WASMLocalObjectRef *ref)
  725. {
  726. ref->val = NULL;
  727. ref->prev = exec_env->cur_local_object_ref;
  728. exec_env->cur_local_object_ref = ref;
  729. }
  730. WASMLocalObjectRef *
  731. wasm_runtime_pop_local_obj_ref(WASMExecEnv *exec_env)
  732. {
  733. WASMLocalObjectRef *local_ref = exec_env->cur_local_object_ref;
  734. exec_env->cur_local_object_ref = exec_env->cur_local_object_ref->prev;
  735. return local_ref;
  736. }
  737. void
  738. wasm_runtime_pop_local_obj_refs(WASMExecEnv *exec_env, uint32 n)
  739. {
  740. bh_assert(n > 0);
  741. do {
  742. exec_env->cur_local_object_ref = exec_env->cur_local_object_ref->prev;
  743. } while (--n > 0);
  744. }
  745. WASMLocalObjectRef *
  746. wasm_runtime_get_cur_local_obj_ref(WASMExecEnv *exec_env)
  747. {
  748. WASMLocalObjectRef *local_ref = exec_env->cur_local_object_ref;
  749. bh_assert(local_ref);
  750. return local_ref;
  751. }
  752. void
  753. wasm_runtime_gc_prepare(WASMExecEnv *exec_env)
  754. {
  755. #if 0
  756. /* TODO: implement wasm_runtime_gc_prepare for multi-thread */
  757. exec_env->is_gc_reclaiming = false;
  758. wasm_thread_suspend_all();
  759. exec_env->is_gc_reclaim = 1;
  760. exec_env->requesting_suspend = 0;
  761. #endif
  762. }
  763. void
  764. wasm_runtime_gc_finalize(WASMExecEnv *exec_env)
  765. {
  766. #if 0
  767. /* TODO: implement wasm_runtime_gc_finalize for multi-thread */
  768. wasm_thread_resume_all();
  769. exec_env->doing_gc_reclaim = 0;
  770. #endif
  771. }
  772. bool
  773. wasm_runtime_get_wasm_object_ref_list(WASMObjectRef obj,
  774. bool *p_is_compact_mode,
  775. uint32 *p_ref_num, uint16 **p_ref_list,
  776. uint32 *p_ref_start_offset)
  777. {
  778. return wasm_object_get_ref_list(obj, p_is_compact_mode, p_ref_num,
  779. p_ref_list, p_ref_start_offset);
  780. }
  781. bool
  782. wasm_runtime_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  783. {
  784. #if WASM_ENABLE_INTERP != 0
  785. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  786. return wasm_traverse_gc_rootset(exec_env, heap);
  787. }
  788. #endif
  789. #if WASM_ENABLE_AOT != 0
  790. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  791. return aot_traverse_gc_rootset(exec_env, heap);
  792. }
  793. #endif
  794. return false;
  795. }
  796. void
  797. wasm_runtime_set_gc_heap_handle(WASMModuleInstanceCommon *module_inst,
  798. void *gc_heap_handle)
  799. {
  800. #if WASM_ENABLE_INTERP != 0
  801. if (module_inst->module_type == Wasm_Module_Bytecode)
  802. ((WASMModuleInstance *)module_inst)->e->common.gc_heap_handle =
  803. gc_heap_handle;
  804. #endif
  805. #if WASM_ENABLE_AOT != 0
  806. if (module_inst->module_type == Wasm_Module_AoT) {
  807. AOTModuleInstanceExtra *e =
  808. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  809. e->common.gc_heap_handle = gc_heap_handle;
  810. }
  811. #endif
  812. }
  813. void *
  814. wasm_runtime_get_gc_heap_handle(WASMModuleInstanceCommon *module_inst)
  815. {
  816. #if WASM_ENABLE_INTERP != 0
  817. if (module_inst->module_type == Wasm_Module_Bytecode)
  818. return ((WASMModuleInstance *)module_inst)->e->common.gc_heap_handle;
  819. #endif
  820. #if WASM_ENABLE_AOT != 0
  821. if (module_inst->module_type == Wasm_Module_AoT) {
  822. AOTModuleInstanceExtra *e =
  823. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  824. return e->common.gc_heap_handle;
  825. }
  826. #endif
  827. return NULL;
  828. }
  829. bool
  830. wasm_runtime_get_wasm_object_extra_info_flag(WASMObjectRef obj)
  831. {
  832. return obj->header & WASM_OBJ_EXTRA_INFO_FLAG;
  833. }
  834. void
  835. wasm_runtime_set_wasm_object_extra_info_flag(WASMObjectRef obj, bool set)
  836. {
  837. if (set) {
  838. obj->header |= WASM_OBJ_EXTRA_INFO_FLAG;
  839. }
  840. else {
  841. obj->header &= ~WASM_OBJ_EXTRA_INFO_FLAG;
  842. }
  843. }