From 53cdba96d3a741792dd0e17fa18e95f85e3ad226 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 22 Jun 2022 13:15:21 -0500 Subject: [PATCH] avm2: Convert flash.geom.Matrix to ActionScript I've kept the rust `flash.geom` module, even though it's now empty, since we'll need to add things like `flash.geom.Transform` native methods in the future. --- core/src/avm2/globals.rs | 1 - core/src/avm2/globals/flash/geom.rs | 2 - core/src/avm2/globals/flash/geom/Matrix.as | 33 ++++ core/src/avm2/globals/flash/geom/matrix.rs | 191 --------------------- core/src/avm2/globals/globals.as | 1 + 5 files changed, 34 insertions(+), 194 deletions(-) create mode 100644 core/src/avm2/globals/flash/geom/Matrix.as delete mode 100644 core/src/avm2/globals/flash/geom/matrix.rs diff --git a/core/src/avm2/globals.rs b/core/src/avm2/globals.rs index 98df35ee4ea0..aed4fa5c9749 100644 --- a/core/src/avm2/globals.rs +++ b/core/src/avm2/globals.rs @@ -819,7 +819,6 @@ pub fn load_player_globals<'gc>( )?; // package `flash.geom` - class(activation, flash::geom::matrix::create_class(mc), script)?; // package `flash.media` avm2_system_class!( diff --git a/core/src/avm2/globals/flash/geom.rs b/core/src/avm2/globals/flash/geom.rs index 55d05bf87419..c99886b400fb 100644 --- a/core/src/avm2/globals/flash/geom.rs +++ b/core/src/avm2/globals/flash/geom.rs @@ -1,3 +1 @@ //! `flash.geom` namespace - -pub mod matrix; diff --git a/core/src/avm2/globals/flash/geom/Matrix.as b/core/src/avm2/globals/flash/geom/Matrix.as new file mode 100644 index 000000000000..eaeb61c02f8b --- /dev/null +++ b/core/src/avm2/globals/flash/geom/Matrix.as @@ -0,0 +1,33 @@ +package flash.geom { + public class Matrix { + public var a:Number; + public var b:Number; + public var c:Number; + public var d:Number; + public var tx:Number; + public var ty:Number; + + public function Matrix(a:Number = 1, b:Number = 0, c:Number = 0, d:Number = 1, tx:Number = 0, ty:Number = 0) { + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.tx = tx; + this.ty = ty; + } + + public function identity():void { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + } + + public function scale(sx:Number, sy:Number):void { + this.a *= sx; + this.d *= sy; + } + } +} diff --git a/core/src/avm2/globals/flash/geom/matrix.rs b/core/src/avm2/globals/flash/geom/matrix.rs deleted file mode 100644 index 2c5811c32121..000000000000 --- a/core/src/avm2/globals/flash/geom/matrix.rs +++ /dev/null @@ -1,191 +0,0 @@ -//! `flash.geom.Matrix` builtin/prototype - -use crate::avm2::class::Class; -use crate::avm2::method::{Method, NativeMethodImpl}; -use crate::avm2::{Activation, Error, Namespace, Object, QName, TObject, Value}; -use gc_arena::{GcCell, MutationContext}; - -#[allow(clippy::too_many_arguments)] -fn set_values<'gc>( - this: &mut Object<'gc>, - activation: &mut Activation<'_, 'gc, '_>, - a: f64, - b: f64, - c: f64, - d: f64, - tx: f64, - ty: f64, -) -> Result<(), Error> { - this.set_property( - &QName::new(Namespace::public(), "a").into(), - a.into(), - activation, - )?; - this.set_property( - &QName::new(Namespace::public(), "b").into(), - b.into(), - activation, - )?; - this.set_property( - &QName::new(Namespace::public(), "c").into(), - c.into(), - activation, - )?; - this.set_property( - &QName::new(Namespace::public(), "d").into(), - d.into(), - activation, - )?; - this.set_property( - &QName::new(Namespace::public(), "tx").into(), - tx.into(), - activation, - )?; - this.set_property( - &QName::new(Namespace::public(), "ty").into(), - ty.into(), - activation, - )?; - - Ok(()) -} - -/// Implements `flash.geom.Matrix`'s instance constructor. -pub fn instance_init<'gc>( - activation: &mut Activation<'_, 'gc, '_>, - this: Option>, - args: &[Value<'gc>], -) -> Result, Error> { - if let Some(mut this) = this { - let a = args - .get(0) - .unwrap_or(&1f64.into()) - .coerce_to_number(activation)?; - - let b = args - .get(1) - .unwrap_or(&0f64.into()) - .coerce_to_number(activation)?; - - let c = args - .get(2) - .unwrap_or(&0f64.into()) - .coerce_to_number(activation)?; - - let d = args - .get(3) - .unwrap_or(&1f64.into()) - .coerce_to_number(activation)?; - - let tx = args - .get(4) - .unwrap_or(&0f64.into()) - .coerce_to_number(activation)?; - - let ty = args - .get(5) - .unwrap_or(&0f64.into()) - .coerce_to_number(activation)?; - - set_values(&mut this, activation, a, b, c, d, tx, ty)?; - } - Ok(Value::Undefined) -} - -/// Implements `flash.geom.Matrix`'s class initializer. -pub fn class_init<'gc>( - _activation: &mut Activation<'_, 'gc, '_>, - _this: Option>, - _args: &[Value<'gc>], -) -> Result, Error> { - Ok(Value::Undefined) -} - -/// Implements `identity' -pub fn identity<'gc>( - activation: &mut Activation<'_, 'gc, '_>, - this: Option>, - _args: &[Value<'gc>], -) -> Result, Error> { - if let Some(mut this) = this { - set_values(&mut this, activation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0)?; - } - Ok(Value::Undefined) -} - -/// Implements `scale' -pub fn scale<'gc>( - activation: &mut Activation<'_, 'gc, '_>, - this: Option>, - args: &[Value<'gc>], -) -> Result, Error> { - if let Some(mut this) = this { - let sx = if let Some(sx) = args.get(0) { - sx.coerce_to_number(activation)? - } else { - return Err(format!( - "Expected 2 arguments in flash.geom::Matrix/scale(), got {}", - args.len() - ) - .into()); - }; - let sy = if let Some(sy) = args.get(1) { - sy.coerce_to_number(activation)? - } else { - return Err(format!( - "Expected 2 arguments in flash.geom::Matrix/scale(), got {}", - args.len() - ) - .into()); - }; - - let a = this - .get_property(&QName::new(Namespace::public(), "a").into(), activation)? - .coerce_to_number(activation)?; - let d = this - .get_property(&QName::new(Namespace::public(), "d").into(), activation)? - .coerce_to_number(activation)?; - - this.set_property( - &QName::new(Namespace::public(), "a").into(), - (sx * a).into(), - activation, - )?; - - this.set_property( - &QName::new(Namespace::public(), "d").into(), - (sy * d).into(), - activation, - )?; - } - Ok(Value::Undefined) -} - -/// Construct `Matrix`'s class. -pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { - let class = Class::new( - QName::new(Namespace::package("flash.geom"), "Matrix"), - Some(QName::new(Namespace::public(), "Object").into()), - Method::from_builtin(instance_init, "", mc), - Method::from_builtin(class_init, "", mc), - mc, - ); - - let mut write = class.write(mc); - - const PUBLIC_INSTANCE_NUMBER_SLOTS: &[(&str, Option)] = &[ - ("a", None), - ("b", None), - ("c", None), - ("d", None), - ("tx", None), - ("ty", None), - ]; - write.define_public_slot_number_instance_traits(PUBLIC_INSTANCE_NUMBER_SLOTS); - - const PUBLIC_INSTANCE_METHODS: &[(&str, NativeMethodImpl)] = - &[("identity", identity), ("scale", scale)]; - write.define_public_builtin_instance_methods(mc, PUBLIC_INSTANCE_METHODS); - - class -} diff --git a/core/src/avm2/globals/globals.as b/core/src/avm2/globals/globals.as index 233c0f2b030c..351341d40775 100644 --- a/core/src/avm2/globals/globals.as +++ b/core/src/avm2/globals/globals.as @@ -30,6 +30,7 @@ include "flash/display/SWFVersion.as" include "flash/display/TriangleCulling.as" include "flash/geom/ColorTransform.as" include "flash/geom/Orientation3D.as" +include "flash/geom/Matrix.as" include "flash/geom/Point.as" include "flash/geom/Rectangle.as" include "flash/net/SharedObjectFlushStatus.as"