Expose XML document constructor, including text parsing ability

This commit is contained in:
David Wendt 2019-12-20 15:40:50 -05:00
parent 554f0dc1e5
commit 4f5ac09b73
2 changed files with 52 additions and 4 deletions

View File

@ -176,6 +176,8 @@ pub fn create_globals<'gc>(
let xmlnode_proto: Object<'gc> = let xmlnode_proto: Object<'gc> =
xml::create_xmlnode_proto(gc_context, object_proto, function_proto); xml::create_xmlnode_proto(gc_context, object_proto, function_proto);
let xml_proto: Object<'gc> = xml::create_xml_proto(gc_context, xmlnode_proto, function_proto);
//TODO: These need to be constructors and should also set `.prototype` on each one //TODO: These need to be constructors and should also set `.prototype` on each one
let object = ScriptObject::function( let object = ScriptObject::function(
gc_context, gc_context,
@ -226,6 +228,12 @@ pub fn create_globals<'gc>(
Some(function_proto), Some(function_proto),
Some(xmlnode_proto), Some(xmlnode_proto),
); );
let xml = ScriptObject::function(
gc_context,
Executable::Native(xml::xml_constructor),
Some(function_proto),
Some(xml_proto),
);
let listeners = SystemListeners::new(gc_context, Some(array_proto)); let listeners = SystemListeners::new(gc_context, Some(array_proto));
@ -238,6 +246,7 @@ pub fn create_globals<'gc>(
globals.define_value(gc_context, "Sound", sound.into(), EnumSet::empty()); globals.define_value(gc_context, "Sound", sound.into(), EnumSet::empty());
globals.define_value(gc_context, "TextField", text_field.into(), EnumSet::empty()); globals.define_value(gc_context, "TextField", text_field.into(), EnumSet::empty());
globals.define_value(gc_context, "XMLNode", xmlnode.into(), EnumSet::empty()); globals.define_value(gc_context, "XMLNode", xmlnode.into(), EnumSet::empty());
globals.define_value(gc_context, "XML", xml.into(), EnumSet::empty());
globals.force_set_function( globals.force_set_function(
"Number", "Number",
number, number,

View File

@ -3,7 +3,7 @@
use crate::avm1::return_value::ReturnValue; use crate::avm1::return_value::ReturnValue;
use crate::avm1::script_object::ScriptObject; use crate::avm1::script_object::ScriptObject;
use crate::avm1::{Avm1, Error, Object, TObject, UpdateContext, Value}; use crate::avm1::{Avm1, Error, Object, TObject, UpdateContext, Value};
use crate::xml::XMLNode; use crate::xml::{XMLDocument, XMLNode};
use gc_arena::MutationContext; use gc_arena::MutationContext;
use std::mem::swap; use std::mem::swap;
@ -16,14 +16,14 @@ pub fn xmlnode_constructor<'gc>(
) -> Result<ReturnValue<'gc>, Error> { ) -> Result<ReturnValue<'gc>, Error> {
match ( match (
args.get(0).map(|v| v.as_number(avm, ac).map(|v| v as u32)), args.get(0).map(|v| v.as_number(avm, ac).map(|v| v as u32)),
args.get(1).map(|v| v.as_string()), args.get(1).map(|v| v.clone().coerce_to_string(avm, ac)),
this.as_xml_node(), this.as_xml_node(),
) { ) {
(Some(Ok(1)), Some(Ok(strval)), Some(ref mut this_node)) => { (Some(Ok(1)), Some(Ok(ref strval)), Some(ref mut this_node)) => {
let mut xmlelement = XMLNode::new_text(ac.gc_context, strval); let mut xmlelement = XMLNode::new_text(ac.gc_context, strval);
swap(&mut xmlelement, this_node); swap(&mut xmlelement, this_node);
} }
(Some(Ok(3)), Some(Ok(strval)), Some(ref mut this_node)) => { (Some(Ok(3)), Some(Ok(ref strval)), Some(ref mut this_node)) => {
let mut xmlelement = XMLNode::new_element(ac.gc_context, strval)?; let mut xmlelement = XMLNode::new_element(ac.gc_context, strval)?;
swap(&mut xmlelement, this_node); swap(&mut xmlelement, this_node);
} }
@ -44,3 +44,42 @@ pub fn create_xmlnode_proto<'gc>(
xmlnode_proto.into() xmlnode_proto.into()
} }
/// XML (document) constructor
pub fn xml_constructor<'gc>(
avm: &mut Avm1<'gc>,
ac: &mut UpdateContext<'_, 'gc, '_>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<ReturnValue<'gc>, Error> {
match (
args.get(0).map(|v| v.clone().coerce_to_string(avm, ac)),
this.as_xml_document(),
) {
(Some(Ok(ref string)), Some(ref mut this_doc)) => {
let mut xmldoc = XMLDocument::from_str(ac.gc_context, string)?;
swap(&mut xmldoc, this_doc);
}
(None, Some(ref mut this_doc)) => {
let mut emptydoc = XMLDocument::new(ac.gc_context);
swap(&mut emptydoc, this_doc);
}
//Non-string argument or not an XML document
_ => {}
};
Ok(Value::Undefined.into())
}
/// Construct the prototype for `XML`.
pub fn create_xml_proto<'gc>(
gc_context: MutationContext<'gc, '_>,
proto: Object<'gc>,
_fn_proto: Object<'gc>,
) -> Object<'gc> {
let xml_proto = ScriptObject::object(gc_context, Some(proto));
xml_proto.into()
}