From 1cde56df63b43791e8f530e728eb2ed56def42b9 Mon Sep 17 00:00:00 2001 From: Lord-McSweeney Date: Sat, 18 Jan 2025 20:20:26 -0800 Subject: [PATCH] avm2: Remove some uses of static strings --- core/src/avm2/globals/q_name.rs | 6 +++--- core/src/avm2/globals/xml.rs | 12 +++++++----- core/src/avm2/object/proxy_object.rs | 4 ++-- core/src/avm2/object/qname_object.rs | 6 +++--- core/src/avm2/value.rs | 7 ++++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/src/avm2/globals/q_name.rs b/core/src/avm2/globals/q_name.rs index c8eb21771114..bf61a121b045 100644 --- a/core/src/avm2/globals/q_name.rs +++ b/core/src/avm2/globals/q_name.rs @@ -65,7 +65,7 @@ pub fn q_name_constructor<'gc>( }; if let Value::Object(Object::QNameObject(qname)) = local_arg { - this.set_local_name(activation.gc(), qname.local_name()); + this.set_local_name(activation.gc(), qname.local_name(activation.strings())); } else { this.set_local_name(activation.gc(), local_arg.coerce_to_string(activation)?); } @@ -102,14 +102,14 @@ pub fn q_name_constructor<'gc>( /// Implements `QName.localName`'s getter pub fn get_local_name<'gc>( - _activation: &mut Activation<'_, 'gc>, + activation: &mut Activation<'_, 'gc>, this: Value<'gc>, _args: &[Value<'gc>], ) -> Result, Error<'gc>> { let this = this.as_object().unwrap(); if let Some(this) = this.as_qname_object() { - return Ok(this.local_name().into()); + return Ok(this.local_name(activation.strings()).into()); } Ok(Value::Undefined) diff --git a/core/src/avm2/globals/xml.rs b/core/src/avm2/globals/xml.rs index 07ba781ec692..ba76727eac5d 100644 --- a/core/src/avm2/globals/xml.rs +++ b/core/src/avm2/globals/xml.rs @@ -253,7 +253,7 @@ pub fn set_name<'gc>( // 2. If (Type(name) is Object) and (name.[[Class]] == "QName") and (name.uri == null) Value::Object(Object::QNameObject(qname)) if qname.is_any_namespace() => { // a. Let name = name.localName - qname.local_name().into() + qname.local_name(activation.strings()).into() } value => value, }; @@ -269,9 +269,11 @@ pub fn set_name<'gc>( .as_qname_object() .unwrap(); + let new_local_name = new_name.local_name(activation.strings()); + // NOTE: avmplus addition - if !crate::avm2::e4x::is_xml_name(new_name.local_name()) { - return Err(make_error_1117(activation, new_name.local_name())); + if !crate::avm2::e4x::is_xml_name(new_local_name) { + return Err(make_error_1117(activation, new_local_name)); } // 4. If x.[[Class]] == "processing-instruction", let n.uri be the empty string @@ -288,7 +290,7 @@ pub fn set_name<'gc>( // 5. Let x.[[Name]] = n node.set_namespace(ns, activation.gc()); - node.set_local_name(new_name.local_name(), activation.gc()); + node.set_local_name(new_local_name, activation.gc()); // NOTE: avmplus addition if let Some(ns) = ns { @@ -1303,7 +1305,7 @@ pub fn set_local_name<'gc>( // 2. If (Type(name) is Object) and (name.[[Class]] == "QName") let name = if let Some(qname) = name.as_object().and_then(|x| x.as_qname_object()) { // 2.a. Let name = name.localName - qname.local_name() + qname.local_name(activation.strings()) // 3. Else } else { // 3.a. Let name = ToString(name) diff --git a/core/src/avm2/object/proxy_object.rs b/core/src/avm2/object/proxy_object.rs index e11c7e845af1..016adee325f3 100644 --- a/core/src/avm2/object/proxy_object.rs +++ b/core/src/avm2/object/proxy_object.rs @@ -132,8 +132,8 @@ impl<'gc> TObject<'gc> for ProxyObject<'gc> { &prop, &[name .local_name() - .map(Value::from) - .unwrap_or_else(|| "*".into())], + .unwrap_or_else(|| activation.strings().ascii_char(b'*')) + .into()], activation, )? .coerce_to_boolean()) diff --git a/core/src/avm2/object/qname_object.rs b/core/src/avm2/object/qname_object.rs index 80a116e8a1ac..999677c1c60f 100644 --- a/core/src/avm2/object/qname_object.rs +++ b/core/src/avm2/object/qname_object.rs @@ -89,10 +89,10 @@ impl<'gc> QNameObject<'gc> { write_name.set_local_name(local); } - pub fn local_name(&self) -> AvmString<'gc> { + pub fn local_name(&self, context: &mut StringContext<'gc>) -> AvmString<'gc> { let name = self.name(); - name.local_name().unwrap_or("*".into()) + name.local_name().unwrap_or(context.ascii_char(b'*')) } pub fn set_is_qname(&self, mc: &Mutation<'gc>, is_qname: bool) { @@ -159,7 +159,7 @@ impl<'gc> TObject<'gc> for QNameObject<'gc> { ) -> Result, Error<'gc>> { // NOTE: Weird avmplus behavior, get_enumerant_name returns uri first, but get_enumerant_value returns localName first. Ok(match index { - 1 => self.local_name().into(), + 1 => self.local_name(activation.strings()).into(), 2 => self .uri(activation.strings()) .unwrap_or_else(|| activation.strings().empty()) diff --git a/core/src/avm2/value.rs b/core/src/avm2/value.rs index 87303b628c4f..fc1ba8d172de 100644 --- a/core/src/avm2/value.rs +++ b/core/src/avm2/value.rs @@ -776,7 +776,7 @@ impl<'gc> Value<'gc> { Value::Bool(true) => "true".into(), Value::Bool(false) => "false".into(), Value::Number(n) if n.is_nan() => "NaN".into(), - Value::Number(n) if *n == 0.0 => "0".into(), + Value::Number(n) if *n == 0.0 => activation.strings().ascii_char(b'0'), Value::Number(n) if *n < 0.0 => AvmString::new_utf8( activation.gc(), format!("-{}", Value::Number(-n).coerce_to_string(activation)?), @@ -806,7 +806,7 @@ impl<'gc> Value<'gc> { } Value::Integer(i) => { if *i >= 0 && *i < 10 { - activation.strings().make_char('0' as u16 + *i as u16) + activation.strings().ascii_char(b'0' + *i as u8) } else { AvmString::new_utf8(activation.gc(), i.to_string()) } @@ -1706,7 +1706,8 @@ impl<'gc> Value<'gc> { if let Value::Object(Object::QNameObject(other_qname)) = other { return Ok(self_qname.uri(activation.strings()) == other_qname.uri(activation.strings()) - && self_qname.local_name() == other_qname.local_name()); + && self_qname.local_name(activation.strings()) + == other_qname.local_name(activation.strings())); } }