Skip to content

Commit 508fcd6

Browse files
committed
pool_string should return a Ref<str> just like value::abc_string.
1 parent a406bda commit 508fcd6

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

core/src/avm2/activation.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::avm2::{value, Avm2, Error};
1313
use crate::context::UpdateContext;
1414
use gc_arena::{Collect, Gc, GcCell, MutationContext};
1515
use smallvec::SmallVec;
16+
use std::cell::Ref;
1617
use std::io::Cursor;
1718
use swf::avm2::read::Reader;
1819
use swf::avm2::types::{
@@ -322,12 +323,27 @@ impl<'a, 'gc: 'a> Activation<'a, 'gc> {
322323
}
323324

324325
/// Retrieve a string from the current constant pool.
325-
fn pool_string(
326+
fn pool_string<'b>(
326327
&self,
327-
method: Gc<'gc, BytecodeMethod<'gc>>,
328+
method: &'b BytecodeMethod<'gc>,
328329
index: Index<String>,
329-
) -> Result<String, Error> {
330-
Ok(value::abc_string(&method.abc(), index)?.to_string())
330+
) -> Result<Ref<'b, str>, Error> {
331+
let mut maybe_error = None;
332+
let maybe_ref = Ref::map(method.abc_ref(), |abc| {
333+
match value::abc_string(abc, index) {
334+
Ok(s) => s,
335+
Err(e) => {
336+
maybe_error = Some(e);
337+
""
338+
}
339+
}
340+
});
341+
342+
if let Some(err) = maybe_error {
343+
Err(err)
344+
} else {
345+
Ok(maybe_ref)
346+
}
331347
}
332348

333349
/// Retrieve a namespace from the current constant pool.
@@ -585,7 +601,8 @@ impl<'a, 'gc: 'a> Activation<'a, 'gc> {
585601
method: Gc<'gc, BytecodeMethod<'gc>>,
586602
value: Index<String>,
587603
) -> Result<FrameControl<'gc>, Error> {
588-
self.avm2.push(self.pool_string(method, value)?);
604+
self.avm2
605+
.push(self.pool_string(&method, value)?.to_string());
589606
Ok(FrameControl::Continue)
590607
}
591608

core/src/avm2/method.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::avm2::value::Value;
77
use crate::avm2::Error;
88
use crate::context::UpdateContext;
99
use gc_arena::{Collect, CollectionContext, Gc, MutationContext};
10+
use std::cell::Ref;
1011
use std::fmt;
1112
use std::rc::Rc;
1213
use swf::avm2::types::{AbcFile, Index, Method as AbcMethod, MethodBody as AbcMethodBody};
@@ -98,6 +99,11 @@ impl<'gc> BytecodeMethod<'gc> {
9899
self.txunit.abc()
99100
}
100101

102+
/// Retrieve a reference to the underlying ABC file.
103+
pub fn abc_ref(&self) -> Ref<AbcFile> {
104+
self.txunit.abc_ref()
105+
}
106+
101107
/// Get the underlying translation unit this method was defined in.
102108
pub fn translation_unit(&self) -> TranslationUnit<'gc> {
103109
self.txunit

core/src/avm2/script.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::avm2::method::{BytecodeMethod, Method};
55
use crate::avm2::r#trait::Trait;
66
use crate::avm2::Error;
77
use gc_arena::{Collect, Gc, GcCell, MutationContext};
8+
use std::cell::Ref;
89
use std::collections::HashMap;
910
use std::mem::drop;
1011
use std::rc::Rc;
@@ -64,6 +65,12 @@ impl<'gc> TranslationUnit<'gc> {
6465
self.0.read().abc.0.clone()
6566
}
6667

68+
/// Retrieve a reference to the underlying `AbcFile` for this translation
69+
/// unit.
70+
pub fn abc_ref(&self) -> Ref<AbcFile> {
71+
Ref::map(self.0.read(), |tu| &*tu.abc.0)
72+
}
73+
6774
/// Load a method from the ABC file and return it's method definition.
6875
pub fn load_method(
6976
self,

0 commit comments

Comments
 (0)