@@ -16,8 +16,7 @@ use crate::{
1616 meta_ops,
1717 types:: { RegisterIndex , VarCount } ,
1818 AnyCallback , AnySequence , BadThreadMode , CallbackReturn , Closure , Context , Error ,
19- FromMultiValue , Fuel , Function , IntoMultiValue , SequencePoll , Stack , ThreadError , TypeError ,
20- Value ,
19+ FromMultiValue , Fuel , Function , IntoMultiValue , SequencePoll , Stack , TypeError , VMError , Value ,
2120} ;
2221
2322use super :: run_vm;
@@ -195,13 +194,22 @@ impl<'gc> Thread<'gc> {
195194 while state. mode ( ) == ThreadMode :: Normal {
196195 match state. frames . pop ( ) . expect ( "no frame to step" ) {
197196 Frame :: Callback ( callback) => {
198- fuel. consume_fuel ( FUEL_PER_CALLBACK ) ;
197+ let mut rfuel = match fuel. recurse ( ) {
198+ Ok ( r) => r,
199+ Err ( err) => {
200+ state. unwind ( & ctx, err. into ( ) ) ;
201+ continue ;
202+ }
203+ } ;
204+
205+ rfuel. consume_fuel ( FUEL_PER_CALLBACK ) ;
199206 state. frames . push ( Frame :: Calling ) ;
200207
201208 assert ! ( state. error. is_none( ) ) ;
202209 let mut stack = mem:: replace ( & mut state. external_stack , Stack :: new ( & ctx) ) ;
203210 drop ( state) ;
204- let seq = callback. call ( ctx, fuel, & mut stack) ;
211+ let seq = callback. call ( ctx, & mut rfuel, & mut stack) ;
212+ drop ( rfuel) ;
205213 state = self . 0 . borrow_mut ( & ctx) ;
206214 state. external_stack = stack;
207215
@@ -216,18 +224,27 @@ impl<'gc> Thread<'gc> {
216224 }
217225 }
218226 Frame :: Sequence ( mut sequence) => {
219- fuel. consume_fuel ( FUEL_PER_SEQ_STEP ) ;
227+ let mut rfuel = match fuel. recurse ( ) {
228+ Ok ( r) => r,
229+ Err ( err) => {
230+ state. unwind ( & ctx, err. into ( ) ) ;
231+ continue ;
232+ }
233+ } ;
234+
235+ rfuel. consume_fuel ( FUEL_PER_SEQ_STEP ) ;
220236 state. frames . push ( Frame :: Calling ) ;
221237
222238 let mut stack = mem:: replace ( & mut state. external_stack , Stack :: new ( & ctx) ) ;
223239 let error = state. error . take ( ) ;
224240 drop ( state) ;
225241 let fin = if let Some ( error) = error {
226242 assert ! ( stack. is_empty( ) ) ;
227- sequence. error ( ctx, fuel , error, & mut stack)
243+ sequence. error ( ctx, & mut rfuel , error, & mut stack)
228244 } else {
229- sequence. poll ( ctx, fuel , & mut stack)
245+ sequence. poll ( ctx, & mut rfuel , & mut stack)
230246 } ;
247+ drop ( rfuel) ;
231248 state = self . 0 . borrow_mut ( & ctx) ;
232249 state. external_stack = stack;
233250
@@ -414,11 +431,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
414431 }
415432
416433 // Place the current frame's varargs at the given register, expecting the given count
417- pub ( crate ) fn varargs (
418- & mut self ,
419- dest : RegisterIndex ,
420- count : VarCount ,
421- ) -> Result < ( ) , ThreadError > {
434+ pub ( crate ) fn varargs ( & mut self , dest : RegisterIndex , count : VarCount ) -> Result < ( ) , VMError > {
422435 match self . state . frames . last_mut ( ) {
423436 Some ( Frame :: Lua {
424437 bottom,
@@ -427,7 +440,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
427440 ..
428441 } ) => {
429442 if * is_variable {
430- return Err ( ThreadError :: ExpectedVariable ( false ) ) ;
443+ return Err ( VMError :: ExpectedVariableStack ( false ) ) ;
431444 }
432445
433446 let varargs_start = * bottom + 1 ;
@@ -459,7 +472,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
459472 mc : & Mutation < ' gc > ,
460473 table_base : RegisterIndex ,
461474 count : VarCount ,
462- ) -> Result < ( ) , ThreadError > {
475+ ) -> Result < ( ) , VMError > {
463476 let Some ( & mut Frame :: Lua {
464477 base,
465478 ref mut is_variable,
@@ -471,7 +484,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
471484 } ;
472485
473486 if count. is_variable ( ) != * is_variable {
474- return Err ( ThreadError :: ExpectedVariable ( count. is_variable ( ) ) ) ;
487+ return Err ( VMError :: ExpectedVariableStack ( count. is_variable ( ) ) ) ;
475488 }
476489
477490 let table_ind = base + table_base. 0 as usize ;
@@ -527,7 +540,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
527540 func : RegisterIndex ,
528541 args : VarCount ,
529542 returns : VarCount ,
530- ) -> Result < ( ) , ThreadError > {
543+ ) -> Result < ( ) , VMError > {
531544 match self . state . frames . last_mut ( ) {
532545 Some ( Frame :: Lua {
533546 expected_return,
@@ -536,7 +549,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
536549 ..
537550 } ) => {
538551 if * is_variable != args. is_variable ( ) {
539- return Err ( ThreadError :: ExpectedVariable ( args. is_variable ( ) ) ) ;
552+ return Err ( VMError :: ExpectedVariableStack ( args. is_variable ( ) ) ) ;
540553 }
541554
542555 * expected_return = Some ( LuaReturn :: Normal ( returns) ) ;
@@ -598,7 +611,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
598611 func : RegisterIndex ,
599612 arg_count : u8 ,
600613 returns : VarCount ,
601- ) -> Result < ( ) , ThreadError > {
614+ ) -> Result < ( ) , VMError > {
602615 match self . state . frames . last_mut ( ) {
603616 Some ( Frame :: Lua {
604617 expected_return,
@@ -607,7 +620,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
607620 ..
608621 } ) => {
609622 if * is_variable {
610- return Err ( ThreadError :: ExpectedVariable ( false ) ) ;
623+ return Err ( VMError :: ExpectedVariableStack ( false ) ) ;
611624 }
612625
613626 consume_call_fuel ( self . fuel , arg_count as usize ) ;
@@ -672,7 +685,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
672685 func : Function < ' gc > ,
673686 args : & [ Value < ' gc > ] ,
674687 ret_index : Option < RegisterIndex > ,
675- ) -> Result < ( ) , ThreadError > {
688+ ) -> Result < ( ) , VMError > {
676689 match self . state . frames . last_mut ( ) {
677690 Some ( Frame :: Lua {
678691 expected_return,
@@ -682,7 +695,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
682695 ..
683696 } ) => {
684697 if * is_variable {
685- return Err ( ThreadError :: ExpectedVariable ( false ) ) ;
698+ return Err ( VMError :: ExpectedVariableStack ( false ) ) ;
686699 }
687700
688701 consume_call_fuel ( self . fuel , args. len ( ) ) ;
@@ -738,7 +751,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
738751 ctx : Context < ' gc > ,
739752 func : RegisterIndex ,
740753 args : VarCount ,
741- ) -> Result < ( ) , ThreadError > {
754+ ) -> Result < ( ) , VMError > {
742755 match self . state . frames . last ( ) {
743756 Some ( & Frame :: Lua {
744757 bottom,
@@ -747,7 +760,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
747760 ..
748761 } ) => {
749762 if is_variable != args. is_variable ( ) {
750- return Err ( ThreadError :: ExpectedVariable ( args. is_variable ( ) ) ) ;
763+ return Err ( VMError :: ExpectedVariableStack ( args. is_variable ( ) ) ) ;
751764 }
752765
753766 self . state . close_upvalues ( & ctx, bottom) ;
@@ -814,7 +827,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
814827 mc : & Mutation < ' gc > ,
815828 start : RegisterIndex ,
816829 count : VarCount ,
817- ) -> Result < ( ) , ThreadError > {
830+ ) -> Result < ( ) , VMError > {
818831 match self . state . frames . pop ( ) {
819832 Some ( Frame :: Lua {
820833 bottom,
@@ -823,7 +836,7 @@ impl<'gc, 'a> LuaFrame<'gc, 'a> {
823836 ..
824837 } ) => {
825838 if is_variable != count. is_variable ( ) {
826- return Err ( ThreadError :: ExpectedVariable ( count. is_variable ( ) ) ) ;
839+ return Err ( VMError :: ExpectedVariableStack ( count. is_variable ( ) ) ) ;
827840 }
828841 self . state . close_upvalues ( mc, bottom) ;
829842
0 commit comments