blib/lib/Wasm/Wasmtime/Module.pm

filter: Statementstmt 98.4% 124 / 126 Branchbran 88.2% 30 / 34 Conditioncond 66.6% 10 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 24 / 24 Podpod 100% 4 / 4 totaltotal 91.9% 195 / 212 CC 24 SCAR 13.4
CC
24
Cov
94%
__ANON__
7.1
__ANON__
6.0
_args
6.0
CRAP 3.8
« blib/lib/Wasm/Wasmtime/TableType.pm ↑ Summary blib/lib/Wasm/Wasmtime/Func.pm »
1 package Wasm::Wasmtime::Module;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Ref::Util qw( is_blessed_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Wasm::Wasmtime::FFI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Wasm::Wasmtime::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Wasm::Wasmtime::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Wasm::Wasmtime::Module::Exports;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Wasm::Wasmtime::Module::Imports;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Wasm::Wasmtime::ImportType;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Wasm::Wasmtime::ExportType;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Ref::Util qw( is_blessed_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ABSTRACT: Wasmtime module class
18 22 our $VERSION = '0.23'; # VERSION
19  
20  
21 22 $ffi_prefix = 'wasm_module_';
22 22 $ffi->load_custom_type('::PtrObject' => 'wasm_module_t' => __PACKAGE__);
23  
24 sub _args
25 {
26 64 ▶ my $wasm;
Subroutine _args: called CC=6 SCAR=17.9
27 my $data;
28 64 ▶ if(@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 12 52 64
29 {
30 12 $data = shift;
31 12 $wasm = Wasm::Wasmtime::ByteVec->new($data);
32 }
33 else
34 {
35 52 my $key = shift;
36 52 ▶ if($key eq 'wat')
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'wat') { } 36 16 52
elsif ($key eq 'wasm') { } 1 15 16
elsif ($key eq 'file') { } 15 0 15
37 {
38 36 require Wasm::Wasmtime::Wat2Wasm;
39 36 $data = Wasm::Wasmtime::Wat2Wasm::wat2wasm(shift);
40 36 $wasm = Wasm::Wasmtime::ByteVec->new($data);
41 }
42 elsif($key eq 'wasm')
43 {
44 1 $data = shift;
45 1 $wasm = Wasm::Wasmtime::ByteVec->new($data);
46 }
47 elsif($key eq 'file')
48 {
49 15 require Wasm::Wasmtime::Wat2Wasm;
50 15 require Path::Tiny;
51 15 my $path = Path::Tiny->new(shift);
52 15 ▶ if($path->basename =~ /\.wat/)
Branch: 1 branch
BranchTrueFalseTotal
if ($path->basename =~ /\.wat/) { } 13 2 15
53 {
54 13 $data = Wasm::Wasmtime::Wat2Wasm::wat2wasm($path->slurp_utf8);
55 13 $wasm = Wasm::Wasmtime::ByteVec->new($data);
56 }
57 else
58 {
59 2 $data = $path->slurp_raw;
60 2 $wasm = Wasm::Wasmtime::ByteVec->new($data);
61 }
62 }
63 }
64 64 (\$wasm, \$data);
65 }
66  
67  
68 $ffi->attach( [ wasmtime_module_new => 'new' ] => ['wasm_engine_t', 'wasm_byte_vec_t*', 'opaque*'] => 'wasmtime_error_t' => sub {
69 56 ▶ my $xsub = shift;
Subroutine __ANON__: called CC=7 SCAR=19.5
70 56 my $class = shift;
71 56 my $store;
72 my $engine;
73 56 ▶ if(defined $_[0] && is_blessed_ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and is_blessed_ref($_[0])) 46 10 56
Condition66%2/3
defined $_[0] and is_blessed_ref($_[0])
!ll&&!rl&&r
01046
MC/DC50%1/2
defined $_[0] and is_blessed_ref($_[0])
defined $_[0] is_blessed_ref($_[0])
Truth table66%2/3
A and B
A= defined $_[0]B= is_blessed_ref($_[0])
ABresult
0X0
100
111
74 {
75 46 ▶ if($_[0]->isa('Wasm::Wasmtime::Engine'))
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->isa('Wasm::Wasmtime::Engine')) { } 45 1 46
elsif ($_[0]->isa('Wasm::Wasmtime::Store')) { } 1 0 1
76 {
77 45 $engine = shift;
78 }
79 elsif($_[0]->isa('Wasm::Wasmtime::Store'))
80 {
81 1 Carp::carp("Passing a Wasm::Wasmtime::Store into the module constructor is deprecated, please pass a Wasm::Wasmtime::Engine object instead");
82 1 $store = shift;
83 1 $engine = $store->engine;
84 }
85 }
86 56 ▶ $engine ||= Wasm::Wasmtime::Engine->new;
Condition66%2/3
$engine ||= "Wasm::Wasmtime::Engine"->new
l!l&&r!l&&!r
46100
MC/DC0%0/2
$engine ||= "Wasm::Wasmtime::Engine"->new
$engine "Wasm::Wasmtime::Engine"->new
Truth table66%2/3
A ||= B
A= $engineB= "Wasm::Wasmtime::Engine"->new
ABresult
1X1
011
000
87 56 my($wasm, $data) = _args(@_);
88 56 my $ptr;
89 56 ▶ if(my $error = $xsub->($engine, $$wasm, \$ptr))
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &$xsub($engine, $$wasm, \$ptr)) 1 55 56
90 {
91 1 Carp::croak("error creating module: " . $error->message);
92 }
93 55 bless { ptr => $ptr, engine => $engine, store => $store }, $class;
94 22 });
95  
96  
97 $ffi->attach( [ wasmtime_module_deserialize => 'deserialize' ] => ['wasm_engine_t', 'wasm_byte_vec_t*', 'opaque*'] => 'wasmtime_error_t' => sub {
98 2 ▶ my $xsub = shift;
Subroutine __ANON__: called CC=5 SCAR=16.6
99 2 my $class = shift;
100 2 my $engine;
101 2 ▶ $engine = defined $_[0] && is_blessed_ref $_[0] && $_[0]->isa('Wasm::Wasmtime::Engine') ? shift : Wasm::Wasmtime::Engine->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && is_blessed_ref($_[0]) && $_[0]->isa('Wasm::Wasmtime::Engine') ? : 1 1 2
Condition66%4/6
defined $_[0] and is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Engine")
!ll&&!rl&&r
101
defined $_[0] and is_blessed_ref($_[0])
!ll&&!rl&&r
011
MC/DC33%1/3
defined $_[0] and is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Engine")
defined $_[0] is_blessed_ref($_[0]) $_[0]->isa("Wasm::Wasmtime::Engine")
Truth table50%2/4
A and B and C
A= defined $_[0]B= is_blessed_ref($_[0])C= $_[0]->isa("Wasm::Wasmtime::Engine")
ABCresult
0XX0
10X0
1100
1111
102 2 my $serialized = Wasm::Wasmtime::ByteVec->new($_[0]);
103 2 my $ptr;
104 2 ▶ if(my $error = $xsub->($engine, $serialized, \$ptr))
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &$xsub($engine, $serialized, \$ptr)) 0 2 2
105 {
106 0 Carp::croak("error creating module: " . $error->message);
107 }
108 2 bless { ptr => $ptr, store => undef, engine => $engine }, $class;
109 22 });
110  
111  
112 $ffi->attach( [ wasmtime_module_validate => 'validate' ] => ['wasm_store_t', 'wasm_byte_vec_t*'] => 'wasmtime_error_t' => sub {
113 8 ▶ my $xsub = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
114 8 my $class = shift;
115 8 ▶ my $store = defined $_[0] && ref($_[0]) eq 'Wasm::Wasmtime::Store' ? shift : Wasm::Wasmtime::Store->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'Wasm::Wasmtime::Store' ? : 3 5 8
Condition66%2/3
defined $_[0] and ref $_[0] eq "Wasm::Wasmtime::Store"
!ll&&!rl&&r
053
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "Wasm::Wasmtime::Store"
defined $_[0] ref $_[0] eq "Wasm::Wasmtime::Store"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "Wasm::Wasmtime::Store"
ABresult
0X0
100
111
116 8 my($wasm, $data) = _args(@_);
117 8 my $error = $xsub->($store, $$wasm);
118 wantarray ## no critic (Community::Wantarray)
119 8 ▶ ? $error ? (0, $error->message) : (1, '')
Branch: 3 branches
BranchTrueFalseTotal
wantarray ? : 2 6 8
$error ? : 1 1 2
$error ? : 2 4 6
120 : $error ? 0 : 1;
121 22 });
122  
123  
124 sub exports
125 {
126 56 ▶ Wasm::Wasmtime::Module::Exports->new(shift);
Subroutine exports: called CC=1 SCAR=0.0
Pod documented
127 }
128  
129 $ffi->attach( [ exports => '_exports' ]=> [ 'wasm_module_t', 'wasm_exporttype_vec_t*' ] => sub {
130 95 ▶ my($xsub, $self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
131 95 my $exports = Wasm::Wasmtime::ExportTypeVec->new;
132 95 $xsub->($self, $exports);
133 95 $exports->to_list;
134 22 });
135  
136  
137 sub imports
138 {
139 43 ▶ Wasm::Wasmtime::Module::Imports->new(shift);
Subroutine imports: called CC=1 SCAR=0.0
Pod documented
140 }
141  
142 $ffi->attach( [ imports => '_imports' ] => [ 'wasm_module_t', 'wasm_importtype_vec_t*' ] => sub {
143 91 ▶ my($xsub, $self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
144 91 my $imports = Wasm::Wasmtime::ImportTypeVec->new;
145 91 $xsub->($self, $imports);
146 91 $imports->to_list;
147 22 });
148  
149  
150 $ffi->attach( [ 'wasmtime_module_serialize' => 'serialize' ] => [ 'wasm_module_t', 'wasm_byte_vec_t*' ] => 'wasmtime_error_t' => sub {
151 3 ▶ my($xsub, $self) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
152 3 my $s = Wasm::Wasmtime::ByteVec->new;
153 3 ▶ if(my $error = $xsub->($self, $s))
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &$xsub($self, $s)) { } 0 3 3
154 {
155 0 Carp::croak("error serializing module: " . $error->message);
156 }
157 else
158 {
159 3 my $s2 = $s->get;
160 3 $s->delete;
161 3 return $s2;
162 }
163 22 });
164  
165  
166 8 ▶sub engine { shift->{engine} }
Subroutine engine: called CC=1 SCAR=0.0
Pod documented
167  
168  
169 sub to_string
170 {
171 4 ▶ my($self) = @_;
Subroutine to_string: called CC=3 SCAR=11.0
Pod documented
172 4 my @externs = (@{ $self->imports }, @{ $self->exports });
173 4 ▶ return "(module)\n" unless @externs;
Branch: 1 branch
BranchTrueFalseTotal
unless @externs 3 1 4
174 1 my $string = "(module\n";
175 1 foreach my $extern (@externs)
176 {
177 1 $string .= " " . $extern->to_string . "\n";
178 }
179 1 $string .= ")\n";
180 }
181  
182 22 _generate_destroy();
183  
184 22 1;
185  
186 __END__
« blib/lib/Wasm/Wasmtime/TableType.pm ↑ Summary blib/lib/Wasm/Wasmtime/Func.pm »