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

avm2: Fix Object.prototype.toString applied to Vector #15562

Merged
merged 2 commits into from
Mar 13, 2024
Merged
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
6 changes: 1 addition & 5 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,11 +977,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());

Ok(AvmString::new_utf8(
activation.context.gc_context,
format!("[object {class_name}]"),
)
.into())
Ok(AvmString::new_utf8(activation.gc(), format!("[object {class_name}]")).into())
}

/// Implement the result of calling `Object.prototype.toLocaleString` on this
Expand Down
16 changes: 14 additions & 2 deletions core/src/avm2/object/vector_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::string::AvmString;
use crate::avm2::value::Value;
use crate::avm2::vector::VectorStorage;
use crate::avm2::Error;
Expand Down Expand Up @@ -250,8 +251,19 @@ impl<'gc> TObject<'gc> for VectorObject<'gc> {
}
}

fn to_string(&self, _activation: &mut Activation<'_, 'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
// Implements the special `Object.prototype.toString` behavior for Vector.
fn to_string(&self, activation: &mut Activation<'_, 'gc>) -> Result<Value<'gc>, Error<'gc>> {
let inner_name = if let Some(class_object) = self.0.read().vector.value_type() {
class_object
.inner_class_definition()
.read()
.name()
.to_qualified_name(activation.gc())
} else {
AvmString::new_utf8(activation.gc(), "*")
};

Ok(AvmString::new_utf8(activation.gc(), format!("[object Vector.<{inner_name}>]")).into())
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Expand Down
21 changes: 21 additions & 0 deletions tests/tests/swfs/avm2/vector_object_toString/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package {
import flash.display.Sprite;
public class Test extends Sprite {
public function Test() {
trace("// Object.prototype.toString.call(new Vector.<*>())");
trace(Object.prototype.toString.call(new Vector.<*>()));

trace("// Object.prototype.toString.call(new Vector.<Object>())");
trace(Object.prototype.toString.call(new Vector.<Object>()));

trace("// Object.prototype.toString.call(new Vector.<String>())");
trace(Object.prototype.toString.call(new Vector.<String>()));

trace("// Object.prototype.toString.call(new Vector.<XML>())");
trace(Object.prototype.toString.call(new Vector.<XML>()));

trace("// Object.prototype.toString.call(new Vector.<Sprite>())");
trace(Object.prototype.toString.call(new Vector.<Sprite>()));
}
}
}
10 changes: 10 additions & 0 deletions tests/tests/swfs/avm2/vector_object_toString/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Object.prototype.toString.call(new Vector.<*>())
[object Vector.<*>]
// Object.prototype.toString.call(new Vector.<Object>())
[object Vector.<Object>]
// Object.prototype.toString.call(new Vector.<String>())
[object Vector.<String>]
// Object.prototype.toString.call(new Vector.<XML>())
[object Vector.<XML>]
// Object.prototype.toString.call(new Vector.<Sprite>())
[object Vector.<flash.display::Sprite>]
Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/vector_object_toString/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_frames = 1