Skip to content

Commit

Permalink
[Truffle] #arity basically working.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Mar 8, 2015
1 parent c38ac67 commit c3a3f56
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 90 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/hash/default_proc_tags.txt

This file was deleted.

21 changes: 0 additions & 21 deletions spec/truffle/tags/core/method/arity_tags.txt
@@ -1,25 +1,4 @@
fails:Method#arity returns n, where n is the number of required arguments, when there are zero or more required arguments only
fails:Method#arity returns n, where n is the number of required arguments and method created via defined_method
fails:Method#arity returns -(n+1), where n is the number of required arguments, when there is at least one optional argument
fails:Method#arity returns -(n+1), where n is the number of required arguments, when there is a splat argument, regardless of optional arguments
fails:Method#arity returns the same value regardless of the presence of a block
fails:Method#arity for a Method generated by respond_to_missing? returns -1
fails:"Method#arity returns zero for method definition \n def m() end"
fails:"Method#arity returns zero for method definition \n def n(&b) end"
fails:"Method#arity returns positive values for method definition \n def m(a) end\n def n(a, b) end\n def o(a, b, c) end\n def p(a, b, c, d) end"
fails:"Method#arity returns positive values for method definition \n def m(a:) end\n def n(a:, b:) end\n def o(a: 1, b:, c:, d: 2) end"
fails:"Method#arity returns positive values for method definition \n def m(a, b:) end\n def n(a, b:, &l) end"
fails:"Method#arity returns positive values for method definition \n def m(a, b, c:, d: 1) end\n def n(a, b, c:, d: 1, **k, &l) end"
fails:"Method#arity returns negative values for method definition \n def m(a=1) end\n def n(a=1, b=2) end"
fails:"Method#arity returns negative values for method definition \n def m(a, b=1) end\n def n(a, b, c=1, d=2) end"
fails:"Method#arity returns negative values for method definition \n def m(a=1, *b) end\n def n(a=1, b=2, *c) end"
fails:"Method#arity returns negative values for method definition \n def m(*) end\n def n(*a) end"
fails:"Method#arity returns negative values for method definition \n def m(a, *) end\n def n(a, *b) end\n def o(a, b, *c) end\n def p(a, b, c, *d) end"
fails:"Method#arity returns negative values for method definition \n def m(*a, b) end\n def n(*a, b, c) end\n def o(*a, b, c, d) end"
fails:"Method#arity returns negative values for method definition \n def m(a, *b, c) end\n def n(a, b, *c, d, e) end"
fails:"Method#arity returns negative values for method definition \n def m(a, b=1, c=2, *d, e, f) end\n def n(a, b, c=1, *d, e, f, g) end"
fails:"Method#arity returns negative values for method definition \n def m(a: 1) end\n def n(a: 1, b: 2) end"
fails:"Method#arity returns negative values for method definition \n def m(a=1, b: 2) end\n def n(*a, b: 1) end\n def o(a=1, b: 2) end\n def p(a=1, *b, c: 2, &l) end"
fails:"Method#arity returns negative values for method definition \n def m(**k, &l) end\n def n(*a, **k) end\n def o(a: 1, b: 2, **k) end"
fails:"Method#arity returns negative values for method definition \n def m(a=1, *b, c:, d: 2, **k, &l) end"
fails:"Method#arity returns negative values for method definition \n def m(a, b=1, *c, d, e:, f: 2, **k, &l) end\n def n(a, b=1, *c, d:, e:, f: 2, **k, &l) end\n def o(a=0, b=1, *c, d, e:, f: 2, **k, &l) end\n def p(a=0, b=1, *c, d:, e:, f: 2, **k, &l) end"
51 changes: 0 additions & 51 deletions spec/truffle/tags/core/proc/arity_tags.txt
@@ -1,65 +1,14 @@
fails:Proc#arity returns -1 for a block taking |a = 0, *b| argument
fails:Proc#arity returns -1 for a lambda taking one optional argument
fails:Proc#arity returns -2 for a lambda taking |a, b = 0| argument
fails:Proc#arity returns -2 for a lambda taking |a, b = 0, c = 0| argument
fails:Proc#arity returns -2 for a lambda taking |(a, b), c = 0| argument
fails:Proc#arity returns 0 for a Proc taking one optional argument
fails:Proc#arity returns 1 for a Proc taking |a, b = 0| argument
fails:Proc#arity returns 1 for a Proc taking |a, b = 0, c = 0| argument
fails:Proc#arity returns 1 for a Proc taking |(a, b), c = 0| argument
fails:Proc#arity returns 0 for a block taking no arguments
fails:Proc#arity returns 0 for a block taking || arguments
fails:Proc#arity returns 1 for a block taking |a| arguments
fails:Proc#arity returns 1 for a block taking |a, | arguments
fails:Proc#arity returns -2 for a block taking |a, *| arguments
fails:Proc#arity returns -2 for a block taking |a, *b| arguments
fails:Proc#arity returns -3 for a block taking |a, b, *c| arguments
fails:Proc#arity returns 2 for a block taking |a, b| arguments
fails:Proc#arity returns 3 for a block taking |a, b, c| arguments
fails:Proc#arity returns -1 for a block taking |*| arguments
fails:Proc#arity returns -1 for a block taking |*a| arguments
fails:Proc#arity returns 1 for a block taking |(a, b)| arguments
fails:Proc#arity returns 1 for a block taking |(a, *)| arguments
fails:Proc#arity returns 1 for a block taking |(a, *b)| arguments
fails:Proc#arity returns 2 for a block taking |a, (b, c)| arguments
fails:Proc#arity returns 2 for a block taking |a, (b, *c)| arguments
fails:Proc#arity returns 2 for a block taking |(a, b), c| arguments
fails:Proc#arity returns -2 for a block taking |(a, b), *c| arguments
fails:Proc#arity returns 2 for a block taking |(a, *b), c| arguments
fails:"Proc#arity for instances created with -> () { } returns zero for definition \n @a = -> () {}"
fails:"Proc#arity for instances created with -> () { } returns zero for definition \n @a = -> (&b) {}"
fails:"Proc#arity for instances created with -> () { } returns positive values for definition \n @a = -> (a) { }\n @b = -> (a, b) { }\n @c = -> (a, b, c) { }\n @d = -> (a, b, c, d) { }"
fails:"Proc#arity for instances created with -> () { } returns positive values for definition \n @a = -> (a:) { }\n @b = -> (a:, b:) { }\n @c = -> (a: 1, b:, c:, d: 2) { }"
fails:"Proc#arity for instances created with -> () { } returns positive values for definition \n @a = -> (a, b:) { }\n @b = -> (a, b:, &l) { }"
fails:"Proc#arity for instances created with -> () { } returns positive values for definition \n @a = -> (a, b, c:, d: 1) { }\n @b = -> (a, b, c:, d: 1, **k, &l) { }"
fails:"Proc#arity for instances created with -> () { } returns positive values for definition \n @a = -> ((a, (*b, c))) { }\n @b = -> (a, (*b, c), d, (*e), (*)) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a=1) { }\n @b = -> (a=1, b=2) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a, b=1) { }\n @b = -> (a, b, c=1, d=2) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a=1, *b) { }\n @b = -> (a=1, b=2, *c) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (*) { }\n @b = -> (*a) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a, *) { }\n @b = -> (a, *b) { }\n @c = -> (a, b, *c) { }\n @d = -> (a, b, c, *d) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (*a, b) { }\n @b = -> (*a, b, c) { }\n @c = -> (*a, b, c, d) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a, *b, c) { }\n @b = -> (a, b, *c, d, e) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a, b=1, c=2, *d, e, f) { }\n @b = -> (a, b, c=1, *d, e, f, g) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a: 1) { }\n @b = -> (a: 1, b: 2) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a=1, b: 2) { }\n @b = -> (*a, b: 1) { }\n @c = -> (a=1, b: 2) { }\n @d = -> (a=1, *b, c: 2, &l) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (**k, &l) { }\n @b= -> (*a, **k) { }\n @c = ->(a: 1, b: 2, **k) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a=1, *b, c:, d: 2, **k, &l) { }"
fails:"Proc#arity for instances created with -> () { } returns negative values for definition \n @a = -> (a, b=1, *c, d, e:, f: 2, **k, &l) { }\n @b = -> (a, b=1, *c, d:, e:, f: 2, **k, &l) { }\n @c = -> (a=0, b=1, *c, d, e:, f: 2, **k, &l) { }\n @d = -> (a=0, b=1, *c, d:, e:, f: 2, **k, &l) { }"
fails:"Proc#arity for instances created with lambda { || } returns zero for definition \n @a = lambda { }\n @b = lambda { || }"
fails:"Proc#arity for instances created with lambda { || } returns zero for definition \n @a = lambda { |&b| }"
fails:"Proc#arity for instances created with lambda { || } returns positive values for definition \n @a = lambda { |a| }\n @b = lambda { |a, b| }\n @c = lambda { |a, b, c| }\n @d = lambda { |a, b, c, d| }"
fails:"Proc#arity for instances created with lambda { || } returns positive values for definition \n @a = lambda { |a:| }\n @b = lambda { |a:, b:| }\n @c = lambda { |a: 1, b:, c:, d: 2| }"
fails:"Proc#arity for instances created with lambda { || } returns positive values for definition \n @a = lambda { |a, b:| }\n @b = lambda { |a, b:, &l| }"
fails:"Proc#arity for instances created with lambda { || } returns positive values for definition \n @a = lambda { |a, b, c:, d: 1| }\n @b = lambda { |a, b, c:, d: 1, **k, &l| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a=1| }\n @b = lambda { |a=1, b=2| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a, b=1| }\n @b = lambda { |a, b, c=1, d=2| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a=1, *b| }\n @b = lambda { |a=1, b=2, *c| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |*| }\n @b = lambda { |*a| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a, *| }\n @b = lambda { |a, *b| }\n @c = lambda { |a, b, *c| }\n @d = lambda { |a, b, c, *d| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |*a, b| }\n @b = lambda { |*a, b, c| }\n @c = lambda { |*a, b, c, d| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a, *b, c| }\n @b = lambda { |a, b, *c, d, e| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a, b=1, c=2, *d, e, f| }\n @b = lambda { |a, b, c=1, *d, e, f, g| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a: 1| }\n @b = lambda { |a: 1, b: 2| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |a=1, b: 2| }\n @b = lambda { |*a, b: 1| }\n @c = lambda { |a=1, b: 2| }\n @d = lambda { |a=1, *b, c: 2, &l| }"
fails:"Proc#arity for instances created with lambda { || } returns negative values for definition \n @a = lambda { |**k, &l| }\n @b = lambda { |*a, **k| }\n @c = lambda { |a: 1, b: 2, **k| }"
Expand Down
16 changes: 0 additions & 16 deletions spec/truffle/tags/core/unboundmethod/arity_tags.txt
Expand Up @@ -2,23 +2,7 @@ fails:UnboundMethod#arity returns the number of arguments accepted by a method,
fails:UnboundMethod#arity returns the number arguments accepted by a method, using Module#instance_method
fails:UnboundMethod#arity if optional arguments returns the negative number of mandatory arguments, using Method#unbind
fails:UnboundMethod#arity if optional arguments returns the negative number of mandatory arguments, using Module#instance_method
fails:"UnboundMethod#arity returns zero for method definition \n def m() end"
fails:"UnboundMethod#arity returns zero for method definition \n def n(&b) end"
fails:"UnboundMethod#arity returns positive values for method definition \n def m(a) end\n def n(a, b) end\n def o(a, b, c) end\n def p(a, b, c, d) end"
fails:"UnboundMethod#arity returns positive values for method definition \n def m(a:) end\n def n(a:, b:) end\n def o(a: 1, b:, c:, d: 2) end"
fails:"UnboundMethod#arity returns positive values for method definition \n def m(a, b:) end\n def n(a, b:, &l) end"
fails:"UnboundMethod#arity returns positive values for method definition \n def m(a, b, c:, d: 1) end\n def n(a, b, c:, d: 1, **k, &l) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a=1) end\n def n(a=1, b=2) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a, b=1) end\n def n(a, b, c=1, d=2) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a=1, *b) end\n def n(a=1, b=2, *c) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(*) end\n def n(*a) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a, *) end\n def n(a, *b) end\n def o(a, b, *c) end\n def p(a, b, c, *d) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(*a, b) end\n def n(*a, b, c) end\n def o(*a, b, c, d) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a, *b, c) end\n def n(a, b, *c, d, e) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a, b=1, c=2, *d, e, f) end\n def n(a, b, c=1, *d, e, f, g) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a: 1) end\n def n(a: 1, b: 2) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a=1, b: 2) end\n def n(*a, b: 1) end\n def o(a=1, b: 2) end\n def p(a=1, *b, c: 2, &l) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(**k, &l) end\n def n(*a, **k) end\n def o(a: 1, b: 2, **k) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a=1, *b, c:, d: 2, **k, &l) end"
fails:"UnboundMethod#arity returns negative values for method definition \n def m(a, b=1, *c, d, e:, f: 2, **k, &l) end\n def n(a, b=1, *c, d:, e:, f: 2, **k, &l) end\n def o(a=0, b=1, *c, d, e:, f: 2, **k, &l) end\n def p(a=0, b=1, *c, d:, e:, f: 2, **k, &l) end"
fails:UnboundMethod#arity for a Method generated by respond_to_missing? returns -1
Expand Up @@ -69,6 +69,24 @@ public boolean equal(RubyMethod a, Object b) {

}

