`SwfMovie::from_data` is falliable and we should not hide those errors behind panics.

This commit is contained in:
David Wendt 2020-02-26 18:15:01 -05:00
parent 0313225164
commit ded7737ac0
3 changed files with 13 additions and 10 deletions

View File

@ -294,9 +294,9 @@ impl<'gc> Loader<'gc> {
},
)?;
let data = fetch.await;
if let Ok(data) = data {
let movie = Arc::new(SwfMovie::from_data(&data));
let data = (fetch.await).and_then(|data| Ok((data.len(), SwfMovie::from_data(&data)?)));
if let Ok((length, movie)) = data {
let movie = Arc::new(movie);
player
.lock()
@ -322,8 +322,8 @@ impl<'gc> Loader<'gc> {
&[
"onLoadProgress".into(),
Value::Object(broadcaster),
data.len().into(),
data.len().into(),
length.into(),
length.into(),
],
);
avm.run_stack_till_empty(uc)?;
@ -374,6 +374,8 @@ impl<'gc> Loader<'gc> {
//TODO: Inspect the fetch error.
//This requires cooperation from the backend to send abstract
//error types we can actually inspect.
//This also can get errors from decoding an invalid SWF file,
//too. We should distinguish those to player code.
player.lock().expect("Could not lock player!!").update(
|avm, uc| -> Result<(), Error> {
let (clip, broadcaster) = match uc.load_manager.get_loader(handle) {

View File

@ -149,7 +149,7 @@ impl Player {
input: Input,
swf_data: Vec<u8>,
) -> Result<Arc<Mutex<Self>>, Error> {
let movie = Arc::new(SwfMovie::from_data(&swf_data));
let movie = Arc::new(SwfMovie::from_data(&swf_data)?);
info!(
"{}x{}",

View File

@ -2,7 +2,8 @@ use gc_arena::Collect;
use std::sync::Arc;
use swf::{Header, TagCode};
pub type DecodeResult = Result<(), Box<dyn std::error::Error>>;
pub type Error = Box<dyn std::error::Error>;
pub type DecodeResult = Result<(), Error>;
pub type SwfStream<R> = swf::read::Reader<std::io::Cursor<R>>;
/// An open, fully parsed SWF movie ready to play back, either in a Player or a
@ -41,8 +42,8 @@ impl SwfMovie {
}
/// Construct a movie based on the contents of the SWF datastream.
pub fn from_data(swf_data: &[u8]) -> Self {
let swf_stream = swf::read::read_swf_header(&swf_data[..]).unwrap();
pub fn from_data(swf_data: &[u8]) -> Result<Self, Error> {
let swf_stream = swf::read::read_swf_header(&swf_data[..])?;
let header = swf_stream.header;
let mut reader = swf_stream.reader;
@ -66,7 +67,7 @@ impl SwfMovie {
data
};
Self { header, data }
Ok(Self { header, data })
}
pub fn header(&self) -> &Header {