Skip to content
Browse files

Implement arity method on NCI PMCs, plus a test for it.

git-svn-id: https://svn.parrot.org/parrot/trunk@23952 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
1 parent 637246f commit 6586a2ca6fba7ac675bb53772fda5b555a8ddd4e @jnthn jnthn committed
Showing with 50 additions and 3 deletions.
  1. +21 −2 src/pmc/nci.pmc
  2. +29 −1 t/pmc/nci.t
View
23 src/pmc/nci.pmc
@@ -102,7 +102,7 @@ Sets the specified function pointer and signature (C<*key>).
nci_info->signature = key;
/* Arity is length of that string minus one (the return type). */
- nci_info->arity = string_length(INTERP, key);
+ nci_info->arity = string_length(INTERP, key) - 1;
/* Build call function. */
nci_info->func = build_call_func(INTERP, SELF, key);
@@ -192,7 +192,7 @@ shifted down.
typedef INTVAL (*nci_sub_t)(PARROT_INTERP, PMC *);
nci_sub_t func = PObj_flag_TEST(private2, SELF)
- ? (nci_sub_t) PMC_struct_val(SELF)
+ ? (nci_sub_t) D2FPTR(PMC_struct_val(SELF))
: (nci_sub_t) D2FPTR(nci_info->func);
PMC *cont;
@@ -250,6 +250,25 @@ Returns the boolean value of the pointer.
return (0 != (INTVAL)nci_info->func);
}
+/*
+
+=item C<METHOD INTVAL arity()>
+
+Return the arity of the NCI (the number of arugments).
+
+=cut
+
+*/
+ METHOD INTVAL arity() {
+ Parrot_NCI *nci_info = PARROT_NCI(SELF);
+ if (nci_info != NULL && nci_info->func != NULL)
+ return nci_info->arity;
+ else
+ real_exception(INTERP, NULL, INVALID_OPERATION,
+ "You cannot get the arity of an undefined NCI.");
+ return 0; /* Unreachable; avoids warning. */
+ }
+
}
/*
View
30 t/pmc/nci.t
@@ -6,7 +6,7 @@ use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
-use Parrot::Test tests => 64;
+use Parrot::Test tests => 65;
use Parrot::Config qw(%PConfig);
=head1 NAME
@@ -2613,6 +2613,34 @@ X: 1
Y: 100
OUTPUT
+pir_output_is( << 'CODE', << 'OUTPUT', "arity" );
+.sub test :main
+ .local string library_name
+ library_name = 'libnci_test'
+ .local pmc libnci_test
+ libnci_test = loadlib library_name
+
+ .local pmc nci_c
+ nci_c = dlfunc libnci_test, "nci_c", "c"
+ $I0 = nci_c.arity()
+ say $I0
+
+ .local pmc multiply
+ multiply = dlfunc libnci_test, "nci_pii", "pii"
+ $I0 = multiply.arity()
+ say $I0
+
+ .local pmc nci_iiii
+ nci_iiii = dlfunc libnci_test, "nci_iiii", "iiii"
+ $I0 = nci_iiii.arity()
+ say $I0
+.end
+CODE
+0
+2
+3
+OUTPUT
+
# Local Variables:
# mode: cperl
# cperl-indent-level: 4

0 comments on commit 6586a2c

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