Skip to content

Commit

Permalink
Showing 12 changed files with 98 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import org.jruby.ir.IRFlags;
import org.jruby.ir.IRScope;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import org.jruby.ir.IRScope;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.PositionAware;
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package org.jruby.internal.runtime.methods;

import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Signature;

/**
* Represents a method object that can return a Signature and an array of ArgumentDescriptors.
*/
public interface IRMethodArgs {
// FIXME: Should get pushed to DynamicMethod
public Signature getSignature();
public ArgumentDescriptor[] getArgumentDescriptors();

public enum ArgumentType {
key, keyreq, keyrest, block, opt, rest, req
}

public class ArgumentDescriptor {
public final ArgumentType type;
public final String name;
public static final ArgumentDescriptor[] EMPTY_ARRAY = new ArgumentDescriptor[0];
/**
* Get the Signature for this method.
*/
public Signature getSignature();

public ArgumentDescriptor(ArgumentType type, String name) {
this.type = type;
this.name = name;
}
/**
* Get the array of ArgumentDescriptors that represent the arguments to this method.
*/
public ArgumentDescriptor[] getArgumentDescriptors();

public String toShortDesc() {
switch (type) {
case keyreq: return "K" + name;
case keyrest: return "e" + name;
case req: return "q" + name;
default: return type.name().charAt(0) + name;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import org.jruby.ir.IRScope;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.jruby.internal.runtime.methods;

/**
* Get a list of argument descriptors in prefixed form.
*
* The prefixed form has a single leading character to indicate the type of argument
* followed immediately by the name of the argument.
*
* @see IRMethodArgs
* @see org.jruby.runtime.ArgumentType
*/
public interface MethodArgs2 {
public String[] getParameterList();
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
@@ -7,9 +7,8 @@
import org.jruby.ast.*;
import org.jruby.ast.types.INameNode;
import org.jruby.compiler.NotCompilableException;
import org.jruby.internal.runtime.methods.IRMethodArgs;
import org.jruby.internal.runtime.methods.IRMethodArgs.ArgumentDescriptor;
import org.jruby.internal.runtime.methods.IRMethodArgs.ArgumentType;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.ArgumentType;
import org.jruby.ir.instructions.*;
import org.jruby.ir.instructions.defined.GetErrorInfoInstr;
import org.jruby.ir.instructions.defined.RestoreErrorInfoInstr;
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/ir/IRMethod.java
Original file line number Diff line number Diff line change
@@ -5,8 +5,7 @@
import java.util.HashMap;
import java.util.Map;
import org.jruby.ast.MethodDefNode;
import org.jruby.internal.runtime.methods.IRMethodArgs;
import org.jruby.internal.runtime.methods.IRMethodArgs.ArgumentDescriptor;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.ir.representations.BasicBlock;
28 changes: 28 additions & 0 deletions core/src/main/java/org/jruby/runtime/ArgumentDescriptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jruby.runtime;

/**
* A description of a single argument in a Ruby argument list.
*/
public class ArgumentDescriptor {
/** The type of the argument */
public final ArgumentType type;

/** The name of the argument */
public final String name;

public static final ArgumentDescriptor[] EMPTY_ARRAY = new ArgumentDescriptor[0];

public ArgumentDescriptor(ArgumentType type, String name) {
this.type = type;
this.name = name;
}

/**
* Generate the prefixed version of this descriptor.
*
* @see org.jruby.internal.runtime.methods.MethodArgs2
*/
public String toPrefixForm() {
return type.prefix + name;
}
}
31 changes: 31 additions & 0 deletions core/src/main/java/org/jruby/runtime/ArgumentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jruby.runtime;

/**
* Created by headius on 5/8/15.
*/
public enum ArgumentType {
key('k'), keyreq('K'), keyrest('e'), block('b'), opt('o'), rest('r'), req('q');

ArgumentType(char prefix) {
this.prefix = prefix;
}

public static ArgumentType valueOf(char prefix) {
switch (prefix) {
case 'k': return key;
case 'K': return keyreq;
case 'e': return keyrest;
case 'b': return block;
case 'o': return opt;
case 'r': return rest;
case 'q': return req;
// for 'R' used by prefix for to represent anonymous restarg
case 'R': return rest;
// for 'nil' used by old compiler to represent array destructuring
case 'n': return req;
default: return null;
}
}

public final char prefix;
}
53 changes: 12 additions & 41 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.Unrescuable;
import org.jruby.internal.runtime.methods.*;
import org.jruby.internal.runtime.methods.IRMethodArgs.ArgumentDescriptor;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.operands.UndefinedValue;
import org.jruby.javasupport.JavaClass;
@@ -2595,50 +2594,22 @@ public static RubyArray parameterListToParameters(Ruby runtime, String[] paramet
RubyArray parms = RubyArray.newEmptyArray(runtime);

for (String param : parameterList) {
if (param == null) continue; // FIXME: How does this happen?
if (param.equals("NONE")) break;
if (param.equals("nil")) param = "n"; // make length 1 so we don't look for a name

RubyArray elem = RubyArray.newEmptyArray(runtime);
if (param.equals("nil")) {
// marker for masgn args (the parens in "a, b, (c, d)"
elem.add(RubySymbol.newSymbol(runtime, isLambda ? "req" : "opt"));
parms.add(elem);
continue;
}
ArgumentType type = ArgumentType.valueOf(param.charAt(0));

if (param.length() == 0) System.out.println(Arrays.toString(parameterList));
if (param.charAt(0) == 'q') {
// required/normal arg
elem.add(RubySymbol.newSymbol(runtime, isLambda ? "req" : "opt"));
} else if (param.charAt(0) == 'r') {
// named rest arg
elem.add(RubySymbol.newSymbol(runtime, "rest"));
} else if (param.charAt(0) == 'R') {
// unnamed rest arg (star)
elem.add(RubySymbol.newSymbol(runtime, "rest"));
parms.add(elem);
continue;
} else if (param.charAt(0) == 'o') {
// optional arg
elem.add(RubySymbol.newSymbol(runtime, "opt"));
if (param.length() == 1) {
// no name; continue
parms.add(elem);
continue;
}
} else if (param.charAt(0) == 'b') {
// block arg
elem.add(RubySymbol.newSymbol(runtime, "block"));
} else if (param.charAt(0) == 'k') {
elem.add(RubySymbol.newSymbol(runtime, "key"));
} else if (param.charAt(0) == 'K') {
elem.add(RubySymbol.newSymbol(runtime, "keyreq"));
} else if (param.charAt(0) == 'e') {
elem.add(RubySymbol.newSymbol(runtime, "keyrest"));
}
// for lambdas, we call required args optional
if (type == ArgumentType.req && !isLambda) type = ArgumentType.opt;

RubySymbol typeSym = RubySymbol.newSymbol(runtime, type.name());

// 'R', 'o', 'n' forms can get here without a name
RubyArray elem;
if (param.length() > 1) {
elem.add(RubySymbol.newSymbol(runtime, param.substring(1)));
elem = RubyArray.newArray(runtime, typeSym, RubySymbol.newSymbol(runtime, param.substring(1)));
} else {
elem = RubyArray.newArray(runtime, typeSym);
}

parms.add(elem);
@@ -2651,7 +2622,7 @@ public static RubyArray parameterListToParameters(Ruby runtime, String[] paramet
public static String[] irMethodArgsToParameters(ArgumentDescriptor[] argDesc) {
String[] tmp = new String[argDesc.length];
for (int i = 0; i < tmp.length; i++) {
tmp[i] = argDesc[i].toShortDesc();
tmp[i] = argDesc[i].toPrefixForm();
}

return tmp;

0 comments on commit 60b462e

Please sign in to comment.