STRling/NativeClient.pm

filter: Statementstmt 34.5% 58 / 168 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 50.0% 20 / 40 Podpod 0.0% 0 / 9 totaltotal 23.5% 78 / 331 CC 43 SCAR 39.8
CC
43
Cov
22%
_decode_response
182.0
_execute_bytes
110.0
load
72.0
CRAP 53.3
↑ Summary STRling/StrictJSON.pm »
1 package STRling::NativeAdapterError;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use overload '""' => sub { $_[0]->{message} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
7  
8 # STRling-public-arity: new=3..4
9 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
10 0 my ($class, $kind, $message, $status) = @_;
11 0 ▶ $message .= " (status $status)" if defined $status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $status 0 0 0
12 0 return bless { kind => $kind, message => $message, status => $status }, $class;
13 }
14  
15 package STRling::InteropProtocolError;
16  
17 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 0 ▶use overload '""' => sub { $_[0]->{message} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
21  
22 # STRling-public-arity: new=2
23 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
24 0 my ($class, $response) = @_;
25 0 my $error = $response->{error};
26 return bless {
27 code => $error->{code},
28 path => $error->{path},
29 operation => $response->{operation},
30 0 message => "$error->{code} at $error->{path}",
31 }, $class;
32 }
33  
34 package STRling::NativeClient;
35  
36 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use STRling::StrictJSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 ▶use constant INTEROP_PROTOCOL_VERSION => '1.0.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant NATIVE_ABI_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant MAX_INTEROP_REQUEST_BYTES => 10_485_760;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant MAX_INTEROP_RESPONSE_BYTES => 33_554_432;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # STRling-public-arity: load=2
51 0 ▶sub load {
Subroutine load: not called CC=8 SCAR=42.8
Pod undocumented
52 0 my ($class, $library_path) = @_;
53 0 ▶ die STRling::NativeAdapterError->new('native_load', 'native STRling library path must be absolute')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $library_path and "File::Spec"->file_name_is_absolute($library_path) 0 0 0
Condition0%0/3
defined $library_path and "File::Spec"->file_name_is_absolute($library_path)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $library_path and "File::Spec"->file_name_is_absolute($library_path)
defined $library_path "File::Spec"->file_name_is_absolute($library_path)
Truth table0%0/3
A and B
A= defined $library_pathB= "File::Spec"->file_name_is_absolute($library_path)
ABresult
0X0
100
111
54 unless defined($library_path) && File::Spec->file_name_is_absolute($library_path);
55 0 my $normalized = abs_path($library_path);
56 0 ▶ die STRling::NativeAdapterError->new('native_load', "native STRling library not found: $library_path")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $normalized and -f $normalized 0 0 0
Condition0%0/3
defined $normalized and -f $normalized
!ll&&!rl&&r
000
MC/DC0%0/2
defined $normalized and -f $normalized
defined $normalized -f $normalized
Truth table0%0/3
A and B
A= defined $normalizedB= -f $normalized
ABresult
0X0
100
111
57 unless defined($normalized) && -f $normalized;
58  
59 0 ▶ eval { require FFI::Platypus; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require FFI::Platypus; 1 } } or die "STRling::NativeAdapterError"->new("native_load", "FFI::Platypus is required") 0 0 0
60 or die STRling::NativeAdapterError->new('native_load', 'FFI::Platypus is required');
61 0 my $ffi = FFI::Platypus->new(api => 2);
62 0 $ffi->lib($normalized);
63 0 my ($abi, $execute, $free);
64 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $abi = $ffi->function("strling_interop_abi_version_v1", [], "uint32"); $execute = $ffi->function("strling_interop_execute_v1", ["opaque", "size_t", "opaque"], "uint32"); $free = $ffi->function("strling_interop_owned_bytes_free_v1", ["opaque"], "uint32"); 1 } } or die "STRling::NativeAdapterError"->new("native_load", "cannot load the complete strling.c-abi v1") 0 0 0
65 0 $abi = $ffi->function('strling_interop_abi_version_v1' => [] => 'uint32');
66 0 $execute = $ffi->function('strling_interop_execute_v1' => ['opaque', 'size_t', 'opaque'] => 'uint32');
67 0 $free = $ffi->function('strling_interop_owned_bytes_free_v1' => ['opaque'] => 'uint32');
68 0 1;
69 } or die STRling::NativeAdapterError->new('native_load', 'cannot load the complete strling.c-abi v1');
70 0 my $actual = $abi->call();
71 0 ▶ die STRling::NativeAdapterError->new('native_abi', 'expected strling.c-abi 1', $actual)
Branch: 1 branch
BranchTrueFalseTotal
unless $actual == 1 0 0 0
72 unless $actual == NATIVE_ABI_VERSION;
73 0 return bless {
74 library_path => $normalized,
75 ffi => $ffi,
76 execute_fn => $execute,
77 free_fn => $free,
78 closed => 0,
79 active_calls => 0,
80 }, $class;
81 }
82  
83 # STRling-public-arity: library_path=1
84 0 ▶sub library_path { return $_[0]->{library_path}; }
Subroutine library_path: not called CC=1 SCAR=6.9
Pod undocumented
85 # STRling-public-arity: is_closed=1
86 0 ▶sub is_closed { return $_[0]->{closed} ? 1 : 0; }
Subroutine is_closed: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'closed'} ? : 0 0 0
87  
88 # STRling-public-arity: execute=2
89 0 ▶sub execute {
Subroutine execute: not called CC=5 SCAR=34.0
Pod undocumented
90 0 my ($self, $request) = @_;
91 0 my $encoded = eval { STRling::StrictJSON::encode($request) };
92 0 ▶ die STRling::NativeAdapterError->new('transport', "interop request is not strict JSON: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
93 0 ▶ die STRling::NativeAdapterError->new('transport', 'interop request exceeds ' . MAX_INTEROP_REQUEST_BYTES . ' bytes')
Branch: 1 branch
BranchTrueFalseTotal
if length $encoded > 10485760 0 0 0
94 if length($encoded) > MAX_INTEROP_REQUEST_BYTES;
95 0 $self->_begin_call();
96 0 my ($result, $error);
97 0 ▶ eval { $result = $self->_execute_bytes($encoded); 1 } or $error = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $result = $self->_execute_bytes($encoded); 1 } } or $error = $@ 0 0 0
98 0 $self->_end_call();
99 0 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
100 0 return $result;
101 }
102  
103 # STRling-public-arity: describe=1
104 0 ▶sub describe {
Subroutine describe: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self) = @_;
106 0 return $self->_completed_result($self->_envelope('describe', {}));
107 }
108  
109 # STRling-public-arity: compile=2..3
110 0 ▶sub compile {
Subroutine compile: not called CC=2 SCAR=17.9
Pod undocumented
111 0 my ($self, $compile_request, $target_profile) = @_;
112 0 my $payload = { compile_request => $compile_request };
113 0 ▶ $payload->{target_profile} = $target_profile if defined $target_profile;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target_profile 0 0 0
114 0 return $self->_completed_result($self->_envelope('compile', $payload));
115 }
116  
117 # STRling-public-arity: inspect_target_profile=2
118 0 ▶sub inspect_target_profile {
Subroutine inspect_target_profile: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my ($self, $target_profile) = @_;
120 0 return $self->_completed_result($self->_envelope('target_profile.inspect', { target_profile => $target_profile }));
121 }
122  
123 # STRling-public-arity: simply_compile=2..3
124 0 ▶sub simply_compile {
Subroutine simply_compile: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ($self, $builder_request, $target_profile) = @_;
126 0 my $payload = { builder_request => $builder_request };
127 0 ▶ $payload->{target_profile} = $target_profile if defined $target_profile;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target_profile 0 0 0
128 0 return $self->_completed_result($self->_envelope('simply.compile', $payload));
129 }
130  
131 # STRling-public-arity: close=1
132 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my ($self) = @_;
134 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
135 die STRling::NativeAdapterError->new('closed_client', 'cannot close native STRling client during an active call')
136 0 ▶ if $self->{active_calls};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_calls"} 0 0 0
137 0 $self->{closed} = 1;
138 0 delete $self->{execute_fn};
139 0 delete $self->{free_fn};
140 0 delete $self->{ffi};
141 0 return;
142 }
143  
144 0 ▶sub _begin_call {
Subroutine _begin_call: not called CC=2 SCAR=17.9
145 0 my ($self) = @_;
146 0 ▶ die STRling::NativeAdapterError->new('closed_client', 'native STRling client is closed') if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
147 0 ++$self->{active_calls};
148 }
149  
150 0 ▶sub _end_call { --$_[0]->{active_calls}; }
Subroutine _end_call: not called CC=1 SCAR=6.9
151  
152 0 ▶sub _execute_bytes {
Subroutine _execute_bytes: not called CC=10 SCAR=47.0
153 0 my ($self, $encoded) = @_;
154 0 require FFI::Platypus::Buffer;
155 0 require FFI::Platypus::Memory;
156 0 my ($input, $input_length) = FFI::Platypus::Buffer::scalar_to_buffer($encoded);
157 0 my $descriptor_size = $Config::Config{ptrsize} + $Config::Config{sizesize};
158 0 my $output = FFI::Platypus::Memory::malloc($descriptor_size);
159 0 FFI::Platypus::Memory::memset($output, 0, $descriptor_size);
160 0 my ($result, $primary_error);
161 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $status = $self->{"execute_fn"}->call($input, $input_length, $output); die "STRling::NativeAdapterError"->new("native_abi", "native STRling execution failed", $status) if $status != 0; my $descriptor = FFI::Platypus::Buffer::buffer_to_scalar($output, $descriptor_size); my $pointer_pack = $Config::Config{"ptrsize"} == 8 ? "Q" : "L"; my $size_pack = $Config::Config{"sizesize"} == 8 ? "Q" : "L"; () = ; die "STRling::NativeAdapterError"->new("transport", "native STRling returned an invalid or oversized response") if not $data && $length or $length > 33554432; my $raw = FFI::Platypus::Buffer::buffer_to_scalar($data, $length); $result = $self->_decode_response($raw); 1 } } or $primary_error = $@ 0 0 0
162 0 my $status = $self->{execute_fn}->call($input, $input_length, $output);
163 0 ▶ die STRling::NativeAdapterError->new('native_abi', 'native STRling execution failed', $status) if $status != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status != 0 0 0 0
164 0 my $descriptor = FFI::Platypus::Buffer::buffer_to_scalar($output, $descriptor_size);
165 0 ▶ my $pointer_pack = $Config::Config{ptrsize} == 8 ? 'Q' : 'L';
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{'ptrsize'} == 8 ? : 0 0 0
166 0 ▶ my $size_pack = $Config::Config{sizesize} == 8 ? 'Q' : 'L';
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{'sizesize'} == 8 ? : 0 0 0
167 0 my ($data, $length) = unpack("$pointer_pack $size_pack", $descriptor);
168 0 ▶ die STRling::NativeAdapterError->new('transport', 'native STRling returned an invalid or oversized response')
Branch: 1 branch
BranchTrueFalseTotal
if not $data && $length or $length > 33554432 0 0 0
Condition0%0/6
not $data && $length or $length > 33554432
l!l&&r!l&&!r
000
$data && $length
!ll&&!rl&&r
000
MC/DC0%0/3
not $data && $length or $length > 33554432
$data $length $length > 33554432
Truth table0%0/4
not(A && B) or C
A= $dataB= $lengthC= $length > 33554432
ABCresult
0XX1
10X1
1111
1100
169 if !$data || !$length || $length > MAX_INTEROP_RESPONSE_BYTES;
170 0 my $raw = FFI::Platypus::Buffer::buffer_to_scalar($data, $length);
171 0 $result = $self->_decode_response($raw);
172 0 1;
173 } or $primary_error = $@;
174 0 my $release_status = $self->{free_fn}->call($output);
175 0 FFI::Platypus::Memory::free($output);
176 0 ▶ die $primary_error if $primary_error;
Branch: 1 branch
BranchTrueFalseTotal
if $primary_error 0 0 0
177 0 ▶ die STRling::NativeAdapterError->new('native_abi', 'native STRling response release failed', $release_status)
Branch: 1 branch
BranchTrueFalseTotal
if $release_status != 0 0 0 0
178 if $release_status != 0;
179 0 return $result;
180 }
181  
182 0 ▶sub _decode_response {
Subroutine _decode_response: not called CC=13 SCAR=52.0
183 0 my ($self, $raw) = @_;
184 0 my $value = eval { STRling::StrictJSON::decode_object($raw) };
185 0 ▶ die STRling::NativeAdapterError->new('transport', "native STRling response is not strict UTF-8 JSON: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
186 die STRling::NativeAdapterError->new('transport', 'interop response has an unsupported version')
187 0 ▶ unless defined($value->{interop_protocol_version}) && $value->{interop_protocol_version} eq INTEROP_PROTOCOL_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value->{"interop_protocol_version"} and $value->{"interop_protocol_version"} eq "1.0.0" 0 0 0
Condition0%0/3
defined $value->{"interop_protocol_version"} and $value->{"interop_protocol_version"} eq "1.0.0"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value->{"interop_protocol_version"} and $value->{"interop_protocol_version"} eq "1.0.0"
defined $value->{"interop_protocol_version"} $value->{"interop_protocol_version"} eq "1.0.0"
Truth table0%0/3
A and B
A= defined $value->{"interop_protocol_version"}B= $value->{"interop_protocol_version"} eq "1.0.0"
ABresult
0X0
100
111
188 0 my $status = $value->{status};
189 0 ▶ die STRling::NativeAdapterError->new('transport', 'interop response has an unsupported status')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status and $status eq "completed" || $status eq "error" 0 0 0
Condition0%0/6
defined $status and $status eq "completed" || $status eq "error"
!ll&&!rl&&r
000
$status eq "completed" or $status eq "error"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $status and $status eq "completed" || $status eq "error"
defined $status $status eq "completed" $status eq "error"
Truth table0%0/4
A and B or C
A= defined $statusB= $status eq "completed"C= $status eq "error"
ABCresult
0XX0
1000
11X1
1011
190 unless defined($status) && ($status eq 'completed' || $status eq 'error');
191 die STRling::NativeAdapterError->new('transport', 'completed interop response has no result')
192 0 ▶ if $status eq 'completed' && !exists($value->{result});
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "completed" and not exists $value->{"result"} 0 0 0
Condition0%0/3
$status eq "completed" and not exists $value->{"result"}
!ll&&!rl&&r
000
MC/DC0%0/2
$status eq "completed" and not exists $value->{"result"}
$status eq "completed" not exists $value->{"result"}
Truth table0%0/3
A and not(B)
A= $status eq "completed"B= not exists $value->{"result"}
ABresult
0X0
100
111
193 0 ▶ if ($status eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "error") 0 0 0
194 0 my $error = $value->{error};
195 die STRling::NativeAdapterError->new('transport', 'failed interop response has no stable code and path')
196 0 ▶ unless ref($error) eq 'HASH' && _is_json_string($error->{code}) && _is_json_string($error->{path});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $error eq "HASH" and _is_json_string($error->{"code"}) and _is_json_string($error->{"path"}) 0 0 0
Condition0%0/6
ref $error eq "HASH" and _is_json_string($error->{"code"}) and _is_json_string($error->{"path"})
!ll&&!rl&&r
000
ref $error eq "HASH" and _is_json_string($error->{"code"})
!ll&&!rl&&r
000
MC/DC0%0/3
ref $error eq "HASH" and _is_json_string($error->{"code"}) and _is_json_string($error->{"path"})
ref $error eq "HASH" _is_json_string($error->{"code"}) _is_json_string($error->{"path"})
Truth table0%0/4
A and B and C
A= ref $error eq "HASH"B= _is_json_string($error->{"code"})C= _is_json_string($error->{"path"})
ABCresult
0XX0
10X0
1100
1111
197 }
198 0 return $value;
199 }
200  
201 0 ▶sub _is_json_string {
Subroutine _is_json_string: not called CC=3 SCAR=24.8
202 0 my ($value) = @_;
203 0 ▶ return 0 if !defined($value) || ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 0 0
Condition0%0/3
not defined $value or ref $value
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table0%0/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
204 0 return (B::svref_2object(\$value)->FLAGS & B::SVf_POK()) != 0;
205 }
206  
207 0 ▶sub _envelope {
Subroutine _envelope: not called CC=1 SCAR=6.9
208 0 my ($self, $operation, $payload) = @_;
209 0 return { interop_protocol_version => INTEROP_PROTOCOL_VERSION, operation => $operation, payload => $payload };
210 }
211  
212 0 ▶sub _completed_result {
Subroutine _completed_result: not called CC=2 SCAR=17.9
213 0 my ($self, $request) = @_;
214 0 my $response = $self->execute($request);
215 0 ▶ die STRling::InteropProtocolError->new($response) if $response->{status} eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq "error" 0 0 0
216 0 return $response->{result};
217 }
218  
219 1 1;
↑ Summary STRling/StrictJSON.pm »