Skip to content

Commit abc1d5d

Browse files
Lord-McSweeneyLord-McSweeney
Lord-McSweeney
authored andcommitted
avm2: Remove most of the remaining static strings in avm2
1 parent c21870b commit abc1d5d

File tree

12 files changed

+58
-42
lines changed

12 files changed

+58
-42
lines changed

core/src/avm1/globals/point.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,14 @@ fn distance<'gc>(
180180
.coerce_to_object(activation);
181181
let b = args.get(1).unwrap_or(&Value::Undefined);
182182
let delta = a.call_method(
183-
"subtract".into(),
183+
istr!("subtract"),
184184
&[b.to_owned()],
185185
activation,
186186
ExecutionReason::FunctionCall,
187187
)?;
188-
delta.coerce_to_object(activation).get("length", activation)
188+
delta
189+
.coerce_to_object(activation)
190+
.get(istr!("length"), activation)
189191
}
190192

191193
fn polar<'gc>(

core/src/avm2/globals.rs

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub struct SystemClassDefs<'gc> {
212212
pub rectangletexture: Class<'gc>,
213213
pub display_object: Class<'gc>,
214214
pub sprite: Class<'gc>,
215+
pub urlrequestheader: Class<'gc>,
215216
pub contextmenuitem: Class<'gc>,
216217
}
217218

@@ -377,6 +378,7 @@ impl<'gc> SystemClassDefs<'gc> {
377378
rectangletexture: object,
378379
display_object: object,
379380
sprite: object,
381+
urlrequestheader: object,
380382
contextmenuitem: object,
381383
}
382384
}
@@ -977,6 +979,7 @@ pub fn init_native_system_classes(activation: &mut Activation<'_, '_>) {
977979
"RectangleTexture",
978980
rectangletexture
979981
),
982+
("flash.net", "URLRequestHeader", urlrequestheader),
980983
("flash.ui", "ContextMenuItem", contextmenuitem),
981984
]
982985
);

core/src/avm2/globals/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'gc> ArrayIter<'gc> {
233233
end_index: u32,
234234
) -> Result<Self, Error<'gc>> {
235235
let length = Value::from(array_object)
236-
.get_public_property("length", activation)?
236+
.get_public_property(istr!("length"), activation)?
237237
.coerce_to_u32(activation)?;
238238

