From 931080cd49a0006717e881a2296156c9a3b5dbd6 Mon Sep 17 00:00:00 2001 From: EmperorBale Date: Wed, 28 Jul 2021 17:12:30 -0700 Subject: [PATCH] avm2: Implement valueOf --- core/src/avm2/globals/date.rs | 11 ++++------- core/src/avm2/object/date_object.rs | 6 +++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/src/avm2/globals/date.rs b/core/src/avm2/globals/date.rs index 89e70a839..c0f05ccb6 100644 --- a/core/src/avm2/globals/date.rs +++ b/core/src/avm2/globals/date.rs @@ -304,18 +304,14 @@ pub fn class_init<'gc>( Ok(Value::Undefined) } -/// Implements `time` property's getter, and the `getTime` method. +/// Implements `time` property's getter, and the `getTime` method. This will also be used for `valueOf`. pub fn time<'gc>( - _activation: &mut Activation<'_, 'gc, '_>, + activation: &mut Activation<'_, 'gc, '_>, this: Option>, _args: &[Value<'gc>], ) -> Result, Error> { if let Some(this) = this.and_then(|this| this.as_date_object()) { - if let Some(date) = this.date_time() { - return Ok((date.timestamp_millis() as f64).into()); - } else { - return Ok(f64::NAN.into()); - } + return this.value_of(activation.context.gc_context); } Ok(Value::Undefined) @@ -998,6 +994,7 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc> ("setUTCFullYear", set_full_year_utc), ("getUTCDay", day_utc), ("getTimezoneOffset", timezone_offset), + ("valueOf", time), ]; write.define_public_builtin_instance_methods(mc, PUBLIC_INSTANCE_METHODS); diff --git a/core/src/avm2/object/date_object.rs b/core/src/avm2/object/date_object.rs index 3bdb1b896..cebb86f37 100644 --- a/core/src/avm2/object/date_object.rs +++ b/core/src/avm2/object/date_object.rs @@ -77,7 +77,11 @@ impl<'gc> TObject<'gc> for DateObject<'gc> { } fn value_of(&self, _mc: MutationContext<'gc, '_>) -> Result, Error> { - Ok(Value::Object(Object::from(*self))) + if let Some(date) = self.date_time() { + return Ok((date.timestamp_millis() as f64).into()); + } else { + return Ok(f64::NAN.into()); + } } fn as_date_object(&self) -> Option> {