blib/lib/Zuzu/Module/Worker.pm

filter: Statementstmt 13.2% 58 / 439 Branchbran 0.0% 0 / 178 Conditioncond 0.0% 0 / 130 MC/DCmcdc 0.0% 0 / 76 Subroutinesub 25.9% 14 / 54 Podpod 0.0% 0 / 1 totaltotal 8.2% 72 / 878 CC 139 SCAR 40.5
CC
139
Cov
8%
_worker_child_reply
132.0
_worker_child_handle_reply
132.0
_normalise_native_args
90.0
CRAP 57.7
« blib/lib/Zuzu/Module/Math/BigNum.pm ↑ Summary blib/lib/Zuzu/Module/URL.pm »
1 package Zuzu::Module::Worker;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Storable qw( fd_retrieve freeze nstore_fd thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Zuzu::Module::Marshal;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Zuzu::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 native_class
17 native_function
18 native_object
19 2 );
20 2 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Zuzu::Value::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Zuzu::Value::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 my @DENIAL_CAPABILITIES = qw(
26 fs
27 net
28 perl
29 js
30 proc
31 db
32 clib
33 gui
34 worker
35 );
36 2 my %DENIAL_CAPABILITY = map { $_ => 1 } @DENIAL_CAPABILITIES;
37  
38 0 ▶sub _runtime_error {
Subroutine _runtime_error: not called CC=1 SCAR=6.9
39 0 my ( $message ) = @_;
40  
41 0 return Zuzu::Error->new_runtime(
42 message => $message,
43 file => '<std/worker>',
44 line => 0,
45 );
46 }
47  
48 0 ▶sub _throw_exception {
Subroutine _throw_exception: not called CC=1 SCAR=6.9
49 0 my ( $runtime, $message ) = @_;
50  
51 0 die $runtime->_task_exception(
52 'Exception',
53 $message,
54 '<std/worker>',
55 0,
56 );
57 }
58  
59 0 ▶sub _task_exception {
Subroutine _task_exception: not called CC=1 SCAR=6.9
60 0 my ( $runtime, $class_name, $message ) = @_;
61  
62 0 return $runtime->_task_exception(
63 $class_name,
64 $message,
65 '<std/worker>',
66 0,
67 );
68 }
69  
70 0 ▶sub _fulfilled_task {
Subroutine _fulfilled_task: not called CC=1 SCAR=6.9
71 0 my ( $runtime, $name, $value ) = @_;
72  
73 0 return $runtime->_new_task(
74 name => $name,
75 status => 'fulfilled',
76 result => $value,
77 );
78 }
79  
80 0 ▶sub _rejected_task {
Subroutine _rejected_task: not called CC=1 SCAR=6.9
81 0 my ( $runtime, $name, $class_name, $message ) = @_;
82  
83 0 return $runtime->_new_task(
84 name => $name,
85 status => 'rejected',
86 error => _task_exception( $runtime, $class_name, $message ),
87 );
88 }
89  
90 0 ▶sub _throw_marshalling_exception {
Subroutine _throw_marshalling_exception: not called CC=3 SCAR=24.8
91 0 my ( $runtime, $message ) = @_;
92  
93 0 my $class;
94 0 eval {
95 0 my $env = $runtime->_load_module( 'std/marshal', '<std/worker>', 0 );
96 0 my $ref = $env->find_ref('MarshallingException');
97 0 ▶ $class = $$ref if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
98 0 1;
99 };
100 0 ▶ $class //= $runtime->{_builtin_classes}{Exception};
Condition0%0/3
$class //= $runtime->{"_builtin_classes"}{"Exception"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$class //= $runtime->{"_builtin_classes"}{"Exception"}
$class $runtime->{"_builtin_classes"}{"Exception"}
Truth table0%0/3
A //= B
A= $classB= $runtime->{"_builtin_classes"}{"Exception"}
ABresult
1X1
011
000
101  
102 0 die {
103 _zuzu_throw => 1,
104 value => $runtime->_instantiate_builtin_object(
105 $class,
106 {
107 message => $message,
108 file => '<std/worker>',
109 line => 0,
110 },
111 ),
112 };
113 }
114  
115 0 ▶sub _normalise_native_args {
Subroutine _normalise_native_args: not called CC=9 SCAR=45.0
116 0 my ( @args ) = @_;
117  
118 0 my $named = {};
119 0 my $named_pairs = [];
120 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 2 and ref $args[-2] eq "HASH" and ref $args[-1] eq "ARRAY") 0 0 0
Condition0%0/6
@args >= 2 and ref $args[-2] eq "HASH" and ref $args[-1] eq "ARRAY"
!ll&&!rl&&r
000
@args >= 2 and ref $args[-2] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@args >= 2 and ref $args[-2] eq "HASH" and ref $args[-1] eq "ARRAY"
@args >= 2 ref $args[-2] eq "HASH" ref $args[-1] eq "ARRAY"
Truth table0%0/4
A and B and C
A= @args >= 2B= ref $args[-2] eq "HASH"C= ref $args[-1] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
121 @args >= 2
122 and ref( $args[-2] ) eq 'HASH'
123 and ref( $args[-1] ) eq 'ARRAY'
124 ) {
125 0 $named_pairs = pop @args;
126 0 $named = pop @args;
127 }
128 shift @args
129 0 ▶ if @args
Branch: 1 branch
BranchTrueFalseTotal
if @args and builtin::blessed($args[0]) and $args[0]->isa("Zuzu::Value::Class") and ($args[0]->name // "") eq "Worker" 0 0 0
Condition0%0/11
@args and builtin::blessed($args[0]) and $args[0]->isa("Zuzu::Value::Class") and ($args[0]->name // "") eq "Worker"
!ll&&!rl&&r
000
@args and builtin::blessed($args[0]) and $args[0]->isa("Zuzu::Value::Class")
!ll&&!rl&&r
000
@args and builtin::blessed($args[0])
!ll&&!rl&&r
000
$args[0]->name // ""
l!l
00
MC/DC0%0/4
@args and builtin::blessed($args[0]) and $args[0]->isa("Zuzu::Value::Class") and ($args[0]->name // "") eq "Worker"
@args builtin::blessed($args[0]) $args[0]->isa("Zuzu::Value::Class") $args[0]->name
Truth table0%0/5
A and B and C and D
A= @argsB= builtin::blessed($args[0])C= $args[0]->isa("Zuzu::Value::Class")D= $args[0]->name
ABCDresult
0XXX0
10XX0
110X0
11100
11111
130 and blessed( $args[0] )
131 and $args[0]->isa('Zuzu::Value::Class')
132 and ( $args[0]->name // '' ) eq 'Worker';
133  
134 0 return ( \@args, $named, $named_pairs );
135 }
136  
137 0 ▶sub _parse_worker_options {
Subroutine _parse_worker_options: not called CC=8 SCAR=42.8
138 0 my ( $runtime, $named ) = @_;
139  
140 0 my @extra_denials;
141 0 ▶ for my $key ( sort CORE::keys %{ $named // {} } ) {
Condition0%0/2
$named // {}
l!l
00
MC/DC0%0/1
$named // {}
$named
Truth table0%0/2
A
A= $named
Aresult
11
00
142 0 my ($capability) = $key =~ /\Adeny_(.+)\z/;
143 0 ▶ if ( !defined $capability or !$DENIAL_CAPABILITY{$capability} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $capability and $DENIAL_CAPABILITY{$capability}) 0 0 0
Condition0%0/3
defined $capability and $DENIAL_CAPABILITY{$capability}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $capability and $DENIAL_CAPABILITY{$capability}
defined $capability $DENIAL_CAPABILITY{$capability}
Truth table0%0/3
A and B
A= defined $capabilityB= $DENIAL_CAPABILITY{$capability}
ABresult
0X0
100
111
144 0 die _runtime_error("Unknown named argument '$key' for Worker.spawn");
145 }
146  
147 0 my $value = $named->{$key};
148 0 ▶ if (
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
149 !blessed($value)
150 or !$value->isa('Zuzu::Value::Boolean')
151 ) {
152 0 my $type = $runtime->_type_name($value);
153 0 die _runtime_error(
154 "TypeException: Worker.spawn named argument '$key' "
155 . "expects Boolean, got $type",
156 );
157 }
158  
159 0 ▶ push @extra_denials, $capability if $value->value;
Branch: 1 branch
BranchTrueFalseTotal
if $value->value 0 0 0
160 }
161  
162 0 return @extra_denials;
163 }
164  
165 0 ▶sub _effective_denials {
Subroutine _effective_denials: not called CC=2 SCAR=17.9
166 0 my ( $runtime, @extra_denials ) = @_;
167  
168 0 my %deny = map { $_ => 1 } grep {
169 0 $runtime->is_denied($_)
170 } @DENIAL_CAPABILITIES;
171 0 $deny{$_} = 1 for @extra_denials;
172  
173 0 return sort CORE::keys %deny;
174 }
175  
176 0 ▶sub _worker_runtime {
Subroutine _worker_runtime: not called CC=6 SCAR=37.4
177 0 my ( $parent, $denials ) = @_;
178  
179 return Zuzu::Runtime->new(
180 0 ▶ lib => [ @{ $parent->lib // [] } ],
Condition0%0/2
$parent->lib // []
l!l
00
MC/DC0%0/1
$parent->lib // []
$parent->lib
Truth table0%0/2
A
A= $parent->lib
Aresult
11
00
181 0 ▶ builtin => { %{ $parent->builtin // {} } },
Condition0%0/2
$parent->builtin // {}
l!l
00
MC/DC0%0/1
$parent->builtin // {}
$parent->builtin
Truth table0%0/2
A
A= $parent->builtin
Aresult
11
00
182 0 ▶ deny => [ @{ $denials // [] } ],
Condition0%0/2
$denials // []
l!l
00
MC/DC0%0/1
$denials // []
$denials
Truth table0%0/2
A
A= $denials
Aresult
11
00
183 0 ▶ deny_modules => [ @{ $parent->deny_modules // [] } ],
Condition0%0/2
$parent->deny_modules // []
l!l
00
MC/DC0%0/1
$parent->deny_modules // []
$parent->deny_modules
Truth table0%0/2
A
A= $parent->deny_modules
Aresult
11
00
184 0 ▶ forbid => [ @{ $parent->forbid // [] } ],
Condition0%0/2
$parent->forbid // []
l!l
00
MC/DC0%0/1
$parent->forbid // []
$parent->forbid
Truth table0%0/2
A
A= $parent->forbid
Aresult
11
00
185 );
186 }
187  
188 0 ▶sub _error_text {
Subroutine _error_text: not called CC=9 SCAR=45.0
189 0 my ( $runtime, $error ) = @_;
190  
191 0 ▶ if ( ref($error) eq 'HASH' and $error->{_zuzu_throw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq "HASH" and $error->{"_zuzu_throw"}) 0 0 0
Condition0%0/3
ref $error eq "HASH" and $error->{"_zuzu_throw"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $error eq "HASH" and $error->{"_zuzu_throw"}
ref $error eq "HASH" $error->{"_zuzu_throw"}
Truth table0%0/3
A and B
A= ref $error eq "HASH"B= $error->{"_zuzu_throw"}
ABresult
0X0
100
111
192 0 my $value = $error->{value};
193 0 my $text = eval { $runtime->_to_String($value) };
194 0 ▶ return $text if defined $text and $text ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and $text ne "" 0 0 0
Condition0%0/3
defined $text and $text ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and $text ne ""
defined $text $text ne ""
Truth table0%0/3
A and B
A= defined $textB= $text ne ""
ABresult
0X0
100
111
195 }
196 0 ▶ if ( blessed($error) and $error->isa('Zuzu::Error') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa("Zuzu::Error")) 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("Zuzu::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Zuzu::Error")
builtin::blessed($error) $error->isa("Zuzu::Error")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("Zuzu::Error")
ABresult
0X0
100
111
197 0 ▶ return $error->message // "$error";
Condition0%0/2
$error->message // "$error"
l!l
00
MC/DC0%0/1
$error->message // "$error"
$error->message
Truth table0%0/2
A
A= $error->message
Aresult
11
00
198 }
199  
200 0 ▶ return defined $error ? "$error" : 'worker failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
201 }
202  
203 0 ▶sub _call_worker_callable {
Subroutine _call_worker_callable: not called CC=2 SCAR=17.9
204 0 my ( $runtime, $callable, $arguments ) = @_;
205  
206 0 ▶ if ( $callable->{_bound_self} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callable->{"_bound_self"}) 0 0 0
207 return $runtime->_call_method(
208 $callable,
209 $callable->{_bound_self},
210 0 $arguments,
211 {},
212 [],
213 '<std/worker>',
214 0,
215 );
216 }
217  
218 0 return $runtime->_call_function(
219 $callable,
220 $arguments,
221 {},
222 [],
223 '<std/worker>',
224 0,
225 );
226 }
227  
228 0 ▶sub _worker_child_reply {
Subroutine _worker_child_reply: not called CC=11 SCAR=48.8
229 0 my ( $parent_runtime, $request_bytes, $denials ) = @_;
230  
231 0 my $child_runtime = _worker_runtime( $parent_runtime, $denials );
232 0 my $reply;
233 0 my $ok = eval {
234 0 my $request = Zuzu::Module::Marshal::load_value(
235 $child_runtime,
236 $request_bytes,
237 );
238 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($request) and $request->isa("Zuzu::Value::Array")) 0 0 0
Condition0%0/3
builtin::blessed($request) and $request->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($request) and $request->isa("Zuzu::Value::Array")
builtin::blessed($request) $request->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($request)B= $request->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
239 !blessed($request)
240 or !$request->isa('Zuzu::Value::Array')
241 ) {
242 0 die "Worker.spawn payload did not decode as Array";
243 }
244 0 my @request = $request->resolved_items;
245 0 ▶ if ( @request != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@request != 2) 0 0 0
246 0 die "Worker.spawn payload has invalid arity";
247 }
248  
249 0 my ( $callable, $arguments ) = @request;
250 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")) 0 0 0
Condition0%0/3
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
builtin::blessed($callable) $callable->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($callable)B= $callable->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
251 !blessed($callable)
252 or !$callable->isa('Zuzu::Value::Function')
253 ) {
254 0 die "Worker.spawn payload callable did not decode as Function";
255 }
256 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array")) 0 0 0
Condition0%0/3
builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array")
builtin::blessed($arguments) $arguments->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($arguments)B= $arguments->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
257 !blessed($arguments)
258 or !$arguments->isa('Zuzu::Value::Array')
259 ) {
260 0 die "Worker.spawn payload arguments did not decode as Array";
261 }
262  
263 0 my @arguments = $arguments->resolved_items;
264 0 my $result = _call_worker_callable(
265 $child_runtime,
266 $callable,
267 \@arguments,
268 );
269 0 ▶ $result = $result->await
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Zuzu::Value::Task") 0 0 0
Condition0%0/3
builtin::blessed($result) and $result->isa("Zuzu::Value::Task")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Zuzu::Value::Task")
builtin::blessed($result) $result->isa("Zuzu::Value::Task")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->isa("Zuzu::Value::Task")
ABresult
0X0
100
111
270 if blessed($result) and $result->isa('Zuzu::Value::Task');
271 0 $reply = {
272 ok => 1,
273 bytes => Zuzu::Module::Marshal::dump_value(
274 $child_runtime,
275 $result,
276 ),
277 };
278 0 1;
279 };
280 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
281 0 $reply = {
282 ok => 0,
283 error_string => _error_text( $child_runtime, $@ ),
284 };
285 }
286  
287 0 return $reply;
288 }
289  
290 0 ▶sub _write_reply {
Subroutine _write_reply: not called CC=4 SCAR=30.0
291 0 my ( $writer, $reply ) = @_;
292  
293 0 my $stored = eval {
294 0 ▶ $writer->autoflush(1) if $writer and $writer->can('autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if $writer and $writer->can("autoflush") 0 0 0
Condition0%0/3
$writer and $writer->can("autoflush")
!ll&&!rl&&r
000
MC/DC0%0/2
$writer and $writer->can("autoflush")
$writer $writer->can("autoflush")
Truth table0%0/3
A and B
A= $writerB= $writer->can("autoflush")
ABresult
0X0
100
111
295 0 nstore_fd( { ok => 1, value => $reply }, $writer );
296 0 1;
297 };
298 0 ▶ if ( !$stored ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored) 0 0 0
299 0 my $fallback = {
300 ok => 0,
301 error_string => "Worker result could not be serialized: $@",
302 };
303 0 eval { nstore_fd( $fallback, $writer ); 1 };
304 }
305  
306 0 return;
307 }
308  
309 0 ▶sub _start_worker_process {
Subroutine _start_worker_process: not called CC=7 SCAR=40.3
310 0 my ( $runtime, $request_bytes, $denials ) = @_;
311  
312 0 $runtime->assert_capability(
313 'worker',
314 'Worker.spawn is denied by runtime policy',
315 '<std/worker>',
316 0,
317 );
318  
319 0 ▶ pipe( my $request_reader, my $request_writer )
Branch: 1 branch
BranchTrueFalseTotal
pipe my $request_reader, my $request_writer or die _runtime_error("Could not create worker request pipe: $!") 0 0 0
320 or die _runtime_error("Could not create worker request pipe: $!");
321 0 ▶ pipe( my $result_reader, my $result_writer )
Branch: 1 branch
BranchTrueFalseTotal
pipe my $result_reader, my $result_writer or die _runtime_error("Could not create worker result pipe: $!") 0 0 0
322 or die _runtime_error("Could not create worker result pipe: $!");
323  
324 0 my $pid = fork();
325 0 ▶ die _runtime_error("Could not fork worker: $!") if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
326  
327 0 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
328 0 close $request_writer;
329 0 close $result_reader;
330 0 eval { setpgrp( 0, 0 ); 1 };
331 0 my $request = eval { fd_retrieve($request_reader) };
332 0 my $reply;
333 0 ▶ if ( !$request ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $request) { } 0 0 0
334 0 $reply = {
335 ok => 0,
336 error_string => "Worker request could not be read: $@",
337 };
338 }
339 else {
340 $reply = _worker_child_reply(
341 $runtime,
342 $request->{request_bytes},
343 0 $denials,
344 );
345 }
346 0 _write_reply( $result_writer, $reply );
347 0 close $request_reader;
348 0 close $result_writer;
349 0 POSIX::_exit(0);
350 }
351  
352 0 close $request_reader;
353 0 close $result_writer;
354 0 my $sent = eval {
355 0 nstore_fd( { request_bytes => $request_bytes }, $request_writer );
356 0 1;
357 };
358 0 close $request_writer;
359 0 ▶ if ( !$sent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 0 0 0
360 0 kill 'TERM', -$pid;
361 0 kill 'TERM', $pid;
362 0 waitpid( $pid, 0 );
363 0 close $result_reader;
364 0 die _runtime_error("Could not send worker request: $@");
365 }
366  
367 0 return $runtime->_new_task(
368 name => 'Worker.spawn.process',
369 status => 'running',
370 pid => $pid,
371 reader => $result_reader,
372 schedule => 1,
373 );
374 }
375  
376 0 ▶sub _write_frame {
Subroutine _write_frame: not called CC=5 SCAR=34.0
377 0 my ( $writer, $frame ) = @_;
378  
379 0 return eval {
380 0 ▶ $writer->autoflush(1) if $writer and $writer->can('autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if $writer and $writer->can("autoflush") 0 0 0
Condition0%0/3
$writer and $writer->can("autoflush")
!ll&&!rl&&r
000
MC/DC0%0/2
$writer and $writer->can("autoflush")
$writer $writer->can("autoflush")
Truth table0%0/3
A and B
A= $writerB= $writer->can("autoflush")
ABresult
0X0
100
111
381 0 my $payload = freeze($frame);
382 0 my $record = pack( 'N', length($payload) ) . $payload;
383 0 my $offset = 0;
384 0 ▶ while ( $offset < length($record) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $record) 0 0 0
385 0 my $written = syswrite(
386 $writer,
387 $record,
388 length($record) - $offset,
389 $offset,
390 );
391 0 ▶ die "short write to worker channel" if !defined $written;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $written 0 0 0
392 0 $offset += $written;
393 }
394 0 return 1;
395 };
396 }
397  
398 0 ▶sub _read_exact {
Subroutine _read_exact: not called CC=4 SCAR=30.0
399 0 my ( $reader, $length ) = @_;
400  
401 0 my $buffer = '';
402 0 ▶ while ( length($buffer) < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer < $length) 0 0 0
403 0 my $chunk = '';
404 0 my $read = sysread(
405 $reader,
406 $chunk,
407 $length - length($buffer),
408 );
409 0 ▶ die "worker channel closed" if !defined $read or $read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read or $read == 0 0 0 0
Condition0%0/3
not defined $read or $read == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $read or $read == 0
not defined $read $read == 0
Truth table0%0/3
not(A) or B
A= not defined $readB= $read == 0
ABresult
1X1
011
000
410 0 $buffer .= $chunk;
411 }
412  
413 0 return $buffer;
414 }
415  
416 0 ▶sub _read_frame {
Subroutine _read_frame: not called CC=3 SCAR=24.8
417 0 my ( $reader ) = @_;
418  
419 0 my $frame = eval {
420 0 my $header = _read_exact( $reader, 4 );
421 0 my $length = unpack( 'N', $header );
422 0 thaw( _read_exact( $reader, $length ) );
423 };
424 0 ▶ return ( 0, "Worker channel could not be read: $@" )
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $frame 0 0 0
Condition0%0/3
$@ or not $frame
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $frame
$@ not $frame
Truth table0%0/3
A or not(B)
A= $@B= not $frame
ABresult
1X1
011
000
425 if $@ or !$frame;
426 0 return ( 1, $frame );
427 }
428  
429 0 ▶sub _frame_ready {
Subroutine _frame_ready: not called CC=3 SCAR=24.8
430 0 my ( $reader ) = @_;
431  
432 0 ▶ return 0 if !$reader;
Branch: 1 branch
BranchTrueFalseTotal
unless $reader 0 0 0
433 0 my $select = IO::Select->new($reader);
434 0 ▶ return $select->can_read(0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$select->can_read(0) ? : 0 0 0
435 }
436  
437 0 ▶sub _build_worker_inbox_class {
Subroutine _build_worker_inbox_class: not called CC=1 SCAR=6.9
438 0 my ( $runtime ) = @_;
439  
440 0 my $inbox_class = native_class( name => 'WorkerInbox' );
441  
442 $inbox_class->methods->{send} = native_function(
443 name => 'send',
444 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
445 0 my ( $self, $value ) = @_;
446 return _rejected_task(
447 $runtime,
448 'WorkerInbox.send',
449 'ChannelClosedException',
450 'send on closed worker inbox',
451 0 ▶ ) if $self->slots->{closed_out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"closed_out"} 0 0 0
452  
453 0 my $bytes;
454 0 my $ok = eval {
455 0 $bytes = Zuzu::Module::Marshal::dump_value(
456 $runtime,
457 $value,
458 );
459 0 1;
460 };
461 0 ▶ return _rejected_task(
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
462 $runtime,
463 'WorkerInbox.send',
464 'MarshallingException',
465 "WorkerInbox.send failed to marshal value: $@",
466 ) if !$ok;
467  
468 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (_write_frame($self->slots->{"writer"}, {"type", "message", "bytes", $bytes})) 0 0 0
469 !_write_frame(
470 $self->slots->{writer},
471 { type => 'message', bytes => $bytes },
472 )
473 ) {
474 0 $self->slots->{closed_out} = 1;
475 0 return _rejected_task(
476 $runtime,
477 'WorkerInbox.send',
478 'ChannelClosedException',
479 'worker inbox output is closed',
480 );
481 }
482  
483 0 return _fulfilled_task( $runtime, 'WorkerInbox.send', $value );
484 },
485 0 );
486  
487 $inbox_class->methods->{recv} = native_function(
488 name => 'recv',
489 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 0 my ( $self ) = @_;
491 return $runtime->_new_task(
492 name => 'WorkerInbox.recv',
493 status => 'waiting',
494 0 ▶ poll_cb => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
495 return (
496 1,
497 0,
498 _task_exception(
499 $runtime,
500 'ChannelClosedException',
501 'worker inbox closed',
502 ),
503 0 ▶ ) if $self->slots->{closed_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"closed_in"} 0 0 0
504 return ( 0, 1, undef )
505 0 ▶ if !_frame_ready( $self->slots->{reader} );
Branch: 1 branch
BranchTrueFalseTotal
unless _frame_ready($self->slots->{"reader"}) 0 0 0
506  
507 0 my ( $ok, $frame ) = _read_frame( $self->slots->{reader} );
508 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
509 0 $self->slots->{closed_in} = 1;
510 return (
511 0 1,
512 0,
513 _task_exception(
514 $runtime,
515 'ChannelClosedException',
516 $frame,
517 ),
518 );
519 }
520 0 ▶ if ( $frame->{type} eq 'close' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"type"} eq "close") 0 0 0
521 0 $self->slots->{closed_in} = 1;
522 return (
523 0 1,
524 0,
525 _task_exception(
526 $runtime,
527 'ChannelClosedException',
528 'worker inbox closed',
529 ),
530 );
531 }
532 0 ▶ if ( $frame->{type} eq 'cancel' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"type"} eq "cancel") 0 0 0
533 0 $self->slots->{closed_in} = 1;
534 return (
535 1,
536 0,
537 _task_exception(
538 $runtime,
539 'CancelledException',
540 0 ▶ $frame->{reason} // 'worker cancelled',
Condition0%0/2
$frame->{"reason"} // "worker cancelled"
l!l
00
MC/DC0%0/1
$frame->{"reason"} // "worker cancelled"
$frame->{"reason"}
Truth table0%0/2
A
A= $frame->{"reason"}
Aresult
11
00
541 ),
542 );
543 }
544 0 ▶ if ( $frame->{type} ne 'message' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"type"} ne "message") 0 0 0
545 return (
546 0 1,
547 0,
548 _task_exception(
549 $runtime,
550 'Exception',
551 'WorkerInbox received invalid frame',
552 ),
553 );
554 }
555 0 my $value = eval {
556 Zuzu::Module::Marshal::load_value(
557 $runtime,
558 $frame->{bytes},
559 0 );
560 };
561 return (
562 0 ▶ 1,
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
563 0,
564 _task_exception(
565 $runtime,
566 'Exception',
567 "WorkerInbox failed to unmarshal value: $@",
568 ),
569 ) if $@;
570  
571 0 return ( 1, 1, $value );
572 },
573 0 );
574 },
575 0 );
576  
577 $inbox_class->methods->{close} = native_function(
578 name => 'close',
579 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
580 0 my ( $self ) = @_;
581 return _fulfilled_task( $runtime, 'WorkerInbox.close', undef )
582 0 ▶ if $self->slots->{closed_out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"closed_out"} 0 0 0
583 0 $self->slots->{closed_out} = 1;
584 0 _write_frame( $self->slots->{writer}, { type => 'close' } );
585 0 return _fulfilled_task( $runtime, 'WorkerInbox.close', undef );
586 },
587 0 );
588  
589 0 return $inbox_class;
590 }
591  
592 0 ▶sub _worker_child_handle_reply {
Subroutine _worker_child_handle_reply: not called CC=11 SCAR=48.8
593 0 my ( $parent_runtime, $request_bytes, $denials, $in_reader, $out_writer ) = @_;
594  
595 0 my $child_runtime = _worker_runtime( $parent_runtime, $denials );
596 0 my $reply;
597 0 my $ok = eval {
598 0 my $request = Zuzu::Module::Marshal::load_value(
599 $child_runtime,
600 $request_bytes,
601 );
602 0 ▶ die "Worker.spawn_handle payload did not decode as Array"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($request) and $request->isa("Zuzu::Value::Array") 0 0 0
Condition0%0/3
builtin::blessed($request) and $request->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($request) and $request->isa("Zuzu::Value::Array")
builtin::blessed($request) $request->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($request)B= $request->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
603 if !blessed($request) or !$request->isa('Zuzu::Value::Array');
604 0 my @request = $request->resolved_items;
605 0 ▶ die "Worker.spawn_handle payload has invalid arity" if @request != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @request != 2 0 0 0
606  
607 0 my ( $callable, $arguments ) = @request;
608 0 ▶ die "Worker.spawn_handle payload callable did not decode as Function"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function") 0 0 0
Condition0%0/3
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
builtin::blessed($callable) $callable->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($callable)B= $callable->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
609 if !blessed($callable) or !$callable->isa('Zuzu::Value::Function');
610 0 ▶ die "Worker.spawn_handle payload arguments did not decode as Array"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array") 0 0 0
Condition0%0/3
builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arguments) and $arguments->isa("Zuzu::Value::Array")
builtin::blessed($arguments) $arguments->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($arguments)B= $arguments->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
611 if !blessed($arguments) or !$arguments->isa('Zuzu::Value::Array');
612  
613 0 my $inbox_class = _build_worker_inbox_class($child_runtime);
614 0 my $inbox = native_object(
615 class => $inbox_class,
616 slots => {
617 reader => $in_reader,
618 writer => $out_writer,
619 closed_in => 0,
620 closed_out => 0,
621 },
622 const => {
623 reader => 1,
624 writer => 1,
625 closed_in => 0,
626 closed_out => 0,
627 },
628 );
629 0 my @arguments = ( $inbox, $arguments->resolved_items );
630 0 my $result = _call_worker_callable(
631 $child_runtime,
632 $callable,
633 \@arguments,
634 );
635 0 ▶ $result = $result->await
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Zuzu::Value::Task") 0 0 0
Condition0%0/3
builtin::blessed($result) and $result->isa("Zuzu::Value::Task")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Zuzu::Value::Task")
builtin::blessed($result) $result->isa("Zuzu::Value::Task")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->isa("Zuzu::Value::Task")
ABresult
0X0
100
111
636 if blessed($result) and $result->isa('Zuzu::Value::Task');
637 0 $reply = {
638 ok => 1,
639 bytes => Zuzu::Module::Marshal::dump_value(
640 $child_runtime,
641 $result,
642 ),
643 };
644 0 1;
645 };
646 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
647 0 $reply = {
648 ok => 0,
649 error_string => _error_text( $child_runtime, $@ ),
650 };
651 }
652 0 _write_frame( $out_writer, { type => 'close' } );
653  
654 0 return $reply;
655 }
656  
657 0 ▶sub _start_worker_handle_process {
Subroutine _start_worker_handle_process: not called CC=6 SCAR=37.4
658 0 my ( $runtime, $request_bytes, $denials ) = @_;
659  
660 0 $runtime->assert_capability(
661 'worker',
662 'Worker.spawn_handle is denied by runtime policy',
663 '<std/worker>',
664 0,
665 );
666  
667 0 ▶ pipe( my $parent_reader, my $child_writer )
Branch: 1 branch
BranchTrueFalseTotal
pipe my $parent_reader, my $child_writer or die _runtime_error("Could not create worker output pipe: $!") 0 0 0
668 or die _runtime_error("Could not create worker output pipe: $!");
669 0 ▶ pipe( my $child_reader, my $parent_writer )
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_reader, my $parent_writer or die _runtime_error("Could not create worker input pipe: $!") 0 0 0
670 or die _runtime_error("Could not create worker input pipe: $!");
671 0 ▶ pipe( my $result_reader, my $result_writer )
Branch: 1 branch
BranchTrueFalseTotal
pipe my $result_reader, my $result_writer or die _runtime_error("Could not create worker result pipe: $!") 0 0 0
672 or die _runtime_error("Could not create worker result pipe: $!");
673  
674 0 my $pid = fork();
675 0 ▶ die _runtime_error("Could not fork worker: $!") if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
676  
677 0 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
678 0 close $parent_reader;
679 0 close $parent_writer;
680 0 close $result_reader;
681 0 eval { setpgrp( 0, 0 ); 1 };
682 0 my $reply = _worker_child_handle_reply(
683 $runtime,
684 $request_bytes,
685 $denials,
686 $child_reader,
687 $child_writer,
688 );
689 0 _write_reply( $result_writer, $reply );
690 0 close $child_reader;
691 0 close $child_writer;
692 0 close $result_writer;
693 0 POSIX::_exit(0);
694 }
695  
696 0 close $child_reader;
697 0 close $child_writer;
698 0 close $result_writer;
699  
700 0 return ( $pid, $parent_reader, $parent_writer, $result_reader );
701 }
702  
703 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=6.1
Pod undocumented
704 2 my ( $class, $runtime ) = @_;
705  
706 2 $runtime->assert_capability(
707 'worker',
708 'std/worker is denied by runtime policy',
709 '<std/worker>',
710 0,
711 );
712  
713 2 my $worker_class = native_class( name => 'Worker' );
714 2 my $handle_class = native_class( name => 'WorkerHandle' );
715  
716 $handle_class->methods->{send} = native_function(
717 name => 'send',
718 0 ▶ native => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
719 0 my ( $self, $value ) = @_;
720 return _rejected_task(
721 $runtime,
722 'WorkerHandle.send',
723 'ChannelClosedException',
724 'send on closed worker handle',
725 ) if $self->slots->{local_closed}
726 or $self->slots->{remote_closed}
727 or $self->slots->{cancelled}
728 0 ▶ or $self->slots->{result_task}->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"local_closed"} or $self->slots->{"remote_closed"} or $self->slots->{"cancelled"} or $self->slots->{"result_task"}->is_done 0 0 0
Condition0%0/9
$self->slots->{"local_closed"} or $self->slots->{"remote_closed"} or $self->slots->{"cancelled"} or $self->slots->{"result_task"}->is_done
l!l&&r!l&&!r
000
$self->slots->{"local_closed"} or $self->slots->{"remote_closed"} or $self->slots->{"cancelled"}
l!l&&r!l&&!r
000
$self->slots->{"local_closed"} or $self->slots->{"remote_closed"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->slots->{"local_closed"} or $self->slots->{"remote_closed"} or $self->slots->{"cancelled"} or $self->slots->{"result_task"}->is_done
$self->slots->{"local_closed"} $self->slots->{"remote_closed"} $self->slots->{"cancelled"} $self->slots->{"result_task"}->is_done
Truth table0%0/5
A or B or C or D
A= $self->slots->{"local_closed"}B= $self->slots->{"remote_closed"}C= $self->slots->{"cancelled"}D= $self->slots->{"result_task"}->is_done
ABCDresult
1XXX1
01XX1
001X1
00011
00000
729  
730 0 my $bytes;
731 0 my $ok = eval {
732 0 $bytes = Zuzu::Module::Marshal::dump_value(
733 $runtime,
734 $value,
735 );
736 0 1;
737 };
738 0 ▶ return _rejected_task(
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
739 $runtime,
740 'WorkerHandle.send',
741 'Exception',
742 "WorkerHandle.send failed to marshal value: $@",
743 ) if !$ok;
744  
745 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (_write_frame($self->slots->{"writer"}, {"type", "message", "bytes", $bytes})) 0 0 0
746 !_write_frame(
747 $self->slots->{writer},
748 { type => 'message', bytes => $bytes },
749 )
750 ) {
751 0 $self->slots->{local_closed} = 1;
752 0 return _rejected_task(
753 $runtime,
754 'WorkerHandle.send',
755 'ChannelClosedException',
756 'worker handle input is closed',
757 );
758 }
759  
760 0 return _fulfilled_task( $runtime, 'WorkerHandle.send', $value );
761 },
762 2 );
763  
764 $handle_class->methods->{recv} = native_function(
765 name => 'recv',
766 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
767 0 my ( $self ) = @_;
768 return $runtime->_new_task(
769 name => 'WorkerHandle.recv',
770 status => 'waiting',
771 0 ▶ poll_cb => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
772 0 ▶ if ( @{ $self->slots->{queue} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->slots->{"queue"};}) 0 0 0
773 0 return ( 1, 1, shift @{ $self->slots->{queue} } );
774 }
775 0 ▶ if ( $self->slots->{cancelled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->slots->{"cancelled"}) 0 0 0
776 return (
777 0 1,
778 0,
779 _task_exception(
780 $runtime,
781 'CancelledException',
782 'worker cancelled',
783 ),
784 );
785 }
786 0 ▶ if ( $self->slots->{remote_closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->slots->{"remote_closed"}) 0 0 0
787 return (
788 0 1,
789 0,
790 _task_exception(
791 $runtime,
792 'ChannelClosedException',
793 'worker handle closed',
794 ),
795 );
796 }
797 return ( 0, 1, undef )
798 0 ▶ if !_frame_ready( $self->slots->{reader} );
Branch: 1 branch
BranchTrueFalseTotal
unless _frame_ready($self->slots->{"reader"}) 0 0 0
799  
800 0 my ( $ok, $frame ) = _read_frame( $self->slots->{reader} );
801 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
802 0 $self->slots->{remote_closed} = 1;
803 return (
804 0 1,
805 0,
806 _task_exception(
807 $runtime,
808 'ChannelClosedException',
809 $frame,
810 ),
811 );
812 }
813 0 ▶ if ( $frame->{type} eq 'close' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"type"} eq "close") 0 0 0
814 0 $self->slots->{remote_closed} = 1;
815 return (
816 0 1,
817 0,
818 _task_exception(
819 $runtime,
820 'ChannelClosedException',
821 'worker handle closed',
822 ),
823 );
824 }
825 0 ▶ if ( $frame->{type} ne 'message' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"type"} ne "message") 0 0 0
826 return (
827 0 1,
828 0,
829 _task_exception(
830 $runtime,
831 'Exception',
832 'WorkerHandle received invalid frame',
833 ),
834 );
835 }
836 0 my $value = eval {
837 Zuzu::Module::Marshal::load_value(
838 $runtime,
839 $frame->{bytes},
840 0 );
841 };
842 return (
843 0 ▶ 1,
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
844 0,
845 _task_exception(
846 $runtime,
847 'Exception',
848 "WorkerHandle failed to unmarshal value: $@",
849 ),
850 ) if $@;
851  
852 0 return ( 1, 1, $value );
853 },
854 0 );
855 },
856 2 );
857  
858 $handle_class->methods->{close} = native_function(
859 name => 'close',
860 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
861 0 my ( $self ) = @_;
862 return _fulfilled_task( $runtime, 'WorkerHandle.close', undef )
863 0 ▶ if $self->slots->{local_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"local_closed"} 0 0 0
864 0 $self->slots->{local_closed} = 1;
865 0 _write_frame( $self->slots->{writer}, { type => 'close' } );
866 0 ▶ close $self->slots->{writer} if $self->slots->{writer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"writer"} 0 0 0
867 0 return _fulfilled_task( $runtime, 'WorkerHandle.close', undef );
868 },
869 2 );
870  
871 $handle_class->methods->{cancel} = native_function(
872 name => 'cancel',
873 0 ▶ native => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
874 0 my ( $self, $reason ) = @_;
875 0 ▶ return $self if $self->slots->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"cancelled"} 0 0 0
876 0 $self->slots->{cancelled} = 1;
877 0 $self->slots->{local_closed} = 1;
878 0 $self->slots->{remote_closed} = 1;
879 _write_frame(
880 $self->slots->{writer},
881 {
882 0 ▶ type => 'cancel',
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 0 0 0
883 reason => defined $reason ? "$reason" : 'worker cancelled',
884 },
885 );
886 0 my $pid = $self->slots->{pid};
887 0 ▶ if ( defined $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 0 0 0
888 0 kill 'TERM', -$pid;
889 0 kill 'TERM', $pid;
890 }
891 0 my $task = $self->slots->{result_task};
892 0 ▶ $task->cancel(
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($task) and $task->isa("Zuzu::Value::Task") and not $task->is_done 0 0 0
defined $reason ? : 0 0 0
Condition0%0/6
builtin::blessed($task) and $task->isa("Zuzu::Value::Task") and not $task->is_done
!ll&&!rl&&r
000
builtin::blessed($task) and $task->isa("Zuzu::Value::Task")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($task) and $task->isa("Zuzu::Value::Task") and not $task->is_done
builtin::blessed($task) $task->isa("Zuzu::Value::Task") not $task->is_done
Truth table0%0/4
A and B and not(C)
A= builtin::blessed($task)B= $task->isa("Zuzu::Value::Task")C= not $task->is_done
ABCresult
0XX0
10X0
1100
1111
893 _task_exception(
894 $runtime,
895 'CancelledException',
896 defined $reason ? "$reason" : 'worker cancelled',
897 ),
898 ) if blessed($task) and $task->isa('Zuzu::Value::Task')
899 and !$task->is_done;
900 0 return $self;
901 },
902 2 );
903  
904 $handle_class->methods->{result} = native_function(
905 name => 'result',
906 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
907 0 my ( $self ) = @_;
908 0 return $self->slots->{result_task};
909 },
910 2 );
911  
912 $handle_class->methods->{status} = native_function(
913 name => 'status',
914 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
915 0 my ( $self ) = @_;
916 0 return $self->slots->{result_task}->status;
917 },
918 2 );
919  
920 $handle_class->methods->{done} = native_function(
921 name => 'done',
922 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
923 0 my ( $self ) = @_;
924 return Zuzu::Value::Boolean->new(
925 0 ▶ value => $self->slots->{result_task}->is_done ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$self->slots->{'result_task'}->is_done ? : 0 0 0
926 );
927 },
928 2 );
929  
930 $worker_class->static_methods->{spawn} = native_function(
931 name => 'spawn',
932 accepts_named => 1,
933 0 ▶ native => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
934 0 my ( $args, $named ) = _normalise_native_args(@_);
935 0 ▶ if ( @$args < 1 or @$args > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args < 1 or @$args > 2) 0 0 0
Condition0%0/3
@$args < 1 or @$args > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@$args < 1 or @$args > 2
@$args < 1 @$args > 2
Truth table0%0/3
A or B
A= @$args < 1B= @$args > 2
ABresult
1X1
011
000
936 0 die _runtime_error(
937 'Worker.spawn expects Callable and optional Array arguments',
938 );
939 }
940  
941 0 my $callable = $args->[0];
942 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")) 0 0 0
Condition0%0/3
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
builtin::blessed($callable) $callable->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($callable)B= $callable->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
943 !blessed($callable)
944 or !$callable->isa('Zuzu::Value::Function')
945 ) {
946 0 my $type = $runtime->_type_name($callable);
947 0 die _runtime_error(
948 "TypeException: Worker.spawn expects Function, got $type",
949 );
950 }
951  
952 0 ▶ my $worker_args = $args->[1] // Zuzu::Value::Array->new;
Condition0%0/3
$args->[1] // "Zuzu::Value::Array"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->[1] // "Zuzu::Value::Array"->new
$args->[1] "Zuzu::Value::Array"->new
Truth table0%0/3
A // B
A= $args->[1]B= "Zuzu::Value::Array"->new
ABresult
1X1
011
000
953 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")) 0 0 0
Condition0%0/3
builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")
builtin::blessed($worker_args) $worker_args->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($worker_args)B= $worker_args->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
954 !blessed($worker_args)
955 or !$worker_args->isa('Zuzu::Value::Array')
956 ) {
957 0 my $type = $runtime->_type_name($worker_args);
958 0 die _runtime_error(
959 "TypeException: Worker.spawn expects Array arguments, got $type",
960 );
961 }
962  
963 0 my @extra_denials = _parse_worker_options( $runtime, $named );
964 0 my @denials = _effective_denials( $runtime, @extra_denials );
965 0 my $request = Zuzu::Value::Array->new(
966 items => [ $callable, $worker_args ],
967 );
968 0 my $request_bytes;
969 eval {
970 0 $request_bytes = Zuzu::Module::Marshal::dump_value(
971 $runtime,
972 $request,
973 );
974 0 1;
975 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $request_bytes = Zuzu::Module::Marshal::dump_value($runtime, $request); 1 } } or do { my $error = $@; _throw_marshalling_exception($runtime, "Worker.spawn failed to marshal request: $error") } 0 0 0
976 0 my $error = $@;
977 0 _throw_marshalling_exception(
978 $runtime,
979 "Worker.spawn failed to marshal request: $error",
980 );
981 };
982 0 my $process = _start_worker_process(
983 $runtime,
984 $request_bytes,
985 \@denials,
986 );
987  
988 return $runtime->_new_task(
989 name => 'Worker.spawn',
990 schedule => 1,
991 0 ▶ on_cancel => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
992 0 my ( $task ) = @_;
993 0 ▶ $process->cancel( $task->error )
Branch: 1 branch
BranchTrueFalseTotal
if not $process->is_done 0 0 0
994 if !$process->is_done;
995 },
996 0 ▶ thunk => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
997 0 my $reply;
998 0 my $ok = eval {
999 0 $reply = $process->await;
1000 0 1;
1001 };
1002 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
1003 0 my $message = 'Worker process failed: '
1004 . _error_text( $runtime, $@ );
1005 0 _throw_exception(
1006 $runtime,
1007 $message,
1008 );
1009 }
1010 0 ▶ if ( ref($reply) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $reply ne "HASH") 0 0 0
1011 0 _throw_exception(
1012 $runtime,
1013 'Worker process returned invalid reply',
1014 );
1015 }
1016 0 ▶ if ( !$reply->{ok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->{"ok"}) 0 0 0
1017 _throw_exception(
1018 $runtime,
1019 0 ▶ $reply->{error_string} // 'worker failed',
Condition0%0/2
$reply->{"error_string"} // "worker failed"
l!l
00
MC/DC0%0/1
$reply->{"error_string"} // "worker failed"
$reply->{"error_string"}
Truth table0%0/2
A
A= $reply->{"error_string"}
Aresult
11
00
1020 );
1021 }
1022  
1023 return Zuzu::Module::Marshal::load_value(
1024 $runtime,
1025 $reply->{bytes},
1026 0 );
1027 },
1028 0 );
1029 },
1030 2 );
1031  
1032 $worker_class->static_methods->{spawn_handle} = native_function(
1033 name => 'spawn_handle',
1034 accepts_named => 1,
1035 0 ▶ native => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
1036 0 my ( $args, $named ) = _normalise_native_args(@_);
1037 0 ▶ if ( @$args < 1 or @$args > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args < 1 or @$args > 2) 0 0 0
Condition0%0/3
@$args < 1 or @$args > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@$args < 1 or @$args > 2
@$args < 1 @$args > 2
Truth table0%0/3
A or B
A= @$args < 1B= @$args > 2
ABresult
1X1
011
000
1038 0 die _runtime_error(
1039 'Worker.spawn_handle expects Callable and optional Array arguments',
1040 );
1041 }
1042  
1043 0 my $callable = $args->[0];
1044 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")) 0 0 0
Condition0%0/3
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($callable) and $callable->isa("Zuzu::Value::Function")
builtin::blessed($callable) $callable->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($callable)B= $callable->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
1045 !blessed($callable)
1046 or !$callable->isa('Zuzu::Value::Function')
1047 ) {
1048 0 my $type = $runtime->_type_name($callable);
1049 0 die _runtime_error(
1050 "TypeException: Worker.spawn_handle expects Function, got $type",
1051 );
1052 }
1053  
1054 0 ▶ my $worker_args = $args->[1] // Zuzu::Value::Array->new;
Condition0%0/3
$args->[1] // "Zuzu::Value::Array"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->[1] // "Zuzu::Value::Array"->new
$args->[1] "Zuzu::Value::Array"->new
Truth table0%0/3
A // B
A= $args->[1]B= "Zuzu::Value::Array"->new
ABresult
1X1
011
000
1055 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")) 0 0 0
Condition0%0/3
builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($worker_args) and $worker_args->isa("Zuzu::Value::Array")
builtin::blessed($worker_args) $worker_args->isa("Zuzu::Value::Array")
Truth table0%0/3
A and B
A= builtin::blessed($worker_args)B= $worker_args->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
1056 !blessed($worker_args)
1057 or !$worker_args->isa('Zuzu::Value::Array')
1058 ) {
1059 0 my $type = $runtime->_type_name($worker_args);
1060 0 die _runtime_error(
1061 "TypeException: Worker.spawn_handle expects Array arguments, got $type",
1062 );
1063 }
1064  
1065 0 my @extra_denials = _parse_worker_options( $runtime, $named );
1066 0 my @denials = _effective_denials( $runtime, @extra_denials );
1067 0 my $request = Zuzu::Value::Array->new(
1068 items => [ $callable, $worker_args ],
1069 );
1070 0 my $request_bytes;
1071 eval {
1072 0 $request_bytes = Zuzu::Module::Marshal::dump_value(
1073 $runtime,
1074 $request,
1075 );
1076 0 1;
1077 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $request_bytes = Zuzu::Module::Marshal::dump_value($runtime, $request); 1 } } or do { my $error = $@; _throw_marshalling_exception($runtime, "Worker.spawn_handle failed to marshal request: $error") } 0 0 0
1078 0 my $error = $@;
1079 0 _throw_marshalling_exception(
1080 $runtime,
1081 "Worker.spawn_handle failed to marshal request: $error",
1082 );
1083 };
1084  
1085 0 my ( $pid, $reader, $writer, $result_reader )
1086 = _start_worker_handle_process(
1087 $runtime,
1088 $request_bytes,
1089 \@denials,
1090 );
1091 0 my $process = $runtime->_new_task(
1092 name => 'WorkerHandle.process',
1093 status => 'running',
1094 pid => $pid,
1095 reader => $result_reader,
1096 schedule => 1,
1097 );
1098 my $result_task = $runtime->_new_task(
1099 name => 'WorkerHandle.result',
1100 process => 1,
1101 0 ▶ on_cancel => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1102 0 my ( $task ) = @_;
1103 0 ▶ $process->cancel( $task->error )
Branch: 1 branch
BranchTrueFalseTotal
if not $process->is_done 0 0 0
1104 if !$process->is_done;
1105 },
1106 0 ▶ thunk => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1107 0 my $reply = $process->await;
1108 0 ▶ if ( ref($reply) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $reply ne "HASH") 0 0 0
1109 0 _throw_exception(
1110 $runtime,
1111 'Worker process returned invalid reply',
1112 );
1113 }
1114 0 ▶ if ( !$reply->{ok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->{"ok"}) 0 0 0
1115 _throw_exception(
1116 $runtime,
1117 0 ▶ $reply->{error_string} // 'worker failed',
Condition0%0/2
$reply->{"error_string"} // "worker failed"
l!l
00
MC/DC0%0/1
$reply->{"error_string"} // "worker failed"
$reply->{"error_string"}
Truth table0%0/2
A
A= $reply->{"error_string"}
Aresult
11
00
1118 );
1119 }
1120  
1121 return Zuzu::Module::Marshal::load_value(
1122 $runtime,
1123 $reply->{bytes},
1124 0 );
1125 },
1126 0 );
1127  
1128 0 return native_object(
1129 class => $handle_class,
1130 slots => {
1131 pid => $pid,
1132 reader => $reader,
1133 writer => $writer,
1134 queue => [],
1135 local_closed => 0,
1136 remote_closed => 0,
1137 cancelled => 0,
1138 result_task => $result_task,
1139 },
1140 const => {
1141 pid => 1,
1142 reader => 1,
1143 writer => 1,
1144 queue => 1,
1145 local_closed => 0,
1146 remote_closed => 0,
1147 cancelled => 0,
1148 result_task => 1,
1149 },
1150 );
1151 },
1152 2 );
1153  
1154 return {
1155 2 Worker => $worker_class,
1156 WorkerHandle => $handle_class,
1157 };
1158 }
1159  
1160 2 1;
1161  
1162 =pod
1163  
1164 =head1 COPYRIGHT AND LICENCE
1165  
1166 B<< Zuzu::Module::Worker >> is copyright Toby Inkster.
1167  
1168 It is free software; you may redistribute it and/or modify it under
1169 the terms of either the Artistic License 1.0 or the GNU General Public
1170 License version 2.
1171  
1172 =cut
« blib/lib/Zuzu/Module/Math/BigNum.pm ↑ Summary blib/lib/Zuzu/Module/URL.pm »