From faa1f3d6b45d5b71e81ce9a9e7ccd3fd158e947f Mon Sep 17 00:00:00 2001 From: relrelb Date: Fri, 11 Feb 2022 20:48:34 +0200 Subject: [PATCH] avm1: Refactor `XmlNodeObject::from_xml_node` Revert some of e50aea864bfc18faf4bcf579f6c182f9cf63c980 for an even better approach - Remove `XmlNodeObject::empty_node` entirely by making `XmlNodeObject::from_xml_node` a suitable alternative. That is, being able to accept a custom `proto` like before. Also, make it return an `XmlNodeObject` instead of an `Object`, and add a few `.into()` where needed. --- core/src/avm1/globals/xml_node.rs | 7 ++-- core/src/avm1/object/xml_node_object.rs | 48 ++++++++----------------- core/src/xml/tree.rs | 6 ++-- 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/core/src/avm1/globals/xml_node.rs b/core/src/avm1/globals/xml_node.rs index 971fcb8c1..11019c4d0 100644 --- a/core/src/avm1/globals/xml_node.rs +++ b/core/src/avm1/globals/xml_node.rs @@ -416,8 +416,9 @@ pub fn create_proto<'gc>( proto: Object<'gc>, fn_proto: Object<'gc>, ) -> Object<'gc> { - let xmlnode_proto = XmlNodeObject::empty_node(gc_context, Some(proto)); - let object = xmlnode_proto.as_script_object().unwrap(); + let node = XmlNode::new_text(gc_context, AvmString::default()); + let xml_node_proto = XmlNodeObject::from_xml_node(gc_context, node, Some(proto)); + let object = xml_node_proto.as_script_object().unwrap(); define_properties_on(PROTO_DECLS, gc_context, object, fn_proto); - xmlnode_proto + xml_node_proto.into() } diff --git a/core/src/avm1/object/xml_node_object.rs b/core/src/avm1/object/xml_node_object.rs index f249f2e2f..a31292551 100644 --- a/core/src/avm1/object/xml_node_object.rs +++ b/core/src/avm1/object/xml_node_object.rs @@ -23,41 +23,21 @@ pub struct XmlNodeObjectData<'gc> { } impl<'gc> XmlNodeObject<'gc> { - /// Construct a new XML node and object pair. - pub fn empty_node( - gc_context: MutationContext<'gc, '_>, - proto: Option>, - ) -> Object<'gc> { - let mut xml_node = XmlNode::new_text(gc_context, AvmString::default()); - let base_object = ScriptObject::object(gc_context, proto); - let object = XmlNodeObject(GcCell::allocate( - gc_context, - XmlNodeObjectData { - base: base_object, - node: xml_node, - }, - )) - .into(); - - xml_node.introduce_script_object(gc_context, object); - - object - } - /// Construct an XmlNodeObject for an already existing node. pub fn from_xml_node( - activation: &mut Activation<'_, 'gc, '_>, - xml_node: XmlNode<'gc>, - ) -> Object<'gc> { - let proto = activation.context.avm1.prototypes.xml_node; - Self(GcCell::allocate( - activation.context.gc_context, + gc_context: MutationContext<'gc, '_>, + mut node: XmlNode<'gc>, + proto: Option>, + ) -> Self { + let object = Self(GcCell::allocate( + gc_context, XmlNodeObjectData { - base: ScriptObject::object(activation.context.gc_context, Some(proto)), - node: xml_node, + base: ScriptObject::object(gc_context, proto), + node, }, - )) - .into() + )); + node.introduce_script_object(gc_context, object.into()); + object } } @@ -79,10 +59,12 @@ impl<'gc> TObject<'gc> for XmlNodeObject<'gc> { activation: &mut Activation<'_, 'gc, '_>, this: Object<'gc>, ) -> Result, Error<'gc>> { - Ok(XmlNodeObject::empty_node( + Ok(Self::from_xml_node( activation.context.gc_context, + XmlNode::new_text(activation.context.gc_context, AvmString::default()), Some(this), - )) + ) + .into()) } fn as_xml_node(&self) -> Option> { diff --git a/core/src/xml/tree.rs b/core/src/xml/tree.rs index 7d83da937..649c44cd2 100644 --- a/core/src/xml/tree.rs +++ b/core/src/xml/tree.rs @@ -614,9 +614,9 @@ impl<'gc> XmlNode<'gc> { match self.get_script_object() { Some(object) => object, None => { - let object = XmlNodeObject::from_xml_node(activation, *self); - self.introduce_script_object(activation.context.gc_context, object); - object + let proto = activation.context.avm1.prototypes().xml_node; + XmlNodeObject::from_xml_node(activation.context.gc_context, *self, Some(proto)) + .into() } } }