Skip to content

Commit

Permalink
Restore $! in ensure blocks only if it has been rescued
Browse files Browse the repository at this point in the history
* Also updated related stale FIXME.
  • Loading branch information
subbuss committed May 10, 2015
1 parent 74db0b7 commit 48d0f64
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,10 @@ private void emitEnsureBlocks(IRLoop loop) {
// ensure blocks from the loops they are present in.
if (loop != null && ebi.innermostLoop != loop) break;

// SSS FIXME: Should $! be restored before or after the ensure block is run?
addInstr(new PutGlobalVarInstr("$!", ebi.savedGlobalException));
// $! should be restored before the ensure block is run
if (ebi.savedGlobalException != null) {
addInstr(new PutGlobalVarInstr("$!", ebi.savedGlobalException));
}

// Clone into host scope
ebi.cloneIntoHostScope(this);
Expand Down Expand Up @@ -2225,9 +2227,11 @@ public Operand buildEnsureInternal(Node ensureBodyNode, Node ensurerNode) {
activeRescuers.peek());

ensureBodyBuildStack.push(ebi);
// Restore $!
addInstr(new PutGlobalVarInstr("$!", savedGlobalException));
ebi.savedGlobalException = savedGlobalException;
// Restore $! if we the exception was rescued
if (ensureBodyNode != null) {
addInstr(new PutGlobalVarInstr("$!", savedGlobalException));
ebi.savedGlobalException = savedGlobalException;
}
Operand ensureRetVal = ensurerNode == null ? manager.getNil() : build(ensurerNode);
ensureBodyBuildStack.pop();

Expand Down

0 comments on commit 48d0f64

Please sign in to comment.