| 1 | package Wasm::Wasmtime::Instance; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use 5.008004; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use FFI::C::Util qw( addressof ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use FFI::Platypus::Buffer (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Wasm::Wasmtime::FFI; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Wasm::Wasmtime::Module; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Wasm::Wasmtime::Extern; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use Wasm::Wasmtime::Func; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Wasm::Wasmtime::Memory; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Wasm::Wasmtime::Trap; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use Wasm::Wasmtime::Instance::Exports; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use Ref::Util qw( is_ref is_blessed_ref is_plain_coderef is_plain_scalarref ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | # ABSTRACT: Wasmtime instance class | ||||||||||||||||||||||||||||||||||||
| 19 | 15 | our $VERSION = '0.24'; # VERSION | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 15 | $ffi_prefix = 'wasmtime_instance_'; | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | sub _cast_import | ||||||||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_cast_import: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||||||||||
| 27 | 5 | my($ii, $mi, $store, $keep, $slot) = @_; | |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | 5 | ▶ | if(is_blessed_ref($ii) && $ii->isa('Wasm::Wasmtime::Extern')) | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
is_blessed_ref($ii) and $ii->isa("Wasm::Wasmtime::Extern")
MC/DC50%1/2
is_blessed_ref($ii) and $ii->isa("Wasm::Wasmtime::Extern")
is_blessed_ref($ii)
$ii->isa("Wasm::Wasmtime::Extern")
Truth table66%2/3
A and B
A= is_blessed_ref($ii)B= $ii->isa("Wasm::Wasmtime::Extern")
| |||||||||||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||||||||||
| 31 | 2 | return $ii->_fill_extern($slot); | |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | elsif(is_plain_coderef($ii)) | ||||||||||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | if($mi->type->kind eq 'functype') | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||||||||||
| 37 | 1 | my $f = Wasm::Wasmtime::Func->new($store, $mi->type, $ii); | |||||||||||||||||||||||||||||||||||
| 38 | 1 | push @$keep, $f; | |||||||||||||||||||||||||||||||||||
| 39 | 1 | return $f->_fill_extern($slot); | |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||
| 42 | ▶ | elsif(is_plain_scalarref($ii) || !defined $ii) | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
is_plain_scalarref($ii) or not defined $ii
MC/DC0%0/2
is_plain_scalarref($ii) or not defined $ii
is_plain_scalarref($ii)
not defined $ii
Truth table66%2/3
A or not(B)
A= is_plain_scalarref($ii)B= not defined $ii
| |||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if($mi->type->kind eq 'memorytype') | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||
| 46 | 2 | my $m = Wasm::Wasmtime::Memory->new($store, $mi->type); | |||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | $$ii = $m if is_plain_scalarref($ii); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 2 | push @$keep, $m; | |||||||||||||||||||||||||||||||||||
| 49 | 2 | return $m->_fill_extern($slot); | |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | 0 | Carp::croak("Non-extern object as import"); | |||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | 17 | ▶ | $ffi->attach( [ wasmtime_instance_new => 'new' ] => ['opaque','wasmtime_module_t','opaque','size_t','wasmtime_instance_t','opaque*'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.7
| |||||||||||||||||||||||||||||||||||||
| 56 | 17 | my $xsub = shift; | |||||||||||||||||||||||||||||||||||
| 57 | 17 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 58 | 17 | my $module = shift; | |||||||||||||||||||||||||||||||||||
| 59 | 17 | ▶ | my $store = is_blessed_ref($_[0]) && $_[0]->isa('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")
| |||||||||||||||||||||||||||||||||||||
| 60 | ? shift | ||||||||||||||||||||||||||||||||||||
| 61 | : Carp::croak('Wasm::Wasmtime::Instance->new requires a Wasm::Wasmtime::Store object'); | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 17 | my($imports) = @_; | |||||||||||||||||||||||||||||||||||
| 64 | 17 | ▶ | $imports ||= []; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$imports ||= []
MC/DC100%1/1
$imports ||= []
$imports
Truth table100%2/2
A
A= $imports
| |||||||||||||||||||||||||||||||||||||
| 65 | 17 | ▶ | Carp::confess("imports is not an array reference") unless ref($imports) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | 17 | my @mi = @{ $module->imports }; | |||||||||||||||||||||||||||||||||||
| 68 | 17 | ▶ | Carp::croak("Got @{[ scalar @$imports ]} imports, but expected @{[ scalar @mi ]}") | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 69 | if @$imports != @mi; | ||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | 16 | my @keep; | |||||||||||||||||||||||||||||||||||
| 72 | my $array; | ||||||||||||||||||||||||||||||||||||
| 73 | 16 | ▶ | if(@$imports) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||||
| 75 | 5 | $array = Wasm::Wasmtime::ExternArray->new(scalar @$imports); | |||||||||||||||||||||||||||||||||||
| 76 | 5 | foreach my $i (0..$#$imports) | |||||||||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||||||||||
| 78 | 5 | _cast_import($imports->[$i], $mi[$i], $store, \@keep, $array->[$i]); | |||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | 16 | my $data = Wasm::Wasmtime::InstanceData->new; | |||||||||||||||||||||||||||||||||||
| 83 | 16 | my $trap; | |||||||||||||||||||||||||||||||||||
| 84 | 16 | ▶ | if(my $error = $xsub->($store->context, $module, $array ? addressof($array) : undef, scalar @$imports, $data, \$trap)) | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||
| 86 | 0 | Carp::croak("error creating instance: " . $error->message); | |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | 16 | ▶ | if($trap) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||||||
| 90 | 0 | die Wasm::Wasmtime::Trap->new($trap); | |||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||
| 93 | bless { | ||||||||||||||||||||||||||||||||||||
| 94 | 16 | data => $data, | |||||||||||||||||||||||||||||||||||
| 95 | store => $store, | ||||||||||||||||||||||||||||||||||||
| 96 | module => $module, | ||||||||||||||||||||||||||||||||||||
| 97 | keep => \@keep, | ||||||||||||||||||||||||||||||||||||
| 98 | }, $class; | ||||||||||||||||||||||||||||||||||||
| 99 | 15 | }); | |||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | # used internally by Wasm::Wasmtime::Linker->instantiate | ||||||||||||||||||||||||||||||||||||
| 102 | sub _new_from_data | ||||||||||||||||||||||||||||||||||||
| 103 | 20 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_new_from_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 104 | 20 | my($class, $data, $store, $module) = @_; | |||||||||||||||||||||||||||||||||||
| 105 | 20 | bless { | |||||||||||||||||||||||||||||||||||
| 106 | data => $data, | ||||||||||||||||||||||||||||||||||||
| 107 | store => $store, | ||||||||||||||||||||||||||||||||||||
| 108 | module => $module, | ||||||||||||||||||||||||||||||||||||
| 109 | keep => [], | ||||||||||||||||||||||||||||||||||||
| 110 | }, $class; | ||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 31 | ▶ | sub module { shift->{module} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
module: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub store { shift->{store} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
store: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 158 | ▶ | sub context { shift->{store}->context } | ||||||||||||||||||||||||||||||||||
|
Subroutine
context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | sub exports | ||||||||||||||||||||||||||||||||||||
| 124 | 29 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
exports: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 125 | 29 | Wasm::Wasmtime::Instance::Exports->new(shift); | |||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 15 | $ffi->attach( [ wasmtime_instance_export_nth => '_export_nth' ] => ['opaque','wasmtime_instance_t','size_t','opaque*','size_t*','wasmtime_extern_t'] => 'bool' ); | |||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | sub _exports | ||||||||||||||||||||||||||||||||||||
| 131 | 52 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_exports: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 132 | 52 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 133 | 52 | my @exports; | |||||||||||||||||||||||||||||||||||
| 134 | 52 | for(my $i = 0; ; $i++) | |||||||||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||||||||||
| 136 | 158 | my $extern = Wasm::Wasmtime::ExternData->new; | |||||||||||||||||||||||||||||||||||
| 137 | 158 | my $name_ptr; | |||||||||||||||||||||||||||||||||||
| 138 | 158 | my $name_len = 0; | |||||||||||||||||||||||||||||||||||
| 139 | 158 | ▶ | last unless _export_nth($self->context, $self->{data}, $i, \$name_ptr, \$name_len, $extern); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | 106 | my $obj = Wasm::Wasmtime::Extern->from_extern($extern, $self->{store}); | |||||||||||||||||||||||||||||||||||
| 141 | 106 | $extern->free; | |||||||||||||||||||||||||||||||||||
| 142 | 106 | push @exports, $obj; | |||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||
| 144 | 52 | @exports; | |||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | 15 | 1; | |||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||