Skip to content

Commit

Permalink
Push lambda non-local returns & breaks through local exceptions
Browse files Browse the repository at this point in the history
This ensures that in the block call protocol scenario,
for lambdas as well as non-lambdas, all non-return control flow
(exceptions, breaks, nonlocal-returns) goes through the GEB.
subbuss committed Dec 18, 2015
1 parent a9cf20c commit 11e3766
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java
Original file line number Diff line number Diff line change
@@ -121,9 +121,9 @@ public static void checkForLJE(ThreadContext context, DynamicScope dynScope, boo
* Handle non-local returns (ex: when nested in closures, root scopes of module/class/sclass bodies)
*/
public static IRubyObject initiateNonLocalReturn(ThreadContext context, DynamicScope dynScope, Block.Type blockType, IRubyObject returnValue) {
// If not in a lambda, check if this was a non-local return
if (IRRuntimeHelpers.inLambda(blockType)) return returnValue;
if (IRRuntimeHelpers.inLambda(blockType)) throw new IRWrappedLambdaReturnValue(returnValue);

// If not in a lambda, check if this was a non-local return
while (dynScope != null) {
StaticScope ss = dynScope.getStaticScope();
// SSS FIXME: Why is scopeType empty? Looks like this static-scope
@@ -171,7 +171,7 @@ public static IRubyObject initiateBreak(ThreadContext context, DynamicScope dynS
// Ensures would already have been run since the IR builder makes
// sure that ensure code has run before we hit the break. Treat
// the break as a regular return from the closure.
return breakValue;
throw new IRWrappedLambdaReturnValue(breakValue);
} else {
StaticScope scope = dynScope.getStaticScope();
IRScopeType scopeType = scope.getScopeType();
@@ -193,7 +193,9 @@ public static IRubyObject initiateBreak(ThreadContext context, DynamicScope dynS

@JIT
public static IRubyObject handleBreakAndReturnsInLambdas(ThreadContext context, StaticScope scope, DynamicScope dynScope, Object exc, Block.Type blockType) throws RuntimeException {
if ((exc instanceof IRBreakJump) && inNonMethodBodyLambda(scope, blockType)) {
if (exc instanceof IRWrappedLambdaReturnValue) {
return ((IRWrappedLambdaReturnValue)exc).returnValue;
} else if ((exc instanceof IRBreakJump) && inNonMethodBodyLambda(scope, blockType)) {
// We just unwound all the way up because of a non-local break
context.setSavedExceptionInLambda(IRException.BREAK_LocalJumpError.getException(context.getRuntime()));
return null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jruby.ir.runtime;

import org.jruby.exceptions.Unrescuable;
import org.jruby.runtime.builtin.IRubyObject;

public class IRWrappedLambdaReturnValue extends RuntimeException implements Unrescuable {
public final IRubyObject returnValue;

public IRWrappedLambdaReturnValue(IRubyObject v) {
this.returnValue = v;
}
}

0 comments on commit 11e3766

Please sign in to comment.