avm2: Don't mutably borrow for pos()

This commit is contained in:
CUB3D 2021-04-02 23:17:28 +01:00 committed by kmeisthax
parent 96f89a1643
commit d5c2e5559b
5 changed files with 26 additions and 5 deletions

View File

@ -2393,14 +2393,14 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
) -> Result<FrameControl<'gc>, Error> {
let index = self.context.avm2.pop().coerce_to_i32(self)?;
//TODO: can we just subtract the size of *this* op to save some of this maths
let offset = case_offsets
.get(index as usize)
.copied()
.unwrap_or(default_offset)
+ current_pos as i32;
let current_pos = reader.pos(full_data) as i32;
reader.seek(full_data, offset - current_pos);
+ current_pos as i32
- reader.pos(full_data) as i32;
reader.seek(full_data, offset);
Ok(FrameControl::Continue)
}

View File

@ -15,6 +15,11 @@ impl<'a> ReadSwfExt<'a> for Reader<'a> {
fn as_mut_slice(&mut self) -> &mut &'a [u8] {
&mut self.input
}
#[inline(always)]
fn as_slice(&self) -> &'a [u8] {
&self.input
}
}
impl<'a> Reader<'a> {

View File

@ -12,6 +12,11 @@ impl<'a> ReadSwfExt<'a> for Reader<'a> {
fn as_mut_slice(&mut self) -> &mut &'a [u8] {
&mut self.input
}
#[inline(always)]
fn as_slice(&self) -> &'a [u8] {
&self.input
}
}
impl<'a> Reader<'a> {

View File

@ -6,9 +6,15 @@ use std::io::{self, Read};
pub trait ReadSwfExt<'a> {
fn as_mut_slice(&mut self) -> &mut &'a [u8];
fn as_slice(&self) -> &'a [u8];
fn pos(&self, data: &[u8]) -> usize {
self.as_slice().as_ptr() as usize - data.as_ptr() as usize
}
// TODO: Make this fallible?
fn seek(&mut self, data: &'a [u8], relative_offset: isize) {
let mut pos = self.as_mut_slice().as_ptr() as usize - data.as_ptr() as usize;
let mut pos = self.pos(data);
pos = (pos as isize + relative_offset) as usize;
pos = pos.min(data.len());
*self.as_mut_slice() = &data[pos..];

View File

@ -243,6 +243,11 @@ impl<'a> ReadSwfExt<'a> for Reader<'a> {
fn as_mut_slice(&mut self) -> &mut &'a [u8] {
&mut self.input
}
#[inline(always)]
fn as_slice(&self) -> &'a [u8] {
&self.input
}
}
impl<'a> Reader<'a> {