| 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 2.00; | ||||||||||||||||||||||||||
|
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 Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 35 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Private class for Wasm::Wasmtime | ||||||||||||||||||||||||||||
| 18 | 35 | our $VERSION = '0.24'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 35 | our @EXPORT = qw( $ffi $ffi_prefix _generate_vec_class _generate_destroy ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub _lib | ||||||||||||||||||||||||||||
| 24 | 36 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_lib: called CC=8 SCAR=21.7
| |||||||||||||||||||||||||||||
| 25 | 36 | ▶ | return $ENV{WASM_WASMTIME_FFI} if defined $ENV{WASM_WASMTIME_FFI}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Sentinel symbols for a libwasmtime this distribution can actually use: the | ||||||||||||||||||||||||||||
| 28 | # modern store/context instance API plus the WAT, compiler and WASI features. | ||||||||||||||||||||||||||||
| 29 | # (This also rejects the feature-reduced "min" build shipped alongside the | ||||||||||||||||||||||||||||
| 30 | # standard one by Alien::wasmtime.) | ||||||||||||||||||||||||||||
| 31 | 36 | my @symbols = qw( | |||||||||||||||||||||||||||
| 32 | wasmtime_wat2wasm | ||||||||||||||||||||||||||||
| 33 | wasmtime_module_new | ||||||||||||||||||||||||||||
| 34 | wasmtime_linker_define_wasi | ||||||||||||||||||||||||||||
| 35 | wasmtime_instance_export_nth | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | 36 | my $lib = find_lib lib => 'wasmtime', symbol => \@symbols; | |||||||||||||||||||||||||||
| 38 | 36 | ▶ | return $lib if $lib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Fall back to Alien::wasmtime if it is installed (see the dynamic prereq in | ||||||||||||||||||||||||||||
| 41 | # dist.ini, which requires it when no system libwasmtime can be found). | ||||||||||||||||||||||||||||
| 42 | # Alien::wasmtime ships both a full library and a feature-reduced "min" build, | ||||||||||||||||||||||||||||
| 43 | # and ->dynamic_libs may list the "min" one first, so probe each candidate | ||||||||||||||||||||||||||||
| 44 | # ourselves and take the first that has every symbol we need. | ||||||||||||||||||||||||||||
| 45 | 36 | ▶ | if(eval { require Alien::wasmtime; 1 }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 36 | foreach my $candidate (Alien::wasmtime->dynamic_libs) | |||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | 36 | my $ok = eval { | |||||||||||||||||||||||||||
| 50 | 36 | my $probe = FFI::Platypus->new( api => 2, lib => $candidate ); | |||||||||||||||||||||||||||
| 51 | 36 | my $found = 1; | |||||||||||||||||||||||||||
| 52 | 36 | foreach my $sym (@symbols) | |||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 144 | ▶ | $found = 0, last unless defined $probe->find_symbol($sym); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 36 | $found; | |||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | 36 | ▶ | return $candidate if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | die "unable to locate a modern libwasmtime; set the WASM_WASMTIME_FFI " | |||||||||||||||||||||||||||
| 63 | . "environment variable to the full path of the wasmtime shared library " | ||||||||||||||||||||||||||||
| 64 | . "(developed against wasmtime 48.0.1)\n"; | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 35 | our $ffi_prefix = 'wasm_'; | |||||||||||||||||||||||||||
| 68 | 35 | our $ffi = FFI::Platypus->new( api => 2 ); | |||||||||||||||||||||||||||
| 69 | 35 | FFI::C->ffi($ffi); | |||||||||||||||||||||||||||
| 70 | 35 | $ffi->lib(__PACKAGE__->_lib); | |||||||||||||||||||||||||||
| 71 | 2536 | ▶ | $ffi->mangler(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 2536 | my $name = shift; | |||||||||||||||||||||||||||
| 73 | 2536 | ▶ | return $name if $name =~ /^(wasm|wasmtime|wasi)_/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1154 | return $ffi_prefix . $name; | |||||||||||||||||||||||||||
| 75 | 35 | }); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | { package Wasm::Wasmtime::Vec; | ||||||||||||||||||||||||||||
| 78 | 35 | ▶ | use FFI::Platypus::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 35 | record_layout_1( | |||||||||||||||||||||||||||
| 80 | $ffi, | ||||||||||||||||||||||||||||
| 81 | size_t => 'size', | ||||||||||||||||||||||||||||
| 82 | opaque => 'data', | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 35 | { package Wasm::Wasmtime::ByteVec; | |||||||||||||||||||||||||||
| 87 | 35 | ▶ | use base qw( Wasm::Wasmtime::Vec ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 35 | $ffi->type('record(Wasm::Wasmtime::ByteVec)' => 'wasm_byte_vec_t'); | |||||||||||||||||||||||||||
| 90 | 35 | $ffi_prefix = 'wasm_byte_vec_'; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub new | ||||||||||||||||||||||||||||
| 93 | 81 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 81 | my $class = shift; | |||||||||||||||||||||||||||
| 95 | 81 | ▶ | if(@_ == 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 1 | my($data, $size) = FFI::Platypus::Buffer::scalar_to_buffer($_[0]); | |||||||||||||||||||||||||||
| 98 | 1 | return $class->SUPER::new( | |||||||||||||||||||||||||||
| 99 | size => $size, | ||||||||||||||||||||||||||||
| 100 | data => $data, | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else | ||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 80 | return $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub get | ||||||||||||||||||||||||||||
| 110 | 305 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 305 | my($self) = @_; | |||||||||||||||||||||||||||
| 112 | 305 | FFI::Platypus::Buffer::buffer_to_scalar($self->data, $self->size); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 35 | $ffi->attach( delete => ['wasm_byte_vec_t*'] => 'void' ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub _generic_vec_delete | ||||||||||||||||||||||||||||
| 119 | 161 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_generic_vec_delete: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 161 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 121 | 161 | $xsub->($self); | |||||||||||||||||||||||||||
| 122 | # cannot use SUPER::DELETE because we aren't | ||||||||||||||||||||||||||||
| 123 | # in the right package. | ||||||||||||||||||||||||||||
| 124 | 161 | Wasm::Wasmtime::Vec::DESTROY($self); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _generate_vec_class | ||||||||||||||||||||||||||||
| 128 | 71 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_generate_vec_class: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | 71 | my %opts = @_; | |||||||||||||||||||||||||||
| 130 | 71 | my($class) = caller; | |||||||||||||||||||||||||||
| 131 | 71 | my $type = $class; | |||||||||||||||||||||||||||
| 132 | 71 | $type =~ s/^.*:://; | |||||||||||||||||||||||||||
| 133 | 71 | my $v_type = "wasm_@{[ lc $type ]}_vec_t"; | |||||||||||||||||||||||||||
| 134 | 71 | my $vclass = "Wasm::Wasmtime::${type}Vec"; | |||||||||||||||||||||||||||
| 135 | 71 | my $prefix = "wasm_@{[ lc $type ]}_vec"; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Sub::Install::install_sub({ | ||||||||||||||||||||||||||||
| 138 | 340 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 340 | my($self) = @_; | |||||||||||||||||||||||||||
| 140 | 340 | my $size = $self->size; | |||||||||||||||||||||||||||
| 141 | 340 | ▶ | return () if $size == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 212 | my $ptrs = $ffi->cast('opaque', "opaque[$size]", $self->data); | |||||||||||||||||||||||||||
| 143 | 212 | map { $class->new($_, $self) } @$ptrs; | |||||||||||||||||||||||||||
| 144 | }, | ||||||||||||||||||||||||||||
| 145 | 71 | into => $vclass, | |||||||||||||||||||||||||||
| 146 | as => 'to_list', | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 35 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 71 | @{join '::', $vclass, 'ISA'} = ('Wasm::Wasmtime::Vec'); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 71 | $ffi_prefix = "${prefix}_"; | |||||||||||||||||||||||||||
| 154 | 71 | $ffi->type("record($vclass)" => $v_type); | |||||||||||||||||||||||||||
| 155 | $ffi->attach( [ delete => join('::', $vclass, 'DESTROY') ] => ["$v_type*"] => \&_generic_vec_delete) | ||||||||||||||||||||||||||||
| 156 | 71 | ▶ | if !defined($opts{delete}) || $opts{delete}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $opts{"delete"} or $opts{"delete"}
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"}
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub _wrapper_destroy | ||||||||||||||||||||||||||||
| 161 | 760 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_wrapper_destroy: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 162 | 760 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 163 | 760 | ▶ | return if Devel::GlobalDestruction::in_global_destruction(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 760 | ▶ | if(defined $self->{ptr} && !defined $self->{owner}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"ptr"} and not defined $self->{"owner"}
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"}
| |||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||
| 166 | 171 | $xsub->($self->{ptr}); | |||||||||||||||||||||||||||
| 167 | 171 | delete $self->{ptr}; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # _generate_destroy( $c_delete_function_name ) | ||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||
| 173 | # Attaches a DESTROY method into the calling package that calls the named | ||||||||||||||||||||||||||||
| 174 | # wasmtime/wasm "delete" function on $self->{ptr} (unless the object is owned | ||||||||||||||||||||||||||||
| 175 | # by another object). For backwards compatibility, when no name is given the | ||||||||||||||||||||||||||||
| 176 | # mangled "${ffi_prefix}delete" symbol is used. | ||||||||||||||||||||||||||||
| 177 | sub _generate_destroy | ||||||||||||||||||||||||||||
| 178 | 307 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_generate_destroy: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 179 | 307 | my($fn) = @_; | |||||||||||||||||||||||||||
| 180 | 307 | my $caller = caller; | |||||||||||||||||||||||||||
| 181 | 307 | ▶ | $fn = 'delete' unless defined $fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 307 | $ffi->attach( [ $fn => join('::', $caller, 'DESTROY') ] => [ 'opaque' ] => 'void' => \&_wrapper_destroy); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | { package Wasm::Wasmtime::Error; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 35 | $ffi_prefix = 'wasmtime_error_'; | |||||||||||||||||||||||||||
| 188 | $ffi->custom_type( | ||||||||||||||||||||||||||||
| 189 | wasmtime_error_t => { | ||||||||||||||||||||||||||||
| 190 | native_type => 'opaque', | ||||||||||||||||||||||||||||
| 191 | 271 | ▶ | native_to_perl => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 192 | 271 | ▶ | defined $_[0] ? __PACKAGE__->new($_[0]) : undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | }, | ||||||||||||||||||||||||||||
| 194 | }, | ||||||||||||||||||||||||||||
| 195 | 35 | ); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Sub::Install::install_sub({ | ||||||||||||||||||||||||||||
| 198 | 10 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 199 | 10 | my($class, $ptr, $owner) = @_; | |||||||||||||||||||||||||||
| 200 | 10 | bless { | |||||||||||||||||||||||||||
| 201 | ptr => $ptr, | ||||||||||||||||||||||||||||
| 202 | owner => $owner, | ||||||||||||||||||||||||||||
| 203 | }, $class; | ||||||||||||||||||||||||||||
| 204 | }, | ||||||||||||||||||||||||||||
| 205 | 35 | into => __PACKAGE__, | |||||||||||||||||||||||||||
| 206 | as => 'new', | ||||||||||||||||||||||||||||
| 207 | }); | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 8 | ▶ | $ffi->attach( message => ['wasmtime_error_t','wasm_byte_vec_t*'] => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 210 | 8 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 211 | 8 | my $message = Wasm::Wasmtime::ByteVec->new; | |||||||||||||||||||||||||||
| 212 | 8 | $xsub->($self->{ptr}, $message); | |||||||||||||||||||||||||||
| 213 | 8 | my $ret = $message->get; | |||||||||||||||||||||||||||
| 214 | 8 | $message->delete; | |||||||||||||||||||||||||||
| 215 | 8 | $ret; | |||||||||||||||||||||||||||
| 216 | 35 | }); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # wasmtime_error_exit_status( error, int* ) -> bool | ||||||||||||||||||||||||||||
| 219 | 4 | ▶ | $ffi->attach( [ exit_status => 'exit_status' ] => ['wasmtime_error_t','int*'] => 'bool' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 220 | 4 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 221 | 4 | my $status; | |||||||||||||||||||||||||||
| 222 | 4 | ▶ | $xsub->($self->{ptr}, \$status) ? $status : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 35 | }); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 10 | ▶ | $ffi->attach( [ delete => "DESTROY" ] => ['wasmtime_error_t'] => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 226 | 10 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 227 | 10 | ▶ | if(defined $self->{ptr} && !defined $self->{owner}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"ptr"} and not defined $self->{"owner"}
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"}
| |||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | 10 | $xsub->($self->{ptr}); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 35 | }); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||
| 235 | # Modern Wasmtime "store handle" types. These are small plain-old-data | ||||||||||||||||||||||||||||
| 236 | # structures (a store id plus a private handle) that do not own anything and | ||||||||||||||||||||||||||||
| 237 | # have no destructor. Every operation on them requires a wasmtime_context_t. | ||||||||||||||||||||||||||||
| 238 | # | ||||||||||||||||||||||||||||
| 239 | # We model them with FFI::C structs of the exact C layout and always pass them | ||||||||||||||||||||||||||||
| 240 | # by pointer. The layouts below match the wasmtime C ABI from roughly v40 | ||||||||||||||||||||||||||||
| 241 | # onward (developed and tested against 48.0.1); older wasmtime releases used a | ||||||||||||||||||||||||||||
| 242 | # different in-memory representation and are not supported. | ||||||||||||||||||||||||||||
| 243 | # | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 35 | { package Wasm::Wasmtime::FuncData; | |||||||||||||||||||||||||||
| 246 | # typedef struct wasmtime_func { uint64_t store_id; void *__private; } | ||||||||||||||||||||||||||||
| 247 | 35 | FFI::C->struct( wasmtime_func_t => [ | |||||||||||||||||||||||||||
| 248 | store_id => 'uint64', | ||||||||||||||||||||||||||||
| 249 | _private => 'opaque', | ||||||||||||||||||||||||||||
| 250 | ]); | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 35 | { package Wasm::Wasmtime::MemoryData; | |||||||||||||||||||||||||||
| 254 | # typedef struct wasmtime_memory { | ||||||||||||||||||||||||||||
| 255 | # struct { uint64_t store_id; uint32_t __private1; }; // padded to 16 bytes | ||||||||||||||||||||||||||||
| 256 | # uint32_t __private2; // at offset 16 | ||||||||||||||||||||||||||||
| 257 | # }; // 24 bytes total | ||||||||||||||||||||||||||||
| 258 | 35 | FFI::C->struct( wasmtime_memory_t => [ | |||||||||||||||||||||||||||
| 259 | store_id => 'uint64', | ||||||||||||||||||||||||||||
| 260 | _p1 => 'uint32', | ||||||||||||||||||||||||||||
| 261 | _pad0 => 'uint32', | ||||||||||||||||||||||||||||
| 262 | _p2 => 'uint32', | ||||||||||||||||||||||||||||
| 263 | _pad1 => 'uint32', | ||||||||||||||||||||||||||||
| 264 | ]); | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 35 | { package Wasm::Wasmtime::TableData; | |||||||||||||||||||||||||||
| 268 | # same layout as wasmtime_memory_t (24 bytes) | ||||||||||||||||||||||||||||
| 269 | 35 | FFI::C->struct( wasmtime_table_t => [ | |||||||||||||||||||||||||||
| 270 | store_id => 'uint64', | ||||||||||||||||||||||||||||
| 271 | _p1 => 'uint32', | ||||||||||||||||||||||||||||
| 272 | _pad0 => 'uint32', | ||||||||||||||||||||||||||||
| 273 | _p2 => 'uint32', | ||||||||||||||||||||||||||||
| 274 | _pad1 => 'uint32', | ||||||||||||||||||||||||||||
| 275 | ]); | ||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 35 | { package Wasm::Wasmtime::GlobalData; | |||||||||||||||||||||||||||
| 279 | # typedef struct wasmtime_global { uint64_t store_id; uint32_t p1,p2,p3; } // 24 bytes | ||||||||||||||||||||||||||||
| 280 | 35 | FFI::C->struct( wasmtime_global_t => [ | |||||||||||||||||||||||||||
| 281 | store_id => 'uint64', | ||||||||||||||||||||||||||||
| 282 | _p1 => 'uint32', | ||||||||||||||||||||||||||||
| 283 | _p2 => 'uint32', | ||||||||||||||||||||||||||||
| 284 | _p3 => 'uint32', | ||||||||||||||||||||||||||||
| 285 | _pad0 => 'uint32', | ||||||||||||||||||||||||||||
| 286 | ]); | ||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 35 | { package Wasm::Wasmtime::InstanceData; | |||||||||||||||||||||||||||
| 290 | # typedef struct wasmtime_instance { uint64_t store_id; size_t __private; } | ||||||||||||||||||||||||||||
| 291 | 35 | FFI::C->struct( wasmtime_instance_t => [ | |||||||||||||||||||||||||||
| 292 | store_id => 'uint64', | ||||||||||||||||||||||||||||
| 293 | _private => 'size_t', | ||||||||||||||||||||||||||||
| 294 | ]); | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 35 | our %EXTERN_KIND = ( | |||||||||||||||||||||||||||
| 298 | 0 => 'func', | ||||||||||||||||||||||||||||
| 299 | 1 => 'global', | ||||||||||||||||||||||||||||
| 300 | 2 => 'table', | ||||||||||||||||||||||||||||
| 301 | 3 => 'memory', | ||||||||||||||||||||||||||||
| 302 | 4 => 'sharedmemory', | ||||||||||||||||||||||||||||
| 303 | 5 => 'tag', | ||||||||||||||||||||||||||||
| 304 | ); | ||||||||||||||||||||||||||||
| 305 | 35 | our %EXTERN_KIND_NUM = reverse %EXTERN_KIND; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | { package Wasm::Wasmtime::ExternUnion; | ||||||||||||||||||||||||||||
| 308 | 35 | FFI::C->union( wasmtime_extern_union_t => [ | |||||||||||||||||||||||||||
| 309 | func => 'wasmtime_func_t', | ||||||||||||||||||||||||||||
| 310 | global => 'wasmtime_global_t', | ||||||||||||||||||||||||||||
| 311 | table => 'wasmtime_table_t', | ||||||||||||||||||||||||||||
| 312 | memory => 'wasmtime_memory_t', | ||||||||||||||||||||||||||||
| 313 | ]); | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 35 | { package Wasm::Wasmtime::ExternData; | |||||||||||||||||||||||||||
| 317 | # typedef struct wasmtime_extern { uint8_t kind; wasmtime_extern_union_t of; } | ||||||||||||||||||||||||||||
| 318 | 35 | FFI::C->struct( wasmtime_extern_t => [ | |||||||||||||||||||||||||||
| 319 | kind => 'uint8', | ||||||||||||||||||||||||||||
| 320 | of => 'wasmtime_extern_union_t', | ||||||||||||||||||||||||||||
| 321 | ]); | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # Frees any owned resources held by an owned wasmtime_extern_t (as returned by | ||||||||||||||||||||||||||||
| 324 | # e.g. wasmtime_instance_export_get). Safe/harmless for func/global/table/ | ||||||||||||||||||||||||||||
| 325 | # memory kinds; the Perl-side struct memory is still freed by FFI::C. | ||||||||||||||||||||||||||||
| 326 | 35 | $ffi->attach( [ wasmtime_extern_delete => 'free' ] => ['wasmtime_extern_t'] => 'void' ); | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 35 | { package Wasm::Wasmtime::ExternArray; | |||||||||||||||||||||||||||
| 330 | 35 | FFI::C->array( wasmtime_extern_array_t => [ 'wasmtime_extern_t' ], { nullable => 1 } ); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # | ||||||||||||||||||||||||||||
| 334 | # wasmtime_val_t: a tagged union of WebAssembly values. Numbering here is the | ||||||||||||||||||||||||||||
| 335 | # wasmtime_valkind_t numbering, which differs from wasm_valkind_t (used by | ||||||||||||||||||||||||||||
| 336 | # Wasm::Wasmtime::ValType) for the reference types. | ||||||||||||||||||||||||||||
| 337 | # | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 35 | our %VAL_KIND = ( | |||||||||||||||||||||||||||
| 340 | 0 => 'i32', | ||||||||||||||||||||||||||||
| 341 | 1 => 'i64', | ||||||||||||||||||||||||||||
| 342 | 2 => 'f32', | ||||||||||||||||||||||||||||
| 343 | 3 => 'f64', | ||||||||||||||||||||||||||||
| 344 | 4 => 'v128', | ||||||||||||||||||||||||||||
| 345 | 5 => 'funcref', | ||||||||||||||||||||||||||||
| 346 | 6 => 'externref', | ||||||||||||||||||||||||||||
| 347 | 7 => 'anyref', | ||||||||||||||||||||||||||||
| 348 | ); | ||||||||||||||||||||||||||||
| 349 | 35 | our %VAL_KIND_NUM = reverse %VAL_KIND; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | { package Wasm::Wasmtime::ValUnion; | ||||||||||||||||||||||||||||
| 352 | 35 | FFI::C->union( wasmtime_valunion_t => [ | |||||||||||||||||||||||||||
| 353 | i32 => 'sint32', | ||||||||||||||||||||||||||||
| 354 | i64 => 'sint64', | ||||||||||||||||||||||||||||
| 355 | f32 => 'float', | ||||||||||||||||||||||||||||
| 356 | f64 => 'double', | ||||||||||||||||||||||||||||
| 357 | funcref => 'uint64[2]', | ||||||||||||||||||||||||||||
| 358 | v128 => 'uint8[16]', | ||||||||||||||||||||||||||||
| 359 | _pad => 'uint8[24]', | ||||||||||||||||||||||||||||
| 360 | ]); | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 35 | { package Wasm::Wasmtime::Val; | |||||||||||||||||||||||||||
| 364 | 35 | FFI::C->struct( wasmtime_val_t => [ | |||||||||||||||||||||||||||
| 365 | kind => 'uint8', | ||||||||||||||||||||||||||||
| 366 | of => 'wasmtime_valunion_t', | ||||||||||||||||||||||||||||
| 367 | ]); | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | sub to_perl | ||||||||||||||||||||||||||||
| 370 | 59 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_perl: called CC=6 SCAR=23.5
| |||||||||||||||||||||||||||||
| 371 | 59 | my $self = shift; | |||||||||||||||||||||||||||
| 372 | 59 | my $kind = $VAL_KIND{$self->kind}; | |||||||||||||||||||||||||||
| 373 | 59 | ▶ | return undef unless defined $kind; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 59 | ▶ | return $self->of->i32 if $kind eq 'i32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 12 | ▶ | return $self->of->i64 if $kind eq 'i64'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | ▶ | return $self->of->f32 if $kind eq 'f32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | ▶ | return $self->of->f64 if $kind eq 'f64'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | # reference / vector types not (yet) marshalled into Perl space | ||||||||||||||||||||||||||||
| 379 | 0 | return undef; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # Wasm::Wasmtime::Val->from_perl($kind_string, $value) -> Wasm::Wasmtime::Val | ||||||||||||||||||||||||||||
| 383 | sub from_perl | ||||||||||||||||||||||||||||
| 384 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
from_perl: called CC=6 SCAR=24.7
| |||||||||||||||||||||||||||||
| 385 | 4 | my($class, $kind, $value) = @_; | |||||||||||||||||||||||||||
| 386 | 4 | my $num = $VAL_KIND_NUM{$kind}; | |||||||||||||||||||||||||||
| 387 | 4 | ▶ | Carp::croak("cannot marshal WebAssembly value of type $kind") unless defined $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 4 | my $self = $class->new; | |||||||||||||||||||||||||||
| 389 | 4 | $self->kind($num); | |||||||||||||||||||||||||||
| 390 | 4 | ▶ | if($kind eq 'i32') { $self->of->i32($value) } | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 391 | 0 | elsif($kind eq 'i64') { $self->of->i64($value) } | |||||||||||||||||||||||||||
| 392 | 0 | elsif($kind eq 'f32') { $self->of->f32($value) } | |||||||||||||||||||||||||||
| 393 | 0 | elsif($kind eq 'f64') { $self->of->f64($value) } | |||||||||||||||||||||||||||
| 394 | 0 | else { Carp::croak("cannot marshal WebAssembly value of type $kind") } | |||||||||||||||||||||||||||
| 395 | 4 | $self; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 35 | { package Wasm::Wasmtime::ValArray; | |||||||||||||||||||||||||||
| 400 | 35 | FFI::C->array( wasmtime_val_array_t => [ 'wasmtime_val_t' ], { nullable => 1 } ); | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | sub to_perl | ||||||||||||||||||||||||||||
| 403 | 37 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_perl: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 404 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 405 | 37 | map { $_->to_perl } @$self; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # from_perl(\@values, \@valtypes) where @valtypes are Wasm::Wasmtime::ValType | ||||||||||||||||||||||||||||
| 409 | sub from_perl | ||||||||||||||||||||||||||||
| 410 | 31 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
from_perl: called CC=8 SCAR=23.9
| |||||||||||||||||||||||||||||
| 411 | 31 | my($class, $vals, $types) = @_; | |||||||||||||||||||||||||||
| 412 | 31 | ▶ | return undef unless @$types; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 31 | my $array = $class->new(scalar @$types); | |||||||||||||||||||||||||||
| 414 | 31 | foreach my $i (0..$#$types) | |||||||||||||||||||||||||||
| 415 | { | ||||||||||||||||||||||||||||
| 416 | 70 | my $kind = $types->[$i]->kind; | |||||||||||||||||||||||||||
| 417 | 70 | my $num = $VAL_KIND_NUM{$kind}; | |||||||||||||||||||||||||||
| 418 | 70 | ▶ | Carp::croak("cannot marshal value of type $kind") unless defined $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 70 | my $slot = $array->[$i]; | |||||||||||||||||||||||||||
| 420 | 70 | $slot->kind($num); | |||||||||||||||||||||||||||
| 421 | 70 | ▶ | if($kind eq 'i32') { $slot->of->i32($vals->[$i]) } | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 422 | 14 | elsif($kind eq 'i64') { $slot->of->i64($vals->[$i]) } | |||||||||||||||||||||||||||
| 423 | 0 | elsif($kind eq 'f32') { $slot->of->f32($vals->[$i]) } | |||||||||||||||||||||||||||
| 424 | 0 | elsif($kind eq 'f64') { $slot->of->f64($vals->[$i]) } | |||||||||||||||||||||||||||
| 425 | 0 | else { Carp::croak("cannot marshal value of type $kind") } | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | 31 | $array; | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 35 | 1; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | __END__ | ||||||||||||||||||||||||||||