Skip to content

Commit

Permalink
core: use istr! instead of statically-known ascii_char calls
Browse files Browse the repository at this point in the history
  • Loading branch information
moulins committed Feb 8, 2025
1 parent cb5a5da commit 4e3047c
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/src/avm1/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub fn join<'gc>(
let separator = if let Some(v) = args.get(0) {
v.coerce_to_string(activation)?
} else {
",".into()
istr!(",")
};

if length <= 0 {
Expand Down
3 changes: 2 additions & 1 deletion core/src/avm1/globals/number.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! `Number` class impl
use gc_arena::Gc;
use ruffle_macros::istr;

use crate::avm1::activation::Activation;
use crate::avm1::clamp::Clamp;
Expand Down Expand Up @@ -134,7 +135,7 @@ fn to_string<'gc>(
Ordering::Greater => (number, false),
Ordering::Equal => {
// Bail out immediately if we're 0.
return Ok("0".into());
return Ok(istr!("0").into());
}
};

Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/globals/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn split<'gc>(
// and the empty string behaves the same as undefined does in later SWF versions.
let is_swf5 = activation.swf_version() == 5;
if let Some(delimiter) = match args.get(0).unwrap_or(&Value::Undefined) {
&Value::Undefined => is_swf5.then_some(",".into()),
&Value::Undefined => is_swf5.then_some(istr!(",")),
v => Some(v.coerce_to_string(activation)?).filter(|s| !(is_swf5 && s.is_empty())),
} {
if delimiter.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions core/src/avm1/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ impl<'gc> Value<'gc> {
Ok(match self {
Value::Undefined if activation.swf_version() < 7 => istr!(""),
Value::Bool(true) if activation.swf_version() < 5 => {
activation.strings().ascii_char(b'1')
istr!("1")
}
Value::Bool(false) if activation.swf_version() < 5 => {
activation.strings().ascii_char(b'0')
istr!("0")
}
Value::Object(object) => {
if let Some(object) = object
Expand Down Expand Up @@ -571,7 +571,7 @@ fn f64_to_string<'gc>(activation: &mut Activation<'_, 'gc>, mut n: f64) -> AvmSt
// FIXME is there an easy way to use istr! here?
AvmString::new_utf8_bytes(activation.gc(), b"-Infinity")
} else if n == 0.0 {
activation.strings().ascii_char(b'0')
istr!("0")
} else if n >= -2147483648.0 && n <= 2147483647.0 && n.fract() == 0.0 {
// Fast path for integers.
let n = n as i32;
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn join<'gc>(

if let Some(array) = this.as_array_storage() {
let string_separator = if matches!(separator, Value::Undefined) {
activation.strings().ascii_char(b',')
istr!(",")
} else {
separator.coerce_to_string(activation)?
};
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/avmplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn display_name<'gc>(
if let Some(name) = name {
name.to_qualified_name_or_star(context)
} else {
context.ascii_char(b'*')
istr!(context, "*")
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/avm2/globals/int.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `int` impl
use ruffle_macros::istr;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::{make_error_1003, make_error_1004};
Expand Down Expand Up @@ -166,7 +168,7 @@ fn to_string<'gc>(
if let Some(this) = this.as_object() {
let int_proto = activation.avm2().classes().int.prototype();
if Object::ptr_eq(int_proto, this) {
return Ok("0".into());
return Ok(istr!("0").into());
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/avm2/globals/number.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `Number` impl
use ruffle_macros::istr;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::{make_error_1002, make_error_1003, make_error_1004};
Expand Down Expand Up @@ -309,7 +311,7 @@ fn to_string<'gc>(
if let Some(this) = this.as_object() {
let number_proto = activation.avm2().classes().number.prototype();
if Object::ptr_eq(number_proto, this) {
return Ok("0".into());
return Ok(istr!("0").into());
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/avm2/globals/uint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `uint` impl
use ruffle_macros::istr;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::{make_error_1003, make_error_1004};
Expand Down Expand Up @@ -166,7 +168,7 @@ fn to_string<'gc>(
if let Some(this) = this.as_object() {
let uint_proto = activation.avm2().classes().uint.prototype();
if Object::ptr_eq(uint_proto, this) {
return Ok("0".into());
return Ok(istr!("0").into());
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/avm2/multiname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::string::{AvmString, StringContext, WStr, WString};
use bitflags::bitflags;
use gc_arena::Gc;
use gc_arena::{Collect, Mutation};
use ruffle_macros::istr;
use std::fmt::Debug;
use std::ops::Deref;
use swf::avm2::types::{Index, Multiname as AbcMultiname, NamespaceSet as AbcNamespaceSet};
Expand Down Expand Up @@ -480,7 +481,7 @@ impl<'gc> Multiname<'gc> {
/// This is used by `describeType`
pub fn to_qualified_name_or_star(&self, context: &mut StringContext<'gc>) -> AvmString<'gc> {
if self.is_any_name() {
context.ascii_char(b'*')
istr!(context, "*")
} else {
self.to_qualified_name(context.gc())
}
Expand All @@ -497,7 +498,7 @@ impl<'gc> Multiname<'gc> {

if ns.is_empty() {
// Special-case this to avoid allocating.
self.name.unwrap_or_else(|| context.ascii_char(b'*'))
self.name.unwrap_or_else(|| istr!(context, "*"))
} else {
let mut uri = WString::new();
uri.push_str(ns);
Expand Down
6 changes: 2 additions & 4 deletions core/src/avm2/object/proxy_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::avm2::Error;
use crate::avm2::Multiname;
use core::fmt;
use gc_arena::{Collect, Gc, GcWeak};
use ruffle_macros::istr;

/// A class instance allocator that allocates Proxy objects.
pub fn proxy_allocator<'gc>(
Expand Down Expand Up @@ -130,10 +131,7 @@ impl<'gc> TObject<'gc> for ProxyObject<'gc> {
Ok(self_val
.call_method(
proxy_methods::HAS_PROPERTY,
&[name
.local_name()
.unwrap_or_else(|| activation.strings().ascii_char(b'*'))
.into()],
&[name.local_name().unwrap_or_else(|| istr!("*")).into()],
activation,
)?
.coerce_to_boolean())
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/object/qname_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'gc> QNameObject<'gc> {
pub fn local_name(&self, context: &mut StringContext<'gc>) -> AvmString<'gc> {
let name = self.name();

name.local_name().unwrap_or(context.ascii_char(b'*'))
name.local_name().unwrap_or_else(|| istr!(context, "*"))
}

pub fn set_is_qname(&self, mc: &Mutation<'gc>, is_qname: bool) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/avm2/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::avm2::TranslationUnit;
use crate::avm2::Value;
use crate::string::{AvmString, StringContext};
use gc_arena::{Collect, Gc};
use ruffle_macros::istr;

use super::class::Class;

Expand Down Expand Up @@ -116,7 +117,7 @@ impl<'gc> PropertyClass<'gc> {
match self {
PropertyClass::Class(class) => class.name().to_qualified_name(context.gc()),
PropertyClass::Name(name, _) => name.to_qualified_name_or_star(context),
PropertyClass::Any => context.ascii_char(b'*'),
PropertyClass::Any => istr!(context, "*"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ impl<'gc> Value<'gc> {
Value::Bool(true) => istr!("true"),
Value::Bool(false) => istr!("false"),
Value::Number(n) if n.is_nan() => istr!("NaN"),
Value::Number(n) if *n == 0.0 => activation.strings().ascii_char(b'0'),
Value::Number(n) if *n == 0.0 => istr!("0"),
Value::Number(n) if *n < 0.0 => AvmString::new_utf8(
activation.gc(),
format!("-{}", Value::Number(-n).coerce_to_string(activation)?),
Expand Down

0 comments on commit 4e3047c

Please sign in to comment.