diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 4d9754c..be00f16 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -657,7 +657,7 @@ impl ClosureRunner { vec![value.unwrap().clone(), index_value.clone()], ) { Ok(value) => { - StackValue::Value(value.first().unwrap().clone()) + StackValue::Value(value?.first().unwrap().clone()) } Err(_) => StackValue::Value(Value::Nil), } @@ -816,7 +816,7 @@ impl ClosureRunner { let mut metamethod_params = vec![value.clone()]; metamethod_params.extend(params); let ret_values = - self.call_metamethod(&metatable, "__call", metamethod_params)?; + self.call_metamethod(&metatable, "__call", metamethod_params)??; if *ret_len != 0 { for i in 0..=(*ret_len - 2) { @@ -890,7 +890,7 @@ impl ClosureRunner { "__concat", lhs, rhs, - )?), + )??), ); } @@ -903,7 +903,7 @@ impl ClosureRunner { "__add", lhs, rhs, - )?), + )??), ); } Instruction::Sub(res, lhs, rhs) => { @@ -915,7 +915,7 @@ impl ClosureRunner { "__sub", lhs, rhs, - )?), + )??), ); } Instruction::Mult(res, lhs, rhs) => { @@ -927,7 +927,7 @@ impl ClosureRunner { "__mult", lhs, rhs, - )?), + )??), ); } Instruction::Div(res, lhs, rhs) => { @@ -939,7 +939,7 @@ impl ClosureRunner { "__div", lhs, rhs, - )?), + )??), ); } Instruction::IDiv(res, lhs, rhs) => { @@ -951,7 +951,7 @@ impl ClosureRunner { "__idiv", lhs, rhs, - )?), + )??), ); } Instruction::Mod(res, lhs, rhs) => { @@ -963,7 +963,7 @@ impl ClosureRunner { "__mod", lhs, rhs, - )?), + )??), ); } Instruction::Exp(res, lhs, rhs) => { @@ -975,7 +975,7 @@ impl ClosureRunner { "__exp", lhs, rhs, - )?), + )??), ); } @@ -988,7 +988,7 @@ impl ClosureRunner { "__band", lhs, rhs, - )?), + )??), ); } Instruction::BitOr(res, lhs, rhs) => { @@ -1000,7 +1000,7 @@ impl ClosureRunner { "__bor", lhs, rhs, - )?), + )??), ); } Instruction::BitXOr(res, lhs, rhs) => { @@ -1012,7 +1012,7 @@ impl ClosureRunner { "__bxor", lhs, rhs, - )?), + )??), ); } Instruction::BitSRight(res, lhs, rhs) => { @@ -1024,7 +1024,7 @@ impl ClosureRunner { "__shr", lhs, rhs, - )?), + )??), ); } Instruction::BitSLeft(res, lhs, rhs) => { @@ -1036,7 +1036,7 @@ impl ClosureRunner { "__shl", lhs, rhs, - )?), + )??), ); } @@ -1051,7 +1051,7 @@ impl ClosureRunner { metatable, "__eq", vec![lhs.clone(), rhs.clone()], - )? + )?? .first() .unwrap() .is_truthy(), @@ -1073,7 +1073,7 @@ impl ClosureRunner { "__lt", lhs.clone(), rhs.clone(), - )? + )?? .is_truthy(), ))), ); @@ -1088,7 +1088,7 @@ impl ClosureRunner { "__le", lhs.clone(), rhs.clone(), - )? + )?? .is_truthy(), ))), ); @@ -1115,7 +1115,7 @@ impl ClosureRunner { value.unm(), "__unm", value.clone(), - )?), + )??), ); } Instruction::Len(res, reg) => { @@ -1130,7 +1130,7 @@ impl ClosureRunner { Ok(result) => { self.set_stack( *res, - StackValue::Value(result.first().unwrap().clone()), + StackValue::Value(result?.first().unwrap().clone()), ); } Err(_) => { @@ -1216,20 +1216,20 @@ impl ClosureRunner { value: Result, metamethod: &str, param: Value, - ) -> Result { + ) -> Result, RuntimeError> { match value { - Ok(value) => Ok(value), + Ok(value) => Ok(Ok(value)), Err(_) => match ¶m { Value::Table { metatable, .. } => { if metatable .borrow() .contains_key(&IndexableValue::String(metamethod.to_owned())) { - Ok(self - .call_metamethod(metatable, metamethod, vec![param.clone()])? - .into_iter() - .next() - .unwrap()) + Ok(extract_ret_value(self.call_metamethod( + metatable, + metamethod, + vec![param.clone()], + )?)) } else { Err(RuntimeError::InvalidUnop( metamethod.to_owned(), @@ -1251,35 +1251,31 @@ impl ClosureRunner { metamethod: &str, lhs: Value, rhs: Value, - ) -> Result { + ) -> Result, RuntimeError> { match value { - Ok(value) => Ok(value), + Ok(value) => Ok(Ok(value)), Err(_) => { if let Value::Table { metatable, .. } = &lhs { if metatable .borrow() .contains_key(&IndexableValue::String(metamethod.to_owned())) { - Ok(self - .call_metamethod(&metatable, metamethod, vec![lhs.clone(), rhs])? - .into_iter() - .next() - .unwrap()) + Ok(extract_ret_value(self.call_metamethod( + &metatable, + metamethod, + vec![lhs.clone(), rhs], + )?)) } else { if let Value::Table { metatable, .. } = &rhs { if metatable .borrow() .contains_key(&IndexableValue::String(metamethod.to_owned())) { - Ok(self - .call_metamethod( - &metatable, - metamethod, - vec![lhs, rhs.clone()], - )? - .into_iter() - .next() - .unwrap()) + Ok(extract_ret_value(self.call_metamethod( + &metatable, + metamethod, + vec![lhs, rhs.clone()], + )?)) } else { Err(RuntimeError::InvalidBinop( metamethod.to_owned(), @@ -1300,11 +1296,11 @@ impl ClosureRunner { .borrow() .contains_key(&IndexableValue::String(metamethod.to_owned())) { - Ok(self - .call_metamethod(&metatable, metamethod, vec![lhs, rhs.clone()])? - .into_iter() - .next() - .unwrap()) + Ok(extract_ret_value(self.call_metamethod( + &metatable, + metamethod, + vec![lhs, rhs.clone()], + )?)) } else { Err(RuntimeError::InvalidBinop( metamethod.to_owned(), @@ -1328,25 +1324,28 @@ impl ClosureRunner { metatable: &Table, metamethod: &str, params: Vec, - ) -> Result, RuntimeError> { + ) -> Result, RuntimeError>, RuntimeError> { if let Some(value) = metatable .borrow() .get(&IndexableValue::String(metamethod.to_owned())) { match value { Value::RustFunction(function) => { - let result = function.borrow_mut().execute(params)?; + let result = function.borrow_mut().execute(params); Ok(result) } Value::Function(closure) => { let mut runnable = closure.run(params); #[allow(unused_assignments)] - let mut return_value = None; + let mut return_value = Ok(None); while { - return_value = runnable.next()?; - return_value.is_none() + return_value = runnable.next(); + match return_value { + Ok(None) => true, + _ => false, + } } {} - Ok(return_value.unwrap()) + Ok(return_value.map(|v| v.unwrap())) } _ => Err(RuntimeError::MetafunctionNotCallable(value.clone())), } @@ -1371,3 +1370,7 @@ impl ClosureRunner { ) } } + +fn extract_ret_value(values: Result, RuntimeError>) -> Result { + values.map(|v| v.into_iter().next().unwrap()) +}