blib/lib/XS/JIT/Header.pm

filter: Statementstmt 72.1% 225 / 312 Branchbran 37.1% 52 / 140 Conditioncond 40.6% 26 / 64 MC/DCmcdc 10.2% 4 / 39 Subroutinesub 90.9% 30 / 33 Podpod 90.9% 10 / 11 totaltotal 57.9% 347 / 599 CC 105 SCAR 36.3
CC
105
Cov
59%
attach
102.5
_generate_wrapper
82.5
compile
56.0
CRAP 37.6
↑ Summary blib/lib/XS/JIT.pm »
1 package XS::JIT::Header;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.22';
8  
9 3 ▶use XS::JIT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use XS::JIT::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use XS::JIT::Header::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use XS::JIT::Header::TypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub new {
18 14 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=8 SCAR=22.8
Pod documented
19  
20 my $self = bless {
21 header => $opts{header},
22 lib => $opts{lib},
23 include => $opts{include} || [],
24 define => $opts{define} || {},
25 package => $opts{package} || caller(),
26 cache_dir => $opts{cache_dir} || '_CACHED_XS',
27 prefix => $opts{prefix} || '',
28 14 ▶ force => $opts{force} || 0,
Condition61%8/13
$opts{'include'} || []
l!l
014
$opts{'define'} || {}
l!l
014
$opts{'package'} || caller()
l!l&&r!l&&!r
1310
$opts{'cache_dir'} || '_CACHED_XS'
l!l
131
$opts{'prefix'} || ''
l!l
014
$opts{'force'} || 0
l!l
014
MC/DC0%0/1
$opts{'include'} || []
$opts{'include'}
MC/DC0%0/1
$opts{'define'} || {}
$opts{'define'}
MC/DC0%0/2
$opts{'package'} || caller()
$opts{'package'} caller()
MC/DC100%1/1
$opts{'cache_dir'} || '_CACHED_XS'
$opts{'cache_dir'}
MC/DC0%0/1
$opts{'prefix'} || ''
$opts{'prefix'}
MC/DC0%0/1
$opts{'force'} || 0
$opts{'force'}
Truth table50%1/2
A
A= $opts{'include'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'define'}
Aresult
11
00
Truth table66%2/3
A || B
A= $opts{'package'}B= caller()
ABresult
1X1
011
000
Truth table100%2/2
A
A= $opts{'cache_dir'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'prefix'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'force'}
Aresult
11
00
29  
30 # Internal state
31 _parser => undef,
32 _functions => [], # Functions to compile
33 _compiled => 0,
34 _module_id => undef,
35 }, $class;
36  
37 # Parse header if provided
38 14 ▶ if ($self->{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header"}) 14 0 14
39 14 $self->_parse_header();
40 }
41  
42 13 return $self;
43 }
44  
45 # Parse the header file
46 sub _parse_header {
47 14 ▶ my ($self) = @_;
Subroutine _parse_header: called CC=3 SCAR=11.0
48  
49 $self->{_parser} = XS::JIT::Header::Parser->new(
50 include => $self->{include},
51 define => $self->{define},
52 14 );
53  
54 14 my $header = $self->{header};
55  
56 # Handle system headers (e.g., 'math.h')
57 14 ▶ if ($header !~ m{^[/.]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header =~ m[^[/.]]) 5 9 14
58 5 $header = $self->_find_header($header);
59 }
60  
61 14 ▶ if (-f $header) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $header) { } 13 1 14
62 13 $self->{_parser}->parse_file($header);
63 13 $self->{_header_path} = $header;
64 }
65 else {
66 1 die "Cannot find header file: $self->{header}";
67 }
68  
69 # Generate unique module ID based on header content and options
70 13 $self->{_module_id} = $self->_generate_module_id();
71  
72 13 return $self;
73 }
74  
75 # Get system include paths - uses Perl's Config which knows where headers are
76 sub system_include_paths {
77 5 ▶ my ($class_or_self) = @_;
Subroutine system_include_paths: called CC=11 SCAR=34.5
Pod undocumented
78  
79 5 my @paths;
80  
81 # Primary: Perl's configured system include (most reliable)
82 5 ▶ push @paths, $Config{usrinc} if $Config{usrinc};
Branch: 1 branch
BranchTrueFalseTotal
if $XS::JIT::Header::Config{"usrinc"} 5 0 5
83  
84 # From Perl's library paths (may contain include dirs)
85 5 ▶ if ($Config{locincpth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XS::JIT::Header::Config{"locincpth"}) 5 0 5
86 5 push @paths, split(/\s+/, $Config{locincpth});
87 }
88  
89 # Standard fallbacks
90 5 push @paths, '/usr/include', '/usr/local/include';
91  
92 # macOS: use xcrun to find SDK (cached)
93 5 ▶ if ($^O eq 'darwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "darwin") 0 5 5
94 0 state $sdk_path;
95 0 ▶ unless (defined $sdk_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sdk_path) 0 0 0
96 0 $sdk_path = `xcrun --show-sdk-path 2>/dev/null`;
97 0 ▶ chomp $sdk_path if $sdk_path;
Branch: 1 branch
BranchTrueFalseTotal
if $sdk_path 0 0 0
98 0 ▶ $sdk_path //= '';
Condition0%0/2
$sdk_path //= ""
l!l
00
MC/DC0%0/1
$sdk_path //= ""
$sdk_path
Truth table0%0/2
A
A= $sdk_path
Aresult
11
00
99 }
100 0 ▶ push @paths, "$sdk_path/usr/include" if $sdk_path && -d "$sdk_path/usr/include";
Branch: 1 branch
BranchTrueFalseTotal
if $sdk_path and -d "$sdk_path/usr/include" 0 0 0
Condition0%0/3
$sdk_path and -d "$sdk_path/usr/include"
!ll&&!rl&&r
000
MC/DC0%0/2
$sdk_path and -d "$sdk_path/usr/include"
$sdk_path -d "$sdk_path/usr/include"
Truth table0%0/3
A and B
A= $sdk_pathB= -d "$sdk_path/usr/include"
ABresult
0X0
100
111
101 }
102  
103 # Filter to existing directories and dedupe
104 5 my %seen;
105 5 ▶ return grep { defined $_ && -d $_ && !$seen{$_}++ } @paths;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and -d $_ and not $seen{$_}++ 20 25 45
Condition66%2/3
defined $_ and -d $_
!ll&&!rl&&r
02520
MC/DC50%1/2
defined $_ and -d $_
defined $_ -d $_
Truth table66%2/3
A and B
A= defined $_B= -d $_
ABresult
0X0
100
111
106 }
107  
108 # Find a header file in system include paths
109 sub _find_header {
110 5 ▶ my ($self, $header) = @_;
Subroutine _find_header: called CC=3 SCAR=11.3
111  
112 # User-specified include paths first
113 my @search_paths = (
114 5 @{$self->{include}},
115 $self->system_include_paths,
116 );
117  
118 5 for my $dir (@search_paths) {
119 5 my $path = File::Spec->catfile($dir, $header);
120 5 ▶ return $path if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 5 0 5
121 }
122  
123 # Not found - return original (will fail later with clear error)
124 0 return $header;
125 }
126  
127 # Generate unique module ID for caching
128 sub _generate_module_id {
129 13 ▶ my ($self) = @_;
Subroutine _generate_module_id: called CC=3 SCAR=11.4
130  
131 my $sig = join("\0",
132 $self->{header} || '',
133 $self->{lib} || '',
134 $self->{package},
135 $self->{prefix},
136 13 ▶ map { "$_=$self->{define}{$_}" } sort keys %{$self->{define}},
Condition50%2/4
$self->{'header'} || ''
l!l
130
$self->{'lib'} || ''
l!l
013
MC/DC0%0/1
$self->{'header'} || ''
$self->{'header'}
MC/DC0%0/1
$self->{'lib'} || ''
$self->{'lib'}
Truth table50%1/2
A
A= $self->{'header'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'lib'}
Aresult
11
00
137 );
138  
139 13 my $hash = substr(md5_hex($sig), 0, 8);
140 13 my $pkg = $self->{package};
141 13 $pkg =~ s/::/_/g;
142  
143 13 return "${pkg}_Header_${hash}";
144 }
145  
146 # Attach a single function
147 sub attach {
148 15 ▶ my ($self, $c_name, @args) = @_;
Subroutine attach: called CC=20 SCAR=46.3
Pod documented
149  
150 15 ▶ die "Function name required" unless defined $c_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c_name 0 15 15
151  
152 # Parse arguments: attach('c_name') or attach('c_name' => 'perl_name')
153 # or attach('c_name' => ['arg_types'] => 'return_type')
154 15 my ($perl_name, $arg_types, $return_type);
155  
156 15 ▶ if (@args == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (@args == 0) { } 15 0 15
elsif (@args == 1 and not ref $args[0]) { } 0 0 0
elsif (@args == 2 and ref $args[0] eq 'ARRAY') { } 0 0 0
elsif (@args == 3) { } 0 0 0
157 # attach('func') - use same name, strip prefix
158 15 $perl_name = $c_name;
159 15 ▶ $perl_name =~ s/^\Q$self->{prefix}\E// if $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefix"} 0 15 15
160 }
161 ▶ elsif (@args == 1 && !ref $args[0]) {
Condition0%0/3
@args == 1 and not ref $args[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@args == 1 and not ref $args[0]
@args == 1 not ref $args[0]
Truth table0%0/3
A and not(B)
A= @args == 1B= not ref $args[0]
ABresult
0X0
100
111
162 # attach('c_func' => 'perl_func')
163 0 $perl_name = $args[0];
164 }
165 ▶ elsif (@args == 2 && ref $args[0] eq 'ARRAY') {
Condition0%0/3
@args == 2 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@args == 2 and ref $args[0] eq "ARRAY"
@args == 2 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @args == 2B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
166 # attach('c_func' => ['int', 'char*'] => 'int')
167 0 $perl_name = $c_name;
168 0 ▶ $perl_name =~ s/^\Q$self->{prefix}\E// if $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefix"} 0 0 0
169 0 $arg_types = $args[0];
170 0 $return_type = $args[1];
171 }
172 elsif (@args == 3) {
173 # attach('c_func' => 'perl_func' => ['int'] => 'int')
174 0 $perl_name = $args[0];
175 0 $arg_types = $args[1];
176 0 $return_type = $args[2];
177 }
178  
179 # Get function info from parser if not explicitly provided
180 15 my $func_info = $self->{_parser}->function($c_name);
181  
182 15 ▶ if (!$func_info && !$arg_types) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func_info or $arg_types) 0 15 15
Condition33%1/3
$func_info or $arg_types
l!l&&r!l&&!r
1500
MC/DC0%0/2
$func_info or $arg_types
$func_info $arg_types
Truth table33%1/3
A or B
A= $func_infoB= $arg_types
ABresult
1X1
011
000
183 0 die "Unknown function '$c_name' and no type information provided";
184 }
185  
186 # Use parsed info if not explicitly provided
187 15 ▶ $arg_types //= $func_info->{param_types} if $func_info;
Branch: 1 branch
BranchTrueFalseTotal
if $func_info 15 0 15
Condition33%1/3
$arg_types //= $func_info->{"param_types"}
l!l&&r!l&&!r
0150
MC/DC0%0/2
$arg_types //= $func_info->{"param_types"}
$arg_types $func_info->{"param_types"}
Truth table33%1/3
A //= B
A= $arg_typesB= $func_info->{"param_types"}
ABresult
1X1
011
000
188 15 ▶ $return_type //= $func_info->{return_type} if $func_info;
Branch: 1 branch
BranchTrueFalseTotal
if $func_info 15 0 15
Condition33%1/3
$return_type //= $func_info->{"return_type"}
l!l&&r!l&&!r
0150
MC/DC0%0/2
$return_type //= $func_info->{"return_type"}
$return_type $func_info->{"return_type"}
Truth table33%1/3
A //= B
A= $return_typeB= $func_info->{"return_type"}
ABresult
1X1
011
000
189  
190 # Build full Perl name
191 15 ▶ my $full_perl_name = $perl_name =~ /::/ ? $perl_name : "$self->{package}::$perl_name";
Branch: 1 branch
BranchTrueFalseTotal
$perl_name =~ /::/ ? : 0 15 15
192  
193 15 ▶ push @{$self->{_functions}}, {
Branch: 1 branch
BranchTrueFalseTotal
$func_info ? : 15 0 15
Condition50%2/4
$arg_types || []
l!l
150
$return_type || 'void'
l!l
150
MC/DC0%0/1
$arg_types || []
$arg_types
MC/DC0%0/1
$return_type || 'void'
$return_type
Truth table50%1/2
A
A= $arg_types
Aresult
11
00
Truth table50%1/2
A
A= $return_type
Aresult
11
00
194 c_name => $c_name,
195 perl_name => $full_perl_name,
196 arg_types => $arg_types || [],
197 return_type => $return_type || 'void',
198 15 is_variadic => $func_info ? $func_info->{is_variadic} : 0,
199 };
200  
201 15 return $self;
202 }
203  
204 # Attach multiple functions
205 sub attach_all {
206 1 ▶ my ($self, $filter) = @_;
Subroutine attach_all: called CC=7 SCAR=29.6
Pod documented
207  
208 1 my @names = $self->{_parser}->function_names;
209  
210 1 for my $name (@names) {
211 # Apply filter if provided
212 4 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 0 4 4
213 0 ▶ if (ref $filter eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'Regexp') { } 0 0 0
elsif (ref $filter eq 'CODE') { } 0 0 0
214 0 ▶ next unless $name =~ $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$filter/ 0 0 0
215 }
216 elsif (ref $filter eq 'CODE') {
217 0 ▶ next unless $filter->($name);
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter($name) 0 0 0
218 }
219 }
220  
221 4 $self->attach($name);
222 }
223  
224 1 return $self;
225 }
226  
227 # Get list of function names from parsed header
228 sub functions {
229 2 ▶ my ($self) = @_;
Subroutine functions: called CC=1 SCAR=0.0
Pod documented
230 2 return $self->{_parser}->function_names;
231 }
232  
233 # Get function info
234 sub function {
235 4 ▶ my ($self, $name) = @_;
Subroutine function: called CC=1 SCAR=0.0
Pod documented
236 4 return $self->{_parser}->function($name);
237 }
238  
239 # Get list of constant names
240 sub constants {
241 1 ▶ my ($self) = @_;
Subroutine constants: called CC=1 SCAR=0.0
Pod documented
242 1 return $self->{_parser}->constant_names;
243 }
244  
245 # Get constant value
246 sub constant {
247 2 ▶ my ($self, $name) = @_;
Subroutine constant: called CC=2 SCAR=7.1
Pod documented
248 2 my $info = $self->{_parser}->constant($name);
249 2 ▶ return $info ? $info->{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 2 0 2
250 }
251  
252 # Generate XS wrapper code for a function
253 sub _generate_wrapper {
254 14 ▶ my ($self, $func) = @_;
Subroutine _generate_wrapper: called CC=22 SCAR=44.1
255  
256 14 my $b = XS::JIT::Builder->new;
257 14 my $safe_name = $func->{c_name};
258 14 $safe_name =~ s/\W/_/g;
259  
260 14 my @arg_types = @{$func->{arg_types}};
261 14 my $return_type = $func->{return_type};
262  
263 14 $b->xs_function("xs_$safe_name")
264 ->xs_preamble;
265  
266 # Check argument count
267 14 my $num_args = scalar @arg_types;
268 14 ▶ if ($num_args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_args > 0) 10 4 14
269 10 my $usage = join(', ', map { "\$arg$_" } 0 .. $#arg_types);
270 10 $b->check_items($num_args, $num_args, $usage);
271 }
272  
273 # Convert arguments from Perl to C
274 14 my @c_args;
275 14 for my $i (0 .. $#arg_types) {
276 20 my $type = $arg_types[$i];
277 20 my $type_info = XS::JIT::Header::TypeMap::resolve($type);
278 20 my $arg_name = "arg$i";
279  
280 20 ▶ if ($type_info->{perl} eq 'IV') {
Branch: 8 branches
BranchTrueFalseTotal
if ($type_info->{'perl'} eq 'IV') { } 12 8 20
elsif ($type_info->{'perl'} eq 'UV') { } 0 8 8
elsif ($type_info->{'perl'} eq 'NV') { } 8 0 8
elsif ($type_info->{'perl'} eq 'PV') { } 0 0 0
elsif ($type_info->{'perl'} eq 'SV') { } 0 0 0
elsif ($type_info->{'perl'} eq 'HV') { } 0 0 0
elsif ($type_info->{'perl'} eq 'AV') { } 0 0 0
elsif ($type_info->{'perl'} eq 'CV') { } 0 0 0
281 12 $b->line("$type_info->{c} $arg_name = ($type_info->{c})SvIV(ST($i));");
282 }
283 elsif ($type_info->{perl} eq 'UV') {
284 0 ▶ if ($type_info->{is_ptr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_info->{'is_ptr'}) { } 0 0 0
285 0 $b->line("$type_info->{c} $arg_name = INT2PTR($type_info->{c}, SvUV(ST($i)));");
286 }
287 else {
288 0 $b->line("$type_info->{c} $arg_name = ($type_info->{c})SvUV(ST($i));");
289 }
290 }
291 elsif ($type_info->{perl} eq 'NV') {
292 8 $b->line("$type_info->{c} $arg_name = ($type_info->{c})SvNV(ST($i));");
293 }
294 elsif ($type_info->{perl} eq 'PV') {
295 0 $b->line("$type_info->{c} $arg_name = SvPV_nolen(ST($i));");
296 }
297 elsif ($type_info->{perl} eq 'SV') {
298 # Pass SV* directly - no conversion needed
299 0 $b->line("SV* $arg_name = ST($i);");
300 }
301 elsif ($type_info->{perl} eq 'HV') {
302 # Dereference to get HV* from hashref
303 0 $b->line("if (!SvROK(ST($i)) || SvTYPE(SvRV(ST($i))) != SVt_PVHV)");
304 0 $b->line(" croak(\"Argument $i must be a hash reference\");");
305 0 $b->line("HV* $arg_name = (HV*)SvRV(ST($i));");
306 }
307 elsif ($type_info->{perl} eq 'AV') {
308 # Dereference to get AV* from arrayref
309 0 $b->line("if (!SvROK(ST($i)) || SvTYPE(SvRV(ST($i))) != SVt_PVAV)");
310 0 $b->line(" croak(\"Argument $i must be an array reference\");");
311 0 $b->line("AV* $arg_name = (AV*)SvRV(ST($i));");
312 }
313 elsif ($type_info->{perl} eq 'CV') {
314 # Dereference to get CV* from coderef
315 0 $b->line("if (!SvROK(ST($i)) || SvTYPE(SvRV(ST($i))) != SVt_PVCV)");
316 0 $b->line(" croak(\"Argument $i must be a code reference\");");
317 0 $b->line("CV* $arg_name = (CV*)SvRV(ST($i));");
318 }
319 else {
320 # Unknown type - try to cast
321 0 $b->line("$type_info->{c} $arg_name = ($type_info->{c})SvIV(ST($i));");
322 }
323  
324 20 push @c_args, $arg_name;
325 }
326  
327 # Call the C function
328 14 my $ret_info = XS::JIT::Header::TypeMap::resolve($return_type);
329 14 my $call = "$func->{c_name}(" . join(', ', @c_args) . ")";
330  
331 14 ▶ if ($return_type eq 'void') {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_type eq 'void') { } 2 12 14
332 2 $b->line("$call;");
333 2 $b->xs_return_undef;
334 }
335 else {
336 12 $b->line("$ret_info->{c} retval = $call;");
337  
338 12 ▶ if ($ret_info->{perl} eq 'IV') {
Branch: 8 branches
BranchTrueFalseTotal
if ($ret_info->{'perl'} eq 'IV') { } 6 6 12
elsif ($ret_info->{'perl'} eq 'UV') { } 0 6 6
elsif ($ret_info->{'perl'} eq 'NV') { } 4 2 6
elsif ($ret_info->{'perl'} eq 'PV') { } 2 0 2
elsif ($ret_info->{'perl'} eq 'SV') { } 0 0 0
elsif ($ret_info->{'perl'} eq 'HV') { } 0 0 0
elsif ($ret_info->{'perl'} eq 'AV') { } 0 0 0
elsif ($ret_info->{'perl'} eq 'CV') { } 0 0 0
339 6 $b->line("ST(0) = sv_2mortal(newSViv((IV)retval));");
340 }
341 elsif ($ret_info->{perl} eq 'UV') {
342 0 ▶ if ($ret_info->{is_ptr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_info->{'is_ptr'}) { } 0 0 0
343 0 $b->line("ST(0) = sv_2mortal(newSVuv(PTR2UV(retval)));");
344 }
345 else {
346 0 $b->line("ST(0) = sv_2mortal(newSVuv((UV)retval));");
347 }
348 }
349 elsif ($ret_info->{perl} eq 'NV') {
350 4 $b->line("ST(0) = sv_2mortal(newSVnv((NV)retval));");
351 }
352 elsif ($ret_info->{perl} eq 'PV') {
353 2 $b->line("ST(0) = sv_2mortal(newSVpv(retval, 0));");
354 }
355 elsif ($ret_info->{perl} eq 'SV') {
356 # Return SV* directly (mortalized)
357 0 $b->line("ST(0) = sv_2mortal(retval);");
358 }
359 elsif ($ret_info->{perl} eq 'HV') {
360 # Return HV* as hashref (mortalized)
361 0 $b->line("ST(0) = sv_2mortal(newRV_noinc((SV*)retval));");
362 }
363 elsif ($ret_info->{perl} eq 'AV') {
364 # Return AV* as arrayref (mortalized)
365 0 $b->line("ST(0) = sv_2mortal(newRV_noinc((SV*)retval));");
366 }
367 elsif ($ret_info->{perl} eq 'CV') {
368 # Return CV* as coderef (mortalized)
369 0 $b->line("ST(0) = sv_2mortal(newRV_noinc((SV*)retval));");
370 }
371 else {
372 0 $b->line("ST(0) = sv_2mortal(newSViv((IV)retval));");
373 }
374  
375 12 $b->xs_return(1);
376 }
377  
378 14 $b->xs_end;
379  
380 return {
381 14 code => $b->code,
382 xs_name => "xs_$safe_name",
383 };
384 }
385  
386 # Build compiler/linker flags
387 sub _build_cflags {
388 0 ▶ my ($self) = @_;
Subroutine _build_cflags: not called CC=3 SCAR=24.8
389  
390 0 my @flags;
391  
392 # Include paths
393 0 push @flags, map { "-I$_" } @{$self->{include}};
394  
395 # Header directory
396 0 ▶ if ($self->{_header_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_header_path"}) 0 0 0
397 my $dir = File::Spec->rel2abs(
398 0 (File::Spec->splitpath($self->{_header_path}))[1]
399 );
400 0 ▶ push @flags, "-I$dir" if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 0 0 0
401 }
402  
403 # Defines
404 0 push @flags, map { "-D$_=$self->{define}{$_}" } keys %{$self->{define}};
405  
406 0 return join(' ', @flags);
407 }
408  
409 sub _build_ldflags {
410 0 ▶ my ($self) = @_;
Subroutine _build_ldflags: not called CC=4 SCAR=30.0
411  
412 0 ▶ return '' unless defined $self->{lib};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"lib"} 0 0 0
413  
414 0 my $lib = $self->{lib};
415  
416 # If it's a path to a .so/.dylib/.dll, use it directly
417 0 ▶ if ($lib =~ m{[/\\]} || $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib =~ m([/\\]) or $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/) 0 0 0
Condition0%0/3
$lib =~ m([/\\]) or $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$lib =~ m([/\\]) or $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/
$lib =~ m([/\\]) $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/
Truth table0%0/3
A or B
A= $lib =~ m([/\\])B= $lib =~ /\.(?:so|dylib|dll|a)(?:\.\d+)*$/
ABresult
1X1
011
000
418 0 return $lib;
419 }
420  
421 # Otherwise, use -l flag
422 0 return "-l$lib";
423 }
424  
425 # Compile all attached functions
426 sub compile {
427 0 ▶ my ($self) = @_;
Subroutine compile: not called CC=7 SCAR=40.3
Pod documented
428  
429 0 ▶ return 1 if $self->{_compiled} && !$self->{force};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_compiled"} and not $self->{"force"} 0 0 0
Condition0%0/3
$self->{"_compiled"} and not $self->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_compiled"} and not $self->{"force"}
$self->{"_compiled"} not $self->{"force"}
Truth table0%0/3
A and not(B)
A= $self->{"_compiled"}B= not $self->{"force"}
ABresult
0X0
100
111
430  
431 0 ▶ die "No functions attached" unless @{$self->{_functions}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_functions"};} 0 0 0
432  
433 # Generate the include directive
434 0 my $code = "";
435  
436 # Add standard includes
437 0 $code .= "#include <stdlib.h>\n";
438 0 $code .= "#include <string.h>\n";
439  
440 # Add the user's header
441 0 ▶ if ($self->{_header_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_header_path"}) 0 0 0
442 0 $code .= qq{#include "$self->{_header_path}"\n};
443 }
444  
445 0 $code .= "\n";
446  
447 # Generate wrapper code for each function
448 0 my %functions;
449 0 for my $func (@{$self->{_functions}}) {
450 0 my $wrapper = $self->_generate_wrapper($func);
451 0 $code .= $wrapper->{code} . "\n";
452  
453 $functions{$func->{perl_name}} = {
454 source => $wrapper->{xs_name},
455 0 is_xs_native => 1,
456 };
457 }
458  
459 # Compile with XS::JIT
460 # Note: We pass extra flags through environment since XS::JIT
461 # doesn't currently support extra cflags/ldflags directly
462 0 local $ENV{XS_JIT_EXTRA_CFLAGS} = $self->_build_cflags;
463 0 local $ENV{XS_JIT_EXTRA_LDFLAGS} = $self->_build_ldflags;
464  
465 my $result = XS::JIT->compile(
466 code => $code,
467 name => $self->{_module_id},
468 cache_dir => $self->{cache_dir},
469 functions => \%functions,
470 force => $self->{force},
471 0 );
472  
473 0 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
474 0 die "XS::JIT compilation failed";
475 }
476  
477 0 $self->{_compiled} = 1;
478 0 return 1;
479 }
480  
481 # Write module files without JIT compilation
482 # This generates static files that can be distributed and compiled normally
483 sub write_module {
484 6 ▶ my ($self, %opts) = @_;
Subroutine write_module: called CC=4 SCAR=13.9
Pod documented
485  
486 6 ▶ die "No functions attached" unless @{$self->{_functions}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_functions"};} 1 5 6
487  
488 5 ▶ my $dir = $opts{dir} || '.';
Condition50%1/2
$opts{'dir'} || '.'
l!l
50
MC/DC0%0/1
$opts{'dir'} || '.'
$opts{'dir'}
Truth table50%1/2
A
A= $opts{'dir'}
Aresult
11
00
489 5 ▶ my $package = $opts{package} || $self->{package};
Condition66%2/3
$opts{'package'} || $self->{'package'}
l!l&&r!l&&!r
140
MC/DC0%0/2
$opts{'package'} || $self->{'package'}
$opts{'package'} $self->{'package'}
Truth table66%2/3
A || B
A= $opts{'package'}B= $self->{'package'}
ABresult
1X1
011
000
490  
491 # Create directory structure based on package name
492 5 my @parts = split /::/, $package;
493 5 my $module_name = pop @parts; # Last part is the module name
494  
495 # Build directory path
496 5 my $lib_dir = File::Spec->catdir($dir, 'lib', @parts);
497 5 _mkpath($lib_dir);
498  
499 # File paths
500 5 my $pm_file = File::Spec->catfile($lib_dir, "$module_name.pm");
501 5 my $xs_file = File::Spec->catfile($lib_dir, "$module_name.xs");
502 5 my $c_file = File::Spec->catfile($lib_dir, "${module_name}_funcs.c");
503  
504 # Generate the C code (function wrappers)
505 5 my $c_code = $self->_generate_module_c_code();
506  
507 # Generate the XS file (boot section and function registration)
508 5 my $xs_code = $self->_generate_module_xs_code($package, $module_name);
509  
510 # Generate the .pm file
511 5 my $pm_code = $self->_generate_module_pm_code($package);
512  
513 # Write files
514 5 _write_file($c_file, $c_code);
515 5 _write_file($xs_file, $xs_code);
516 5 _write_file($pm_file, $pm_code);
517  
518 # Mark as compiled to prevent auto-compile in DESTROY
519 5 $self->{_compiled} = 1;
520  
521 # Return info about what was created
522 return {
523 5 c_file => $c_file,
524 xs_file => $xs_file,
525 pm_file => $pm_file,
526 package => $package,
527 };
528 }
529  
530 # Generate just the C code portion (for embedding or custom builds)
531 sub write_c_file {
532 3 ▶ my ($self, $filename) = @_;
Subroutine write_c_file: called CC=3 SCAR=11.0
Pod documented
533  
534 3 ▶ die "write_c_file requires a filename" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 1 2 3
535 2 ▶ die "No functions attached" unless @{$self->{_functions}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_functions"};} 1 1 2
536  
537 1 my $code = $self->_generate_module_c_code();
538 1 _write_file($filename, $code);
539  
540 # Mark as compiled to prevent auto-compile in DESTROY
541 1 $self->{_compiled} = 1;
542  
543 1 return $self;
544 }
545  
546 # Generate the C function wrappers
547 sub _generate_module_c_code {
548 6 ▶ my ($self) = @_;
Subroutine _generate_module_c_code: called CC=3 SCAR=11.0
549  
550 6 my $code = "/* Generated by XS::JIT::Header */\n";
551 6 $code .= "/* This file can be compiled as part of a standard XS module */\n\n";
552  
553 # Add standard includes
554 6 $code .= "#include <stdlib.h>\n";
555 6 $code .= "#include <string.h>\n";
556  
557 # Add the user's header
558 6 ▶ if ($self->{_header_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_header_path"}) 6 0 6
559 6 $code .= qq{#include "$self->{_header_path}"\n};
560 }
561  
562 6 $code .= "\n";
563  
564 # Generate wrapper code for each function
565 6 for my $func (@{$self->{_functions}}) {
566 14 my $wrapper = $self->_generate_wrapper($func);
567 14 $code .= $wrapper->{code} . "\n";
568 }
569  
570 6 return $code;
571 }
572  
573 # Generate the XS boot code
574 sub _generate_module_xs_code {
575 5 ▶ my ($self, $package, $module_name) = @_;
Subroutine _generate_module_xs_code: called CC=2 SCAR=6.9
576  
577 5 my $xs = <<"END_XS";
578 /* Generated by XS::JIT::Header */
579  
580 #define PERL_NO_GET_CONTEXT
581 #include "EXTERN.h"
582 #include "perl.h"
583 #include "XSUB.h"
584  
585 /* Include the generated function wrappers */
586 #include "${module_name}_funcs.c"
587  
588 MODULE = $package PACKAGE = $package
589  
590 PROTOTYPES: DISABLE
591  
592 BOOT:
593 {
594 END_XS
595  
596 # Add newXS for each function
597 5 for my $func (@{$self->{_functions}}) {
598 12 my $safe_name = $func->{c_name};
599 12 $safe_name =~ s/\W/_/g;
600 12 my $perl_name = $func->{perl_name};
601  
602 12 $xs .= qq{ newXS("$perl_name", xs_$safe_name, __FILE__);\n};
603 }
604  
605 5 $xs .= "}\n";
606  
607 5 return $xs;
608 }
609  
610 # Generate the .pm file
611 sub _generate_module_pm_code {
612 5 ▶ my ($self, $package) = @_;
Subroutine _generate_module_pm_code: called CC=3 SCAR=11.0
613  
614 5 my $pm = <<"END_PM";
615 package $package;
616  
617 use strict;
618 use warnings;
619  
620 our \$VERSION = '0.01';
621  
622 require XSLoader;
623 XSLoader::load('$package', \$VERSION);
624  
625 1;
626  
627 __END__
↑ Summary blib/lib/XS/JIT.pm »