Skip to content

Instantly share code, notes, and snippets.

@donaldh
Created March 13, 2015 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldh/6d1a12182b49fb5947e5 to your computer and use it in GitHub Desktop.
Save donaldh/6d1a12182b49fb5947e5 to your computer and use it in GitHub Desktop.
Stop leaking UnwindException
diff --git a/src/vm/jvm/QAST/Compiler.nqp b/src/vm/jvm/QAST/Compiler.nqp
index c545b1c..3e56ac7 100644
--- a/src/vm/jvm/QAST/Compiler.nqp
+++ b/src/vm/jvm/QAST/Compiler.nqp
@@ -1708,7 +1708,23 @@ QAST::OperationsJAST.add_core_op('handle', :!inlinable, sub ($qastcomp, $op) {
$tryil.append($prores.jast);
$*STACK.obtain($tryil, $prores);
$tryil.append(JAST::Instruction.new( :op('astore'), $result ));
-
+
+ my $posthan := JAST::InstructionList.new();
+ my $nclab := JAST::Label.new( :name( $qastcomp.unique('non_cont_ex') ) );
+ $posthan.append($DUP);
+ $posthan.append(JAST::Instruction.new( :op('instanceof'), $TYPE_EX_CONT ));
+ $posthan.append(JAST::Instruction.new( :op('ifeq'), $nclab ));
+ $posthan.append($ATHROW);
+ $posthan.append($nclab);
+ $posthan.append($ALOAD_1);
+ $posthan.append($SWAP);
+ $posthan.append(JAST::Instruction.new( :op('invokestatic'),
+ $TYPE_EH, 'dieInternal', $TYPE_EX_RT, $TYPE_TC, $TYPE_THROWABLE ));
+ $posthan.append($ATHROW);
+
+ my $wrapped := JAST::InstructionList.new();
+ $wrapped.append(JAST::TryCatch.new( :try($tryil), :catch($posthan), :type($TYPE_THROWABLE) ));
+
# The catch part just handles unwind; grab the result. Also check "exit
# after unwind" flag, used to force this whole block to exit.
my $catchil := JAST::InstructionList.new();
@@ -1731,7 +1747,7 @@ QAST::OperationsJAST.add_core_op('handle', :!inlinable, sub ($qastcomp, $op) {
# Wrap it all up in try/catch etc.
$il.append($qastcomp.delimit_handler(
- JAST::TryCatch.new( :try($tryil), :catch($catchil), :type($TYPE_EX_UNWIND) ),
+ JAST::TryCatch.new( :try($wrapped), :catch($catchil), :type($TYPE_EX_UNWIND) ),
$*HANDLER_IDX, $handler));
# Evaluate to the result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment