| 1 | package FFI::Platypus::TypeParser; | ||||||||||
| 2 | |||||||||||
| 3 | 53 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 53 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 53 | ▶ | use 5.008004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 53 | ▶ | use List::Util 1.45 qw( uniqstr ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 53 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: FFI Type Parser | ||||||||||
| 10 | 53 | our $VERSION = '2.12'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | # The TypeParser and Type classes are used internally ONLY and | ||||||||||
| 14 | # are not to be exposed to the user. External users should | ||||||||||
| 15 | # not under any circumstances rely on the implementation of | ||||||||||
| 16 | # these classes. | ||||||||||
| 17 | |||||||||||
| 18 | sub new | ||||||||||
| 19 | 432 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 20 | 432 | my($class) = @_; | |||||||||
| 21 | 432 | my $self = bless { types => {}, type_map => {}, abi => -1 }, $class; | |||||||||
| 22 | 432 | $self->build; | |||||||||
| 23 | 432 | $self; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 432 | ▶ | sub build {} | ||||||||
|
Subroutine
build: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 27 | |||||||||||
| 28 | 53 | our %basic_type; | |||||||||
| 29 | |||||||||||
| 30 | # this just checks if the underlying libffi/platypus implementation | ||||||||||
| 31 | # has the basic type. It is used mainly to verify that exotic types | ||||||||||
| 32 | # like longdouble and complex_float are available before the test | ||||||||||
| 33 | # suite tries to use them. | ||||||||||
| 34 | sub have_type | ||||||||||
| 35 | 25010 | ▶ | { | ||||||||
|
Subroutine
have_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 36 | 25010 | my(undef, $name) = @_; | |||||||||
| 37 | 25010 | !!$basic_type{$name}; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub create_type_custom | ||||||||||
| 41 | 207 | ▶ | { | ||||||||
|
Subroutine
create_type_custom: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 42 | 207 | my($self, $name, @rest) = @_; | |||||||||
| 43 | 207 | ▶ | $name = 'opaque' unless defined $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 207 | my $type = $self->parse($name); | |||||||||
| 45 | 207 | ▶ | unless($type->is_customizable) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | { | ||||||||||
| 47 | 2 | croak "$name is not a legal basis for a custom type" | |||||||||
| 48 | } | ||||||||||
| 49 | 205 | $self->_create_type_custom($type, @rest); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # this is the type map provided by the language plugin, if any | ||||||||||
| 53 | # in addition to the basic types (which map to themselves). | ||||||||||
| 54 | sub type_map | ||||||||||
| 55 | 3486 | ▶ | { | ||||||||
|
Subroutine
type_map: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 56 | 3486 | my($self, $new) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 3486 | ▶ | if(defined $new) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | { | ||||||||||
| 60 | 365 | $self->{type_map} = $new; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 3486 | $self->{type_map}; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | # this stores the types that have been mentioned so far. It also | ||||||||||
| 67 | # usually includes aliases. | ||||||||||
| 68 | sub types | ||||||||||
| 69 | 7860 | ▶ | { | ||||||||
|
Subroutine
types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 70 | 7860 | shift->{types}; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # The type parser needs to know the ABI when creating closures | ||||||||||
| 74 | sub abi | ||||||||||
| 75 | 116 | ▶ | { | ||||||||
|
Subroutine
abi: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 76 | 116 | my($self, $new) = @_; | |||||||||
| 77 | 116 | ▶ | $self->{abi} = $new if defined $new; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 116 | $self->{abi}; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | { | ||||||||||
| 82 | 53 | my %store; | |||||||||
| 83 | |||||||||||
| 84 | 53 | foreach my $name (keys %basic_type) | |||||||||
| 85 | { | ||||||||||
| 86 | 848 | my $type_code = $basic_type{$name}; | |||||||||
| 87 | 848 | $store{basic}->{$name} = __PACKAGE__->create_type_basic($type_code); | |||||||||
| 88 | 848 | $store{ptr}->{$name} = __PACKAGE__->create_type_pointer($type_code); | |||||||||
| 89 | 848 | $store{rev}->{$type_code} = $name; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | sub global_types | ||||||||||
| 93 | 4963 | ▶ | { | ||||||||
|
Subroutine
global_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 94 | 4963 | \%store; | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # list all the types that this type parser knows about, including | ||||||||||
| 99 | # those provided by the language plugin (if any), those defined | ||||||||||
| 100 | # by the user, and the basic types that everyone gets. | ||||||||||
| 101 | sub list_types | ||||||||||
| 102 | 53 | ▶ | { | ||||||||
|
Subroutine
list_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 103 | 3 | my($self) = @_; | |||||||||
| 104 | 3 | uniqstr( ( keys %{ $self->type_map } ), ( keys %{ $self->types } ) ); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 53 | our @CARP_NOT = qw( FFI::Platypus ); | |||||||||
| 108 | |||||||||||
| 109 | 53 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||