| 1 | package Wasm::Wasmtime::Func; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use 5.008004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use base qw( Wasm::Wasmtime::Extern ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Ref::Util qw( is_blessed_ref is_plain_arrayref ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Wasm::Wasmtime::FFI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Wasm::Wasmtime::FuncType; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use Wasm::Wasmtime::Trap; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use FFI::C::Util qw( addressof set_array_count ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 16 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 16 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 16 | ▶ | use constant is_func => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 16 | ▶ | use constant kind => 'func'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | use overload | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | '&{}' => sub { my $self = shift; sub { $self->call(@_) } }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 16 | fallback => 1; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # ABSTRACT: Wasmtime function class | ||||||||||||||||||||||||||||
| 22 | 16 | our $VERSION = '0.24'; # VERSION | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | my $val_array_from_ptr = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 26 | 2 | my($ptr, $n) = @_; | |||||||||||||||||||||||||||
| 27 | 2 | ▶ | return undef unless $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 2 | my $array = $ffi->cast('opaque', 'wasmtime_val_array_t', $ptr); | |||||||||||||||||||||||||||
| 29 | 2 | set_array_count($array, $n); | |||||||||||||||||||||||||||
| 30 | 2 | $array; | |||||||||||||||||||||||||||
| 31 | 16 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $set_val = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=25.7
| |||||||||||||||||||||||||||||
| 34 | 1 | my($slot, $kind, $value) = @_; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | if($kind eq 'i32') { $slot->of->i32($value) } | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 36 | 0 | elsif($kind eq 'i64') { $slot->of->i64($value) } | |||||||||||||||||||||||||||
| 37 | 0 | elsif($kind eq 'f32') { $slot->of->f32($value) } | |||||||||||||||||||||||||||
| 38 | 0 | elsif($kind eq 'f64') { $slot->of->f64($value) } | |||||||||||||||||||||||||||
| 39 | 0 | else { Carp::croak("cannot marshal WebAssembly value of type $kind") } | |||||||||||||||||||||||||||
| 40 | 1 | $slot->kind($Wasm::Wasmtime::FFI::VAL_KIND_NUM{$kind}); | |||||||||||||||||||||||||||
| 41 | 16 | }; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 11 | ▶ | $ffi->attach( [ wasmtime_func_new => 'new' ] => ['opaque', 'wasm_functype_t', '(opaque,opaque,opaque,size_t,opaque,size_t)->opaque', 'opaque', 'opaque', 'wasmtime_func_t'] => 'void' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 45 | 11 | my $xsub = shift; | |||||||||||||||||||||||||||
| 46 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 11 | ▶ | Carp::croak("Wasm::Wasmtime::Func->new requires a Wasm::Wasmtime::Store") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Store")
MC/DC0%0/2
is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Store")
is_blessed_ref($_[0])
$_[0]->isa("Wasm::Wasmtime::Store")
Truth table33%1/3
A and B
A= is_blessed_ref($_[0])B= $_[0]->isa("Wasm::Wasmtime::Store")
| |||||||||||||||||||||||||||||
| 49 | unless is_blessed_ref($_[0]) && $_[0]->isa('Wasm::Wasmtime::Store'); | ||||||||||||||||||||||||||||
| 50 | 11 | my $store = shift; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 11 | ▶ | my($functype, $cb) = is_plain_arrayref($_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | ? (Wasm::Wasmtime::FuncType->new($_[0], $_[1]), $_[2]) | ||||||||||||||||||||||||||||
| 54 | : @_; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 11 | my @result_types = $functype->results; | |||||||||||||||||||||||||||
| 57 | 11 | my $param_arity = scalar $functype->params; | |||||||||||||||||||||||||||
| 58 | 11 | my $result_arity = scalar @result_types; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 11 | require Wasm::Wasmtime::Caller; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 11 | ▶ | my $closure = $ffi->closure(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 63 | 11 | my(undef, $caller_ptr, $args_ptr, $nargs, $res_ptr, $nres) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 11 | my $caller = Wasm::Wasmtime::Caller->new($caller_ptr, $store); | |||||||||||||||||||||||||||
| 66 | 11 | unshift @Wasm::Wasmtime::Caller::callers, $caller; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 11 | my @args; | |||||||||||||||||||||||||||
| 69 | 11 | ▶ | if($nargs) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 1 | my $array = $val_array_from_ptr->($args_ptr, $nargs); | |||||||||||||||||||||||||||
| 72 | 1 | @args = map { $_->to_perl } @$array; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 11 | local $@ = ''; | |||||||||||||||||||||||||||
| 76 | 11 | my @ret = eval { $cb->(@args) }; | |||||||||||||||||||||||||||
| 77 | 11 | ▶ | if(my $error = $@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 2 | shift @Wasm::Wasmtime::Caller::callers; | |||||||||||||||||||||||||||
| 80 | 2 | delete $caller->{ptr}; | |||||||||||||||||||||||||||
| 81 | 2 | ▶ | my $trap = is_blessed_ref($error) && $error->isa('Wasm::Wasmtime::Trap') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
is_blessed_ref($error) and $error->isa("Wasm::Wasmtime::Trap")
MC/DC50%1/2
is_blessed_ref($error) and $error->isa("Wasm::Wasmtime::Trap")
is_blessed_ref($error)
$error->isa("Wasm::Wasmtime::Trap")
Truth table66%2/3
A and B
A= is_blessed_ref($error)B= $error->isa("Wasm::Wasmtime::Trap")
| |||||||||||||||||||||||||||||
| 82 | ? $error | ||||||||||||||||||||||||||||
| 83 | : Wasm::Wasmtime::Trap->new("$error"); | ||||||||||||||||||||||||||||
| 84 | 2 | return delete $trap->{ptr}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 9 | ▶ | if($nres) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 1 | my $array = $val_array_from_ptr->($res_ptr, $nres); | |||||||||||||||||||||||||||
| 90 | 1 | foreach my $i (0..$nres-1) | |||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 1 | $set_val->($array->[$i], $result_types[$i]->kind, $ret[$i]); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 9 | shift @Wasm::Wasmtime::Caller::callers; | |||||||||||||||||||||||||||
| 97 | 9 | delete $caller->{ptr}; | |||||||||||||||||||||||||||
| 98 | 9 | return undef; | |||||||||||||||||||||||||||
| 99 | 11 | }); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 11 | my $data = Wasm::Wasmtime::FuncData->new; | |||||||||||||||||||||||||||
| 102 | 11 | $xsub->($store->context, $functype, $closure, undef, undef, $data); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 11 | bless { | |||||||||||||||||||||||||||
| 105 | data => $data, | ||||||||||||||||||||||||||||
| 106 | store => $store, | ||||||||||||||||||||||||||||
| 107 | closure => $closure, | ||||||||||||||||||||||||||||
| 108 | functype => $functype, | ||||||||||||||||||||||||||||
| 109 | }, $class; | ||||||||||||||||||||||||||||
| 110 | 16 | }); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 53 | ▶ | $ffi->attach( [ wasmtime_func_call => 'call' ] => ['opaque', 'wasmtime_func_t', 'opaque', 'size_t', 'opaque', 'size_t', 'opaque*'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 114 | 53 | my $xsub = shift; | |||||||||||||||||||||||||||
| 115 | 53 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 53 | my $functype = $self->type; | |||||||||||||||||||||||||||
| 118 | 53 | my @param_types = $functype->params; | |||||||||||||||||||||||||||
| 119 | 53 | my @result_types = $functype->results; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 53 | ▶ | my $args = @param_types | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | ? Wasm::Wasmtime::ValArray->from_perl([@_], \@param_types) | ||||||||||||||||||||||||||||
| 123 | : undef; | ||||||||||||||||||||||||||||
| 124 | 53 | ▶ | my $results = @result_types | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | ? Wasm::Wasmtime::ValArray->new(scalar @result_types) | ||||||||||||||||||||||||||||
| 126 | : undef; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 53 | my $trap; | |||||||||||||||||||||||||||
| 129 | my $error = $xsub->( | ||||||||||||||||||||||||||||
| 130 | $self->context, $self->{data}, | ||||||||||||||||||||||||||||
| 131 | 53 | ▶ | $args ? addressof($args) : undef, scalar @param_types, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | $results ? addressof($results) : undef, scalar @result_types, | ||||||||||||||||||||||||||||
| 133 | \$trap, | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # A host function invoked directly returns its failure as an error rather | ||||||||||||||||||||||||||||
| 137 | # than via the trap out-parameter; surface both as a trap for consistency | ||||||||||||||||||||||||||||
| 138 | # with how WebAssembly-side traps are reported. | ||||||||||||||||||||||||||||
| 139 | 53 | ▶ | die Wasm::Wasmtime::Trap->from_error($error) if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 50 | ▶ | die Wasm::Wasmtime::Trap->new($trap) if $trap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 50 | ▶ | return unless defined $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 37 | my @out = $results->to_perl; | |||||||||||||||||||||||||||
| 144 | 37 | ▶ | wantarray ? @out : $out[0]; ## no critic (Community::Wantarray) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 16 | }); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub attach | ||||||||||||||||||||||||||||
| 149 | 30 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
attach: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 30 | ▶ | my $package = @_ == 2 ? shift : caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 30 | my $name = shift; | |||||||||||||||||||||||||||
| 153 | 30 | ▶ | if($package->can($name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 1 | Carp::carp("attaching ${package}::$name replaces existing subroutine"); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | Sub::Install::reinstall_sub({ | ||||||||||||||||||||||||||||
| 158 | 32 | ▶ | code => sub { $self->call(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 30 | into => $package, | |||||||||||||||||||||||||||
| 160 | as => $name, | ||||||||||||||||||||||||||||
| 161 | }); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 37 | ▶ | $ffi->attach( [ wasmtime_func_type => '_type' ] => ['opaque', 'wasmtime_func_t'] => 'wasm_functype_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | 37 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 167 | 37 | $xsub->($self->context, $self->{data}); | |||||||||||||||||||||||||||
| 168 | 16 | }); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub type | ||||||||||||||||||||||||||||
| 171 | 66 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
type: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 66 | my($self) = @_; | |||||||||||||||||||||||||||
| 173 | 66 | ▶ | $self->{functype} ||= $self->_type; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"functype"} ||= $self->_type
MC/DC0%0/2
$self->{"functype"} ||= $self->_type
$self->{"functype"}
$self->_type
Truth table66%2/3
A ||= B
A= $self->{"functype"}B= $self->_type
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 3 | ▶ | sub param_arity { scalar shift->type->params } | ||||||||||||||||||||||||||
|
Subroutine
param_arity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 3 | ▶ | sub result_arity { scalar shift->type->results } | ||||||||||||||||||||||||||
|
Subroutine
result_arity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 16 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||