@CoreMethod(names = "arity")
public abstract static class ArityNode extends CoreMethodNode {

public ArityNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ArityNode(ArityNode prev) {
super(prev);
}

@Specialization
public int arity(RubyMethod method) {
return method.getMethod().getSharedMethodInfo().getArity().getArityNumber();
}

}

@CoreMethod(names = "call", needsBlock = true, argumentsAsArray = true)
public abstract static class CallNode extends CoreMethodNode {

Expand Down
Expand Up @@ -48,7 +48,7 @@ public ArityNode(ArityNode prev) {

@Specialization
public int arity(RubyProc proc) {
return 1;
return proc.getSharedMethodInfo().getArity().getArityNumber();
}

}
Expand Down
Expand Up @@ -25,6 +25,24 @@
@CoreClass(name = "UnboundMethod")
public abstract class UnboundMethodNodes {

@CoreMethod(names = "arity")
public abstract static class ArityNode extends CoreMethodNode {

public ArityNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ArityNode(ArityNode prev) {
super(prev);
}

@Specialization
public int arity(RubyUnboundMethod method) {
return method.getMethod().getSharedMethodInfo().getArity().getArityNumber();
}

}

@CoreMethod(names = "bind", required = 1)
public abstract static class BindNode extends CoreMethodNode {

Expand Down
24 changes: 24 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/runtime/methods/Arity.java
Expand Up @@ -42,4 +42,28 @@ public boolean hasKeywords() {
return hasKeywords;
}

public int getArityNumber() {
int count = required;

if (hasKeywords) {
count++;
}

if (optional > 0 || allowsMore) {
count = -count - 1;
}

return count;
}

@Override
public String toString() {
return "Arity{" +
"required=" + required +
", optional=" + optional +
", allowsMore=" + allowsMore +
", hasKeywords=" + hasKeywords +
'}';
}

}

0 comments on commit c3a3f56

Please sign in to comment.