| 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( set_array_count addressof ); | ||||||||||||||||||||||||||
|
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
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 16 | ▶ | fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | ; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # ABSTRACT: Wasmtime function class | ||||||||||||||||||||||||||||
| 23 | 16 | our $VERSION = '0.23'; # VERSION | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 16 | $ffi_prefix = 'wasm_func_'; | |||||||||||||||||||||||||||
| 27 | 16 | $ffi->load_custom_type('::PtrObject' => 'wasm_func_t' => __PACKAGE__); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | $ffi->attach( [ wasmtime_func_new => 'new' ] => ['wasm_store_t', 'wasm_functype_t', '(opaque,opaque,opaque)->opaque'] => 'wasm_func_t' => sub { | ||||||||||||||||||||||||||||
| 31 | 12 | ▶ | my $xsub = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 32 | 12 | my $class = shift; | |||||||||||||||||||||||||||
| 33 | 12 | ▶ | if(is_blessed_ref $_[0] && $_[0]->isa('Wasm::Wasmtime::Store')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Store")
MC/DC50%1/2
is_blessed_ref($_[0]) and $_[0]->isa("Wasm::Wasmtime::Store")
is_blessed_ref($_[0])
$_[0]->isa("Wasm::Wasmtime::Store")
Truth table66%2/3
A and B
A= is_blessed_ref($_[0])B= $_[0]->isa("Wasm::Wasmtime::Store")
| |||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||
| 35 | 11 | my $store = shift; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | my($functype, $cb) = is_plain_arrayref($_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | ? (Wasm::Wasmtime::FuncType->new($_[0], $_[1]), $_[2]) | ||||||||||||||||||||||||||||
| 38 | : @_; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | my $param_arity = scalar $functype->params; | |||||||||||||||||||||||||||
| 41 | 11 | my $result_arity = scalar$functype->results; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 11 | require Wasm::Wasmtime::Caller; | |||||||||||||||||||||||||||
| 44 | my $wrapper = $ffi->closure(sub { | ||||||||||||||||||||||||||||
| 45 | 11 | ▶ | my($caller, $params, $results) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 46 | 11 | $caller = Wasm::Wasmtime::Caller->new($caller); | |||||||||||||||||||||||||||
| 47 | 11 | unshift @Wasm::Wasmtime::Caller::callers, $caller; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 11 | ▶ | my @args = $param_arity ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | my $args = Wasm::Wasmtime::ValVec->from_c($params); | |||||||||||||||||||||||||||
| 51 | 1 | $args->to_perl; | |||||||||||||||||||||||||||
| 52 | } : (); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 11 | local $@ = ''; | |||||||||||||||||||||||||||
| 55 | 11 | my @ret = eval { | |||||||||||||||||||||||||||
| 56 | 11 | $cb->(@args); | |||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | 11 | ▶ | if(my $error = $@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 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")
| |||||||||||||||||||||||||||||
| 61 | ? $error | ||||||||||||||||||||||||||||
| 62 | : Wasm::Wasmtime::Trap->new($store, "$error\0"); | ||||||||||||||||||||||||||||
| 63 | 2 | delete $caller->{ptr}; | |||||||||||||||||||||||||||
| 64 | 2 | shift @Wasm::Wasmtime::Caller::callers; | |||||||||||||||||||||||||||
| 65 | 2 | return delete $trap->{ptr}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else | ||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 9 | ▶ | if($result_arity) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 1 | $results = Wasm::Wasmtime::ValVec->from_c($results); | |||||||||||||||||||||||||||
| 72 | 1 | my @types = $functype->results; | |||||||||||||||||||||||||||
| 73 | 1 | foreach my $i (0..$#types) | |||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 1 | my $kind = $types[$i]->kind; | |||||||||||||||||||||||||||
| 76 | 1 | my $result = $results->get($i); | |||||||||||||||||||||||||||
| 77 | 1 | $result->kind($types[$i]->kind_num); | |||||||||||||||||||||||||||
| 78 | 1 | $result->of->$kind(shift @ret); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 9 | delete $caller->{ptr}; | |||||||||||||||||||||||||||
| 82 | 9 | shift @Wasm::Wasmtime::Caller::callers; | |||||||||||||||||||||||||||
| 83 | 9 | return undef; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 11 | }); | |||||||||||||||||||||||||||
| 86 | 11 | my $self = $xsub->($store, $functype, $wrapper); | |||||||||||||||||||||||||||
| 87 | 11 | $self->{store} = $store; | |||||||||||||||||||||||||||
| 88 | 11 | $self->{wrapper} = $wrapper; | |||||||||||||||||||||||||||
| 89 | 11 | return $self; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 1 | my ($ptr, $owner) = @_; | |||||||||||||||||||||||||||
| 94 | 1 | bless { | |||||||||||||||||||||||||||
| 95 | ptr => $ptr, | ||||||||||||||||||||||||||||
| 96 | owner => $owner, | ||||||||||||||||||||||||||||
| 97 | }, $class; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 16 | }); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | $ffi->attach( call => ['wasm_func_t', 'record(Wasm::Wasmtime::Vec)*', 'record(Wasm::Wasmtime::Vec)*'] => 'wasm_trap_t' => sub { | ||||||||||||||||||||||||||||
| 103 | 53 | ▶ | my $xsub = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 104 | 53 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 53 | my @params = $self->type->params; | |||||||||||||||||||||||||||
| 106 | 53 | my $args = Wasm::Wasmtime::ValVec->from_perl(\@_, \@params); | |||||||||||||||||||||||||||
| 107 | 53 | ▶ | my $results = $self->result_arity ? Wasm::Wasmtime::ValVec->new($self->result_arity) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 53 | ▶ | my $args_vec = Wasm::Wasmtime::Vec->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | size => scalar @params, | ||||||||||||||||||||||||||||
| 111 | data => defined $args ? addressof($args) : undef, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 53 | ▶ | my $results_vec = Wasm::Wasmtime::Vec->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | size => $self->result_arity, | ||||||||||||||||||||||||||||
| 116 | data => defined $results ? addressof($results) : undef, | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 53 | my $trap = $xsub->($self, $args_vec, $results_vec); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 53 | ▶ | die $trap if $trap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 50 | ▶ | return unless defined $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 37 | my @results = $results->to_perl; | |||||||||||||||||||||||||||
| 124 | 37 | ▶ | wantarray ? @results : $results[0]; ## no critic (Community::Wantarray) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 16 | }); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub attach | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
attach: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 30 | ▶ | my $package = @_ == 2 ? shift : caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 30 | my $name = shift; | |||||||||||||||||||||||||||
| 133 | 30 | ▶ | if($package->can($name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 1 | Carp::carp("attaching ${package}::$name replaces existing subroutine"); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | Sub::Install::reinstall_sub({ | ||||||||||||||||||||||||||||
| 138 | 32 | ▶ | code => sub { $self->call(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 30 | into => $package, | |||||||||||||||||||||||||||
| 140 | as => $name, | ||||||||||||||||||||||||||||
| 141 | }); | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | $ffi->attach( type => ['wasm_func_t'] => 'wasm_functype_t' => sub { | ||||||||||||||||||||||||||||
| 146 | 60 | ▶ | my($xsub, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 147 | 60 | my $type = $xsub->($self); | |||||||||||||||||||||||||||
| 148 | 60 | ▶ | $type->{owner} = $self->{owner} || $self; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'owner'} || $self
MC/DC0%0/2
$self->{'owner'} || $self
$self->{'owner'}
$self
Truth table66%2/3
A || B
A= $self->{'owner'}B= $self
| |||||||||||||||||||||||||||||
| 149 | 60 | $type; | |||||||||||||||||||||||||||
| 150 | 16 | }); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | $ffi->attach( param_arity => ['wasm_func_t'] => 'size_t' => sub { | ||||||||||||||||||||||||||||
| 154 | 3 | ▶ | my($xsub, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 3 | $xsub->($self); | |||||||||||||||||||||||||||
| 156 | 16 | }); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $ffi->attach( result_arity => ['wasm_func_t'] => 'size_t' => sub { | ||||||||||||||||||||||||||||
| 160 | 146 | ▶ | my($xsub, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 146 | $xsub->($self); | |||||||||||||||||||||||||||
| 162 | 16 | }); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 16 | __PACKAGE__->_cast(0); | |||||||||||||||||||||||||||
| 165 | 16 | _generate_destroy(); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 16 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||