Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push the delayed func of setTimeout into local rootset #147

Closed
wants to merge 1 commit into from

Conversation

WenLY1
Copy link
Contributor

@WenLY1 WenLY1 commented Feb 5, 2024

No description provided.

@WenLY1 WenLY1 changed the title push the delayed function of setTimeout into local rootset push the delayed func of setTimeout into local rootset Feb 5, 2024
@@ -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_object_ref(env, &local_ref);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is not designed for such usage, it can't extend the lifecycle of the object to larger scope.

Copy link
Contributor Author

@WenLY1 WenLY1 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, if I want the closure keep alive untill clearTimeout is called or the timer is exceeded, how can I achieve this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have a timer library in TS layer, and there will be an array to hold all these closures so that the closure will not be collected. And when the timer is cancelled, remove the closure from the array, then it can be collected.

The native API here don't need to accept the closure any more, the callback dispatch can be finished in the TS timer library.

Copy link
Contributor

@xujuntwt95329 xujuntwt95329 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WenLY1 Here is a simple example to demonstrate the TS layer library.

export function setTimeout(
cb: (my_timer: user_timer) => void,
timeout: i32,
): user_timer {
const timer = new user_timer(cb, timeout, false);
timer_list.push(timer);
return timer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants