Skip to content

Commit 3ba53e0

Browse files
committed
Implement NonLocalReturn.
1 parent b1484af commit 3ba53e0

3 files changed

Lines changed: 13 additions & 21 deletions

File tree

‎core/src/main/java/org/jruby/ir/interpreter/Interpreter.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,7 @@ private static IRubyObject processReturnOp(ThreadContext context, Instr instr, O
403403
case NONLOCAL_RETURN: {
404404
NonlocalReturnInstr ri = (NonlocalReturnInstr)instr;
405405
IRubyObject rv = (IRubyObject)retrieveOp(ri.getReturnValue(), context, self, currDynScope, currScope, temp);
406-
// If not in a lambda, check if this was a non-local return
407-
if (!IRRuntimeHelpers.inLambda(blockType)) {
408-
IRRuntimeHelpers.initiateNonLocalReturn(context, currDynScope, ri.maybeLambda, rv);
409-
}
410-
return rv;
406+
return IRRuntimeHelpers.initiateNonLocalReturn(context, currDynScope, blockType, ri.maybeLambda, rv);
411407
}
412408
}
413409
return null;

‎core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public static boolean inProc(Block.Type blockType) {
5353
/*
5454
* Handle non-local returns (ex: when nested in closures, root scopes of module/class/sclass bodies)
5555
*/
56-
public static void initiateNonLocalReturn(ThreadContext context, DynamicScope dynScope, boolean maybeLambda, IRubyObject returnValue) {
56+
public static IRubyObject initiateNonLocalReturn(ThreadContext context, DynamicScope dynScope, Block.Type blockType, boolean maybeLambda, IRubyObject returnValue) {
57+
// If not in a lambda, check if this was a non-local return
58+
if (IRRuntimeHelpers.inLambda(blockType)) return returnValue;
59+
5760
IRStaticScope scope = (IRStaticScope)dynScope.getStaticScope();
5861
IRScopeType scopeType = scope.getScopeType();
5962
boolean inDefineMethod = false;

‎core/src/main/java/org/jruby/ir/targets/JVMVisitor.java‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,21 +1657,14 @@ public void RuntimeHelperCall(RuntimeHelperCall runtimehelpercall) {
16571657

16581658
@Override
16591659
public void NonlocalReturnInstr(NonlocalReturnInstr returninstr) {
1660-
// disable for now
1661-
super.NonlocalReturnInstr(returninstr);
1662-
1663-
if (this.currentScope instanceof IRClosure) {
1664-
/* generate run-time call to check non-local-return, errors, etc */
1665-
SkinnyMethodAdapter a = jvmAdapter();
1666-
jvmMethod().loadContext(); // 1. ThreadContext
1667-
jvmMethod().loadStaticScope(); // 2. current scope
1668-
// 3. ref. to returnInstr.methodIdToReturnFrom
1669-
visit(returninstr.getReturnValue()); // 4. return value
1670-
// boolean about whether we are in a closure or not
1671-
// call to handle non-local return
1672-
} else {
1673-
/* throw IR-return-jump */
1674-
}
1660+
jvmMethod().loadContext();
1661+
jvmLoadLocal(DYNAMIC_SCOPE);
1662+
jvmMethod().loadBlockType();
1663+
jvmAdapter().ldc(returninstr.maybeLambda);
1664+
visit(returninstr.getReturnValue());
1665+
1666+
jvmMethod().invokeIRHelper("initiateNonLocalReturn", sig(IRubyObject.class, ThreadContext.class, DynamicScope.class, Block.Type.class, boolean.class, IRubyObject.class));
1667+
jvmMethod().returnValue();
16751668
}
16761669

16771670
@Override

0 commit comments

Comments
 (0)