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

setTimeout, setInterval and clearInterval (and the same clearTimeout) implementations #4130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hansl
Copy link
Contributor

@hansl hansl commented Jan 13, 2025

This adds non-async implementations of the setTimeout/setInterval API functions. They are by default registered in the context when registering all APIs from boa_runtime.

This adds a new type of jobs that implement HostEnqueueTimeoutJob (see https://tc39.es/ecma262/#sec-hostenqueuetimeoutjob).

This requires #4141 to be merged first.

@hansl hansl requested review from jedel1043 and a team January 13, 2025 23:34
@hansl hansl marked this pull request as draft January 13, 2025 23:41
hansl added 2 commits January 22, 2025 11:06
…imeout`) implementations

This adds non-async implementations of the `setTimeout`/`setInterval` API functions. They are by default registered in the context when registering all APIs from `boa_runtime`.
@hansl hansl marked this pull request as ready for review January 22, 2025 23:32
Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

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

Really nice implementation! I have some opinions on using SystemTime, but aside from that everything else looks great!

Comment on lines +142 to +167
/// Create a new `TimeoutJob` with a timeout and a job.
#[must_use]
pub fn new_unchecked(job: NativeJob, timeout: i64) -> Self {
Self { timeout, job }
}

/// Create a new `TimeoutJob` with a job and a timeout in milliseconds in the future.
#[must_use]
pub fn delayed(job: NativeJob, delay: u64, context: &mut Context) -> Self {
Self::new_unchecked(job, context.host_hooks().utc_now() + (delay as i64))
}

/// Creates a new `TimeoutJob` from a closure and a timeout as [`std::time::SystemTime`].
#[must_use]
pub fn new<F>(f: F, timeout: std::time::SystemTime) -> Self
where
F: FnOnce(&mut Context) -> JsResult<JsValue> + 'static,
{
Self::new_unchecked(
NativeJob::new(f),
timeout
.duration_since(std::time::UNIX_EPOCH)
.expect("Invalid SystemTime")
.as_millis() as i64,
)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we shouldn't depend so heavily on SystemTime. Seems weird to tie one of our job types to something that could panic on certain platforms, and users won't be able to sandbox the engine from the OS if this is in place.

Having new(job: NativeJob, timeout: i64) sounds enough for all usecases, and the specific executor can use the provided timeout to schedule the job as it fits.

Comment on lines +125 to +126
/// The instant this job should be run, in msec since epoch. This will be compared
/// to the host's [`HostHooks::utc_now`] method.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should mention details about SimpleJobExecutor, since every executor can handle the timeout amount in a different way.

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