Skip to content

Commit

Permalink
avm2: Remove some uses of static strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Jan 19, 2025
1 parent dae6a07 commit 1cde56d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions core/src/avm2/globals/q_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
}
Expand Down Expand Up @@ -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<Value<'gc>, 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)
Expand Down
12 changes: 7 additions & 5 deletions core/src/avm2/globals/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/object/proxy_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions core/src/avm2/object/qname_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'gc> TObject<'gc> for QNameObject<'gc> {
) -> Result<Value<'gc>, 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())
Expand Down
7 changes: 4 additions & 3 deletions core/src/avm2/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?),
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()));
}
}

Expand Down

0 comments on commit 1cde56d

Please sign in to comment.