From 8db6375c4ce1ce7e19e7a4c3b48273a107029ef1 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Fri, 17 Mar 2023 14:18:19 +0100 Subject: [PATCH] avm2: Convert display_object_container to use ParametersExt --- .../flash/display/display_object_container.rs | 102 ++++-------------- 1 file changed, 21 insertions(+), 81 deletions(-) diff --git a/core/src/avm2/globals/flash/display/display_object_container.rs b/core/src/avm2/globals/flash/display/display_object_container.rs index 7a3206fc6..24d151783 100644 --- a/core/src/avm2/globals/flash/display/display_object_container.rs +++ b/core/src/avm2/globals/flash/display/display_object_container.rs @@ -124,11 +124,7 @@ pub fn get_child_at<'gc>( .and_then(|this| this.as_display_object()) .and_then(|this| this.as_container()) { - let index = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .coerce_to_i32(activation)?; + let index = args.get_i32(activation, 0)?; return if let Some(child) = dobj.child_by_index(index as usize) { Ok(child.object2()) } else { @@ -174,12 +170,7 @@ pub fn add_child<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(ctr) = parent.as_container() { - let child = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .ok_or("ArgumentError: Child not a valid display object")?; + let child = args.get_object(activation, 0, "child")?; if child.as_display_object().is_none() { let sprite = activation.avm2().classes().sprite; if child.is_of_type(sprite, activation) { @@ -211,17 +202,10 @@ pub fn add_child_at<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { let child = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()) + .get_object(activation, 0, "child")? + .as_display_object() .ok_or("ArgumentError: Child not a valid display object")?; - let target_index = args - .get(1) - .cloned() - .ok_or("ArgumentError: Index to add child at not specified")? - .coerce_to_i32(activation)? as usize; + let target_index = args.get_u32(activation, 1)? as usize; validate_add_operation(activation, parent, child, target_index)?; add_child_to_displaylist(&mut activation.context, parent, child, target_index); @@ -271,19 +255,13 @@ pub fn get_num_children<'gc>( /// Implements `DisplayObjectContainer.contains` pub fn contains<'gc>( - _activation: &mut Activation<'_, 'gc>, + activation: &mut Activation<'_, 'gc>, this: Option>, args: &[Value<'gc>], ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if parent.as_container().is_some() { - if let Some(child) = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()) - { + if let Some(child) = args.get_object(activation, 0, "child")?.as_display_object() { let mut maybe_child_parent = Some(child); while let Some(child_parent) = maybe_child_parent { if DisplayObject::ptr_eq(child_parent, parent) { @@ -301,18 +279,13 @@ pub fn contains<'gc>( /// Implements `DisplayObjectContainer.getChildIndex` pub fn get_child_index<'gc>( - _activation: &mut Activation<'_, 'gc>, + activation: &mut Activation<'_, 'gc>, this: Option>, args: &[Value<'gc>], ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(ctr) = parent.as_container() { - let target_child = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()); + let target_child = args.get_object(activation, 0, "child")?.as_display_object(); if let Some(target_child) = target_child { for (i, child) in ctr.iter_render_list().enumerate() { @@ -335,11 +308,7 @@ pub fn remove_child_at<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(mut ctr) = parent.as_container() { - let target_child = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .coerce_to_i32(activation)?; + let target_child = args.get_i32(activation, 0)?; if target_child >= ctr.num_children() as i32 || target_child < 0 { // Flash error message: The supplied index is out of bounds. @@ -374,16 +343,8 @@ pub fn remove_children<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(mut ctr) = parent.as_container() { - let from = args - .get(0) - .cloned() - .unwrap_or_else(|| 0.into()) - .coerce_to_i32(activation)?; - let to = args - .get(1) - .cloned() - .unwrap_or_else(|| i32::MAX.into()) - .coerce_to_i32(activation)?; + let from = args.get_i32(activation, 0)?; + let to = args.get_i32(activation, 1)?; if from >= ctr.num_children() as i32 || from < 0 { // Flash error message: The supplied index is out of bounds. @@ -438,17 +399,10 @@ pub fn set_child_index<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { let child = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()) + .get_object(activation, 0, "child")? + .as_display_object() .ok_or("ArgumentError: Child not a valid display object")?; - let target_index = args - .get(1) - .cloned() - .ok_or("ArgumentError: Index to add child at not specified")? - .coerce_to_i32(activation)? as usize; + let target_index = args.get_u32(activation, 1)? as usize; let child_parent = child.parent(); if child_parent.is_none() || !DisplayObject::ptr_eq(child_parent.unwrap(), parent) { @@ -472,16 +426,8 @@ pub fn swap_children_at<'gc>( ) -> Result, Error<'gc>> { if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(mut ctr) = parent.as_container() { - let index0 = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .coerce_to_i32(activation)?; - let index1 = args - .get(1) - .cloned() - .unwrap_or(Value::Undefined) - .coerce_to_i32(activation)?; + let index0 = args.get_i32(activation, 0)?; + let index1 = args.get_i32(activation, 1)?; let bounds = ctr.num_children(); if index0 < 0 || index0 as usize >= bounds { @@ -524,18 +470,12 @@ pub fn swap_children<'gc>( if let Some(parent) = this.and_then(|this| this.as_display_object()) { if let Some(mut ctr) = parent.as_container() { let child0 = args - .get(0) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()) + .get_object(activation, 0, "child1")? + .as_display_object() .ok_or("ArgumentError: Child is not a display object")?; let child1 = args - .get(1) - .cloned() - .unwrap_or(Value::Undefined) - .as_object() - .and_then(|o| o.as_display_object()) + .get_object(activation, 1, "child2")? + .as_display_object() .ok_or("ArgumentError: Child is not a display object")?; let index0 = ctr