blib/lib/Zuzu/Module/CLib.pm

filter: Statementstmt 16.6% 36 / 216 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 33.3% 9 / 27 Podpod 0.0% 0 / 1 totaltotal 9.3% 45 / 479 CC 93 SCAR 49.7
CC
93
Cov
9%
_prepare_arg
380.0
_normalize_descriptor
342.0
_type_name
110.0
CRAP 143.4
« blib/lib/Zuzu/Runtime.pm ↑ Summary blib/lib/Zuzu/Module/CSV.pm »
1 package Zuzu::Module::CLib;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use FFI::Platypus 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use FFI::Platypus::Buffer qw( scalar_to_buffer buffer_to_scalar );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 native_class
14 native_function
15 native_object
16 zuzu_to_perl
17 2 );
18 2 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 0 ▶sub _runtime_error {
Subroutine _runtime_error: not called CC=1 SCAR=6.9
22 0 my ( $message ) = @_;
23  
24 0 die Zuzu::Error->new_runtime(
25 message => $message,
26 file => '<std/clib>',
27 line => 0,
28 );
29 }
30  
31 0 ▶sub _bool_value {
Subroutine _bool_value: not called CC=2 SCAR=17.9
32 0 my ( $value ) = @_;
33  
34 0 ▶ return Zuzu::Value::Boolean->new( value => $value ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
35 }
36  
37 0 ▶sub _type_name {
Subroutine _type_name: not called CC=10 SCAR=47.0
38 0 my ( $value ) = @_;
39  
40 0 ▶ return 'Null' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
41 0 ▶ return 'BinaryString'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
42 if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
43 0 ▶ return 'Boolean'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
44 if blessed($value) and $value->isa('Zuzu::Value::Boolean');
45 0 ▶ return 'Object' if blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) 0 0 0
46 0 ▶ if ( !ref($value) and "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/) 0 0 0
Condition0%0/3
not ref $value and "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $value and "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
not ref $value "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
Truth table0%0/3
not(A) and B
A= not ref $valueB= "$value" =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
ABresult
0X0
100
111
47 0 return 'Number';
48 }
49 0 ▶ return 'String' if !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
50 0 return 'Object';
51 }
52  
53 0 ▶sub _normalize_descriptor {
Subroutine _normalize_descriptor: not called CC=18 SCAR=58.3
54 0 my ( $raw, $context ) = @_;
55  
56 0 $raw = zuzu_to_perl($raw);
57 0 my $desc;
58 0 ▶ if ( !ref($raw) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $raw) { } 0 0 0
elsif (ref $raw eq 'HASH') { } 0 0 0
59 0 ▶ $desc = { type => ( defined $raw ? "$raw" : 'null' ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
60 }
61 elsif ( ref($raw) eq 'HASH' ) {
62 0 $desc = { %{ $raw } };
63 }
64 else {
65 0 _runtime_error("$context descriptor must be String or Dict");
66 }
67  
68 0 ▶ my $type = defined $desc->{type} ? "$desc->{type}" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $desc->{'type'} ? : 0 0 0
69 0 $type = lc $type;
70 0 $desc->{type} = $type;
71  
72 0 ▶ if ( $type eq 'null' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'null') { } 0 0 0
elsif ($type eq 'bool') { } 0 0 0
elsif ($type eq 'int') { } 0 0 0
elsif ($type eq 'float') { } 0 0 0
elsif ($type eq 'binary') { } 0 0 0
73 0 $desc->{ffi_type} = 'void';
74 }
75 elsif ( $type eq 'bool' ) {
76 0 $desc->{ffi_type} = 'bool';
77 }
78 elsif ( $type eq 'int' ) {
79 0 ▶ my $bits = exists $desc->{bits} ? int( $desc->{bits} ) : 64;
Branch: 1 branch
BranchTrueFalseTotal
exists $desc->{'bits'} ? : 0 0 0
80 0 ▶ _runtime_error("$context int descriptor only supports bits=64")
Branch: 1 branch
BranchTrueFalseTotal
if $bits != 64 0 0 0
81 if $bits != 64;
82 0 ▶ my $signed = exists $desc->{signed} ? $desc->{signed} ? 1 : 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $desc->{'signed'} ? : 0 0 0
$desc->{'signed'} ? : 0 0 0
83 0 $desc->{bits} = 64;
84 0 $desc->{signed} = $signed;
85 0 ▶ $desc->{ffi_type} = $signed ? 'sint64' : 'uint64';
Branch: 1 branch
BranchTrueFalseTotal
$signed ? : 0 0 0
86 }
87 elsif ( $type eq 'float' ) {
88 0 ▶ my $bits = exists $desc->{bits} ? int( $desc->{bits} ) : 64;
Branch: 1 branch
BranchTrueFalseTotal
exists $desc->{'bits'} ? : 0 0 0
89 0 ▶ _runtime_error("$context float descriptor only supports bits=64")
Branch: 1 branch
BranchTrueFalseTotal
if $bits != 64 0 0 0
90 if $bits != 64;
91 0 $desc->{bits} = 64;
92 0 $desc->{ffi_type} = 'double';
93 }
94 elsif ( $type eq 'binary' ) {
95 0 $desc->{ffi_type} = 'opaque';
96 0 ▶ $desc->{nullable} = $desc->{nullable} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$desc->{'nullable'} ? : 0 0 0
97 }
98 else {
99 0 _runtime_error("$context descriptor has unsupported type '$type'");
100 }
101  
102 0 return $desc;
103 }
104  
105 0 ▶sub _normalize_params {
Subroutine _normalize_params: not called CC=4 SCAR=30.0
106 0 my ( $params ) = @_;
107  
108 0 my $raw = zuzu_to_perl($params);
109 0 ▶ $raw = [] if !defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 0 0
110 0 ▶ _runtime_error('params descriptor must be Array') if ref($raw) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw ne "ARRAY" 0 0 0
111  
112 0 my @out;
113 0 ▶ for ( my $i = 0; $i < @{ $raw }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$raw;}) 0 0 0
114 0 push @out, _normalize_descriptor( $raw->[$i], "parameter $i" );
115 }
116  
117 0 return \@out;
118 }
119  
120 0 ▶sub _assert_open_library {
Subroutine _assert_open_library: not called CC=2 SCAR=17.9
121 0 my ( $state ) = @_;
122  
123 0 ▶ _runtime_error('CLibrary is closed') if $state->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closed"} 0 0 0
124 }
125  
126 0 ▶sub _assert_callable_function {
Subroutine _assert_callable_function: not called CC=3 SCAR=24.8
127 0 my ( $state ) = @_;
128  
129 _runtime_error('CFunction belongs to a closed CLibrary')
130 0 ▶ if $state->{lib_state}{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"lib_state"}{"closed"} 0 0 0
131 0 ▶ _runtime_error('CFunction is closed') if $state->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closed"} 0 0 0
132 }
133  
134 0 ▶sub _prepare_arg {
Subroutine _prepare_arg: not called CC=19 SCAR=59.4
135 0 my ( $desc, $value, $index, $temps ) = @_;
136 0 my $type = $desc->{type};
137  
138 0 ▶ if ( $type eq 'null' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "null") 0 0 0
139 0 ▶ _runtime_error("argument $index must be Null, got " . _type_name($value))
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
140 if defined $value;
141 0 return undef;
142 }
143  
144 0 ▶ if ( $type eq 'bool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bool") 0 0 0
145 0 ▶ _runtime_error("argument $index must be Boolean, got " . _type_name($value))
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
146 if !blessed($value) or !$value->isa('Zuzu::Value::Boolean');
147 0 ▶ return $value->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value->value ? : 0 0 0
148 }
149  
150 0 ▶ if ( $type eq 'int' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int") 0 0 0
151 0 ▶ _runtime_error("argument $index must be Number, got " . _type_name($value))
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
152 if ref($value);
153 0 return 0 + int($value);
154 }
155  
156 0 ▶ if ( $type eq 'float' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "float") 0 0 0
157 0 ▶ _runtime_error("argument $index must be Number, got " . _type_name($value))
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
158 if ref($value);
159 0 return 0 + $value;
160 }
161  
162 0 ▶ if ( $type eq 'binary' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "binary") 0 0 0
163 0 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
164 _runtime_error("argument $index must be BinaryString, got Null")
165 0 ▶ if !$desc->{nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $desc->{"nullable"} 0 0 0
166 0 return undef;
167 }
168 _runtime_error(
169 0 ▶ "argument $index must be BinaryString, got " . _type_name($value)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
170 )
171 if !blessed($value) or !$value->isa('Zuzu::Value::BinaryString');
172  
173 0 ▶ my $bytes = $value->bytes // '';
Condition0%0/2
$value->bytes // ""
l!l
00
MC/DC0%0/1
$value->bytes // ""
$value->bytes
Truth table0%0/2
A
A= $value->bytes
Aresult
11
00
174 $bytes .= "\0" if defined $desc->{terminated_by}
175 0 ▶ and $desc->{terminated_by} eq 'nul';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc->{"terminated_by"} and $desc->{"terminated_by"} eq "nul" 0 0 0
Condition0%0/3
defined $desc->{"terminated_by"} and $desc->{"terminated_by"} eq "nul"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desc->{"terminated_by"} and $desc->{"terminated_by"} eq "nul"
defined $desc->{"terminated_by"} $desc->{"terminated_by"} eq "nul"
Truth table0%0/3
A and B
A= defined $desc->{"terminated_by"}B= $desc->{"terminated_by"} eq "nul"
ABresult
0X0
100
111
176 0 my ( $ptr ) = scalar_to_buffer($bytes);
177 0 push @{ $temps }, $bytes;
178 0 return $ptr;
179 }
180  
181 0 _runtime_error("argument $index has unsupported type '$type'");
182 }
183  
184 0 ▶sub _return_length {
Subroutine _return_length: not called CC=7 SCAR=40.3
185 0 my ( $desc, $args ) = @_;
186  
187 0 ▶ if ( exists $desc->{length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $desc->{"length"}) 0 0 0
188 0 my $length = int( $desc->{length} );
189 0 ▶ _runtime_error('binary return length must be non-negative')
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 0 0 0
190 if $length < 0;
191 0 return $length;
192 }
193  
194 0 ▶ if ( exists $desc->{length_arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $desc->{"length_arg"}) 0 0 0
195 0 my $index = int( $desc->{length_arg} );
196 _runtime_error("binary return length_arg index $index is out of range")
197 0 ▶ if $index < 0 or $index >= @{ $args };
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= @{$args;} 0 0 0
Condition0%0/3
$index < 0 or $index >= @{$args;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$index < 0 or $index >= @{$args;}
$index < 0 $index >= @{$args;}
Truth table0%0/3
A or B
A= $index < 0B= $index >= @{$args;}
ABresult
1X1
011
000
198 0 my $length = int( 0 + $args->[$index] );
199 0 ▶ _runtime_error('binary return length_arg value must be non-negative')
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 0 0 0
200 if $length < 0;
201 0 return $length;
202 }
203  
204 0 return undef;
205 }
206  
207 0 ▶sub _finish_binary_return {
Subroutine _finish_binary_return: not called CC=8 SCAR=42.8
208 0 my ( $ffi, $desc, $ptr, $args ) = @_;
209  
210 0 ▶ return undef if !defined $ptr or !$ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr and $ptr 0 0 0
Condition0%0/3
defined $ptr and $ptr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ptr and $ptr
defined $ptr $ptr
Truth table0%0/3
A and B
A= defined $ptrB= $ptr
ABresult
0X0
100
111
211  
212 0 my $bytes;
213 0 my $length = _return_length( $desc, $args );
214 0 ▶ if ( defined $length ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $length) { } 0 0 0
elsif (defined $desc->{'terminated_by'} and $desc->{'terminated_by'} eq 'nul') { } 0 0 0
215 0 $bytes = buffer_to_scalar( $ptr, $length );
216 }
217 ▶ elsif ( defined $desc->{terminated_by} and $desc->{terminated_by} eq 'nul' ) {
Condition0%0/3
defined $desc->{"terminated_by"} and $desc->{"terminated_by"} eq "nul"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desc->{"terminated_by"} and $desc->{"terminated_by"} eq "nul"
defined $desc->{"terminated_by"} $desc->{"terminated_by"} eq "nul"
Truth table0%0/3
A and B
A= defined $desc->{"terminated_by"}B= $desc->{"terminated_by"} eq "nul"
ABresult
0X0
100
111
218 0 $bytes = $ffi->cast( 'opaque' => 'string', $ptr );
219 }
220 else {
221 0 _runtime_error('binary return requires length, length_arg, or terminated_by');
222 }
223  
224 0 ▶ if ( defined $desc->{free_function} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc->{"free_function"}) 0 0 0
225 0 $desc->{free_function}->call($ptr);
226 }
227  
228 0 ▶ return Zuzu::Value::BinaryString->new( bytes => $bytes // '' );
Condition0%0/2
$bytes // ""
l!l
00
MC/DC0%0/1
$bytes // ""
$bytes
Truth table0%0/2
A
A= $bytes
Aresult
11
00
229 }
230  
231 0 ▶sub _finish_return {
Subroutine _finish_return: not called CC=6 SCAR=37.4
232 0 my ( $ffi, $desc, $raw, $args ) = @_;
233 0 my $type = $desc->{type};
234  
235 0 ▶ return undef if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 0 0 0
236 0 ▶ return _bool_value($raw) if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 0 0
237 0 ▶ return 0 + $raw if $type eq 'int' or $type eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" or $type eq "float" 0 0 0
Condition0%0/3
$type eq "int" or $type eq "float"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "int" or $type eq "float"
$type eq "int" $type eq "float"
Truth table0%0/3
A or B
A= $type eq "int"B= $type eq "float"
ABresult
1X1
011
000
238 0 ▶ return _finish_binary_return( $ffi, $desc, $raw, $args )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "binary" 0 0 0
239 if $type eq 'binary';
240  
241 0 _runtime_error("return descriptor has unsupported type '$type'");
242 }
243  
244 0 ▶sub _library_state_from_object {
Subroutine _library_state_from_object: not called CC=4 SCAR=30.0
245 0 my ( $obj ) = @_;
246  
247 return $obj->slots->{_state}
248 if blessed($obj)
249 and $obj->isa('Zuzu::Value::Object')
250 0 ▶ and exists $obj->slots->{_state};
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"} 0 0 0
Condition0%0/6
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"}
!ll&&!rl&&r
000
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"}
builtin::blessed($obj) $obj->isa("Zuzu::Value::Object") exists $obj->slots->{"_state"}
Truth table0%0/4
A and B and C
A= builtin::blessed($obj)B= $obj->isa("Zuzu::Value::Object")C= exists $obj->slots->{"_state"}
ABCresult
0XX0
10X0
1100
1111
251  
252 0 _runtime_error('Expected CLibrary object');
253 }
254  
255 0 ▶sub _function_state_from_object {
Subroutine _function_state_from_object: not called CC=4 SCAR=30.0
256 0 my ( $obj ) = @_;
257  
258 return $obj->slots->{_state}
259 if blessed($obj)
260 and $obj->isa('Zuzu::Value::Object')
261 0 ▶ and exists $obj->slots->{_state};
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"} 0 0 0
Condition0%0/6
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"}
!ll&&!rl&&r
000
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($obj) and $obj->isa("Zuzu::Value::Object") and exists $obj->slots->{"_state"}
builtin::blessed($obj) $obj->isa("Zuzu::Value::Object") exists $obj->slots->{"_state"}
Truth table0%0/4
A and B and C
A= builtin::blessed($obj)B= $obj->isa("Zuzu::Value::Object")C= exists $obj->slots->{"_state"}
ABCresult
0XX0
10X0
1100
1111
262  
263 0 _runtime_error('Expected CFunction object');
264 }
265  
266 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=5.6
Pod undocumented
267 2 my ( $class, $runtime ) = @_;
268  
269 2 my $clib_class = native_class(
270 name => 'CLib',
271 );
272 2 my $library_class = native_class(
273 name => 'CLibrary',
274 );
275 2 my $function_class = native_class(
276 name => 'CFunction',
277 );
278  
279 $clib_class->static_methods->{open} = native_function(
280 name => 'open',
281 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
282 0 my ( $self, $path ) = @_;
283 0 $runtime->assert_capability(
284 'clib',
285 'CLib.open is denied by runtime policy',
286 '<std/clib>',
287 0,
288 );
289  
290 0 ▶ my $library_path = defined $path ? "$path" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 0 0 0
291 0 my $ffi = FFI::Platypus->new( api => 2 );
292 eval {
293 0 $ffi->lib($library_path);
294 0 1;
295 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ffi->lib($library_path); 1 } } or do { my $error = $@ || "unknown error"; _runtime_error("Could not load C library '${library_path}': $error") } 0 0 0
296 0 ▶ my $error = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
297 0 _runtime_error("Could not load C library '$library_path': $error");
298 };
299  
300 0 return native_object(
301 class => $library_class,
302 slots => {
303 _state => {
304 ffi => $ffi,
305 path => $library_path,
306 closed => 0,
307 },
308 },
309 const => {
310 _state => 1,
311 },
312 );
313 },
314 2 );
315  
316 $library_class->methods->{has_symbol} = native_function(
317 name => 'has_symbol',
318 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
319 0 my ( $self, $name ) = @_;
320 0 my $state = _library_state_from_object($self);
321 0 _assert_open_library($state);
322 0 ▶ my $symbol = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
323 0 my $ptr = eval { $state->{ffi}->find_symbol($symbol) };
324 0 ▶ return _bool_value( defined $ptr && $ptr ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $ptr && $ptr ? : 0 0 0
Condition0%0/3
defined $ptr and $ptr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ptr and $ptr
defined $ptr $ptr
Truth table0%0/3
A and B
A= defined $ptrB= $ptr
ABresult
0X0
100
111
325 },
326 2 );
327  
328 $library_class->methods->{close} = native_function(
329 name => 'close',
330 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 0 my ( $self ) = @_;
332 0 my $state = _library_state_from_object($self);
333 0 $state->{closed} = 1;
334 0 return undef;
335 },
336 2 );
337  
338 $library_class->methods->{func} = native_function(
339 name => 'func',
340 0 ▶ native => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
341 0 my ( $self, $name, $params, $return_type, $options ) = @_;
342 0 my $state = _library_state_from_object($self);
343 0 _assert_open_library($state);
344 0 ▶ my $symbol = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
345 0 ▶ _runtime_error('C function name must not be empty') if $symbol eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $symbol eq "" 0 0 0
346  
347 0 my $param_descs = _normalize_params($params);
348 0 my $return_desc = _normalize_descriptor( $return_type, 'return' );
349 0 my @ffi_params = map { $_->{ffi_type} } @{ $param_descs };
350 0 my $ffi_return = $return_desc->{ffi_type};
351 0 my $callable = eval {
352 0 $state->{ffi}->function( $symbol => \@ffi_params => $ffi_return );
353 };
354 0 ▶ if ( !$callable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callable) 0 0 0
355 0 ▶ my $error = $@ || 'symbol not found';
Condition0%0/2
$@ || 'symbol not found'
l!l
00
MC/DC0%0/1
$@ || 'symbol not found'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
356 0 _runtime_error(
357 "Could not bind C function '$symbol' in '$state->{path}': $error"
358 );
359 }
360  
361 0 ▶ if ( $return_desc->{type} eq 'binary' and defined $return_desc->{free} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_desc->{"type"} eq "binary" and defined $return_desc->{"free"}) 0 0 0
Condition0%0/3
$return_desc->{"type"} eq "binary" and defined $return_desc->{"free"}
!ll&&!rl&&r
000
MC/DC0%0/2
$return_desc->{"type"} eq "binary" and defined $return_desc->{"free"}
$return_desc->{"type"} eq "binary" defined $return_desc->{"free"}
Truth table0%0/3
A and B
A= $return_desc->{"type"} eq "binary"B= defined $return_desc->{"free"}
ABresult
0X0
100
111
362 0 my $free_symbol = "$return_desc->{free}";
363 0 my $free_function = eval {
364 0 $state->{ffi}->function( $free_symbol => ['opaque'] => 'void' );
365 };
366 0 ▶ if ( !$free_function ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($free_function) 0 0 0
367 0 ▶ my $error = $@ || 'symbol not found';
Condition0%0/2
$@ || 'symbol not found'
l!l
00
MC/DC0%0/1
$@ || 'symbol not found'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
368 0 _runtime_error(
369 "Could not bind free function '$free_symbol' in "
370 . "'$state->{path}': $error"
371 );
372 }
373 0 $return_desc->{free_function} = $free_function;
374 }
375  
376 0 return native_object(
377 class => $function_class,
378 slots => {
379 _state => {
380 lib_state => $state,
381 name => $symbol,
382 param_descs => $param_descs,
383 return_desc => $return_desc,
384 callable => $callable,
385 closed => 0,
386 },
387 },
388 const => {
389 _state => 1,
390 },
391 );
392 },
393 2 );
394  
395 $function_class->methods->{call} = native_function(
396 name => 'call',
397 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
398 0 my ( $self, @args ) = @_;
399 0 my $state = _function_state_from_object($self);
400 0 _assert_callable_function($state);
401  
402 0 my $param_descs = $state->{param_descs};
403 _runtime_error(
404 "Function '$state->{name}' expects "
405 0 . scalar( @{ $param_descs } )
406 . ' arguments, got '
407 . scalar(@args)
408 0 ▶ ) if scalar(@args) != scalar( @{ $param_descs } );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != scalar @{$param_descs;} 0 0 0
409  
410 0 my @temps;
411 my @ffi_args;
412 0 ▶ for ( my $i = 0; $i < @{ $param_descs }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$param_descs;}) 0 0 0
413 0 push @ffi_args, _prepare_arg(
414 $param_descs->[$i],
415 $args[$i],
416 $i,
417 \@temps,
418 );
419 }
420  
421 0 my $raw = eval { $state->{callable}->call(@ffi_args) };
422 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
423 0 _runtime_error("C function '$state->{name}' failed: $@");
424 }
425  
426 return _finish_return(
427 $state->{lib_state}{ffi},
428 $state->{return_desc},
429 0 $raw,
430 \@args,
431 );
432 },
433 2 );
434  
435 return {
436 2 CLib => $clib_class,
437 CLibrary => $library_class,
438 CFunction => $function_class,
439 };
440 }
441  
442 2 1;
443  
444 =pod
445  
446 =head1 COPYRIGHT AND LICENCE
447  
448 B<< Zuzu::Module::CLib >> is copyright Toby Inkster.
449  
450 It is free software; you may redistribute it and/or modify it under
451 the terms of either the Artistic License 1.0 or the GNU General Public
452 License version 2.
453  
454 =cut
« blib/lib/Zuzu/Runtime.pm ↑ Summary blib/lib/Zuzu/Module/CSV.pm »