Skip to content

Commit

Permalink
avm2: Cache method body index on Method
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian17 committed Sep 9, 2024
1 parent 8ab225a commit 176b70b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
19 changes: 4 additions & 15 deletions core/src/avm2/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl<'gc> BytecodeMethod<'gc> {
let abc = txunit.abc();
let mut signature = Vec::new();
let mut return_type = Multiname::any();
let mut abc_method_body = None;

if abc.methods.get(abc_method.0 as usize).is_some() {
let method = &abc.methods[abc_method.0 as usize];
Expand All @@ -178,28 +179,16 @@ impl<'gc> BytecodeMethod<'gc> {
.deref()
.clone();

for (index, method_body) in abc.method_bodies.iter().enumerate() {
if method_body.method.0 == abc_method.0 {
return Ok(Self {
txunit,
abc: txunit.abc(),
abc_method: abc_method.0,
abc_method_body: Some(index as u32),
verified_info: GcCell::new(activation.context.gc_context, None),
signature,
return_type,
is_function,
activation_class: Lock::new(None),
});
}
if let Some(body) = method.body {
abc_method_body = Some(body.0);
}
}

Ok(Self {
txunit,
abc: txunit.abc(),
abc_method: abc_method.0,
abc_method_body: None,
abc_method_body,
verified_info: GcCell::new(activation.context.gc_context, None),
signature,
return_type,
Expand Down
11 changes: 9 additions & 2 deletions swf/src/avm2/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ impl<'a> Reader<'a> {

let len = self.read_u30()?;
let mut method_bodies = Vec::with_capacity(len as usize);
for _ in 0..len {
method_bodies.push(self.read_method_body()?);
for body_idx in 0..len {
let body = self.read_method_body()?;
if methods[body.method.0 as usize].body.is_some() {
// TODO: this should somehow throw error 1121 in FP.
return Err(Error::invalid_data("Duplicate method body"));
}
methods[body.method.0 as usize].body = Some(Index::new(body_idx));
method_bodies.push(body);
}

Ok(AbcFile {
Expand Down Expand Up @@ -279,6 +285,7 @@ impl<'a> Reader<'a> {
params,
return_type,
flags,
body: None,
})
}

Expand Down
2 changes: 2 additions & 0 deletions swf/src/avm2/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub struct Method {
pub params: Vec<MethodParam>,
pub return_type: Index<Multiname>,
pub flags: MethodFlags,
// not an ABC MethodInfo property; bound when parsing MethodBodies
pub body: Option<Index<MethodBody>>,
}

bitflags! {
Expand Down
2 changes: 2 additions & 0 deletions swf/src/test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2688,12 +2688,14 @@ pub fn avm2_tests() -> Vec<Avm2TestData> {
params: vec![],
return_type: Index::new(1),
flags: MethodFlags::empty(),
body: Some(Index::new(0)),
},
Method {
name: Index::new(0),
params: vec![],
return_type: Index::new(0),
flags: MethodFlags::empty(),
body: Some(Index::new(1)),
},
],
metadata: vec![],
Expand Down

0 comments on commit 176b70b

Please sign in to comment.