Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.1] Return in a lambda always returns from the block, even when &-ified #1445

Merged
merged 1 commit into from Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions core/src/main/java/org/jruby/runtime/CompiledBlock.java
Expand Up @@ -112,9 +112,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject value, Binding bindi

try {
return callback.call(context, self, realArg, block);
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -134,9 +133,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject

try {
return callback.call(context, self, realArg, block);
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down
15 changes: 6 additions & 9 deletions core/src/main/java/org/jruby/runtime/CompiledBlock19.java
Expand Up @@ -116,9 +116,8 @@ private IRubyObject yieldSpecificInternal(ThreadContext context, IRubyObject[] a

try {
return callback.call(context, self, args, Block.NULL_BLOCK);
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -134,9 +133,8 @@ protected IRubyObject doYield(ThreadContext context, IRubyObject value, Binding
try {
IRubyObject[] realArgs = setupBlockArg(context.runtime, value, self, type);
return callback.call(context, self, realArgs, Block.NULL_BLOCK);
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -160,9 +158,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject
IRubyObject[] preppedArgs = RubyProc.prepareArgs(context, type, arity, args);
IRubyObject[] realArgs = setupBlockArgs(context.runtime.newArrayNoCopyLight(preppedArgs), type, true);
return callback.call(context, self, realArgs, block);
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Expand Up @@ -439,8 +439,18 @@ public static final Map<String, String> map(String... keyValues) {
return map;
}

public static IRubyObject handleNextJump(ThreadContext context, JumpException.NextJump nj) {
return nj.getValue() == null ? context.runtime.getNil() : (IRubyObject)nj.getValue();
/**
* Should be called on jumps out of blocks. Inspects the jump, returning or rethrowing as appropriate
*/
public static IRubyObject handleBlockJump(ThreadContext context, JumpException.FlowControlException jump, Block.Type type) {
// 'next' and Lambda 'return' are local returns from the block, ending the call or yield
if (jump instanceof JumpException.NextJump
|| (jump instanceof JumpException.ReturnJump && type == Block.Type.LAMBDA)) {
return jump.getValue() == null ? context.runtime.getNil() : (IRubyObject)jump.getValue();
}

// other jumps propagate up
throw jump;
}

private static class MethodMissingMethod extends DynamicMethod {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/runtime/Interpreted19Block.java
Expand Up @@ -156,8 +156,8 @@ protected IRubyObject doYield(ThreadContext context, IRubyObject value, Binding
setupBlockArg(context, value, self, Block.NULL_BLOCK, type);

return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down Expand Up @@ -195,8 +195,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject

// This while loop is for restarting the block call in case a 'redo' fires.
return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/java/org/jruby/runtime/InterpretedBlock.java
Expand Up @@ -244,8 +244,8 @@ public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, Bindin

// This while loop is for restarting the block call in case a 'redo' fires.
return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -264,8 +264,8 @@ public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, IRubyO

// This while loop is for restarting the block call in case a 'redo' fires.
return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -284,8 +284,8 @@ public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, IRubyO

// This while loop is for restarting the block call in case a 'redo' fires.
return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -303,8 +303,8 @@ public IRubyObject yield(ThreadContext context, Binding binding, Block.Type type
}

return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down Expand Up @@ -338,8 +338,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject

// This while loop is for restarting the block call in case a 'redo' fires.
return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand All @@ -359,8 +359,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject value,
}

return evalBlockBody(context, binding, self);
} catch (JumpException.NextJump nj) {
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, oldVis, lastFrame);
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/runtime/MethodBlock.java
Expand Up @@ -169,9 +169,8 @@ public IRubyObject yield(ThreadContext context, IRubyObject[] args, IRubyObject
throw bj;
}
}
} catch (JumpException.NextJump nj) {
// A 'next' is like a local return from the block, ending this call or yield.
return Helpers.handleNextJump(context, nj);
} catch (JumpException.FlowControlException jump) {
return Helpers.handleBlockJump(context, jump, type);
} finally {
post(context, binding, null, lastFrame);
}
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestLambdaParameters.rb
Expand Up @@ -2,4 +2,3 @@
exclude :test_do_lambda_source_location, "broken in compiler (#458)"
exclude :test_exact_parameter, "broken in compiler (#458)"
exclude :test_location_on_error, "broken in compiler (#458)"
exclude :test_return, "broken in compiler (#458)"