Skip to content

Commit 403cced

Browse files
avm2: Allocate a non-static string for the Event name when creating an EventObject
1 parent 746c37d commit 403cced

File tree

4 files changed

+53
-67
lines changed

4 files changed

+53
-67
lines changed

core/src/avm2/events.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ pub struct Event<'gc> {
7676

7777
impl<'gc> Event<'gc> {
7878
/// Construct a new event of a given type.
79-
pub fn new<S>(event_type: S) -> Self
80-
where
81-
S: Into<AvmString<'gc>>,
82-
{
79+
pub fn new(event_type: AvmString<'gc>) -> Self {
8380
Event {
8481
bubbles: false,
8582
cancelable: false,
@@ -88,7 +85,7 @@ impl<'gc> Event<'gc> {
8885
current_target: None,
8986
event_phase: EventPhase::AtTarget,
9087
target: None,
91-
event_type: event_type.into(),
88+
event_type,
9289
}
9390
}
9491

core/src/avm2/object/event_object.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,27 @@ impl<'gc> EventObject<'gc> {
6161
/// It's just slightly faster and doesn't require an Activation.
6262
/// This is equivalent to
6363
/// classes.event.construct(activation, &[event_type, false, false])
64-
pub fn bare_default_event<S>(
64+
pub fn bare_default_event(
6565
context: &mut UpdateContext<'gc>,
66-
event_type: S,
67-
) -> EventObject<'gc>
68-
where
69-
S: Into<AvmString<'gc>>,
70-
{
66+
event_type: &str,
67+
) -> EventObject<'gc> {
7168
Self::bare_event(context, event_type, false, false)
7269
}
7370

7471
/// Create a bare Event instance while skipping the usual `construct()` pipeline.
7572
/// It's just slightly faster and doesn't require an Activation.
7673
/// Note that if you need an Event subclass, you need to construct it via .construct().
77-
pub fn bare_event<S>(
74+
pub fn bare_event(
7875
context: &mut UpdateContext<'gc>,
79-
event_type: S,
76+
event_type: &str,
8077
bubbles: bool,
8178
cancelable: bool,
82-
) -> EventObject<'gc>
83-
where
84-
S: Into<AvmString<'gc>>,
85-
{
79+
) -> EventObject<'gc> {
8680
let class = context.avm2.classes().event;
8781
let base = ScriptObjectData::new(class);
8882

83+
let event_type = AvmString::new_utf8(context.gc(), event_type);
84+
8985
let mut event = Event::new(event_type);
9086
event.set_bubbles(bubbles);
9187
event.set_cancelable(cancelable);
@@ -116,21 +112,18 @@ impl<'gc> EventObject<'gc> {
116112
.unwrap()
117113
}
118114

119-
pub fn mouse_event<S>(
115+
pub fn mouse_event(
120116
activation: &mut Activation<'_, 'gc>,
121-
event_type: S,
117+
event_type: &str,
122118
target: DisplayObject<'gc>,
123119
related_object: Option<InteractiveObject<'gc>>,
124120
delta: i32,
125121
bubbles: bool,
126122
button: MouseButton,
127-
) -> EventObject<'gc>
128-
where
129-
S: Into<AvmString<'gc>>,
130-
{
123+
) -> EventObject<'gc> {
131124
let local = target.local_mouse_position(activation.context);
132125

133-
let event_type: AvmString<'gc> = event_type.into();
126+
let event_type = AvmString::new_utf8(activation.gc(), event_type);
134127

135128
let mouse_event_cls = activation.avm2().classes().mouseevent;
136129
Self::from_class_and_args(
@@ -231,17 +224,14 @@ impl<'gc> EventObject<'gc> {
231224
)
232225
}
233226

234-
pub fn text_event<S>(
227+
pub fn text_event(
235228
activation: &mut Activation<'_, 'gc>,
236-
event_type: S,
229+
event_type: &str,
237230
text: AvmString<'gc>,
238231
bubbles: bool,
239232
cancelable: bool,
240-
) -> EventObject<'gc>
241-
where
242-
S: Into<AvmString<'gc>>,
243-
{
244-
let event_type: AvmString<'gc> = event_type.into();
233+
) -> EventObject<'gc> {
234+
let event_type = AvmString::new_utf8(activation.gc(), event_type);
245235

246236
let text_event_cls = activation.avm2().classes().textevent;
247237
Self::from_class_and_args(
@@ -261,12 +251,17 @@ impl<'gc> EventObject<'gc> {
261251

262252
pub fn net_status_event(
263253
activation: &mut Activation<'_, 'gc>,
264-
info: Vec<(impl Into<AvmString<'gc>>, impl Into<AvmString<'gc>>)>,
254+
info: Vec<(&str, &str)>,
265255
) -> EventObject<'gc> {
256+
let event_type = AvmString::new_utf8(activation.gc(), "netStatus");
257+
266258
let info_object = ScriptObject::new_object(activation);
267259
for (key, value) in info {
260+
let key = AvmString::new_utf8(activation.gc(), key);
261+
let value = AvmString::new_utf8(activation.gc(), value);
262+
268263
info_object
269-
.set_string_property_local(key.into(), Value::String(value.into()), activation)
264+
.set_string_property_local(key, Value::String(value), activation)
270265
.unwrap();
271266
}
272267

@@ -275,7 +270,7 @@ impl<'gc> EventObject<'gc> {
275270
activation,
276271
net_status_cls,
277272
&[
278-
"netStatus".into(),
273+
event_type.into(),
279274
//bubbles
280275
false.into(),
281276
//cancelable
@@ -285,16 +280,13 @@ impl<'gc> EventObject<'gc> {
285280
)
286281
}
287282

288-
pub fn progress_event<S>(
283+
pub fn progress_event(
289284
activation: &mut Activation<'_, 'gc>,
290-
event_type: S,
285+
event_type: &str,
291286
bytes_loaded: usize,
292287
bytes_total: usize,
293-
) -> EventObject<'gc>
294-
where
295-
S: Into<AvmString<'gc>>,
296-
{
297-
let event_type: AvmString<'gc> = event_type.into();
288+
) -> EventObject<'gc> {
289+
let event_type = AvmString::new_utf8(activation.gc(), event_type);
298290

299291
let progress_event_cls = activation.avm2().classes().progressevent;
300292
Self::from_class_and_args(
@@ -314,17 +306,14 @@ impl<'gc> EventObject<'gc> {
314306
)
315307
}
316308

317-
pub fn focus_event<S>(
309+
pub fn focus_event(
318310
activation: &mut Activation<'_, 'gc>,
319-
event_type: S,
311+
event_type: &str,
320312
cancelable: bool,
321313
related_object: Option<InteractiveObject<'gc>>,
322314
key_code: u32,
323-
) -> EventObject<'gc>
324-
where
325-
S: Into<AvmString<'gc>>,
326-
{
327-
let event_type: AvmString<'gc> = event_type.into();
315+
) -> EventObject<'gc> {
316+
let event_type = AvmString::new_utf8(activation.gc(), event_type);
328317
let shift_key = activation.context.input.is_key_down(KeyCode::SHIFT);
329318

330319
let focus_event_cls = activation.avm2().classes().focusevent;
@@ -350,12 +339,14 @@ impl<'gc> EventObject<'gc> {
350339
error_msg: AvmString<'gc>,
351340
error_code: u32,
352341
) -> EventObject<'gc> {
342+
let event_type = AvmString::new_utf8(activation.gc(), "ioError");
343+
353344
let io_error_event_cls = activation.avm2().classes().ioerrorevent;
354345
Self::from_class_and_args(
355346
activation,
356347
io_error_event_cls,
357348
&[
358-
"ioError".into(),
349+
event_type.into(),
359350
false.into(),
360351
false.into(),
361352
error_msg.into(),
@@ -369,12 +360,14 @@ impl<'gc> EventObject<'gc> {
369360
status: u16,
370361
redirected: bool,
371362
) -> EventObject<'gc> {
363+
let event_type = AvmString::new_utf8(activation.gc(), "httpStatus");
364+
372365
let http_status_event_cls = activation.avm2().classes().httpstatusevent;
373366
Self::from_class_and_args(
374367
activation,
375368
http_status_event_cls,
376369
&[
377-
"httpStatus".into(),
370+
event_type.into(),
378371
false.into(),
379372
false.into(),
380373
status.into(),

core/src/net_connection.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::avm2::{Activation as Avm2Activation, Avm2, EventObject as Avm2EventOb
77
use crate::backend::navigator::{ErrorResponse, NavigatorBackend, OwnedFuture, Request};
88
use crate::context::UpdateContext;
99
use crate::loader::Error;
10-
use crate::string::AvmString;
1110
use crate::Player;
1211
use flash_lso::packet::{Header, Message, Packet};
1312
use flash_lso::types::{AMFVersion, Value as AmfValue};
@@ -540,14 +539,13 @@ impl FlashRemoting {
540539
match connection.object {
541540
NetConnectionObject::Avm2(object) => {
542541
let mut activation = Avm2Activation::from_nothing(uc);
543-
let url = AvmString::new_utf8(activation.gc(), response.url);
544542
let event = Avm2EventObject::net_status_event(
545543
&mut activation,
546544
vec![
547-
("code", "NetConnection.Call.Failed".into()),
548-
("level", "error".into()),
549-
("details", url),
550-
("description", "HTTP: Failed".into()),
545+
("code", "NetConnection.Call.Failed"),
546+
("level", "error"),
547+
("details", &response.url),
548+
("description", "HTTP: Failed"),
551549
],
552550
);
553551
Avm2::dispatch_event(activation.context, event, object.into());

core/src/streams.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,13 @@ impl<'gc> NetStream<'gc> {
398398
StreamManager::activate(context, self);
399399

400400
if notify {
401-
let trigger =
402-
AvmString::new_utf8(context.gc(), format!("Start Seeking {}", offset as u64));
401+
let trigger = format!("Start Seeking {}", offset as u64);
403402
self.trigger_status_event(
404403
context,
405404
vec![
406-
("description", trigger),
407-
("level", "status".into()),
408-
("code", "NetStream.SeekStart.Notify".into()),
405+
("description", &trigger),
406+
("level", "status"),
407+
("code", "NetStream.SeekStart.Notify"),
409408
],
410409
);
411410
}
@@ -1277,11 +1276,7 @@ impl<'gc> NetStream<'gc> {
12771276
}
12781277

12791278
/// Trigger a status event on the stream.
1280-
pub fn trigger_status_event(
1281-
self,
1282-
context: &mut UpdateContext<'gc>,
1283-
values: Vec<(impl Into<AvmString<'gc>>, impl Into<AvmString<'gc>>)>,
1284-
) {
1279+
pub fn trigger_status_event(self, context: &mut UpdateContext<'gc>, values: Vec<(&str, &str)>) {
12851280
let object = self.0.read().avm_object;
12861281
match object {
12871282
Some(AvmObject::Avm1(object)) => {
@@ -1295,8 +1290,11 @@ impl<'gc> NetStream<'gc> {
12951290
let info_object = Avm1ScriptObject::new(activation.gc(), Some(object_proto));
12961291

12971292
for (key, value) in values {
1293+
let key = AvmString::new_utf8(activation.gc(), key);
1294+
let value = AvmString::new_utf8(activation.gc(), value);
1295+
12981296
info_object
1299-
.set(key.into(), Avm1Value::String(value.into()), &mut activation)
1297+
.set(key, Avm1Value::String(value), &mut activation)
13001298
.expect("valid set");
13011299
}
13021300

0 commit comments

Comments
 (0)