From 3f175238791c3225cc23d1248557b4631cc36d03 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Tue, 12 Mar 2024 18:27:15 +0100 Subject: [PATCH] avm2: Fix toString for primitive objects --- core/src/avm2/globals/boolean.rs | 12 +++++++++--- core/src/avm2/globals/number.rs | 2 +- core/src/avm2/globals/string.rs | 12 +++++++++--- core/src/avm2/object/primitive_object.rs | 4 ---- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/src/avm2/globals/boolean.rs b/core/src/avm2/globals/boolean.rs index 3e8f52551..80d34da90 100644 --- a/core/src/avm2/globals/boolean.rs +++ b/core/src/avm2/globals/boolean.rs @@ -2,6 +2,7 @@ use crate::avm2::activation::Activation; use crate::avm2::class::{Class, ClassAttributes}; +use crate::avm2::error::make_error_1004; use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject}; use crate::avm2::value::Value; @@ -95,9 +96,9 @@ pub fn call_handler<'gc>( .into()) } -/// Implements `Boolean.toString` +/// Implements `Boolean.prototype.toString` fn to_string<'gc>( - _activation: &mut Activation<'_, 'gc>, + activation: &mut Activation<'_, 'gc>, this: Object<'gc>, _args: &[Value<'gc>], ) -> Result, Error<'gc>> { @@ -109,7 +110,12 @@ fn to_string<'gc>( }; } - Ok("false".into()) + let boolean_proto = activation.avm2().classes().boolean.prototype(); + if Object::ptr_eq(boolean_proto, this) { + return Ok("false".into()); + } + + Err(make_error_1004(activation, "Boolean.prototype.toString")) } /// Implements `Boolean.valueOf` diff --git a/core/src/avm2/globals/number.rs b/core/src/avm2/globals/number.rs index 6ec86cf74..6d11a1aca 100644 --- a/core/src/avm2/globals/number.rs +++ b/core/src/avm2/globals/number.rs @@ -311,7 +311,7 @@ pub fn print_with_radix<'gc>( )) } -/// Implements `Number.toString` +/// Implements `Number.prototype.toString` fn to_string<'gc>( activation: &mut Activation<'_, 'gc>, this: Object<'gc>, diff --git a/core/src/avm2/globals/string.rs b/core/src/avm2/globals/string.rs index 6a14a1235..46605c318 100644 --- a/core/src/avm2/globals/string.rs +++ b/core/src/avm2/globals/string.rs @@ -2,6 +2,7 @@ use crate::avm2::activation::Activation; use crate::avm2::class::{Class, ClassAttributes}; +use crate::avm2::error::make_error_1004; use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject}; use crate::avm2::regexp::RegExpFlags; @@ -619,9 +620,9 @@ fn to_lower_case<'gc>( .into()) } -/// Implements `String.toString` +/// Implements `String.prototype.toString` fn to_string<'gc>( - _activation: &mut Activation<'_, 'gc>, + activation: &mut Activation<'_, 'gc>, this: Object<'gc>, _args: &[Value<'gc>], ) -> Result, Error<'gc>> { @@ -631,7 +632,12 @@ fn to_string<'gc>( } } - Ok("".into()) + let string_proto = activation.avm2().classes().string.prototype(); + if Object::ptr_eq(string_proto, this) { + return Ok("".into()); + } + + Err(make_error_1004(activation, "String.prototype.toString")) } /// Implements `String.valueOf` diff --git a/core/src/avm2/object/primitive_object.rs b/core/src/avm2/object/primitive_object.rs index b92fd8c15..a9f95d4d2 100644 --- a/core/src/avm2/object/primitive_object.rs +++ b/core/src/avm2/object/primitive_object.rs @@ -115,10 +115,6 @@ impl<'gc> TObject<'gc> for PrimitiveObject<'gc> { self.0.as_ptr() as *const ObjectPtr } - fn to_string(&self, _activation: &mut Activation<'_, 'gc>) -> Result, Error<'gc>> { - Ok(self.0.read().primitive) - } - fn to_locale_string( &self, activation: &mut Activation<'_, 'gc>,