Skip to content

Commit

Permalink
Implement signature introspection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Oct 8, 2009
1 parent 3def5a5 commit 1cfff7c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -163,6 +163,7 @@ SETTING = \
src/setting/Object.pm \
src/setting/Operators.pm \
src/setting/Pair.pm \
src/setting/Parameter.pm \
src/setting/Range.pm \
src/setting/Str.pm \
src/setting/Temporal.pm \
Expand Down
2 changes: 2 additions & 0 deletions src/classes/Object.pir
Expand Up @@ -16,12 +16,14 @@ care of much of that.

# A few useful constants (just here so they're available going forward).
.const int SIG_ELEM_SLURPY_POS = 8
.const int SIG_ELEM_SLURPY_NAMED = 16
.const int SIG_ELEM_SLURPY = 56
.const int SIG_ELEM_INVOCANT = 64
.const int SIG_ELEM_MULTI_INVOCANT = 128
.const int SIG_ELEM_INVOCANT_AND_MULTI_INVOCANT = 192
.const int SIG_ELEM_IS_RW = 256
.const int SIG_ELEM_IS_COPY = 512
.const int SIG_ELEM_IS_REF = 1024
.const int SIG_ELEM_IS_OPTIONAL = 2048

=head2 Methods
Expand Down
70 changes: 70 additions & 0 deletions src/classes/Signature.pir
Expand Up @@ -25,6 +25,76 @@ P6LowLevelSig and provides higher level access to it.

=over 4

=item params

Returns a C<List> of C<Parameter> descriptors.

=cut

.sub 'params' :method
# Create result.
.local pmc result
result = new 'ResizablePMCArray'

# Grab low level signature we're wrapping.
.local pmc signature
signature = getattribute self, '$!ll_sig'
signature = descalarref signature

# And Parameter proto.
.local pmc parameter
parameter = get_hll_global 'Parameter'

# Loop over parameters.
.local int cur_param, count
count = get_signature_size signature
cur_param = -1
param_loop:
inc cur_param
unless cur_param < count goto param_done

# Get all curent parameter info.
.local pmc nom_type, cons_type, names
.local int flags, optional, invocant, multi_invocant, slurpy, rw, ref, copy, named
.local string name
get_signature_elem signature, cur_param, name, flags, nom_type, cons_type, names, $P1
optional = flags & SIG_ELEM_IS_OPTIONAL
invocant = flags & SIG_ELEM_INVOCANT
multi_invocant = flags & SIG_ELEM_MULTI_INVOCANT
slurpy = flags & SIG_ELEM_SLURPY
rw = flags & SIG_ELEM_IS_RW
ref = flags & SIG_ELEM_IS_REF
copy = flags & SIG_ELEM_IS_COPY

# Make sure constraints is non-null.
unless null cons_type goto cons_done
cons_type = 'undef'()
cons_done:

# Any names?
named = 0
if null names goto no_names
named = 1
names = 'list'(names)
goto names_done
no_names:
names = 'list'()
$I0 = flags & SIG_ELEM_SLURPY_NAMED
unless $I0 goto names_done
named = 1
names_done:

# Create parameter instance. XXX Missing $.default, $.signature
$P0 = parameter.'new'('name'=>name, 'type'=>nom_type, 'constraint'=>cons_type, 'optional'=>optional, 'slurpy'=>slurpy, 'invocant'=>invocant, 'multi_invocant'=>multi_invocant, 'rw'=>rw, 'ref'=>ref, 'copy'=>copy, 'named'=>named, 'named_names'=>names)
push result, $P0
goto param_loop
param_done:

# Turn into a List.
.tailcall 'list'(result :flat)
.end


=item perl

Gets a perl representation of the signature.
Expand Down
2 changes: 1 addition & 1 deletion src/setting/Operators.pm
Expand Up @@ -33,7 +33,7 @@ multi sub infix:<...> (@lhs, Code $generator, :$limit) {
# throws a "Null PMC access in get_bool()" when used in boolean context.
# we have to use an ugly special case here.
# and we can't even used !~~ for that (RT #69364)
if !$generator.^isa(WhateverCode) and any( $generator.signature.params>>.<slurpy> ) {
if !$generator.^isa(WhateverCode) and any( $generator.signature.params>>.slurpy ) {
$argument-indexes = 0..*-1;
} else {
$argument-indexes = *-$c .. *-1;
Expand Down
19 changes: 19 additions & 0 deletions src/setting/Parameter.pm
@@ -0,0 +1,19 @@
class Parameter {
has $.name;
has $.type;
has $.constraints;
has $.rw;
has $.ref;
has $.copy;
method readonly() { !$!rw && !$!ref && !$!copy }
has $.named;
has $.named_names;
has $.slurpy;
has $.optional;
has $.default;
has $.invocant;
has $.multi_invocant;
has $.signature;
}

# vim: ft=perl6

0 comments on commit 1cfff7c

Please sign in to comment.