Skip to content

Commit

Permalink
Rebase with some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lameferret committed Oct 27, 2022
1 parent ccedb40 commit 2fc1d97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
106 changes: 61 additions & 45 deletions boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
string::utf16,
symbol::WellKnownSymbols,
value::{JsValue, PreferredType},
Context, JsResult,
Context, JsError, JsResult,
};
use boa_profiler::Profiler;
use chrono::{prelude::*, Duration, LocalResult};
Expand Down Expand Up @@ -48,7 +48,7 @@ fn ignore_ambiguity<T>(result: LocalResult<T>) -> Option<T> {
}
}

#[derive(Debug, Finalize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Date(Option<NaiveDateTime>);

impl Display for Date {
Expand Down Expand Up @@ -541,8 +541,12 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.getdate
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate
pub fn get_date(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
pub fn get_date(
this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.day()))
} else {
Expand All @@ -561,8 +565,8 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.getday
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
pub fn get_day(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
pub fn get_day(this: &JsValue, _args: &[JsValue], _context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.weekday().num_days_from_sunday()))
} else {
Expand All @@ -583,9 +587,9 @@ impl Date {
pub fn get_full_year(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.year()))
} else {
Expand All @@ -606,9 +610,9 @@ impl Date {
pub fn get_hours(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.hour()))
} else {
Expand All @@ -629,9 +633,9 @@ impl Date {
pub fn get_milliseconds(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.timestamp_subsec_millis()))
} else {
Expand All @@ -652,9 +656,9 @@ impl Date {
pub fn get_minutes(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.minute()))
} else {
Expand All @@ -676,9 +680,9 @@ impl Date {
pub fn get_month(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.month0()))
} else {
Expand All @@ -699,9 +703,9 @@ impl Date {
pub fn get_seconds(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
Ok(JsValue::new(local.second()))
} else {
Expand All @@ -722,7 +726,7 @@ impl Date {
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.getyear
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear
pub fn get_year(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let local = Local::now().timezone().from_utc_datetime(&t);
let year = JsValue::Integer(local.year());
year.sub(&JsValue::from(1900), context)
Expand All @@ -741,8 +745,12 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.gettime
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
pub fn get_time(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
pub fn get_time(
this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.timestamp_millis()))
} else {
Ok(JsValue::nan())
Expand Down Expand Up @@ -799,9 +807,9 @@ impl Date {
pub fn get_utc_date(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.day()))
} else {
Ok(JsValue::nan())
Expand All @@ -822,9 +830,9 @@ impl Date {
pub fn get_utc_day(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.weekday().num_days_from_sunday()))
} else {
Ok(JsValue::nan())
Expand All @@ -844,9 +852,9 @@ impl Date {
pub fn get_utc_full_year(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.year()))
} else {
Ok(JsValue::nan())
Expand All @@ -866,9 +874,9 @@ impl Date {
pub fn get_utc_hours(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.hour()))
} else {
Ok(JsValue::nan())
Expand All @@ -888,9 +896,9 @@ impl Date {
pub fn get_utc_milliseconds(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.timestamp_subsec_millis()))
} else {
Ok(JsValue::nan())
Expand All @@ -910,9 +918,9 @@ impl Date {
pub fn get_utc_minutes(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.minute()))
} else {
Ok(JsValue::nan())
Expand All @@ -933,9 +941,9 @@ impl Date {
pub fn get_utc_month(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.month0()))
} else {
Ok(JsValue::nan())
Expand All @@ -955,9 +963,9 @@ impl Date {
pub fn get_utc_seconds(
this: &JsValue,
_args: &[JsValue],
context: &mut Context,
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.second()))
} else {
Ok(JsValue::nan())
Expand Down Expand Up @@ -1782,7 +1790,7 @@ impl Date {
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
Err(JsValue::new("Function Unimplemented]"))
Err(JsError::from_opaque(JsValue::new("Function Unimplemented")))
}

/// `Date.prototype.toGMTString()`
Expand Down Expand Up @@ -1904,7 +1912,9 @@ impl Date {
_: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
Err(JsValue::new("Function Unimplemented]"))
Err(JsError::from_opaque(JsValue::new(
"Function Unimplemented]",
)))
}

/// `Date.prototype.toTimeString()`
Expand Down Expand Up @@ -1955,7 +1965,9 @@ impl Date {
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
Err(JsValue::new("Function Unimplemented]"))
Err(JsError::from_opaque(JsValue::new(
"Function Unimplemented]",
)))
}

/// `Date.prototype.toUTCString()`
Expand All @@ -1973,11 +1985,11 @@ impl Date {
_args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
if let Some(t) = this_time_value(this)?.0 {
let utc_string = t.format("%a, %d %b %Y %H:%M:%S GMT").to_string();
Ok(JsValue::new(utc_string))
} else {
context.throw_range_error("Invalid time value")
Ok(context.construct_error("Invalid time value"))
}
}

Expand All @@ -1991,8 +2003,12 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.valueof
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf
pub fn value_of(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this, context)?.0 {
pub fn value_of(
this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
if let Some(t) = this_time_value(this)?.0 {
Ok(JsValue::new(t.timestamp_millis()))
} else {
Ok(JsValue::nan())
Expand Down Expand Up @@ -2108,7 +2124,7 @@ impl Date {
let prototype = context.intrinsics().constructors().date().prototype();
let date_time = DateTime::parse_from_rfc3339(&value.to_string(context).expect(
"Utility: Date's string conversion used in limited(internal) area shouldn't fail",
))
).to_std_string().expect("Utility: Conversion of confirmed JsString to std string"))
.expect("Utility: Parse RFC3339 is used in limited(internal) areas shouldn't fail");
let internal_date = Date(Some(date_time.naive_local()));
JsObject::from_proto_and_data(prototype, ObjectData::date(internal_date))
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/object/builtins/jsdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::{
/// Create a `JsDate` object and set date to December 4 1995
///
/// ```
/// use boa_engine::{object::JsDate, Context, JsValue};
/// use boa_engine::{object::builtins::JsDate, Context, JsValue, JsResult};
///
/// fn main() -> Result<(), JsValue> {
/// fn main() -> JsResult<()> {
/// // JS mutable Context
/// let context = &mut Context::default();
///
Expand Down
2 changes: 2 additions & 0 deletions boa_engine/src/object/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod jsarray;
mod jsarraybuffer;
mod jsdate;
mod jsdataview;
mod jsfunction;
mod jsmap;
Expand All @@ -14,6 +15,7 @@ mod jstypedarray;

pub use jsarray::*;
pub use jsarraybuffer::*;
pub use jsdate::*;
pub use jsdataview::*;
pub use jsfunction::*;
pub use jsmap::*;
Expand Down
4 changes: 2 additions & 2 deletions boa_examples/src/bin/jsdate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use boa_engine::{object::JsDate, Context, JsValue};
use boa_engine::{object::builtins::JsDate, Context, JsValue, JsResult};

fn main() -> Result<(), JsValue> {
fn main() -> JsResult<()> {
let context = &mut Context::default();

let date = JsDate::new(context);
Expand Down

0 comments on commit 2fc1d97

Please sign in to comment.