blib/lib/ExtUtils/Constant/XS.pm

filter: Statementstmt 95.2% 101 / 106 Branchbran 84.0% 42 / 50 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 21 / 21 Podpod 23.5% 4 / 17 totaltotal 84.0% 179 / 213 CC 31 SCAR 16.2
CC
31
Cov
90%
params
11.1
C_constant_other_params
6.0
C_constant_other_params_defintion
6.0
CRAP 5.0
« blib/lib/ExtUtils/Constant/Base.pm ↑ Summary blib/lib/ExtUtils/Constant/Aaargh56Hash.pm »
1 package ExtUtils::Constant::XS;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION %XS_Constant %XS_TypeSet @ISA @EXPORT_OK $is_perl56);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use ExtUtils::Constant::Utils 'perl_stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require ExtUtils::Constant::Base;
8  
9  
10 1 @ISA = qw(ExtUtils::Constant::Base Exporter);
11 1 @EXPORT_OK = qw(%XS_Constant %XS_TypeSet);
12  
13 1 $VERSION = '0.03';
14  
15 1 ▶$is_perl56 = ($] < 5.007 && $] > 5.005_50);
Condition33%1/3
$] < 5.007 && $] > 5.0055
!ll&&!rl&&r
100
MC/DC0%0/2
$] < 5.007 && $] > 5.0055
$] < 5.007 $] > 5.0055
Truth table33%1/3
A && B
A= $] < 5.007B= $] > 5.0055
ABresult
0X0
100
111
16  
17 =head1 NAME
18  
19 ExtUtils::Constant::XS - generate C code for XS modules' constants.
20  
21 =head1 SYNOPSIS
22  
23 require ExtUtils::Constant::XS;
24  
25 =head1 DESCRIPTION
26  
27 ExtUtils::Constant::XS overrides ExtUtils::Constant::Base to generate C
28 code for XS modules' constants.
29  
30 =head1 BUGS
31  
32 Nothing is documented.
33  
34 Probably others.
35  
36 =head1 AUTHOR
37  
38 Nicholas Clark <nick@ccl4.org> based on the code in C<h2xs> by Larry Wall and
39 others
40  
41 =cut
42  
43 # '' is used as a flag to indicate non-ascii macro names, and hence the need
44 # to pass in the utf8 on/off flag.
45 1 %XS_Constant = (
46 '' => '',
47 IV => 'PUSHi(iv)',
48 UV => 'PUSHu((UV)iv)',
49 NV => 'PUSHn(nv)',
50 PV => 'PUSHp(pv, strlen(pv))',
51 PVN => 'PUSHp(pv, iv)',
52 SV => 'PUSHs(sv)',
53 YES => 'PUSHs(&PL_sv_yes)',
54 NO => 'PUSHs(&PL_sv_no)',
55 UNDEF => '', # implicit undef
56 );
57  
58 1 %XS_TypeSet = (
59 IV => '*iv_return = ',
60 UV => '*iv_return = (IV)',
61 NV => '*nv_return = ',
62 PV => '*pv_return = ',
63 PVN => ['*pv_return = ', '*iv_return = (IV)'],
64 SV => '*sv_return = ',
65 YES => undef,
66 NO => undef,
67 UNDEF => undef,
68 );
69  
70 sub header {
71 7 ▶ my $start = 1;
Subroutine header: called CC=3 SCAR=11.0
Pod documented
72 7 my @lines;
73 7 push @lines, "#define PERL_constant_NOTFOUND\t$start\n"; $start++;
74 7 push @lines, "#define PERL_constant_NOTDEF\t$start\n"; $start++;
75 7 foreach (sort keys %XS_Constant) {
76 70 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 7 63 70
77 63 push @lines, "#define PERL_constant_IS$_\t$start\n"; $start++;
78 }
79 7 push @lines, << 'EOT';
80  
81 #ifndef NVTYPE
82 typedef double NV; /* 5.6 and later define NVTYPE, and typedef NV to it. */
83 #endif
84 #ifndef aTHX_
85 #define aTHX_ /* 5.6 or later define this for threading support. */
86 #endif
87 #ifndef pTHX_
88 #define pTHX_ /* 5.6 or later define this for threading support. */
89 #endif
90 EOT
91  
92 7 return join '', @lines;
93 }
94  
95 sub valid_type {
96 165 ▶ my ($self, $type) = @_;
Subroutine valid_type: called CC=1 SCAR=0.0
Pod undocumented
97 165 return exists $XS_TypeSet{$type};
98 }
99  
100 # This might actually be a return statement
101 sub assignment_clause_for_type {
102 74 ▶ my $self = shift;
Subroutine assignment_clause_for_type: called CC=5 SCAR=16.2
Pod undocumented
103 74 my $args = shift;
104 74 my $type = $args->{type};
105 74 my $typeset = $XS_TypeSet{$type};
106 74 ▶ if (ref $typeset) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $typeset) { } 1 73 74
elsif (defined $typeset) { } 70 3 73
107 1 ▶ die "Type $type is aggregate, but only single value given"
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 1 1
108 if @_ == 1;
109 1 return map {"$typeset->[$_]$_[$_];"} 0 .. $#$typeset;
110 } elsif (defined $typeset) {
111 70 ▶ confess "Aggregate value given for type $type"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 70 70
112 if @_ > 1;
113 70 return "$typeset$_[0];";
114 }
115 3 return ();
116 }
117  
118 sub return_statement_for_type {
119 74 ▶ my ($self, $type) = @_;
Subroutine return_statement_for_type: called CC=2 SCAR=7.1
Pod undocumented
120 # In the future may pass in an options hash
121 74 ▶ $type = $type->{type} if ref $type;
Branch: 1 branch
BranchTrueFalseTotal
if ref $type 0 74 74
122 74 "return PERL_constant_IS$type;";
123 }
124  
125 sub return_statement_for_notdef {
126 # my ($self) = @_;
127 56 ▶ "return PERL_constant_NOTDEF;";
Subroutine return_statement_for_notdef: called CC=1 SCAR=0.0
Pod undocumented
128 }
129  
130 sub return_statement_for_notfound {
131 # my ($self) = @_;
132 17 ▶ "return PERL_constant_NOTFOUND;";
Subroutine return_statement_for_notfound: called CC=1 SCAR=0.0
Pod undocumented
133 }
134  
135 sub default_type {
136 1 ▶ 'IV';
Subroutine default_type: called CC=1 SCAR=0.0
Pod documented
137 }
138  
139 sub macro_from_name {
140 115 ▶ my ($self, $item) = @_;
Subroutine macro_from_name: called CC=2 SCAR=7.0
Pod undocumented
141 115 my $macro = $item->{name};
142 115 ▶ $macro = $item->{value} unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 0 115 115
143 115 $macro;
144 }
145  
146 sub macro_from_item {
147 143 ▶ my ($self, $item) = @_;
Subroutine macro_from_item: called CC=2 SCAR=6.9
Pod undocumented
148 143 my $macro = $item->{macro};
149 143 ▶ $macro = $self->macro_from_name($item) unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 115 28 143
150 143 $macro;
151 }
152  
153 # Keep to the traditional perl source macro
154 sub memEQ {
155 18 ▶ "memEQ";
Subroutine memEQ: called CC=1 SCAR=0.0
Pod undocumented
156 }
157  
158 sub params {
159 33 ▶ my ($self, $what) = @_;
Subroutine params: called CC=11 SCAR=24.1
Pod documented
160 33 foreach (sort keys %$what) {
161 74 ▶ warn "ExtUtils::Constant doesn't know how to handle values of type $_" unless defined $XS_Constant{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ExtUtils::Constant::XS::XS_Constant{$_} 0 74 74
162 }
163 33 my $params = {};
164 33 ▶ $params->{''} = 1 if $what->{''};
Branch: 1 branch
BranchTrueFalseTotal
if $what->{""} 13 20 33
165 33 ▶ $params->{IV} = 1 if $what->{IV} || $what->{UV} || $what->{PVN};
Branch: 1 branch
BranchTrueFalseTotal
if $what->{"IV"} or $what->{"UV"} or $what->{"PVN"} 2 6 8
Condition83%5/6
$what->{"IV"} or $what->{"UV"} or $what->{"PVN"}
l!l&&r!l&&!r
2526
$what->{"IV"} or $what->{"UV"}
l!l&&r!l&&!r
2508
MC/DC66%2/3
$what->{"IV"} or $what->{"UV"} or $what->{"PVN"}
$what->{"IV"} $what->{"UV"} $what->{"PVN"}
Truth table75%3/4
A or B or C
A= $what->{"IV"}B= $what->{"UV"}C= $what->{"PVN"}
ABCresult
1XX1
01X1
0011
0000
166 33 ▶ $params->{NV} = 1 if $what->{NV};
Branch: 1 branch
BranchTrueFalseTotal
if $what->{"NV"} 2 31 33
167 33 ▶ $params->{PV} = 1 if $what->{PV} || $what->{PVN};
Branch: 1 branch
BranchTrueFalseTotal
if $what->{"PV"} or $what->{"PVN"} 16 17 33
Condition66%2/3
$what->{"PV"} or $what->{"PVN"}
l!l&&r!l&&!r
16017
MC/DC50%1/2
$what->{"PV"} or $what->{"PVN"}
$what->{"PV"} $what->{"PVN"}
Truth table66%2/3
A or B
A= $what->{"PV"}B= $what->{"PVN"}
ABresult
1X1
011
000
168 33 ▶ $params->{SV} = 1 if $what->{SV};
Branch: 1 branch
BranchTrueFalseTotal
if $what->{"SV"} 2 31 33
169 33 return $params;
170 }
171  
172  
173 sub C_constant_prefix_param {
174 19 ▶ "aTHX_ ";
Subroutine C_constant_prefix_param: called CC=1 SCAR=0.0
Pod undocumented
175 }
176  
177 sub C_constant_prefix_param_defintion {
178 18 ▶ "pTHX_ ";
Subroutine C_constant_prefix_param_defintion: called CC=1 SCAR=0.0
Pod undocumented
179 }
180  
181 sub namelen_param_definition {
182 7 ▶ 'STRLEN ' . $_[0] -> namelen_param;
Subroutine namelen_param_definition: called CC=1 SCAR=0.0
Pod undocumented
183 }
184  
185 sub C_constant_other_params_defintion {
186 17 ▶ my ($self, $params) = @_;
Subroutine C_constant_other_params_defintion: called CC=6 SCAR=17.9
Pod undocumented
187 17 my $body = '';
188 17 ▶ $body .= ", int utf8" if $params->{''};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{""} 7 10 17
189 17 ▶ $body .= ", IV *iv_return" if $params->{IV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"IV"} 14 3 17
190 17 ▶ $body .= ", NV *nv_return" if $params->{NV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"NV"} 1 16 17
191 17 ▶ $body .= ", const char **pv_return" if $params->{PV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"PV"} 8 9 17
192 17 ▶ $body .= ", SV **sv_return" if $params->{SV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"SV"} 1 16 17
193 17 $body;
194 }
195  
196 sub C_constant_other_params {
197 10 ▶ my ($self, $params) = @_;
Subroutine C_constant_other_params: called CC=6 SCAR=18.0
Pod undocumented
198 10 my $body = '';
199 10 ▶ $body .= ", utf8" if $params->{''};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{""} 4 6 10
200 10 ▶ $body .= ", iv_return" if $params->{IV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"IV"} 8 2 10
201 10 ▶ $body .= ", nv_return" if $params->{NV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"NV"} 0 10 10
202 10 ▶ $body .= ", pv_return" if $params->{PV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"PV"} 6 4 10
203 10 ▶ $body .= ", sv_return" if $params->{SV};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"SV"} 0 10 10
204 10 $body;
205 }
206  
207 sub dogfood {
208 7 ▶ my ($self, $args, @items) = @_;
Subroutine dogfood: called CC=2 SCAR=7.5
Pod documented
209 my ($package, $subname, $default_type, $what, $indent, $breakout) =
210 7 @{$args}{qw(package subname default_type what indent breakout)};
211 7 my $result = <<"EOT";
212 /* When generated this function returned values for the list of names given
213 in this section of perl code. Rather than manually editing these functions
214 to add or remove constants, which would result in this comment and section
215 of code becoming inaccurate, we recommend that you edit this section of
216 code, and use it to regenerate a new set of constant functions which you
217 then use to replace the originals.
218  
219 Regenerate these constant functions by feeding this entire source file to
220 perl -x
221  
222 #!$^X -w
223 use ExtUtils::Constant qw (constant_types C_constant XS_constant);
224  
225 EOT
226 7 $result .= $self->dump_names ({default_type=>$default_type, what=>$what,
227 indent=>0, declare_types=>1},
228 @items);
229 7 $result .= <<'EOT';
230  
231 print constant_types(), "\n"; # macro defs
232 EOT
233 7 $package = perl_stringify($package);
234 7 $result .=
235 "foreach (C_constant (\"$package\", '$subname', '$default_type', \$types, ";
236 # The form of the indent parameter isn't defined. (Yet)
237 7 ▶ if (defined $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent) { } 0 7 7
238 0 require Data::Dumper;
239 0 $Data::Dumper::Terse=1;
240 0 $Data::Dumper::Terse=1; # Not used once. :-)
241 0 chomp ($indent = Data::Dumper::Dumper ($indent));
242 0 $result .= $indent;
243 } else {
244 7 $result .= 'undef';
245 }
246 7 $result .= ", $breakout" . ', @names) ) {
247 print $_, "\n"; # C constant subs
248 }
249 print "\n#### XS Section:\n";
250 print XS_constant ("' . $package . '", $types);
251 __END__
« blib/lib/ExtUtils/Constant/Base.pm ↑ Summary blib/lib/ExtUtils/Constant/Aaargh56Hash.pm »