239239
Ok(Self {

core/src/avm2/globals/class.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::avm2::object::{ClassObject, Object, TObject};
88
use crate::avm2::value::Value;
99
use crate::avm2::Error;
1010
use crate::avm2::QName;
11+
use crate::string::AvmString;
12+
use ruffle_macros::istr;
1113

1214
pub fn class_allocator<'gc>(
1315
_class: ClassObject<'gc>,
@@ -61,8 +63,9 @@ pub fn create_i_class<'gc>(
6163
let gc_context = activation.gc();
6264
let namespaces = activation.avm2().namespaces;
6365

66+
let class_name = istr!("Class");
6467
let class_i_class = Class::custom_new(
65-
QName::new(namespaces.public_all(), "Class"),
68+
QName::new(namespaces.public_all(), class_name),
6669
Some(object_i_class),
6770
Method::from_builtin(instance_init, "<Class instance initializer>", gc_context),
6871
gc_context,
@@ -100,8 +103,9 @@ pub fn create_c_class<'gc>(
100103
let gc_context = activation.gc();
101104
let namespaces = activation.avm2().namespaces;
102105

106+
let class_name = AvmString::new_utf8(gc_context, "Class$");
103107
let class_c_class = Class::custom_new(
104-
QName::new(namespaces.public_all(), "Class$"),
108+
QName::new(namespaces.public_all(), class_name),
105109
Some(class_i_class),
106110
Method::from_builtin(class_init, "<Class class initializer>", gc_context),
107111
gc_context,

core/src/avm2/globals/flash/display/loader.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::avm2::error::make_error_2007;
77
use crate::avm2::globals::flash::display::display_object::initialize_for_allocator;
88
use crate::avm2::globals::slots::flash_display_loader as loader_slots;
99
use crate::avm2::globals::slots::flash_net_url_request as url_request_slots;
10+
use crate::avm2::globals::slots::flash_net_url_request_header as url_request_header_slots;
1011
use crate::avm2::object::LoaderInfoObject;
1112
use crate::avm2::object::LoaderStream;
1213
use crate::avm2::object::TObject;
@@ -144,28 +145,29 @@ pub fn request_from_url_request<'gc>(
144145

145146
let mut string_headers = IndexMap::default();
146147
if let Some(headers) = headers {
147-
let headers = headers.as_array_object().unwrap();
148-
149148
let headers = headers.as_array_storage().unwrap();
150149

151150
for i in 0..headers.length() {
152-
let Some(header) = headers.get(i) else {
151+
let Some(header) = headers.get(i).and_then(|h| h.as_object()) else {
153152
continue;
154153
};
155154

156-
let name = header
157-
.get_public_property("name", activation)?
158-
.coerce_to_string(activation)?
159-
.to_string();
160-
let value = header
161-
.get_public_property("value", activation)?
162-
.coerce_to_string(activation)?
163-
.to_string();
164-
165-
// Note - testing with Flash Player shows that later entries in the array
166-
// overwrite earlier ones with the same name. Flash Player never sends an HTTP
167-
// request with duplicate headers
168-
string_headers.insert(name, value);
155+
// Non-URLRequestHeader objects are skipped
156+
if header.is_of_type(activation.avm2().class_defs().urlrequestheader) {
157+
let name = header
158+
.get_slot(url_request_header_slots::NAME)
159+
.coerce_to_string(activation)?
160+
.to_string();
161+
let value = header
162+
.get_slot(url_request_header_slots::VALUE)
163+
.coerce_to_string(activation)?
164+
.to_string();
165+
166+
// Note - testing with Flash Player shows that later entries in the array
167+
// overwrite earlier ones with the same name. Flash Player never sends an HTTP
168+
// request with duplicate headers
169+
string_headers.insert(name, value);
170+
}
169171
}
170172
}
171173

core/src/avm2/globals/flash/net/URLRequestHeader.as

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package flash.net {
22
public final class URLRequestHeader {
3+
[Ruffle(NativeAccessible)]
34
public var name: String;
5+
6+
[Ruffle(NativeAccessible)]
47
public var value: String;
58

69
public function URLRequestHeader(name: String = "", value: String = "") {

core/src/avm2/globals/number.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::avm2::error::{make_error_1002, make_error_1003};
55
use crate::avm2::parameters::ParametersExt;
66
use crate::avm2::value::Value;
77
use crate::avm2::{AvmString, Error};
8+
use ruffle_macros::istr;
89

910
pub fn number_constructor<'gc>(
1011
activation: &mut Activation<'_, 'gc>,
@@ -130,14 +131,14 @@ pub fn print_with_radix<'gc>(
130131
}
131132

132133
if number.is_nan() {
133-
return Ok("NaN".into());
134+
return Ok(istr!("NaN"));
134135
}
135136

136137
if number.is_infinite() {
137138
if number < 0.0 {
138-
return Ok("-Infinity".into());
139+
return Ok(AvmString::new_utf8(activation.gc(), "-Infinity"));
139140
} else if number > 0.0 {
140-
return Ok("Infinity".into());
141+
return Ok(istr!("Infinity"));
141142
}
142143
}
143144

core/src/avm2/globals/object.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,9 @@ pub fn create_i_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
296296
let gc_context = activation.gc();
297297
let namespaces = activation.avm2().namespaces;
298298

299+
let class_name = istr!("Object");
299300
let object_i_class = Class::custom_new(
300-
QName::new(namespaces.public_all(), "Object"),
301+
QName::new(namespaces.public_all(), class_name),
301302
None,
302303
Method::from_builtin(instance_init, "<Object instance initializer>", gc_context),
303304
gc_context,
@@ -354,8 +355,9 @@ pub fn create_c_class<'gc>(
354355
let gc_context = activation.gc();
355356
let namespaces = activation.avm2().namespaces;
356357

358+
let class_name = AvmString::new_utf8(gc_context, "Object$");
357359
let object_c_class = Class::custom_new(
358-
QName::new(namespaces.public_all(), "Object$"),
360+
QName::new(namespaces.public_all(), class_name),
359361
Some(class_i_class),
360362
Method::from_builtin(class_init, "<Object class initializer>", gc_context),
361363
gc_context,

core/src/avm2/globals/vector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn call_handler<'gc>(
7777
}
7878

7979
let length = arg
80-
.get_public_property("length", activation)?
80+
.get_public_property(istr!("length"), activation)?
8181
.coerce_to_i32(activation)?;
8282

8383
let arg = arg.as_object().ok_or("Cannot convert to Vector")?;

core/src/avm2/namespace.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,6 @@ pub struct CommonNamespaces<'gc> {
340340
pub(super) internal: Namespace<'gc>,
341341
pub(super) as3: Namespace<'gc>,
342342
pub(super) vector_internal: Namespace<'gc>,
343-
344-
pub(super) __ruffle__: Namespace<'gc>,
345343
}
346344

347345
impl<'gc> CommonNamespaces<'gc> {
@@ -350,19 +348,17 @@ impl<'gc> CommonNamespaces<'gc> {
350348
pub fn new(context: &mut StringContext<'gc>) -> Self {
351349
let empty_string = context.empty();
352350

351+
let as3_namespace_string =
352+
AvmString::new_utf8(context.gc(), "http://adobe.com/AS3/2006/builtin");
353+
let vector_namespace_string = AvmString::new_utf8(context.gc(), "__AS3__.vec");
354+
353355
Self {
354356
public_namespaces: std::array::from_fn(|val| {
355357
Namespace::package(empty_string, ApiVersion::from_usize(val).unwrap(), context)
356358
}),
357359
internal: Namespace::internal(empty_string, context),
358-
as3: Namespace::package(
359-
"http://adobe.com/AS3/2006/builtin",
360-
ApiVersion::AllVersions,
361-
context,
362-
),
363-
vector_internal: Namespace::internal("__AS3__.vec", context),
364-
365-
__ruffle__: Namespace::package("__ruffle__", ApiVersion::AllVersions, context),
360+
as3: Namespace::package(as3_namespace_string, ApiVersion::AllVersions, context),
361+
vector_internal: Namespace::internal(vector_namespace_string, context),
366362
}
367363
}
368364

core/src/avm2/verify.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::avm2::multiname::Multiname;
88
use crate::avm2::op::Op;
99
use crate::avm2::script::TranslationUnit;
1010
use crate::avm2::{Activation, Error, QName};
11-
use crate::string::AvmAtom;
11+
use crate::string::{AvmAtom, AvmString};
1212

1313
use gc_arena::{Collect, Gc};
1414
use std::collections::{HashMap, HashSet};
@@ -420,7 +420,7 @@ pub fn verify_method<'gc>(
420420
return Err(make_error_1014(
421421
activation,
422422
Error1014Type::VerifyError,
423-
"[]".into(),
423+
AvmString::new_utf8(activation.gc(), "[]"),
424424
));
425425
}
426426

@@ -486,7 +486,7 @@ pub fn verify_method<'gc>(
486486
return Err(make_error_1014(
487487
activation,
488488
Error1014Type::VerifyError,
489-
"[]".into(),
489+
AvmString::new_utf8(activation.gc(), "[]"),
490490
));
491491
}
492492

@@ -742,7 +742,7 @@ pub fn resolve_param_config<'gc>(
742742
return Err(make_error_1014(
743743
activation,
744744
Error1014Type::VerifyError,
745-
"[]".into(),
745+
AvmString::new_utf8(activation.gc(), "[]"),
746746
));
747747
}
748748

@@ -780,7 +780,7 @@ fn resolve_return_type<'gc>(
780780
return Err(make_error_1014(
781781
activation,
782782
Error1014Type::VerifyError,
783-
"[]".into(),
783+
AvmString::new_utf8(activation.gc(), "[]"),
784784
));
785785
}
786786

core/src/string/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ define_common_strings! {
9191
str_caption: b"caption",
9292
str_center: b"center",
9393
str_clamp: b"clamp",
94+
str_Class: b"Class",
9495
str_click: b"click",
9596
str_code: b"code",
9697
str_color: b"color",
@@ -183,6 +184,7 @@ define_common_strings! {
183184
str_normal: b"normal",
184185
str_null: b"null",
185186
str_number: b"number",
187+
str_Object: b"Object",
186188
str_object: b"object",
187189
str_onCancel: b"onCancel",
188190
str_onChanged: b"onChanged",
@@ -260,6 +262,7 @@ define_common_strings! {
260262
str_status: b"status",
261263
str_string: b"string",
262264
str_subpixel: b"subpixel",
265+
str_subtract: b"subtract",
263266
str_success: b"success",
264267
str_super: b"super",
265268
str_tabChildren: b"tabChildren",

0 commit comments

Comments
 (0)