Skip to content

Commit

Permalink
push the delayed function of setTimeout into local rootset
Browse files Browse the repository at this point in the history
Signed-off-by: wenlingyun1 <[email protected]>
  • Loading branch information
WenLY1 committed Feb 5, 2024
1 parent c1d8449 commit 4a459ea
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions runtime-library/stdlib/lib_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -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 */
Expand Down

0 comments on commit 4a459ea

Please sign in to comment.