avm2: Implement `Event.toString` (which matches it's valueof behavior)

This commit is contained in:
David Wendt 2020-12-09 18:50:10 -05:00 committed by Mike Welsh
parent dc32d7894c
commit 8a787e6153
4 changed files with 41 additions and 0 deletions

View File

@ -243,6 +243,19 @@ pub fn stop_immediate_propagation<'gc>(
Ok(Value::Undefined) Ok(Value::Undefined)
} }
/// Implements `toString`
pub fn to_string<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
return this.value_of(activation.context.gc_context);
}
Ok(Value::Undefined)
}
/// Construct `Event`'s class. /// Construct `Event`'s class.
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
let class = Class::new( 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"), QName::new(Namespace::public_namespace(), "stopImmediatePropagation"),
Method::from_builtin(stop_immediate_propagation), 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 class
} }

View File

@ -5,12 +5,26 @@
import flash.events.Event; import flash.events.Event;
trace("//e = new Event(\"test_event\");");
var 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.valueOf.call(e));
trace("//Object.prototype.toString.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); 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.valueOf.call(e));
trace("//Object.prototype.toString.call(e)");
trace(Object.prototype.toString.call(e)); trace(Object.prototype.toString.call(e));

View File

@ -1,4 +1,14 @@
//e = new Event("test_event");
//e.toString()
[Event type="test_event" bubbles=false cancelable=false eventPhase=2] [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] [object Event]
//e = new Event("test_event", true, true);
//e.toString()
[Event type="test_event" bubbles=true cancelable=true eventPhase=2] [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] [object Event]