Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8b700b6ffbf4
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 55376a001757
Choose a head ref
  • 6 commits
  • 19 files changed
  • 1 contributor

Commits on May 7, 2015

  1. Start of argumentType removal

    enebo committed May 7, 2015
    Copy the full SHA
    ac66e64 View commit details
  2. Consolidate IR block call logic and use it for all block types. This …

    …is also part of argumentType removal.
    enebo committed May 7, 2015
    Copy the full SHA
    56ae944 View commit details
  3. Copy the full SHA
    1489669 View commit details
  4. Copy the full SHA
    3c352e3 View commit details
  5. Reduce usage of ZERO_ARGS by replacing with Signature.NO_ARGS checks.…

    … Eliminate null checks for signature by passing in one to NullBlockBody
    enebo committed May 7, 2015
    Copy the full SHA
    4b213d7 View commit details
  6. Copy the full SHA
    55376a0 View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
@@ -559,7 +559,7 @@ private IRubyObject initializeCommon(ThreadContext context, IRubyObject arg0, IR
runtime.getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
}

if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
if (block.getSignature() == Signature.NO_ARGUMENTS) {
IRubyObject nil = runtime.getNil();
for (int i = 0; i < ilen; i++) {
store(i, block.yield(context, nil));
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/RubyFixnum.java
Original file line number Diff line number Diff line change
@@ -286,8 +286,7 @@ public IRubyObject times(ThreadContext context, Block block) {
long lvalue = this.value;
boolean checkArity = block.type.checkArity;

if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS ||
block.getSignature() == Signature.NO_ARGUMENTS) {
if (block.getSignature() == Signature.NO_ARGUMENTS) {
if (checkArity) {
// must pass arg
IRubyObject nil = runtime.getNil();
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.Signature;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
@@ -141,7 +142,7 @@ public IRubyObject upto(ThreadContext context, IRubyObject to, Block block) {
private static void fixnumUpto(ThreadContext context, long from, long to, Block block) {
// We must avoid "i++" integer overflow when (to == Long.MAX_VALUE).
Ruby runtime = context.runtime;
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
if (block.getSignature() == Signature.NO_ARGUMENTS) {
IRubyObject nil = runtime.getNil();
long i;
for (i = from; i < to; i++) {
@@ -204,7 +205,7 @@ public IRubyObject downto(ThreadContext context, IRubyObject to, Block block) {
private static void fixnumDownto(ThreadContext context, long from, long to, Block block) {
// We must avoid "i--" integer overflow when (to == Long.MIN_VALUE).
Ruby runtime = context.runtime;
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
if (block.getSignature() == Signature.NO_ARGUMENTS) {
IRubyObject nil = runtime.getNil();
long i;
for (i = from; i > to; i--) {
17 changes: 3 additions & 14 deletions core/src/main/java/org/jruby/RubyMethod.java
Original file line number Diff line number Diff line change
@@ -38,12 +38,10 @@
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.internal.runtime.methods.IRMethodArgs;
import org.jruby.internal.runtime.methods.ProcMethod;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.CompiledBlockCallback19;
import org.jruby.runtime.CompiledBlockLight;
import org.jruby.runtime.CompiledBlockLight19;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ObjectAllocator;
@@ -210,26 +208,17 @@ public int getLine() {
BlockBody body;
if (method instanceof IRMethodArgs) {
Signature signature = ((IRMethodArgs) method).getSignature();
int argumentType = resolveArgumentType(signature.isFixed(), signature.required());
body = CompiledBlockLight19.newCompiledBlockLight(((IRMethodArgs) method).getSignature(),
runtime.getStaticScopeFactory().getDummyScope(), callback, false, argumentType, JRubyLibrary.MethodExtensions.methodParameters(runtime, method));
body = CompiledBlockLight19.newCompiledBlockLight(signature,
runtime.getStaticScopeFactory().getDummyScope(), callback, false, -1, JRubyLibrary.MethodExtensions.methodParameters(runtime, method));
} else {
Arity arity = method.getArity();
int argumentType = resolveArgumentType(arity.isFixed(), arity.required());
body = CompiledBlockLight19.newCompiledBlockLight(method.getArity(),
runtime.getStaticScopeFactory().getDummyScope(), callback, false, argumentType, JRubyLibrary.MethodExtensions.methodParameters(runtime, method));
runtime.getStaticScopeFactory().getDummyScope(), callback, false, -1, JRubyLibrary.MethodExtensions.methodParameters(runtime, method));
}
Block b = new Block(body, context.currentBinding(receiver, Visibility.PUBLIC));

return RubyProc.newProc(runtime, b, Block.Type.LAMBDA);
}

private int resolveArgumentType(boolean isFixed, int required) {
if (!isFixed) return BlockBody.MULTIPLE_ASSIGNMENT;
if (required > 0) return BlockBody.MULTIPLE_ASSIGNMENT;
return BlockBody.ZERO_ARGS;
}

@JRubyMethod
public RubyUnboundMethod unbind() {
RubyUnboundMethod unboundMethod =
3 changes: 0 additions & 3 deletions core/src/main/java/org/jruby/RubyProc.java
Original file line number Diff line number Diff line change
@@ -241,9 +241,6 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args) {
public static IRubyObject[] prepareArgs(ThreadContext context, Block.Type type, BlockBody blockBody, IRubyObject[] args) {
Signature signature = blockBody.getSignature();

// FIXME: which blocks have no signature (and no arity before that?)
if (signature == null) return args;

if (args == null) return IRubyObject.NULL_ARRAY;

if (type == Block.Type.LAMBDA) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
@@ -421,7 +421,7 @@ private void fixnumEach(ThreadContext context, Ruby runtime, Block block) {
to--;
}
long from = ((RubyFixnum) begin).getLongValue();
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
if (block.getSignature() == Signature.NO_ARGUMENTS) {
IRubyObject nil = runtime.getNil();
long i;
for (i = from; i < to; i++) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubySymbol.java
Original file line number Diff line number Diff line change
@@ -419,7 +419,7 @@ public IRubyObject encoding(ThreadContext context) {
public IRubyObject to_proc(ThreadContext context) {
StaticScope scope = context.runtime.getStaticScopeFactory().getDummyScope();
final CallSite site = new FunctionalCachingCallSite(symbol);
BlockBody body = new ContextAwareBlockBody(scope, Signature.OPTIONAL, BlockBody.SINGLE_RESTARG) {
BlockBody body = new ContextAwareBlockBody(scope, Signature.OPTIONAL) {
private IRubyObject yieldInner(ThreadContext context, RubyArray array, Block block) {
if (array.isEmpty()) {
throw context.runtime.newArgumentError("no receiver given");
4 changes: 0 additions & 4 deletions core/src/main/java/org/jruby/ir/IRClosure.java
Original file line number Diff line number Diff line change
@@ -296,10 +296,6 @@ public void setName(String name) {
super.setName(getLexicalParent().getName() + name);
}

public Arity getArity() {
return signature.arity();
}

public Signature getSignature() {
return signature;
}
Original file line number Diff line number Diff line change
@@ -456,11 +456,10 @@ public static IRubyObject yieldSpecific(ThreadContext context, Object blk) {
return b.yieldSpecific(context);
}

public static IRubyObject[] convertValueIntoArgArray(ThreadContext context, IRubyObject value, Arity arity, boolean argIsArray) {
public static IRubyObject[] convertValueIntoArgArray(ThreadContext context, IRubyObject value, int blockArity, boolean argIsArray) {
// SSS FIXME: This should not really happen -- so, some places in the runtime library are breaking this contract.
if (argIsArray && !(value instanceof RubyArray)) argIsArray = false;

int blockArity = arity.getValue();
switch (blockArity) {
case -1 : return argIsArray ? ((RubyArray)value).toJavaArray() : new IRubyObject[] { value };
case 0 : return new IRubyObject[] { value };
56 changes: 13 additions & 43 deletions core/src/main/java/org/jruby/runtime/BlockBody.java
Original file line number Diff line number Diff line change
@@ -36,27 +36,19 @@
import org.jruby.EvalType;
import org.jruby.RubyArray;
import org.jruby.RubyProc;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.builtin.IRubyObject;

/**
* The executable body portion of a closure.
*/
public abstract class BlockBody {
// FIXME: Maybe not best place, but move it to a good home
public static final int ZERO_ARGS = 0;
public static final int MULTIPLE_ASSIGNMENT = 1;
public static final int ARRAY = 2;
public static final int SINGLE_RESTARG = 3;

public static final String[] EMPTY_PARAMETER_LIST = new String[0];

protected final int argumentType;
protected final Signature signature;

public BlockBody(int argumentType, Signature signature) {
this.argumentType = argumentType;
public BlockBody(Signature signature) {
this.signature = signature;
}

@@ -122,10 +114,6 @@ public IRubyObject yield(ThreadContext context, IRubyObject value,
return yield(context, value, binding, type);
}

public int getArgumentType() {
return argumentType;
}

public IRubyObject call(ThreadContext context, Binding binding, Block.Type type) {
IRubyObject[] args = IRubyObject.NULL_ARRAY;
args = prepareArgumentsForCall(context, args, type);
@@ -217,36 +205,18 @@ public boolean isGiven() {
public abstract int getLine();

public IRubyObject[] prepareArgumentsForCall(ThreadContext context, IRubyObject[] args, Block.Type type) {
switch (type) {
case NORMAL: {
// assert false : "can this happen?";
if (args.length == 1 && args[0] instanceof RubyArray) {
if (argumentType == MULTIPLE_ASSIGNMENT || argumentType == SINGLE_RESTARG) {
args = ((RubyArray) args[0]).toJavaArray();
}
break;
}
}
case PROC: {
if (args.length == 1 && args[0] instanceof RubyArray) {
if (argumentType == MULTIPLE_ASSIGNMENT && argumentType != SINGLE_RESTARG) {
args = ((RubyArray) args[0]).toJavaArray();
}
}
break;
}
case LAMBDA:
if (argumentType == ARRAY && args.length != 1) {
context.runtime.getWarnings().warn(ID.MULTIPLE_VALUES_FOR_BLOCK, "multiple values for a block parameter (" + args.length + " for " + getSignature().arityValue() + ")");
if (args.length == 0) {
args = context.runtime.getSingleNilArray();
} else {
args = new IRubyObject[] {context.runtime.newArrayNoCopy(args)};
}
} else {
getSignature().checkArity(context.runtime, args);
if (type == Block.Type.LAMBDA) {
signature.checkArity(context.runtime, args);
} else {
// SSS FIXME: How is it even possible to "call" a NORMAL block?
// I thought only procs & lambdas can be called, and blocks are yielded to.
if (args.length == 1) {
// Convert value to arg-array, unwrapping where necessary
args = IRRuntimeHelpers.convertValueIntoArgArray(context, args[0], signature.arityValue(), type == Block.Type.NORMAL && args[0] instanceof RubyArray);
} else if (getSignature().arityValue() == 1 && !getSignature().restKwargs()) {
// discard excess arguments
args = args.length == 0 ? context.runtime.getSingleNilArray() : new IRubyObject[] { args[0] };
}
break;
}

return args;
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/runtime/CallBlock.java
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ public static Block newCallClosure(IRubyObject self, RubyModule imClass, Arity a
}

private CallBlock(Signature signature, BlockCallback callback, ThreadContext context) {
super(BlockBody.SINGLE_RESTARG, signature);
super(signature);
this.callback = callback;
this.dummyScope = context.runtime.getStaticScopeFactory().getDummyScope();
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/runtime/CallBlock19.java
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public static Block newCallClosure(IRubyObject self, RubyModule imClass, Arity a
}

public CallBlock19(Signature signature, BlockCallback callback, ThreadContext context) {
super(BlockBody.SINGLE_RESTARG, signature);
super(signature);
this.callback = callback;
this.dummy = context.runtime.getStaticScopeFactory().getDummyScope();
}
28 changes: 9 additions & 19 deletions core/src/main/java/org/jruby/runtime/CompiledBlock.java
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ public static BlockBody newCompiledBlock(Arity arity,
}

public CompiledBlock(Signature signature, StaticScope scope, CompiledBlockCallback callback, boolean hasMultipleArgsHead, int argumentType) {
super(scope, signature, argumentType);
super(scope, signature);

this.callback = callback;
this.hasMultipleArgsHead = hasMultipleArgsHead;
@@ -159,15 +159,10 @@ private IRubyObject prepareSelf(Binding binding) {
}

protected IRubyObject setupBlockArgs(ThreadContext context, IRubyObject value, IRubyObject self) {
switch (argumentType) {
case ZERO_ARGS:
return null;
case MULTIPLE_ASSIGNMENT:
case SINGLE_RESTARG:
return value;
default:
return defaultArgsLogic(context.runtime, value);
}
if (signature == Signature.NO_ARGUMENTS) return null;
if (!signature.isFixed() || signature.required() > 1) return value;

return defaultArgsLogic(context.runtime, value);
}

private IRubyObject defaultArgsLogic(Ruby ruby, IRubyObject value) {
@@ -189,15 +184,10 @@ private void blockArgWarning(Ruby ruby, int length) {
}

protected IRubyObject setupBlockArg(Ruby ruby, IRubyObject value, IRubyObject self) {
switch (argumentType) {
case ZERO_ARGS:
return null;
case MULTIPLE_ASSIGNMENT:
case SINGLE_RESTARG:
return ArgsUtil.convertToRubyArray(ruby, value, hasMultipleArgsHead);
default:
return defaultArgLogic(ruby, value);
}
if (signature == Signature.NO_ARGUMENTS) return null;
if (!signature.isFixed() || signature.required() > 1) return ArgsUtil.convertToRubyArray(ruby, value, hasMultipleArgsHead);

return defaultArgLogic(ruby, value);
}

private IRubyObject defaultArgLogic(Ruby ruby, IRubyObject value) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/runtime/CompiledBlock19.java
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ public static BlockBody newCompiledBlock(Arity arity,
}

public CompiledBlock19(Signature signature, StaticScope scope, CompiledBlockCallback19 callback, boolean hasMultipleArgsHead, int argumentType, String[] parameterList) {
super(scope, signature, argumentType);
super(scope, signature);

this.callback = callback;
this.hasMultipleArgsHead = hasMultipleArgsHead;
Original file line number Diff line number Diff line change
@@ -9,14 +9,15 @@ public abstract class ContextAwareBlockBody extends BlockBody {
/** The static scope for the block body */
protected StaticScope scope;

public ContextAwareBlockBody(StaticScope scope, Signature signature, int argumentType) {
super(argumentType, signature);
public ContextAwareBlockBody(StaticScope scope, Signature signature) {
super(signature);

this.scope = scope;
}

@Deprecated
public ContextAwareBlockBody(StaticScope scope, Arity arity, int argumentType) {
this(scope, Signature.from(arity), argumentType);
this(scope, Signature.from(arity));
}

protected Frame pre(ThreadContext context, Binding binding) {
23 changes: 2 additions & 21 deletions core/src/main/java/org/jruby/runtime/IRBlockBody.java
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ public abstract class IRBlockBody extends ContextAwareBlockBody {
protected ThreadLocal<EvalType> evalType;

public IRBlockBody(StaticScope staticScope, String[] parameterList, String fileName, int lineNumber, Signature signature) {
super(staticScope, signature, -1);
super(staticScope, signature);
this.parameterList = parameterList;
this.fileName = fileName;
this.lineNumber = lineNumber;
@@ -81,7 +81,7 @@ public IRubyObject yieldSpecific(ThreadContext context, Binding binding, Type ty
public IRubyObject yieldSpecific(ThreadContext context, IRubyObject arg0, Binding binding, Type type) {
if (arg0 instanceof RubyArray) {
// Unwrap the array arg
IRubyObject[] args = IRRuntimeHelpers.convertValueIntoArgArray(context, arg0, signature.arity(), true);
IRubyObject[] args = IRRuntimeHelpers.convertValueIntoArgArray(context, arg0, signature.arityValue(), true);

// FIXME: arity error is aginst new args but actual error shows arity of original args.
if (type == Type.LAMBDA) signature.checkArity(context.runtime, args);
@@ -154,25 +154,6 @@ protected IRubyObject useBindingSelf(Binding binding) {

protected abstract IRubyObject commonYieldPath(ThreadContext context, IRubyObject[] args, IRubyObject self, Binding binding, Type type, Block block);

@Override
public IRubyObject[] prepareArgumentsForCall(ThreadContext context, IRubyObject[] args, Type type) {
if (type == Type.LAMBDA) {
signature.checkArity(context.runtime, args);
} else {
// SSS FIXME: How is it even possible to "call" a NORMAL block?
// I thought only procs & lambdas can be called, and blocks are yielded to.
if (args.length == 1) {
// Convert value to arg-array, unwrapping where necessary
args = IRRuntimeHelpers.convertValueIntoArgArray(context, args[0], signature.arity(), (type == Type.NORMAL) && (args[0] instanceof RubyArray));
} else if (getSignature().arityValue() == 1 && !getSignature().restKwargs()) {
// discard excess arguments
args = (args.length == 0) ? context.runtime.getSingleNilArray() : new IRubyObject[] { args[0] };
}
}

return args;
}

@Override
public String getFile() {
return fileName;
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ public JavaInternalBlockBody(Ruby runtime, Signature signature) {
* For blocks which cannot be executed in parallel.
*/
public JavaInternalBlockBody(Ruby runtime, ThreadContext originalContext, String methodName, Signature signature) {
super(BlockBody.SINGLE_RESTARG, signature);
super(signature);

this.originalContext = originalContext;
this.methodName = methodName;
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/runtime/MethodBlock.java
Original file line number Diff line number Diff line change
@@ -65,8 +65,8 @@ public static Block createMethodBlock(ThreadContext context, IRubyObject self, D
}

public MethodBlock(RubyMethod method, StaticScope staticScope) {
super(staticScope, Arity.createArity((int) method.arity().getLongValue()), BlockBody.SINGLE_RESTARG);
super(staticScope, Signature.from(Arity.createArity(method.arity().getIntValue())));

this.method = method;
String filename = method.getFilename();
if (filename == null) filename = "(method)";
Loading