blib/lib/Wasm.pm

filter: Statementstmt 96.5% 170 / 176 Branchbran 75.0% 63 / 84 Conditioncond 72.2% 13 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 87.4% 264 / 302 CC 52 SCAR 38.1
CC
52
Cov
88%
import
56.2
_linker
2.0
BEGIN
1.0
CRAP 45.0
↑ Summary blib/lib/Wasm/Wasmtime/Engine.pm »
1 package Wasm;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Ref::Util qw( is_plain_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Wasm::Trap;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Write Perl extensions using Wasm
11 5 our $VERSION = '0.24'; # VERSION
12  
13  
14 5 our %WASM;
15 5 my $linker;
16 my %inst;
17 5 my %pp;
18 5 my $wasi;
19 5 my @keep;
20  
21 sub _linker
22 21 ▶{
Subroutine _linker: called CC=2 SCAR=7.0
23 21 require Wasm::Wasmtime;
24 21 ▶ $linker ||= do {
Condition66%2/3
$linker ||= do { my $linker = "Wasm::Wasmtime::Linker"->new("Wasm::Wasmtime::Store"->new("Wasm::Wasmtime::Engine"->new("Wasm::Wasmtime::Config"->new->wasm_multi_value(1)->cache_config_default))); $linker->allow_shadowing(0); $linker }
l!l&&r!l&&!r
1650
MC/DC0%0/2
$linker ||= do { my $linker = "Wasm::Wasmtime::Linker"->new("Wasm::Wasmtime::Store"->new("Wasm::Wasmtime::Engine"->new("Wasm::Wasmtime::Config"->new->wasm_multi_value(1)->cache_config_default))); $linker->allow_shadowing(0); $linker }
$linker do { my $linker = "Wasm::Wasmtime::Linker"->new("Wasm::Wasmtime::Store"->new("Wasm::Wasmtime::Engine"->new("Wasm::Wasmtime::Config"->new->wasm_multi_value(1)->cache_config_default))); $linker->allow_shadowing(0); $linker }
Truth table66%2/3
A ||= B
A= $linkerB= do { my $linker = "Wasm::Wasmtime::Linker"->new("Wasm::Wasmtime::Store"->new("Wasm::Wasmtime::Engine"->new("Wasm::Wasmtime::Config"->new->wasm_multi_value(1)->cache_config_default))); $linker->allow_shadowing(0); $linker }
ABresult
1X1
011
000
25 5 my $linker = Wasm::Wasmtime::Linker->new(
26 Wasm::Wasmtime::Store->new(
27 Wasm::Wasmtime::Engine->new(
28 Wasm::Wasmtime::Config
29 ->new
30 ->wasm_multi_value(1)
31 ->cache_config_default,
32 ),
33 ),
34 );
35 5 $linker->allow_shadowing(0);
36 5 $linker;
37 };
38 }
39  
40 sub import
41 25 ▶{
Subroutine import: called CC=51 SCAR=40.3
42 25 my $class = shift;
43 25 my($caller, $fn) = caller;
44  
45 25 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 23 25
46  
47 23 ▶ if(defined $_[0] && $_[0] ne '-api')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] ne "-api") 1 22 23
Condition66%2/3
defined $_[0] and $_[0] ne "-api"
!ll&&!rl&&r
0221
MC/DC50%1/2
defined $_[0] and $_[0] ne "-api"
defined $_[0] $_[0] ne "-api"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] ne "-api"
ABresult
0X0
100
111
48 {
49 1 Carp::croak("You MUST specify an api level as the first option");
50 }
51  
52 22 my $api;
53 my $exporter;
54 22 my @module;
55 22 my $package = $caller;
56 22 my $file = $fn;
57  
58 22 my @global;
59  
60 22 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 46 20 66
61 {
62 46 my $key = shift;
63 46 ▶ if($key eq '-api')
Branch: 8 branches
BranchTrueFalseTotal
if ($key eq '-api') { } 23 23 46
elsif ($key eq '-wat') { } 4 19 23
elsif ($key eq '-file') { } 6 13 19
elsif ($key eq '-self') { } 8 5 13
elsif ($key eq '-exporter') { } 3 2 5
elsif ($key eq '-package') { } 1 1 2
elsif ($key eq '-global') { } 1 0 1
elsif ($key eq '-imports') { } 0 0 0
64 {
65 23 ▶ if(defined $api)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api) 1 22 23
66 {
67 1 Carp::croak("Specified -api more than once");
68 }
69 22 $api = shift;
70 22 ▶ unless(defined $api && $api == 0)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $api and $api == 0) 1 21 22
Condition66%2/3
defined $api and $api == 0
!ll&&!rl&&r
0121
MC/DC50%1/2
defined $api and $api == 0
defined $api $api == 0
Truth table66%2/3
A and B
A= defined $apiB= $api == 0
ABresult
0X0
100
111
71 {
72 1 Carp::croak("Currently only -api => 0 is supported");
73 }
74 }
75 elsif($key eq '-wat')
76 {
77 4 my $wat = shift;
78 4 ▶ Carp::croak("-wat undefined") unless defined $wat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wat 0 4 4
79 4 @module = (wat => $wat);
80 }
81 elsif($key eq '-file')
82 {
83 6 my $path = shift;
84 6 ▶ unless(defined $path && -f $path)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and -f $path) 0 6 6
Condition33%1/3
defined $path and -f $path
!ll&&!rl&&r
006
MC/DC0%0/2
defined $path and -f $path
defined $path -f $path
Truth table33%1/3
A and B
A= defined $pathB= -f $path
ABresult
0X0
100
111
85 {
86 0 ▶ $path = 'undef' unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
87 0 Carp::croak("no such file $path");
88 }
89 6 $file = "$path";
90 6 @module = (file => $file);
91 }
92 elsif($key eq '-self')
93 {
94 8 require Path::Tiny;
95 8 my $perl_path = Path::Tiny->new($fn);
96 8 my $basename = $perl_path->basename;
97 8 $basename =~ s/\.(pl|pm)$//;
98 8 my @maybe = sort { $b->stat->mtime <=> $a->stat->mtime } grep { -f $_ } (
99 $perl_path->parent->child($basename . ".wasm"),
100 $perl_path->parent->child($basename . ".wat"),
101 );
102 8 ▶ if(@maybe == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@maybe == 0) { } 0 8 8
103 {
104 0 Carp::croak("unable to find .wasm or .wat file relative to Perl source");
105 }
106 else
107 {
108 8 $file = shift @maybe;
109 8 @module = (file => $file);
110 }
111 }
112 elsif($key eq '-exporter')
113 {
114 3 $exporter = shift;
115 }
116 elsif($key eq '-package')
117 {
118 1 $package = shift;
119 }
120 elsif($key eq '-global')
121 {
122 1 ▶ if(is_plain_arrayref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($_[0])) { } 1 0 1
123 {
124 1 push @global, shift;
125 }
126 else
127 {
128 0 Carp::croak("-global should be an array reference");
129 }
130 }
131 elsif($key eq '-imports')
132 {
133 0 Carp::croak("-imports was removed in Wasm.pm 0.08");
134 }
135 else
136 {
137 0 Carp::croak("Unknown Wasm option: $key");
138 }
139 }
140  
141 20 _linker();
142  
143 20 ▶ if(@global)
Branch: 1 branch
BranchTrueFalseTotal
if (@global) 1 19 20
144 {
145 1 ▶ Carp::croak("Cannot specify both Wasm and -global") if @module;
Branch: 1 branch
BranchTrueFalseTotal
if @module 0 1 1
146 1 foreach my $spec (@global)
147 {
148 1 my($name, $content, $mutability, $value) = @$spec;
149 1 my $global = Wasm::Wasmtime::Global->new(
150 $linker->store,
151 Wasm::Wasmtime::GlobalType->new($content, $mutability),
152 $value,
153 );
154 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 *{"${package}::$name"} = $global->tie;
156 1 $pp{$package} = $file;
157 }
158 1 return;
159 }
160  
161 19 ▶ @module = (wat => '(module)') unless @module;
Branch: 1 branch
BranchTrueFalseTotal
unless @module 1 18 19
162  
163 19 ▶ Carp::croak("The wasm_ namespace is reserved for internal use") if $package =~ /^wasi_/;
Branch: 1 branch
BranchTrueFalseTotal
if $package =~ /^wasi_/ 0 19 19
164 19 ▶ Carp::croak("Wasm for $package already loaded") if $inst{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $inst{$package} 0 19 19
165  
166 19 my $module = Wasm::Wasmtime::Module->new($linker->store->engine, @module);
167  
168 19 foreach my $import (@{ $module->imports })
169 {
170  
171 11 my $module = $import->module;
172  
173 11 ▶ if($module =~ /^(wasi_unstable|wasi_snapshot_preview1)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^(wasi_unstable|wasi_snapshot_preview1)$/) 2 9 11
174 {
175 2 ▶ next if $WASM{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $Wasm::WASM{$module} 0 2 2
176 2 ▶ unless($wasi)
Branch: 1 branch
BranchTrueFalseTotal
unless ($wasi) 2 0 2
177 {
178 2 $linker->store->set_wasi(
179 Wasm::Wasmtime::WasiConfig
180 ->new
181 ->set_argv($0, @ARGV)
182 ->inherit_env
183 ->inherit_stdin
184 ->inherit_stdout
185 ->inherit_stderr
186 ->preopen_dir("/", "/")
187 );
188 2 $linker->define_wasi;
189 2 $wasi = 1;
190 }
191 2 $WASM{$module} = __FILE__; # Maybe Wasi::Snapshot::Preview1 etc.
192 2 next;
193 }
194  
195 9 ▶ if($module ne 'main' && !$inst{$module} && !$pp{$module})
Branch: 1 branch
BranchTrueFalseTotal
if ($module ne "main" and not $inst{$module} and not $pp{$module}) 4 5 9
Condition100%6/6
$module ne "main" and not $inst{$module} and not $pp{$module}
!ll&&!rl&&r
324
$module ne "main" and not $inst{$module}
!ll&&!rl&&r
126
MC/DC100%3/3
$module ne "main" and not $inst{$module} and not $pp{$module}
$module ne "main" not $inst{$module} not $pp{$module}
Truth table100%4/4
A and not(B) and not(C)
A= $module ne "main"B= not $inst{$module}C= not $pp{$module}
ABCresult
0XX0
10X0
1100
1111
196 {
197 4 my $pm = "$module.pm";
198 4 $pm =~ s{::}{/}g;
199 4 eval { require $pm };
200 4 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 3 4
201 {
202 1 $error =~ s/ at (.*?)$//;
203 1 $error .= " module required by WebAssembly at $file";
204 1 Carp::croak("$error");
205 }
206 }
207  
208 8 ▶ next if $inst{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $inst{$module} 4 4 8
209  
210 4 my $name = $import->name;
211 4 my $type = $import->type;
212 4 my $kind = $type->kind;
213  
214 4 my $extern;
215  
216 4 ▶ if($kind eq 'functype')
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'functype') { } 2 2 4
elsif ($kind eq 'globaltype') { } 2 0 2
217 {
218 2 ▶ if(my $f = $module->can("${module}::$name"))
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $module->can("${module}::$name")) 2 0 2
219 {
220 2 $extern = Wasm::Wasmtime::Func->new(
221 $linker->store,
222 $type,
223 $f,
224 );
225 2 push @keep, $extern;
226 }
227 }
228 elsif($kind eq 'globaltype')
229 {
230 5 ▶ if(my $global = do { no strict 'refs'; tied ${"${module}::$name"} })
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $global = do { no strict 'refs'; tied ${"${module}::$name";} }) 2 0 2
231 {
232 2 $extern = $global;
233 }
234 }
235  
236 4 ▶ if($extern)
Branch: 1 branch
BranchTrueFalseTotal
if ($extern) 4 0 4
237 {
238 # TODO: check that the store is the same?
239 4 eval {
240 4 $linker->define(
241 $module,
242 $name,
243 $extern,
244 );
245 };
246 4 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 3 4
247 {
248 1 ▶ if(Wasm::Wasmtime::Error->can('new'))
Branch: 1 branch
BranchTrueFalseTotal
if ('Wasm::Wasmtime::Error'->can('new')) { } 1 0 1
249 {
250 # TODO: if we can do a get on the define that would
251 # be better than doing this regex on the diagnostic.
252 # this is available in the rust api, but not the c api
253 # as of this writing.
254 1 ▶ die $error unless $error =~ /defined twice/;
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /defined twice/ 0 1 1
255 }
256 else
257 {
258 # TODO: also for the prod version of wasmtime we don't
259 # have an error so we end up swallowing other types
260 # of errors, if there are any.
261 }
262 }
263 }
264 }
265  
266 18 my $instance = $inst{$package} = $linker->instantiate($module);
267 18 $linker->define_instance($package, $instance);
268 18 $WASM{$package} = "$file";
269  
270 18 my @me = @{ $module->exports };
271 18 my @ie = @{ $instance->exports };
272  
273 18 my @function_names;
274  
275 18 for my $i (0..$#ie)
276 {
277 40 my $exporttype = $me[$i];
278 40 my $name = $me[$i]->name;
279 40 my $externtype = $exporttype->type;
280 40 my $extern = $ie[$i];
281 40 my $kind = $extern->kind;
282 40 ▶ if($kind eq 'func')
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'func') { } 27 13 40
elsif ($kind eq 'global') { } 2 11 13
elsif ($kind eq 'memory') { } 11 0 11
283 {
284 27 my $func = $extern;
285 27 $func->attach($package, $name);
286 27 push @function_names, $name;
287 }
288 elsif($kind eq 'global')
289 {
290 2 my $global = $extern;
291 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
292 2 *{"${package}::$name"} = $global->tie;
293 }
294 elsif($kind eq 'memory')
295 {
296 11 require Wasm::Memory;
297 11 my $memory = Wasm::Memory->new($extern);
298 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
299 11 *{"${package}::$name"} = \$memory;
300 }
301 }
302  
303 18 ▶ if($exporter)
Branch: 1 branch
BranchTrueFalseTotal
if ($exporter) 3 15 18
304 {
305 3 require Exporter;
306 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
307 3 push @{ "${package}::ISA" }, 'Exporter';
308 3 ▶ if($exporter eq 'all')
Branch: 1 branch
BranchTrueFalseTotal
if ($exporter eq 'all') { } 1 2 3
309 {
310 1 push @{ "${package}::EXPORT" }, @function_names;
311 }
312 else
313 {
314 2 push @{ "${package}::EXPORT_OK" }, @function_names;
315 }
316 }
317 }
318  
319 5 1;
320  
321 __END__
↑ Summary blib/lib/Wasm/Wasmtime/Engine.pm »