Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Another round of kwargs refactoring

  • Loading branch information...
commit 45b8a5b0379097f67ec035ed410a870d59ed0e55 1 parent 18352a2
@enebo enebo authored
View
2  core/src/main/java/org/jruby/ir/instructions/ReceiveArgBase.java
@@ -47,7 +47,7 @@ public String toString() {
return super.toString() + "(" + argIndex + ")";
}
- public IRubyObject receiveArg(ThreadContext context, int kwArgHashCount, IRubyObject[] args) {
+ public IRubyObject receiveArg(ThreadContext context, IRubyObject[] args, boolean keywordArgumentSupplied) {
throw new RuntimeException("ReceiveArgBase.interpret called! " + this.getClass().getName() + " does not define receiveArg");
}
}
View
25 core/src/main/java/org/jruby/ir/instructions/ReceiveKeywordArgInstr.java
@@ -47,19 +47,16 @@ public Instr cloneForInlining(InlinerInfo ii) {
}
@Override
- public IRubyObject receiveArg(ThreadContext context, int kwArgHashCount, IRubyObject[] args) {
- if (kwArgHashCount == 0 || numUsedArgs == args.length) {
- return UndefinedValue.UNDEFINED;
- } else {
- RubyHash lastArg = (RubyHash)args[args.length - 1];
- // If the key exists in the hash, delete and return it.
- RubySymbol argSym = context.getRuntime().newSymbol(argName);
- if (lastArg.fastARef(argSym) != null) {
- // SSS FIXME: Can we use an internal delete here?
- return lastArg.delete(context, argSym, Block.NULL_BLOCK);
- } else {
- return UndefinedValue.UNDEFINED;
- }
- }
+ public IRubyObject receiveArg(ThreadContext context, IRubyObject[] args, boolean keywordArgumentSupplied) {
+ if (!keywordArgumentSupplied) return UndefinedValue.UNDEFINED;
+
+ RubyHash keywordArguments = (RubyHash)args[args.length - 1];
+ RubySymbol keywordName = context.getRuntime().newSymbol(argName);
+
+ if (keywordArguments.fastARef(keywordName) == null) return UndefinedValue.UNDEFINED;
+
+ // SSS FIXME: Can we use an internal delete here?
+ // Enebo FIXME: Delete seems wrong if we are doing this for duplication purposes.
+ return keywordArguments.delete(context, keywordName, Block.NULL_BLOCK);
}
}
View
10 core/src/main/java/org/jruby/ir/instructions/ReceiveKeywordRestArgInstr.java
@@ -41,11 +41,9 @@ public Instr cloneForInlining(InlinerInfo ii) {
}
@Override
- public IRubyObject receiveArg(ThreadContext context, int kwArgHashCount, IRubyObject[] args) {
- if (kwArgHashCount == 0 || numUsedArgs == args.length) {
- return RubyHash.newSmallHash(context.getRuntime());
- } else {
- return args[args.length - 1];
- }
+ public IRubyObject receiveArg(ThreadContext context, IRubyObject[] args, boolean keywordArgumentSupplied) {
+ if (!keywordArgumentSupplied) return RubyHash.newSmallHash(context.getRuntime());
+
+ return args[args.length - 1];
}
}
View
4 core/src/main/java/org/jruby/ir/instructions/ReceiveOptArgInstr.java
@@ -7,6 +7,7 @@
import org.jruby.ir.operands.UndefinedValue;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.transformations.inlining.InlinerInfo;
+import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class ReceiveOptArgInstr extends ReceiveArgBase implements FixedArityInstr {
@@ -62,7 +63,8 @@ public Instr cloneForInlining(InlinerInfo ii) {
}
}
- public IRubyObject receiveOptArg(IRubyObject[] args, boolean keywordArgumentSupplied) {
+ @Override
+ public IRubyObject receiveArg(ThreadContext context, IRubyObject[] args, boolean keywordArgumentSupplied) {
int optArgIndex = argIndex; // which opt arg we are processing? (first one has index 0, second 1, ...).
int argsLength = keywordArgumentSupplied ? args.length - 1 : args.length;
View
19 core/src/main/java/org/jruby/ir/instructions/ReceiveRestArgInstr.java
@@ -50,17 +50,16 @@ public Instr cloneForInlining(InlinerInfo ii) {
private IRubyObject[] NO_PARAMS = new IRubyObject[0];
@Override
- public IRubyObject receiveArg(ThreadContext context, int kwArgLoss, IRubyObject[] parameters) {
- IRubyObject[] args;
- int numAvailableArgs = parameters.length - numUsedArgs - kwArgLoss;
- if (numAvailableArgs <= 0) {
- args = NO_PARAMS;
- } else {
- args = new IRubyObject[numAvailableArgs];
- System.arraycopy(parameters, argIndex, args, 0, numAvailableArgs);
- }
+ public IRubyObject receiveArg(ThreadContext context, IRubyObject[] args, boolean keywordArgumentSupplied) {
+ int argsLength = keywordArgumentSupplied ? args.length - 1 : args.length;
+ int remainingArguments = argsLength - numUsedArgs;
+
+ if (remainingArguments <= 0) return context.runtime.newArray(NO_PARAMS);
+
+ IRubyObject[] restArgs = new IRubyObject[remainingArguments];
+ System.arraycopy(args, argIndex, restArgs, 0, remainingArguments);
- return context.runtime.newArray(args);
+ return context.runtime.newArray(restArgs);
}
@Override
View
14 core/src/main/java/org/jruby/ir/interpreter/Interpreter.java
@@ -295,7 +295,7 @@ private static void interpretFloatOp(AluInstr instr, Operation op, ThreadContext
}
}
- private static void receiveArg(ThreadContext context, Instr i, Operation operation, IRubyObject[] args, int kwArgHashCount, DynamicScope currDynScope, Object[] temp, Object exception, Block block) {
+ private static void receiveArg(ThreadContext context, Instr i, Operation operation, IRubyObject[] args, boolean receivesKeywordArgument, DynamicScope currDynScope, Object[] temp, Object exception, Block block) {
Object result;
ResultInstr instr = (ResultInstr)i;
switch(operation) {
@@ -308,11 +308,6 @@ private static void receiveArg(ThreadContext context, Instr i, Operation operati
result = IRRuntimeHelpers.newProc(context.runtime, block);
setResult(temp, currDynScope, instr.getResult(), result);
return;
- case RECV_OPT_ARG:
- result = ((ReceiveOptArgInstr)instr).receiveOptArg(args, kwArgHashCount > 0);
- // For blocks, missing arg translates to nil
- setResult(temp, currDynScope, instr.getResult(), result);
- return;
case RECV_POST_REQD_ARG:
result = ((ReceivePostReqdArgInstr)instr).receivePostReqdArg(args);
// For blocks, missing arg translates to nil
@@ -325,7 +320,7 @@ private static void receiveArg(ThreadContext context, Instr i, Operation operati
setResult(temp, currDynScope, instr.getResult(), exception);
return;
default:
- result = ((ReceiveArgBase)instr).receiveArg(context, kwArgHashCount, args);
+ result = ((ReceiveArgBase)instr).receiveArg(context, args, receivesKeywordArgument);
setResult(temp, currDynScope, instr.getResult(), result);
return;
}
@@ -549,6 +544,8 @@ private static IRubyObject interpret(ThreadContext context, IRubyObject self,
boolean debug = IRRuntimeHelpers.isDebug();
boolean profile = IRRuntimeHelpers.inProfileMode();
Integer scopeVersion = profile ? Profiler.initProfiling(scope) : 0;
+ // FIXME: As we stand we need to know required args count to know whether a last arg hash is kwargs or just a hash
+ boolean receivesKeywordArgument = IRRuntimeHelpers.extractKwargsCount(args, 0, scope.receivesKeywordArgs()) != 0;
// Enter the looooop!
while (ipc < n) {
@@ -572,8 +569,7 @@ private static IRubyObject interpret(ThreadContext context, IRubyObject self,
interpretFloatOp((AluInstr)instr, operation, context, floats, booleans, temp);
break;
case ARG_OP:
- // FIXME: As we stand we need to know required args count to know whether a last arg hash is kwargs or just a hash
- receiveArg(context, instr, operation, args, IRRuntimeHelpers.extractKwargsCount(args, 0, scope.receivesKeywordArgs()), currDynScope, temp, exception, block);
+ receiveArg(context, instr, operation, args, receivesKeywordArgument, currDynScope, temp, exception, block);
break;
case CALL_OP:
if (profile) Profiler.updateCallSite(instr, scope, scopeVersion);
Please sign in to comment.
Something went wrong with that request. Please try again.