blib/lib/Wasm/Wasmtime/FFI.pm

filter: Statementstmt 99.3% 154 / 155 Branchbran 82.1% 23 / 28 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 32 / 32 Podpod n/a totaltotal 94.7% 218 / 230 CC 18 SCAR 7.6
CC
18
Cov
95%
_lib
4.5
_wrapper_destroy
4.0
__ANON__
3.5
CRAP 2.1
« blib/lib/Wasm/Wasmtime/Config.pm ↑ Summary blib/lib/Wasm/Wasmtime/Engine.pm »
1 package Wasm::Wasmtime::FFI;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use FFI::C 0.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use FFI::C::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use FFI::Platypus 1.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use FFI::Platypus::Buffer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use FFI::CheckLib 0.26 qw( find_lib );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Devel::GlobalDestruction ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use constant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: Private class for Wasm::Wasmtime
17 35 our $VERSION = '0.23'; # VERSION
18  
19  
20 35 our @EXPORT = qw( $ffi $ffi_prefix _generate_vec_class _generate_destroy );
21  
22 sub _lib
23 {
24 35 ▶ return $ENV{WASM_WASMTIME_FFI} if defined $ENV{WASM_WASMTIME_FFI};
Subroutine _lib: called CC=4 SCAR=15.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"WASM_WASMTIME_FFI"} 0 35 35
25 35 my @symbols = (
26 # 0.19.0
27 'wasmtime_func_as_funcref',
28 # 0.20.0 / 0.21.0
29 'wasmtime_module_serialize',
30 'wasmtime_module_deserialize',
31 'wasmtime_store_gc',
32 ## 0.23.0
33 'wasmtime_config_consume_fuel_set',
34 #'wasmtime_config_max_instances_set', # removed in 0.27.0
35 );
36 35 my $lib = find_lib lib => 'wasmtime', symbol => \@symbols;
37 35 ▶ return $lib if $lib;
Branch: 1 branch
BranchTrueFalseTotal
if $lib 0 35 35
38 35 $lib = find_lib lib => 'wasmtime', alien => 'Alien::wasmtime', symbol => \@symbols;
39 35 ▶ return $lib if $lib;
Branch: 1 branch
BranchTrueFalseTotal
if $lib 35 0 35
40 0 die 'unable to find wasmtime 0.19.0 or better';
41 }
42  
43 35 our $ffi_prefix = 'wasm_';
44 35 our $ffi = FFI::Platypus->new( api => 1 );
45 35 FFI::C->ffi($ffi);
46 35 $ffi->lib(__PACKAGE__->_lib);
47 $ffi->mangler(sub {
48 2617 ▶ my $name = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
49 2617 ▶ return $name if $name =~ /^(wasm|wasmtime|wasi)_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(wasm|wasmtime|wasi)_/ 842 1775 2617
50 1775 return $ffi_prefix . $name;
51 35 });
52  
53 { package Wasm::Wasmtime::Vec;
54 35 ▶ use FFI::Platypus::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 35 record_layout_1(
56 $ffi,
57 size_t => 'size',
58 opaque => 'data',
59 );
60 }
61  
62 35 { package Wasm::Wasmtime::ByteVec;
63 35 ▶ use base qw( Wasm::Wasmtime::Vec );
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 35 $ffi->type('record(Wasm::Wasmtime::ByteVec)' => 'wasm_byte_vec_t');
66 35 $ffi_prefix = 'wasm_byte_vec_';
67  
68 sub new
69 {
70 247 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
71 247 ▶ if(@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 169 78 247
72 {
73 169 my($data, $size) = FFI::Platypus::Buffer::scalar_to_buffer($_[0]);
74 169 return $class->SUPER::new(
75 size => $size,
76 data => $data,
77 );
78 }
79 else
80 {
81 78 return $class->SUPER::new(@_);
82 }
83 }
84  
85 sub get
86 {
87 303 ▶ my($self) = @_;
Subroutine get: called CC=1 SCAR=0.0
88 303 FFI::Platypus::Buffer::buffer_to_scalar($self->data, $self->size);
89 }
90  
91 35 $ffi->attach( delete => ['wasm_byte_vec_t*'] => 'void' );
92 }
93  
94 sub _generic_vec_delete
95 {
96 180 ▶ my($xsub, $self) = @_;
Subroutine _generic_vec_delete: called CC=1 SCAR=0.0
97 180 $xsub->($self);
98 # cannot use SUPER::DELETE because we aren't
99 # in the right package.
100 180 Wasm::Wasmtime::Vec::DESTROY($self);
101 }
102  
103 sub _generate_vec_class
104 {
105 87 ▶ my %opts = @_;
Subroutine _generate_vec_class: called CC=3 SCAR=11.0
106 87 my($class) = caller;
107 87 my $type = $class;
108 87 $type =~ s/^.*:://;
109 87 my $v_type = "wasm_@{[ lc $type ]}_vec_t";
110 87 my $vclass = "Wasm::Wasmtime::${type}Vec";
111 87 my $prefix = "wasm_@{[ lc $type ]}_vec";
112  
113 Sub::Install::install_sub({
114 code => sub {
115 334 ▶ my($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
116 334 my $size = $self->size;
117 334 ▶ return () if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 116 218 334
118 218 my $ptrs = $ffi->cast('opaque', "opaque[$size]", $self->data);
119 218 map { $class->new($_, $self) } @$ptrs;
120 },
121 87 into => $vclass,
122 as => 'to_list',
123 });
124  
125 {
126 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 87 @{join '::', $vclass, 'ISA'} = ('Wasm::Wasmtime::Vec');
128 }
129 87 $ffi_prefix = "${prefix}_";
130 87 $ffi->type("record($vclass)" => $v_type);
131 $ffi->attach( [ delete => join('::', $vclass, 'DESTROY') ] => ["$v_type*"] => \&_generic_vec_delete)
132 87 ▶ if !defined($opts{delete}) || $opts{delete};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opts{"delete"} or $opts{"delete"} 60 27 87
Condition66%2/3
not defined $opts{"delete"} or $opts{"delete"}
l!l&&r!l&&!r
60027
MC/DC50%1/2
not defined $opts{"delete"} or $opts{"delete"}
not defined $opts{"delete"} $opts{"delete"}
Truth table66%2/3
not(A) or B
A= not defined $opts{"delete"}B= $opts{"delete"}
ABresult
1X1
011
000
133  
134 }
135  
136 sub _wrapper_destroy
137 {
138 836 ▶ my($xsub, $self) = @_;
Subroutine _wrapper_destroy: called CC=4 SCAR=13.9
139 836 ▶ return if Devel::GlobalDestruction::in_global_destruction();
Branch: 1 branch
BranchTrueFalseTotal
if &Devel::GlobalDestruction::in_global_destruction() 0 836 836
140 836 ▶ if(defined $self->{ptr} && !defined $self->{owner})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ptr"} and not defined $self->{"owner"}) 193 643 836
Condition100%3/3
defined $self->{"ptr"} and not defined $self->{"owner"}
!ll&&!rl&&r
82561193
MC/DC100%2/2
defined $self->{"ptr"} and not defined $self->{"owner"}
defined $self->{"ptr"} not defined $self->{"owner"}
Truth table100%3/3
A and not(B)
A= defined $self->{"ptr"}B= not defined $self->{"owner"}
ABresult
0X0
100
111
141 {
142 193 $xsub->($self);
143 193 delete $self->{ptr};
144 }
145 }
146  
147 sub _generate_destroy
148 {
149 401 ▶ my $caller = caller;
Subroutine _generate_destroy: called CC=3 SCAR=11.0
150 401 my $type = lc $caller;
151 401 ▶ if($type =~ /::linker$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /::linker$/) { } 9 392 401
elsif ($type =~ /::wasi/) { } 21 371 392
152 {
153 9 $type = 'wasmtime_linker_t';
154 }
155 elsif($type =~ /::wasi/)
156 {
157 21 $type =~ s/^.*::wasi(.*)$/wasi_${1}_t/g;
158 }
159 else
160 {
161 371 $type =~ s/^.*:://;
162 371 $type = "wasm_${type}_t";
163 }
164 401 $ffi->attach( [ delete => join('::', $caller, 'DESTROY') ] => [ $type ] => \&_wrapper_destroy);
165 }
166  
167 { package Wasm::Wasmtime::Error;
168  
169 35 $ffi_prefix = 'wasmtime_error_';
170 $ffi->custom_type(
171 wasmtime_error_t => {
172 native_type => 'opaque',
173 native_to_perl => sub {
174 204 ▶ defined $_[0] ? __PACKAGE__->new($_[0]) : undef
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 8 196 204
175 },
176 },
177 35 );
178  
179 Sub::Install::install_sub({
180 code => sub {
181 8 ▶ my($class, $ptr, $owner) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
182 8 bless {
183 ptr => $ptr,
184 owner => $owner,
185 }, $class;
186 },
187 35 into => __PACKAGE__,
188 as => 'new',
189 });
190  
191 $ffi->attach( message => ['wasmtime_error_t','wasm_byte_vec_t*'] => sub {
192 6 ▶ my($xsub, $self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
193 6 my $message = Wasm::Wasmtime::ByteVec->new;
194 6 $xsub->($self->{ptr}, $message);
195 6 my $ret = $message->get;
196 6 $message->delete;
197 6 $ret;
198 35 });
199  
200 $ffi->attach( [ delete => "DESTROY" ] => ['wasmtime_error_t'] => sub {
201 8 ▶ my($xsub, $self) = @_;
Subroutine __ANON__: called CC=3 SCAR=12.5
202 8 ▶ if(defined $self->{ptr} && !defined $self->{owner})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ptr"} and not defined $self->{"owner"}) 8 0 8
Condition33%1/3
defined $self->{"ptr"} and not defined $self->{"owner"}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $self->{"ptr"} and not defined $self->{"owner"}
defined $self->{"ptr"} not defined $self->{"owner"}
Truth table33%1/3
A and not(B)
A= defined $self->{"ptr"}B= not defined $self->{"owner"}
ABresult
0X0
100
111
203 {
204 8 $xsub->($self->{ptr});
205 }
206 35 });
207 }
208  
209 35 { package Wasm::Wasmtime::Val::Of;
210 35 FFI::C->union(of_t => [
211 i32 => 'sint32',
212 i64 => 'sint64',
213 f32 => 'float',
214 f64 => 'double',
215 anyref => 'opaque',
216 funcref => 'opaque',
217 ]);
218 }
219  
220 35 my %kind = (
221 0 => 'i32',
222 1 => 'i64',
223 2 => 'f32',
224 3 => 'f64',
225 128 => 'anyref',
226 129 => 'funcref',
227 );
228  
229 { package Wasm::Wasmtime::Val;
230 35 FFI::C->struct(wasm_val_t => [
231 kind => 'uint8',
232 of => 'of_t',
233 ]);
234  
235 sub to_perl
236 {
237 59 ▶ my $self = shift;
Subroutine to_perl: called CC=1 SCAR=0.0
238 59 my $kind = $kind{$self->kind};
239 59 $self->of->$kind;
240 }
241 }
242  
243 35 { package Wasm::Wasmtime::ValVec;
244 35 FFI::C->array(wasm_val_vec_t => [
245 'wasm_val_t',
246 ], { nullable => 1 });
247  
248 sub to_perl
249 {
250 38 ▶ my $self = shift;
Subroutine to_perl: called CC=1 SCAR=0.0
251 38 map { $_->to_perl } @$self
252 }
253  
254 {
255 35 package Wasm::Wasmtime::ValVecWrapper;
256 35 FFI::C->struct(wasm_val_vec_wrapper_t => [
257 size => 'size_t',
258 data => 'opaque',
259 ]);
260  
261 }
262  
263 $ffi->attach_cast('from_c', 'opaque', 'wasm_val_vec_wrapper_t', sub {
264 2 ▶ my($xsub, undef, $ptr) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
265 2 my $wrapper = $xsub->($ptr);
266 2 my $inner = $ffi->cast('opaque', 'wasm_val_vec_t', $wrapper->data);
267 2 FFI::C::Util::set_array_count($inner, $wrapper->size);
268 2 return $inner;
269 35 });
270  
271 sub from_perl
272 {
273 53 ▶ my($class, $vals, $types) = @_;
Subroutine from_perl: called CC=2 SCAR=6.9
274 53 ▶ @$vals ? $class->new([map { { kind => $_->kind_num, of => { $_->kind => shift @$vals } } } @$types]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$vals ? : 31 22 53
275 }
276 }
277  
278 35 1;
279  
280 __END__
« blib/lib/Wasm/Wasmtime/Config.pm ↑ Summary blib/lib/Wasm/Wasmtime/Engine.pm »