Skip to content
Browse files

Add .arity to complement .count

  • Loading branch information...
1 parent 17f7681 commit fbd41a78ba0685eab1d609f9d0608d69bc582de4 @sorear committed Jun 8, 2011
Showing with 31 additions and 4 deletions.
  1. +28 −2 lib/Builtins.cs
  2. +2 −2 lib/CORE.setting
  3. +1 −0 t/spectest.data
View
30 lib/Builtins.cs
@@ -1327,7 +1327,12 @@ class SubstrLValue: Variable {
return fr.info.name.Substring(fr.info.name.IndexOf(" ")+1);
}
- public static int bif_arity(P6any fcni) {
+ public static Variable bif_count(P6any fcni) {
+ int i = get_count(fcni);
+ return (i == int.MaxValue) ? MakeFloat(double.PositiveInfinity) :
+ MakeInt(i);
+ }
+ public static int get_count(P6any fcni) {
if (!fcni.Isa(Kernel.CodeMO))
return 1; // can't introspect fake subs (?)
SubInfo si = (SubInfo) fcni.GetSlot("info");
@@ -1346,6 +1351,27 @@ class SubstrLValue: Variable {
return arity;
}
+ public static Variable bif_arity(P6any fcni) {
+ if (!fcni.Isa(Kernel.CodeMO))
+ return MakeInt(1); // can't introspect fake subs (?)
+ SubInfo si = (SubInfo) fcni.GetSlot("info");
+ int[] sig = si.sig_i;
+ if (sig == null)
+ return MakeInt(1);
+ int arity = 0;
+ for (int i = 0; i < sig.Length; i += SubInfo.SIG_I_RECORD) {
+ int fl = sig[i + SubInfo.SIG_I_FLAGS];
+ if ((fl & (SubInfo.SIG_F_SLURPY_CAP | SubInfo.SIG_F_SLURPY_POS |
+ SubInfo.SIG_F_SLURPY_PCL | SubInfo.SIG_F_SLURPY_NAM |
+ SubInfo.SIG_F_OPTIONAL | SubInfo.SIG_F_DEFOUTER |
+ SubInfo.SIG_F_HASDEFAULT)) != 0)
+ continue;
+ if ((fl & SubInfo.SIG_F_POSITIONAL) == 0) continue;
+ arity++;
+ }
+ return MakeInt(arity);
+ }
+
class ItemSource {
protected ItemSource() {}
public static ItemSource Empty = new ItemSource();
@@ -1563,7 +1589,7 @@ class CrossSource: ItemSource {
VarDeque iter = new VarDeque(lst);
Variable fcn = iter.Shift();
P6any fcni = fcn.Fetch();
- int arity = bif_arity(fcni);
+ int arity = get_count(fcni);
Frame fr = th.MakeChild(null, CommonMEMap_I, Kernel.AnyP);
fr.lexi0 = 0;
View
4 lib/CORE.setting
@@ -326,8 +326,8 @@ my class Code is Callable {
my class Block is Code {
method ACCEPTS(Mu $t) { defined(self) ?? (self)($t) !! $t.^does(self) }
- method count() { Q:CgOp { (box Int (_cgop bif_arity (@ {self}))) } }
- # arity NYI
+ method count() { Q:CgOp { (_cgop bif_count (@ {self})) } }
+ method arity() { Q:CgOp { (_cgop bif_arity (@ {self})) } }
}
my class Routine is Block {
View
1 t/spectest.data
@@ -78,6 +78,7 @@ S05-metasyntax/null.t
S05-metasyntax/single-quotes.t
S06-multi/lexical-multis.t
S06-advanced_subroutine_features/caller.t
+S06-signature/arity.t
S06-signature/passing-arrays.t
S06-signature/positional-placeholders.t
S06-signature/positional.t

0 comments on commit fbd41a7

Please sign in to comment.
Something went wrong with that request. Please try again.