From dca473fc2ae683d8a06eb5c4e175c373d8623fba Mon Sep 17 00:00:00 2001 From: David Wendt Date: Sun, 5 Sep 2021 17:59:15 -0400 Subject: [PATCH] avm2: Impl `BitmapData.width` and `BitmapData.height` --- .../avm2/globals/flash/display/bitmapdata.rs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/core/src/avm2/globals/flash/display/bitmapdata.rs b/core/src/avm2/globals/flash/display/bitmapdata.rs index 154a21442..08c648d8f 100644 --- a/core/src/avm2/globals/flash/display/bitmapdata.rs +++ b/core/src/avm2/globals/flash/display/bitmapdata.rs @@ -2,7 +2,7 @@ use crate::avm2::activation::Activation; use crate::avm2::class::{Class, ClassAttributes}; -use crate::avm2::method::Method; +use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::names::{Namespace, QName}; use crate::avm2::object::{bitmapdata_allocator, Object, TObject}; use crate::avm2::value::Value; @@ -121,6 +121,32 @@ pub fn class_init<'gc>( Ok(Value::Undefined) } +/// Implements BitmapData.width`'s getter. +pub fn width<'gc>( + _activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data()) { + return Ok((bitmap_data.read().width() as i32).into()); + } + + Ok(Value::Undefined) +} + +/// Implements BitmapData.height`'s getter. +pub fn height<'gc>( + _activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data()) { + return Ok((bitmap_data.read().height() as i32).into()); + } + + Ok(Value::Undefined) +} + /// Construct `BitmapData`'s class. pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { let class = Class::new( @@ -136,5 +162,12 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc> write.set_attributes(ClassAttributes::SEALED); write.set_instance_allocator(bitmapdata_allocator); + const PUBLIC_INSTANCE_PROPERTIES: &[( + &str, + Option, + Option, + )] = &[("width", Some(width), None), ("height", Some(height), None)]; + write.define_public_builtin_instance_properties(mc, PUBLIC_INSTANCE_PROPERTIES); + class }