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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ boa_ast.workspace = true
boa_parser.workspace = true
boa_string.workspace = true
cow-utils.workspace = true
futures-lite.workspace = true
serde = { workspace = true, features = ["derive", "rc"] }
serde_json.workspace = true
rand.workspace = true
Expand Down Expand Up @@ -139,7 +140,6 @@ criterion.workspace = true
float-cmp.workspace = true
indoc.workspace = true
textwrap.workspace = true
futures-lite.workspace = true
test-case.workspace = true

[target.x86_64-unknown-linux-gnu.dev-dependencies]
Expand Down
80 changes: 45 additions & 35 deletions core/engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl BuiltInConstructor for Date {
// b. Return ToDateString(now).
return Ok(JsValue::from(to_date_string_t(
now as f64,
context.host_hooks(),
context.host_hooks().as_ref(),
)));
}

Expand All @@ -222,7 +222,7 @@ impl BuiltInConstructor for Date {
// 3. If numberOfArgs = 0, then
[] => {
// a. Let dv be the time value (UTC) identifying the current time.
Self::utc_now(context.host_hooks())
Self::utc_now(context.host_hooks().as_ref())
}
// 4. Else if numberOfArgs = 1, then
// a. Let value be values[0].
Expand All @@ -243,7 +243,7 @@ impl BuiltInConstructor for Date {
if let Some(v) = v.as_string() {
// 1. Assert: The next step never returns an abrupt completion because v is a String.
// 2. Let tv be the result of parsing v as a date, in exactly the same manner as for the parse method (21.4.3.2).
let tv = parse_date(v, context.host_hooks());
let tv = parse_date(v, context.host_hooks().as_ref());
if let Some(tv) = tv {
tv as f64
} else {
Expand Down Expand Up @@ -296,7 +296,7 @@ impl BuiltInConstructor for Date {
let final_date = make_date(make_day(yr, m, dt), make_time(h, min, s, milli));

// k. Let dv be TimeClip(UTC(finalDate)).
Self(time_clip(utc_t(final_date, context.host_hooks())))
Self(time_clip(utc_t(final_date, context.host_hooks().as_ref())))
}
};

Expand Down Expand Up @@ -343,7 +343,8 @@ impl Date {
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
pub(crate) fn parse(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let date = args.get_or_undefined(0).to_string(context)?;
Ok(parse_date(&date, context.host_hooks()).map_or(JsValue::from(f64::NAN), JsValue::from))
Ok(parse_date(&date, context.host_hooks().as_ref())
.map_or(JsValue::from(f64::NAN), JsValue::from))
}

/// `Date.UTC()`
Expand Down Expand Up @@ -430,7 +431,7 @@ impl Date {
// 5. Return DateFromTime(LocalTime(t)).
Ok(JsValue::from(date_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return DateFromTime(t).
Expand Down Expand Up @@ -467,7 +468,10 @@ impl Date {

if LOCAL {
// 5. Return WeekDay(LocalTime(t)).
Ok(JsValue::from(week_day(local_time(t, context.host_hooks()))))
Ok(JsValue::from(week_day(local_time(
t,
context.host_hooks().as_ref(),
))))
} else {
// 5. Return WeekDay(t).
Ok(JsValue::from(week_day(t)))
Expand Down Expand Up @@ -506,7 +510,7 @@ impl Date {

// 5. Return YearFromTime(LocalTime(t)) - 1900𝔽.
Ok(JsValue::from(
year_from_time(local_time(t, context.host_hooks())) - 1900,
year_from_time(local_time(t, context.host_hooks().as_ref())) - 1900,
))
}

Expand Down Expand Up @@ -540,7 +544,7 @@ impl Date {
// 5. Return YearFromTime(LocalTime(t)).
Ok(JsValue::from(year_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return YearFromTime(t).
Expand Down Expand Up @@ -578,7 +582,7 @@ impl Date {
// 5. Return HourFromTime(LocalTime(t)).
Ok(JsValue::from(hour_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return HourFromTime(t).
Expand Down Expand Up @@ -616,7 +620,7 @@ impl Date {
// 5. Return msFromTime(LocalTime(t)).
Ok(JsValue::from(ms_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return msFromTime(t).
Expand Down Expand Up @@ -654,7 +658,7 @@ impl Date {
// 5. Return MinFromTime(LocalTime(t)).
Ok(JsValue::from(min_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return MinFromTime(t).
Expand Down Expand Up @@ -693,7 +697,7 @@ impl Date {
// 5. Return MonthFromTime(LocalTime(t)).
Ok(JsValue::from(month_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return MonthFromTime(t).
Expand Down Expand Up @@ -731,7 +735,7 @@ impl Date {
// 5. Return SecFromTime(LocalTime(t)).
Ok(JsValue::from(sec_from_time(local_time(
t,
context.host_hooks(),
context.host_hooks().as_ref(),
))))
} else {
// 5. Return SecFromTime(t).
Expand Down Expand Up @@ -797,7 +801,7 @@ impl Date {

// 5. Return (t - LocalTime(t)) / msPerMinute.
Ok(JsValue::from(
(t - local_time(t, context.host_hooks())) / MS_PER_MINUTE,
(t - local_time(t, context.host_hooks().as_ref())) / MS_PER_MINUTE,
))
}

Expand Down Expand Up @@ -840,7 +844,7 @@ impl Date {

if LOCAL {
// 6. Set t to LocalTime(t).
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 7. Let newDate be MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t)).
Expand All @@ -851,7 +855,7 @@ impl Date {

let u = if LOCAL {
// 8. Let u be TimeClip(UTC(newDate)).
time_clip(utc_t(new_date, context.host_hooks()))
time_clip(utc_t(new_date, context.host_hooks().as_ref()))
} else {
// 8. Let v be TimeClip(newDate).
time_clip(new_date)
Expand Down Expand Up @@ -903,7 +907,7 @@ impl Date {
if t.is_nan() {
0.0
} else {
local_time(t, context.host_hooks())
local_time(t, context.host_hooks().as_ref())
}
} else {
// 4. If t is NaN, set t to +0𝔽.
Expand Down Expand Up @@ -936,7 +940,7 @@ impl Date {

let u = if LOCAL {
// 9. Let u be TimeClip(UTC(newDate)).
time_clip(utc_t(new_date, context.host_hooks()))
time_clip(utc_t(new_date, context.host_hooks().as_ref()))
} else {
// 9. Let u be TimeClip(newDate).
time_clip(new_date)
Expand Down Expand Up @@ -1004,7 +1008,7 @@ impl Date {

if LOCAL {
// 9. Set t to LocalTime(t).
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 10. If min is not present, let m be MinFromTime(t).
Expand All @@ -1021,7 +1025,7 @@ impl Date {

let u = if LOCAL {
// 14. Let u be TimeClip(UTC(date)).
time_clip(utc_t(date, context.host_hooks()))
time_clip(utc_t(date, context.host_hooks().as_ref()))
} else {
// 14. Let u be TimeClip(date).
time_clip(date)
Expand Down Expand Up @@ -1077,7 +1081,7 @@ impl Date {

if LOCAL {
// 6. Set t to LocalTime(t).
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 7. Let time be MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms).
Expand All @@ -1090,7 +1094,10 @@ impl Date {

let u = if LOCAL {
// 8. Let u be TimeClip(UTC(MakeDate(Day(t), time))).
time_clip(utc_t(make_date(day(t), time), context.host_hooks()))
time_clip(utc_t(
make_date(day(t), time),
context.host_hooks().as_ref(),
))
} else {
// 8. Let u be TimeClip(MakeDate(Day(t), time)).
time_clip(make_date(day(t), time))
Expand Down Expand Up @@ -1152,7 +1159,7 @@ impl Date {

if LOCAL {
// 8. Set t to LocalTime(t).
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 9. If sec is not present, let s be SecFromTime(t).
Expand All @@ -1166,7 +1173,7 @@ impl Date {

let u = if LOCAL {
// 12. Let u be TimeClip(UTC(date)).
time_clip(utc_t(date, context.host_hooks()))
time_clip(utc_t(date, context.host_hooks().as_ref()))
} else {
// 12. Let u be TimeClip(date).
time_clip(date)
Expand Down Expand Up @@ -1226,7 +1233,7 @@ impl Date {

// 7. Set t to LocalTime(t).
if LOCAL {
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 8. If date is not present, let dt be DateFromTime(t).
Expand All @@ -1240,7 +1247,7 @@ impl Date {

let u = if LOCAL {
// 10. Let u be TimeClip(UTC(newDate)).
time_clip(utc_t(new_date, context.host_hooks()))
time_clip(utc_t(new_date, context.host_hooks().as_ref()))
} else {
// 10. Let u be TimeClip(newDate).
time_clip(new_date)
Expand Down Expand Up @@ -1299,7 +1306,7 @@ impl Date {

// 7. Set t to LocalTime(t).
if LOCAL {
t = local_time(t, context.host_hooks());
t = local_time(t, context.host_hooks().as_ref());
}

// 8. If ms is not present, let milli be msFromTime(t).
Expand All @@ -1313,7 +1320,7 @@ impl Date {

let u = if LOCAL {
// 10. Let u be TimeClip(UTC(date)).
time_clip(utc_t(date, context.host_hooks()))
time_clip(utc_t(date, context.host_hooks().as_ref()))
} else {
// 10. Let u be TimeClip(date).
time_clip(date)
Expand Down Expand Up @@ -1373,7 +1380,7 @@ impl Date {
let t = if t.is_nan() {
0.0
} else {
local_time(t, context.host_hooks())
local_time(t, context.host_hooks().as_ref())
};

// 6. Let yyyy be MakeFullYear(y).
Expand All @@ -1386,7 +1393,7 @@ impl Date {
let date = make_date(d, time_within_day(t));

// 9. Let u be TimeClip(UTC(date)).
let u = time_clip(utc_t(date, context.host_hooks()));
let u = time_clip(utc_t(date, context.host_hooks().as_ref()));

let mut date_mut = this
.as_object()
Expand Down Expand Up @@ -1475,7 +1482,7 @@ impl Date {
};

// 5. Let t be LocalTime(tv).
let t = local_time(tv, context.host_hooks());
let t = local_time(tv, context.host_hooks().as_ref());

// 6. Return DateString(t).
Ok(JsValue::from(date_string(t)))
Expand Down Expand Up @@ -1668,7 +1675,10 @@ impl Date {
.0;

// 4. Return ToDateString(tv).
Ok(JsValue::from(to_date_string_t(tv, context.host_hooks())))
Ok(JsValue::from(to_date_string_t(
tv,
context.host_hooks().as_ref(),
)))
}

/// [`Date.prototype.toTimeString()`][spec].
Expand Down Expand Up @@ -1701,12 +1711,12 @@ impl Date {
}

// 5. Let t be LocalTime(tv).
let t = local_time(tv, context.host_hooks());
let t = local_time(tv, context.host_hooks().as_ref());

// 6. Return the string-concatenation of TimeString(t) and TimeZoneString(tv).
Ok(JsValue::from(js_string!(
&time_string(t),
&time_zone_string(t, context.host_hooks())
&time_zone_string(t, context.host_hooks().as_ref())
)))
}

Expand Down
5 changes: 3 additions & 2 deletions core/engine/src/context/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use time::{OffsetDateTime, UtcOffset};
/// need to be redefined:
///
/// ```
/// use std::rc::Rc;
/// use boa_engine::{
/// context::{Context, ContextBuilder, HostHooks},
/// realm::Realm,
Expand All @@ -42,7 +43,7 @@ use time::{OffsetDateTime, UtcOffset};
/// }
/// }
///
/// let context = &mut ContextBuilder::new().host_hooks(&Hooks).build().unwrap();
/// let context = &mut ContextBuilder::new().host_hooks(Rc::new(Hooks)).build().unwrap();
/// let result = context.eval(Source::from_bytes(r#"eval("let a = 5")"#));
/// assert_eq!(
/// result.unwrap_err().to_string(),
Expand Down Expand Up @@ -175,7 +176,7 @@ pub trait HostHooks {
None
}

/// Gets the current UTC time of the host.
/// Gets the current UTC time of the host, in milliseconds since epoch.
///
/// Defaults to using [`OffsetDateTime::now_utc`] on all targets,
/// which can cause panics if the target doesn't support [`SystemTime::now`][time].
Expand Down
Loading
Loading