avm2: Fix toString for primitive objects

This commit is contained in:
Tom Schuster 2024-03-12 18:27:15 +01:00 committed by Lord-McSweeney
parent 787b6f5dfb
commit 3f17523879
4 changed files with 19 additions and 11 deletions

View File

@ -2,6 +2,7 @@
use crate::avm2::activation::Activation; use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes}; use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::make_error_1004;
use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject}; use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject};
use crate::avm2::value::Value; use crate::avm2::value::Value;
@ -95,9 +96,9 @@ pub fn call_handler<'gc>(
.into()) .into())
} }
/// Implements `Boolean.toString` /// Implements `Boolean.prototype.toString`
fn to_string<'gc>( fn to_string<'gc>(
_activation: &mut Activation<'_, 'gc>, activation: &mut Activation<'_, 'gc>,
this: Object<'gc>, this: Object<'gc>,
_args: &[Value<'gc>], _args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> { ) -> Result<Value<'gc>, 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` /// Implements `Boolean.valueOf`

View File

@ -311,7 +311,7 @@ pub fn print_with_radix<'gc>(
)) ))
} }
/// Implements `Number.toString` /// Implements `Number.prototype.toString`
fn to_string<'gc>( fn to_string<'gc>(
activation: &mut Activation<'_, 'gc>, activation: &mut Activation<'_, 'gc>,
this: Object<'gc>, this: Object<'gc>,

View File

@ -2,6 +2,7 @@
use crate::avm2::activation::Activation; use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes}; use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::make_error_1004;
use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject}; use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject};
use crate::avm2::regexp::RegExpFlags; use crate::avm2::regexp::RegExpFlags;
@ -619,9 +620,9 @@ fn to_lower_case<'gc>(
.into()) .into())
} }
/// Implements `String.toString` /// Implements `String.prototype.toString`
fn to_string<'gc>( fn to_string<'gc>(
_activation: &mut Activation<'_, 'gc>, activation: &mut Activation<'_, 'gc>,
this: Object<'gc>, this: Object<'gc>,
_args: &[Value<'gc>], _args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> { ) -> Result<Value<'gc>, 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` /// Implements `String.valueOf`

View File

@ -115,10 +115,6 @@ impl<'gc> TObject<'gc> for PrimitiveObject<'gc> {
self.0.as_ptr() as *const ObjectPtr self.0.as_ptr() as *const ObjectPtr
} }
fn to_string(&self, _activation: &mut Activation<'_, 'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(self.0.read().primitive)
}
fn to_locale_string( fn to_locale_string(
&self, &self,
activation: &mut Activation<'_, 'gc>, activation: &mut Activation<'_, 'gc>,