From 4a459ea1a4056edae7ad6496d70a4bca4d6d4ee0 Mon Sep 17 00:00:00 2001 From: wenlingyun1 Date: Mon, 5 Feb 2024 10:58:33 +0800 Subject: [PATCH] push the delayed function of setTimeout into local rootset Signed-off-by: wenlingyun1 --- runtime-library/stdlib/lib_timer.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/runtime-library/stdlib/lib_timer.c b/runtime-library/stdlib/lib_timer.c index 74448ba8..36a1afd5 100644 --- a/runtime-library/stdlib/lib_timer.c +++ b/runtime-library/stdlib/lib_timer.c @@ -5,11 +5,13 @@ #include "bh_hashmap.h" #include "wasm_export.h" +#include "gc_export.h" wasm_exec_env_t env; HashMap *timer_map; void *(*_createTimer)(uint64_t timeout); bool (*_destroyTimer)(void *timer_id); +static uint32_t local_obj_ref_count; double setTimeout(wasm_exec_env_t exec_env, void *closure, double delay, void *args) @@ -20,8 +22,13 @@ setTimeout(wasm_exec_env_t exec_env, void *closure, double delay, void *args) if (_createTimer != NULL) { timer_id = _createTimer(delay); bh_hash_map_insert(timer_map, timer_id, closure); + wasm_local_obj_ref_t local_ref; + wasm_runtime_push_local_obj_ref(env, &local_ref); + local_ref.val = (wasm_obj_t)closure; + local_obj_ref_count++; return (double)(uintptr_t)(timer_id); } + return 0; } @@ -30,12 +37,14 @@ clearTimeout(wasm_exec_env_t exec_env, double id) { void *timer_id = (void *)(uintptr_t)id; + local_obj_ref_count--; if (id == 0) { return; } if (_destroyTimer != NULL) { _destroyTimer(timer_id); } + wasm_runtime_pop_local_obj_refs(exec_env, local_obj_ref_count); } /* clang-format off */