From e8db01a09ab5b171cf57718a171d550e32a05cb3 Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Tue, 27 Aug 2019 19:40:00 +0200 Subject: [PATCH] Refactor block stack unwinding to enable finally execution. --- src/compile.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/compile.rs b/src/compile.rs index 2a95190..70bc8f2 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -708,12 +708,20 @@ impl Compiler { &mut self, body: &[ast::Statement], handlers: &[ast::ExceptHandler], - orelse: &Option>, - finalbody: &Option>, + orelse: &Option, + finalbody: &Option, ) -> Result<(), CompileError> { let mut handler_label = self.new_label(); let finally_label = self.new_label(); let else_label = self.new_label(); + + // Setup a finally block if we have a finally statement. + if finalbody.is_some() { + self.emit(Instruction::SetupFinally { + handler: finally_label, + }); + } + // try: self.emit(Instruction::SetupExcept { handler: handler_label, @@ -798,8 +806,9 @@ impl Compiler { self.set_label(finally_label); if let Some(statements) = finalbody { self.compile_statements(statements)?; + self.emit(Instruction::EndFinally); } - // unimplemented!(); + Ok(()) }