File Coverage

blib/lib/InlineX/C2XS/Context.pm
Criterion Covered Total %
statement 6 57 10.5
branch 0 44 0.0
condition 0 15 0.0
subroutine 2 4 50.0
pod 0 2 0.0
total 8 122 6.5


line stmt bran cond sub pod time code
1             package InlineX::C2XS::Context;
2              
3 15     15   49 use strict;
  15         16  
  15         336  
4 15     15   45 use warnings;
  15         14  
  15         12648  
5              
6             our $VERSION = '0.24';
7              
8             ###################################
9             ###################################
10              
11             sub apply_context_args {
12              
13             # $_[0]: The XS file to which we want to apply the context args (aTHX, aTHX_, pTHX, pTHX_).
14             # $_[1]: A reference to a list/array of the C functions to which we wish to apply the
15             # context args.
16              
17 0 0   0 0   die "Usage: InlineX::C2XS::Context::apply_context_args(\$xs_file, \\\@functions)"
18             unless @_ == 2;
19              
20 0 0         open RD, '<', $_[0] or die $!;
21 0           my @xs = ;
22 0 0         open WR, '>', $_[0] or die $!;
23              
24 0           my($aTHX_warn, $pTHX_warn) = (0, 0);
25              
26 0           for(@xs) {
27 0 0         $pTHX_warn = 1 if $_ =~ /pTHX/;
28 0 0         $aTHX_warn = 1 if $_ =~ /aTHX/;
29             }
30              
31 0 0         warn "Potential problem: the string 'aTHX' was found in $_[0]"
32             if $aTHX_warn;
33              
34 0 0         warn "Potential problem: the string 'pTHX' was found in $_[0]"
35             if $pTHX_warn;
36              
37 0           for my $f(@{$_[1]}) {
  0            
38 0           my $seen_pthx = 0;
39 0           for(my $i = 1; $i < @xs; $i++) {
40              
41 0 0 0       if($xs[$i] =~ /.+\b$f\b(\s+)?\(/ && !$seen_pthx) {
42 0 0         $xs[$i] !~ /\((\s+)?(void)?(\s+)?\)/
    0          
43             ? $xs[$i] =~ /(a|p)THX/ ? $xs[$i] = $xs[$i]
44             : $xs[$i] =~ s/\(/\(pTHX_ /
45             : $xs[$i] =~ s/\((\s+)?(void)?(\s+)?\)/\(pTHX\)/;
46 0 0         $seen_pthx = 1 if $xs[$i] =~ /pTHX/;
47             }
48              
49              
50 0 0 0       if(
      0        
51             (
52             $xs[$i] =~ /^(S|H|A)V \*\n/ ||
53             $xs[$i] =~ /^(signed |unsigned )?(long)?\s?int(\s\*)?\n/ ||
54             $xs[$i] =~ /^(long)?\s?double(\s\*)?\n/ ||
55             $xs[$i] =~ /^(signed |unsigned )?long(\s\*)?\n/
56             )
57             && $xs[$i + 1] =~ /^$f\b/
58             ) {
59 0           my $function = $xs[$i + 1];
60 0           chomp $function;
61 0           my $jump = scalar(split /,/, $xs[$i + 1]);
62 0 0         if($xs[$i + 1] !~ /\(\)/) {$function =~ s/\(/\(aTHX_ /}
  0            
63             else {
64 0           $function =~ s/\(\)/\(aTHX\)/;
65 0           $jump--;
66             }
67 0           $function .= ';';
68 0 0         unless($xs[$i + 2 + $jump] =~ /\S/) {
69 0           $xs[$i + 2 + $jump] = "CODE:\n RETVAL = $function\nOUTPUT: RETVAL\n\n";
70             }
71 0           else { warn "$i: $xs[$i + 2 + $jump]\n"}
72             }
73             }
74             }
75              
76             # The following can break if $f appears in comments.
77 0           for my $f (@{$_[1]}) {
  0            
78 0           my $seen_pthx = 0;
79 0           for(my $i = 1; $i < @xs; $i++) {
80 0 0         if($seen_pthx) {
81 0 0         $xs[$i] =~ s/\b$f(\s+)?\((\s+)?\)/$f(aTHX)/
82             unless $xs[$i] =~ /^$f\b/; # XS section - we don't want aTHX/aTHX_ here.
83 0 0 0       $xs[$i] =~ s/\b$f(\s+)?\(/$f(aTHX_ /
84             unless ($xs[$i] =~ /aTHX|pTHX/
85             ||
86             $xs[$i] =~ /^$f\b/); # XS section - we don't want aTHX/aTHX_ here.
87             }
88             else {
89 0 0 0       if($xs[$i] =~ /\b$f\b/ && $xs[$i] =~ /\bpTHX\b|\bpTHX_\b/) {
90 0           $seen_pthx = 1;
91 0           next;
92             }
93 0 0         $seen_pthx = 1 if $xs[$i] =~ /\b$f(\s+)?\(/;
94 0           $xs[$i] =~ s/\b$f(\s+)?\((\s+)?(void)(\s+)?\)/$f(pTHX)/;
95 0 0         $xs[$i] =~ s/\b$f(\s+)?\(/$f(pTHX_ /
96             unless $xs[$i] =~ /(p|a)THX/;
97             }
98             }
99             }
100              
101 0           for(@xs) {print WR $_}
  0            
102              
103 0 0         close WR or die $!;
104              
105 0           print "$_[0] has been rewritten for PERL_NO_GET_CONTEXT\n";
106              
107             }
108              
109             ###################################
110             ###################################
111              
112             sub exclude {
113 0     0 0   my @exclusions = @{$_[1]};
  0            
114              
115 0           for(@exclusions) {
116 0 0         return 1 if $_[0] =~ /$_/;
117             }
118 0           return 0;
119             }
120              
121             1;