avm2: Implement `FrameLabel`.

This commit is contained in:
David Wendt 2020-09-16 21:32:00 -04:00 committed by Mike Welsh
parent 86b07c0007
commit 77a86aef9b
8 changed files with 142 additions and 0 deletions

View File

@ -377,6 +377,12 @@ pub fn load_player_globals<'gc>(activation: &mut Activation<'_, 'gc, '_>) -> Res
flash::display::movieclip::create_class(activation.context.gc_context),
implicit_deriver,
)?;
class(
activation,
gs,
flash::display::framelabel::create_class(activation.context.gc_context),
implicit_deriver,
)?;
Ok(())
}

View File

@ -2,6 +2,7 @@
pub mod displayobject;
pub mod displayobjectcontainer;
pub mod framelabel;
pub mod interactiveobject;
pub mod movieclip;
pub mod sprite;

View File

@ -0,0 +1,114 @@
//! `flash.display.FrameLabel` impl
use crate::avm2::activation::Activation;
use crate::avm2::class::Class;
use crate::avm2::method::Method;
use crate::avm2::names::{Namespace, QName};
use crate::avm2::object::{Object, TObject};
use crate::avm2::traits::Trait;
use crate::avm2::value::Value;
use crate::avm2::Error;
use gc_arena::{GcCell, MutationContext};
/// Implements `flash.display.FrameLabel`'s instance constructor.
pub fn instance_init<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
let name = args
.get(0)
.cloned()
.unwrap_or(Value::Undefined)
.coerce_to_string(activation)?;
let frame = args
.get(1)
.cloned()
.unwrap_or(Value::Undefined)
.coerce_to_i32(activation)?;
if let Some(mut this) = this {
this.set_property(
this,
&QName::new(Namespace::Private("ruffle".into()), "name"),
name.into(),
activation,
)?;
this.set_property(
this,
&QName::new(Namespace::Private("ruffle".into()), "frame"),
frame.into(),
activation,
)?;
}
Ok(Value::Undefined)
}
/// Implements `flash.display.FrameLabel`'s class constructor.
pub fn class_init<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
_this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
Ok(Value::Undefined)
}
/// Implements `FrameLabel.name`.
pub fn name<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(mut this) = this {
return this.get_property(
this,
&QName::new(Namespace::Private("ruffle".into()), "name"),
activation,
);
}
Ok(Value::Undefined)
}
/// Implements `FrameLabel.frame`.
pub fn frame<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(mut this) = this {
return this.get_property(
this,
&QName::new(Namespace::Private("ruffle".into()), "frame"),
activation,
);
}
Ok(Value::Undefined)
}
/// Construct `FrameLabel`'s class.
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
let class = Class::new(
QName::new(Namespace::package("flash.display"), "FrameLabel"),
Some(QName::new(Namespace::package("flash.events"), "EventDispatcher").into()),
Method::from_builtin(instance_init),
Method::from_builtin(class_init),
mc,
);
let mut write = class.write(mc);
write.define_instance_trait(Trait::from_getter(
QName::new(Namespace::public_namespace(), "name"),
Method::from_builtin(name),
));
write.define_instance_trait(Trait::from_getter(
QName::new(Namespace::public_namespace(), "frame"),
Method::from_builtin(frame),
));
class
}

View File

@ -393,6 +393,7 @@ swf_tests! {
(as3_movieclip_next_frame, "avm2/movieclip_next_frame", 5),
(as3_movieclip_prev_scene, "avm2/movieclip_prev_scene", 5),
(as3_movieclip_next_scene, "avm2/movieclip_next_scene", 5),
(as3_framelabel_constr, "avm2/framelabel_constr", 5),
}
// TODO: These tests have some inaccuracies currently, so we use approx_eq to test that numeric values are close enough.

View File

@ -0,0 +1,15 @@
package {
public class Test {
}
}
import flash.display.FrameLabel;
trace("//var val = new FrameLabel(\"test\", 123);");
var val = new FrameLabel("test", 123);
trace("//val.frame");
trace(val.frame);
trace("//val.name");
trace(val.name);

View File

@ -0,0 +1,5 @@
//var val = new FrameLabel("test", 123);
//val.frame
123
//val.name
test

Binary file not shown.

Binary file not shown.