Skip to content

Commit ef4b662

Browse files
committed
[1.9] Add Method#parameters. This change also changes our 1.8-mode extension "args" to structure like 1.9, but nobody was using it anyway (via require 'jruby/ext').
1 parent 65e392a commit ef4b662

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/org/jruby/RubyJRuby.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -695,31 +695,36 @@ public static IRubyObject methodArgs(IRubyObject recv) {
695695
DynamicMethod method = rubyMethod.method;
696696

697697
if (method instanceof MethodArgs) {
698+
RubySymbol req = runtime.newSymbol("req");
699+
RubySymbol opt = runtime.newSymbol("opt");
700+
RubySymbol rest = runtime.newSymbol("rest");
701+
RubySymbol block = runtime.newSymbol("block");
698702
MethodArgs interpMethod = (MethodArgs)method;
699703
ArgsNode args = interpMethod.getArgsNode();
700704
RubyArray argsArray = RubyArray.newArray(runtime);
701705

702-
RubyArray reqArray = RubyArray.newArray(runtime);
703706
ListNode requiredArgs = args.getPre();
704707
for (int i = 0; requiredArgs != null && i < requiredArgs.size(); i++) {
705-
reqArray.append(getNameFrom(runtime, (INameNode) requiredArgs.get(i)));
708+
argsArray.append(RubyArray.newArray(runtime, req, getNameFrom(runtime, (INameNode) requiredArgs.get(i))));
706709
}
707-
argsArray.append(reqArray);
708710

709-
RubyArray optArray = RubyArray.newArray(runtime);
710711
ListNode optArgs = args.getOptArgs();
711712
for (int i = 0; optArgs != null && i < optArgs.size(); i++) {
712-
optArray.append(getNameFrom(runtime, (INameNode) optArgs.get(i)));
713+
argsArray.append(RubyArray.newArray(runtime, opt, getNameFrom(runtime, (INameNode) optArgs.get(i))));
714+
}
715+
716+
ListNode requiredArgsPost = args.getPost();
717+
for (int i = 0; requiredArgs != null && i < requiredArgsPost.size(); i++) {
718+
argsArray.append(RubyArray.newArray(runtime, req, getNameFrom(runtime, (INameNode) requiredArgsPost.get(i))));
713719
}
714-
argsArray.append(optArray);
715720

716-
argsArray.append(getNameFrom(runtime, args.getRestArgNode()));
717-
argsArray.append(getNameFrom(runtime, args.getBlock()));
721+
argsArray.append(RubyArray.newArray(runtime, rest, getNameFrom(runtime, args.getRestArgNode())));
722+
argsArray.append(RubyArray.newArray(runtime, block, getNameFrom(runtime, args.getBlock())));
718723

719724
return argsArray;
720725
}
721726

722-
throw runtime.newTypeError("Method args are only available for standard interpreted or jitted methods");
727+
return runtime.getNil();
723728
}
724729
}
725730

src/org/jruby/RubyMethod.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.jruby.anno.JRubyClass;
3636
import org.jruby.exceptions.JumpException;
3737
import org.jruby.internal.runtime.methods.DynamicMethod;
38+
import org.jruby.internal.runtime.methods.MethodArgs;
3839
import org.jruby.runtime.Block;
3940
import org.jruby.runtime.ClassIndex;
4041
import org.jruby.runtime.DynamicScope;
@@ -292,5 +293,10 @@ public IRubyObject source_location(ThreadContext context) {
292293

293294
return context.getRuntime().getNil();
294295
}
296+
297+
@JRubyMethod(name = "parameters", compat = CompatVersion.RUBY1_9)
298+
public IRubyObject parameters(ThreadContext context) {
299+
return RubyJRuby.MethodExtensions.methodArgs(this);
300+
}
295301
}
296302

src/org/jruby/ast/OptArgNode.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.List;
3131
import org.jruby.Ruby;
32+
import org.jruby.ast.types.INameNode;
3233
import org.jruby.ast.visitor.NodeVisitor;
3334
import org.jruby.lexer.yacc.ISourcePosition;
3435
import org.jruby.runtime.Block;
@@ -39,7 +40,7 @@
3940
*
4041
* @author enebo
4142
*/
42-
public class OptArgNode extends Node {
43+
public class OptArgNode extends Node implements INameNode {
4344
private Node value;
4445

4546
public OptArgNode(ISourcePosition position, Node value) {
@@ -75,4 +76,11 @@ public List<Node> childNodes() {
7576
return Node.createList(value);
7677
}
7778

79+
public String getName() {
80+
if (value instanceof INameNode) {
81+
return ((INameNode)value).getName();
82+
}
83+
return null;
84+
}
85+
7886
}

0 commit comments

Comments
 (0)