gc_common.c 29 KB

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