| 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 Wasm::Wasmtime::FFI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Wasm::Wasmtime::Module; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Wasm::Wasmtime::Extern; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Wasm::Wasmtime::Func; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Wasm::Wasmtime::Trap; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use Wasm::Wasmtime::Instance::Exports; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Ref::Util qw( is_ref is_blessed_ref is_plain_coderef is_plain_scalarref ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ABSTRACT: Wasmtime instance class | ||||||||||||||||||||||||||||
| 16 | 15 | our $VERSION = '0.23'; # VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 15 | $ffi_prefix = 'wasm_instance_'; | |||||||||||||||||||||||||||
| 20 | 15 | $ffi->load_custom_type('::PtrObject' => 'wasm_instance_t' => __PACKAGE__); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub _cast_import | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | 5 | ▶ | my($ii, $mi, $store, $keep) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cast_import: called CC=10 SCAR=24.3
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | if(ref($ii) eq 'Wasm::Wasmtime::Extern') | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||
| 28 | 0 | return $ii->{ptr}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | ▶ | elsif(is_blessed_ref($ii) && $ii->isa('Wasm::Wasmtime::Extern')) | |||||||||||||||||||||||||||
|
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")
| |||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 2 | return $ii->{ptr}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | elsif(is_plain_coderef($ii)) | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | if($mi->type->kind eq 'functype') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 1 | my $f = Wasm::Wasmtime::Func->new( | |||||||||||||||||||||||||||
| 39 | $store, | ||||||||||||||||||||||||||||
| 40 | $mi->type, | ||||||||||||||||||||||||||||
| 41 | $ii, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | 1 | push @$keep, $f; | |||||||||||||||||||||||||||
| 44 | 1 | return $f->{ptr}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | ▶ | 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
| |||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | 2 | ▶ | if($mi->type->kind eq 'memorytype') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 2 | my $m = Wasm::Wasmtime::Memory->new( | |||||||||||||||||||||||||||
| 52 | $store, | ||||||||||||||||||||||||||||
| 53 | $mi->type, | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | $$ii = $m if defined $ii; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 2 | push @$keep, $m; | |||||||||||||||||||||||||||
| 57 | 2 | return $m->{ptr}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | Carp::croak("Non-extern object as import"); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 15 | require FFI::Platypus::Memory; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | $ffi->attach( [ wasmtime_instance_new => 'new' ] => ['wasm_store_t','wasm_module_t','record(Wasm::Wasmtime::Vec)*','opaque*','opaque*'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||||
| 66 | 37 | ▶ | my $xsub = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.6
| |||||||||||||||||||||||||||||
| 67 | 37 | my $class = shift; | |||||||||||||||||||||||||||
| 68 | 37 | my $module = shift; | |||||||||||||||||||||||||||
| 69 | 37 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 70 | ? shift | ||||||||||||||||||||||||||||
| 71 | : Carp::croak('Creating a Wasm::Wasmtime::Instance instance without a Wasm::Wasmtime::Store object is no longer allowed'); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 37 | my $ptr; | |||||||||||||||||||||||||||
| 74 | my @keep; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 37 | ▶ | if(defined $_[0] && !is_ref($_[0])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0] and not is_ref($_[0])
MC/DC100%2/2
defined $_[0] and not is_ref($_[0])
defined $_[0]
not is_ref($_[0])
Truth table100%3/3
A and not(B)
A= defined $_[0]B= not is_ref($_[0])
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 20 | ($ptr) = @_; | |||||||||||||||||||||||||||
| 79 | 20 | return bless { | |||||||||||||||||||||||||||
| 80 | ptr => $ptr, | ||||||||||||||||||||||||||||
| 81 | module => $module, | ||||||||||||||||||||||||||||
| 82 | keep => \@keep, | ||||||||||||||||||||||||||||
| 83 | }, $class; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 17 | my($imports) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 17 | ▶ | $imports ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$imports ||= []
MC/DC100%1/1
$imports ||= []
$imports
Truth table100%2/2
A
A= $imports
| |||||||||||||||||||||||||||||
| 90 | 17 | ▶ | Carp::confess("imports is not an array reference") unless ref($imports) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 17 | my @imports = @$imports; | |||||||||||||||||||||||||||
| 92 | 17 | my $trap; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 17 | my @mi = @{ $module->imports }; | |||||||||||||||||||||||||||
| 96 | 17 | ▶ | if(@mi != @imports) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 1 | Carp::croak("Got @{[ scalar @imports ]} imports, but expected @{[ scalar @mi ]}"); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 16 | @imports = map { _cast_import($_, shift @mi, $store, \@keep) } @imports; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | my $imports_vec = Wasm::Wasmtime::Vec->new( | ||||||||||||||||||||||||||||
| 105 | size => scalar @imports, | ||||||||||||||||||||||||||||
| 106 | 16 | ▶ | data => scalar(@imports) > 0 ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 5 | my $count = scalar @imports; | |||||||||||||||||||||||||||
| 108 | 5 | my $ptr = FFI::Platypus::Memory::malloc($ffi->sizeof('opaque') * $count); | |||||||||||||||||||||||||||
| 109 | # void *memcpy(void *dest, const void *src, size_t n) | ||||||||||||||||||||||||||||
| 110 | 5 | FFI::Platypus->new( lib => [undef] )->function( 'memcpy' => [ 'opaque', "opaque[$count]", 'size_t' ] => 'opaque' )->call($ptr, \@imports, $ffi->sizeof('opaque') * $count); | |||||||||||||||||||||||||||
| 111 | } : undef, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 16 | my $ptr; | |||||||||||||||||||||||||||
| 115 | 16 | ▶ | if(my $error = $xsub->($store, $module, $imports_vec, \$ptr, \$trap)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | FFI::Platypus::Memory::free($imports_vec->data) if defined $imports_vec->data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | Carp::croak("error creating module: " . $error->message); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else | ||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 16 | ▶ | FFI::Platypus::Memory::free($imports_vec->data) if defined $imports_vec->data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 16 | ▶ | if($trap) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 0 | $trap = Wasm::Wasmtime::Trap->new($trap); | |||||||||||||||||||||||||||
| 126 | 0 | die $trap; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 16 | return bless { | |||||||||||||||||||||||||||
| 131 | ptr => $ptr, | ||||||||||||||||||||||||||||
| 132 | module => $module, | ||||||||||||||||||||||||||||
| 133 | keep => \@keep, | ||||||||||||||||||||||||||||
| 134 | }, $class; | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 15 | }); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 31 | ▶ | sub module { shift->{module} } | ||||||||||||||||||||||||||
|
Subroutine
module: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub exports | ||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||
| 147 | 29 | ▶ | Wasm::Wasmtime::Instance::Exports->new(shift); | ||||||||||||||||||||||||||
|
Subroutine
exports: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $ffi->attach( [ exports => '_exports' ] => ['wasm_instance_t','wasm_extern_vec_t*'] => sub { | ||||||||||||||||||||||||||||
| 151 | 52 | ▶ | my($xsub, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 52 | my $externs = Wasm::Wasmtime::ExternVec->new; | |||||||||||||||||||||||||||
| 153 | 52 | $xsub->($self, $externs); | |||||||||||||||||||||||||||
| 154 | 52 | $externs->to_list; | |||||||||||||||||||||||||||
| 155 | 15 | }); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 15 | _generate_destroy(); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 15 | 1; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||