Skip to content

Commit

Permalink
avm2: Get event.target to determine if clone() is necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian17 committed Jan 3, 2025
1 parent 75dba08 commit 693c31a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/src/avm2/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct Event<'gc> {
event_type: AvmString<'gc>,

/// Whether is event has been dispatched before.
// TODO: this field may be redundant now?
dispatched: bool,
}

Expand Down Expand Up @@ -459,9 +460,11 @@ pub fn dispatch_event<'gc>(
parent = parent_dobj.parent();
}

let dispatched = event.as_event().unwrap().dispatched;
let has_target = event
.get_public_property("target", activation)?
.coerce_to_boolean();

let event = if dispatched {
let event = if has_target {
event
.call_public_property("clone", &[], activation)?
.as_object()
Expand Down
63 changes: 63 additions & 0 deletions tests/tests/swfs/avm2/event_target_getter/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// compiled with mxmlc

import flash.events.Event;
import flash.display.DisplayObject;

package {
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.Event;

public class Test extends MovieClip {
public function Test(){
var d = new MovieClip();
trace("// testing trivial getter")
var e1 = new E1("e1", d);
d.dispatchEvent(e1);

trace("// testing 1-cycle-delayed getter")
var e2 = new E2("e2", d);
d.dispatchEvent(e2);
}

}
}


class E1 extends Event {
private var dobj: DisplayObject;
public function E1(type, dobj){
super(type, false, false);
this.dobj = dobj;
}
override public function get target(): Object {
trace("in get target()");
return this.dobj;
}
override public function clone(): Event {
trace("in clone()");
return new E1(this.type, this.dobj);
}
}

class E2 extends Event {
private var ready: Boolean;
private var dobj: DisplayObject;
public function E2(type, d){
super(type, false, false);
this.ready = false;
this.dobj = dobj;
}
override public function get target(): Object {
trace("in get target()");
if (this.ready) {
return this.dobj;
}
this.ready = true;
return null;
}
override public function clone(): Event {
trace("in clone()");
return new E2(this.type, this.dobj);
}
}
5 changes: 5 additions & 0 deletions tests/tests/swfs/avm2/event_target_getter/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// testing trivial getter
in get target()
in clone()
// testing 1-cycle-delayed getter
in get target()
Binary file added tests/tests/swfs/avm2/event_target_getter/test.swf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/tests/swfs/avm2/event_target_getter/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The test observes event dispatching getting `.target` to check if even is being re-dispatched.
num_frames = 1

0 comments on commit 693c31a

Please sign in to comment.