From 8a787e61538a80afefddaaf4049fb39b65b2bc30 Mon Sep 17 00:00:00 2001 From: David Wendt Date: Wed, 9 Dec 2020 18:50:10 -0500 Subject: [PATCH] avm2: Implement `Event.toString` (which matches it's valueof behavior) --- core/src/avm2/globals/flash/events/event.rs | 17 +++++++++++++++++ .../swfs/avm2/event_valueof_tostring/Test.as | 14 ++++++++++++++ .../avm2/event_valueof_tostring/output.txt | 10 ++++++++++ .../swfs/avm2/event_valueof_tostring/test.swf | Bin 721 -> 837 bytes 4 files changed, 41 insertions(+) diff --git a/core/src/avm2/globals/flash/events/event.rs b/core/src/avm2/globals/flash/events/event.rs index fbadff6bc..0a0471b66 100644 --- a/core/src/avm2/globals/flash/events/event.rs +++ b/core/src/avm2/globals/flash/events/event.rs @@ -243,6 +243,19 @@ pub fn stop_immediate_propagation<'gc>( Ok(Value::Undefined) } +/// Implements `toString` +pub fn to_string<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(this) = this { + return this.value_of(activation.context.gc_context); + } + + Ok(Value::Undefined) +} + /// Construct `Event`'s class. pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { let class = Class::new( @@ -303,6 +316,10 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc> QName::new(Namespace::public_namespace(), "stopImmediatePropagation"), Method::from_builtin(stop_immediate_propagation), )); + write.define_instance_trait(Trait::from_method( + QName::new(Namespace::public_namespace(), "toString"), + Method::from_builtin(to_string), + )); class } diff --git a/core/tests/swfs/avm2/event_valueof_tostring/Test.as b/core/tests/swfs/avm2/event_valueof_tostring/Test.as index 5d0b67b14..02a6b801f 100644 --- a/core/tests/swfs/avm2/event_valueof_tostring/Test.as +++ b/core/tests/swfs/avm2/event_valueof_tostring/Test.as @@ -5,12 +5,26 @@ import flash.events.Event; +trace("//e = new Event(\"test_event\");"); var e = new Event("test_event"); +trace("//e.toString()"); +trace(e.toString()); + +trace("//Object.prototype.valueOf.call(e)"); trace(Object.prototype.valueOf.call(e)); + +trace("//Object.prototype.toString.call(e)"); trace(Object.prototype.toString.call(e)); +trace("//e = new Event(\"test_event\", true, true);"); e = new Event("test_event", true, true); +trace("//e.toString()"); +trace(e.toString()); + +trace("//Object.prototype.valueOf.call(e)"); trace(Object.prototype.valueOf.call(e)); + +trace("//Object.prototype.toString.call(e)"); trace(Object.prototype.toString.call(e)); \ No newline at end of file diff --git a/core/tests/swfs/avm2/event_valueof_tostring/output.txt b/core/tests/swfs/avm2/event_valueof_tostring/output.txt index fb268c343..bb1808b2a 100644 --- a/core/tests/swfs/avm2/event_valueof_tostring/output.txt +++ b/core/tests/swfs/avm2/event_valueof_tostring/output.txt @@ -1,4 +1,14 @@ +//e = new Event("test_event"); +//e.toString() [Event type="test_event" bubbles=false cancelable=false eventPhase=2] +//Object.prototype.valueOf.call(e) +[Event type="test_event" bubbles=false cancelable=false eventPhase=2] +//Object.prototype.toString.call(e) [object Event] +//e = new Event("test_event", true, true); +//e.toString() [Event type="test_event" bubbles=true cancelable=true eventPhase=2] +//Object.prototype.valueOf.call(e) +[Event type="test_event" bubbles=true cancelable=true eventPhase=2] +//Object.prototype.toString.call(e) [object Event] diff --git a/core/tests/swfs/avm2/event_valueof_tostring/test.swf b/core/tests/swfs/avm2/event_valueof_tostring/test.swf index 5113bb582c9d85bc0df721222e42a6e8abc22447..92c3d13da5b1d624651c62ad8b2eb125c65f3405 100644 GIT binary patch literal 837 zcmV-L1G@Y}S5qsA1pokeoSjo$Zrer>-d+AINy(CBTe9L*wqh%;)fgn@6h&d7XcIZE z?I!Sp5+DU6ShTc^XwalUu9W7rC+SUc1@)nCJ%oG$cUF{K1$A1W1i3r=eKYe7XUJcW za)D6xH-w599pyBHP=nmw-acPPXlr@+pi-;6dr`4#?Agi7@ax9UR|i)=z3IPs$xfcv z(8qiV{4r{FS-{FQgg&n6iU8nRMyA-lMEYwMb4x6bIV=~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