| 1 | package Wasm::Wasmtime::Module; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use 5.008004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Ref::Util qw( is_blessed_ref ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use FFI::Platypus::Buffer (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Wasm::Wasmtime::FFI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use Wasm::Wasmtime::Engine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use Wasm::Wasmtime::Store; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use Wasm::Wasmtime::Module::Exports; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 22 | ▶ | use Wasm::Wasmtime::Module::Imports; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 22 | ▶ | use Wasm::Wasmtime::ImportType; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 22 | ▶ | use Wasm::Wasmtime::ExportType; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 22 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Wasmtime module class | ||||||||||||||||||||||||||||
| 18 | 22 | our $VERSION = '0.24'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 22 | $ffi_prefix = 'wasmtime_module_'; | |||||||||||||||||||||||||||
| 22 | 22 | $ffi->load_custom_type('::PtrObject' => 'wasmtime_module_t' => __PACKAGE__); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # returns the raw wasm binary as a scalar (caller must keep it alive while | ||||||||||||||||||||||||||||
| 25 | # passing its buffer to C) | ||||||||||||||||||||||||||||
| 26 | sub _wasm_bytes | ||||||||||||||||||||||||||||
| 27 | 64 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_wasm_bytes: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 28 | 64 | ▶ | if(@_ == 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | 12 | return $_[0]; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 52 | my $key = shift; | |||||||||||||||||||||||||||
| 33 | 52 | ▶ | if($key eq 'wat') | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||
| 35 | 36 | require Wasm::Wasmtime::Wat2Wasm; | |||||||||||||||||||||||||||
| 36 | 36 | return Wasm::Wasmtime::Wat2Wasm::wat2wasm(shift); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | elsif($key eq 'wasm') | ||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 1 | return shift; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | elsif($key eq 'file') | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 15 | require Path::Tiny; | |||||||||||||||||||||||||||
| 45 | 15 | my $path = Path::Tiny->new(shift); | |||||||||||||||||||||||||||
| 46 | 15 | ▶ | if($path->basename =~ /\.wat/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 13 | require Wasm::Wasmtime::Wat2Wasm; | |||||||||||||||||||||||||||
| 49 | 13 | return Wasm::Wasmtime::Wat2Wasm::wat2wasm($path->slurp_utf8); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else | ||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 2 | return $path->slurp_raw; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | Carp::croak("unknown module source: $key"); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub _engine | ||||||||||||||||||||||||||||
| 60 | 66 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_engine: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 61 | 66 | my $args = shift; | |||||||||||||||||||||||||||
| 62 | 66 | my $first = $args->[0]; | |||||||||||||||||||||||||||
| 63 | 66 | ▶ | if(defined $first && is_blessed_ref $first) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $first and is_blessed_ref($first)
MC/DC50%1/2
defined $first and is_blessed_ref($first)
defined $first
is_blessed_ref($first)
Truth table66%2/3
A and B
A= defined $firstB= is_blessed_ref($first)
| |||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 50 | ▶ | if($first->isa('Wasm::Wasmtime::Engine')) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 46 | shift @$args; | |||||||||||||||||||||||||||
| 68 | 46 | return $first; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif($first->isa('Wasm::Wasmtime::Store')) | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 4 | shift @$args; | |||||||||||||||||||||||||||
| 73 | 4 | Carp::carp("Passing a Wasm::Wasmtime::Store into the module constructor is deprecated, please pass a Wasm::Wasmtime::Engine object instead"); | |||||||||||||||||||||||||||
| 74 | 4 | return $first->engine; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 16 | return Wasm::Wasmtime::Engine->new; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 56 | ▶ | $ffi->attach( [ wasmtime_module_new => 'new' ] => ['wasm_engine_t', 'opaque', 'size_t', 'opaque*'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 56 | my $xsub = shift; | |||||||||||||||||||||||||||
| 83 | 56 | my $class = shift; | |||||||||||||||||||||||||||
| 84 | 56 | my $engine = _engine(\@_); | |||||||||||||||||||||||||||
| 85 | 56 | my $data = _wasm_bytes(@_); | |||||||||||||||||||||||||||
| 86 | 56 | my($ptr_in, $len) = FFI::Platypus::Buffer::scalar_to_buffer($data); | |||||||||||||||||||||||||||
| 87 | 56 | my $ptr; | |||||||||||||||||||||||||||
| 88 | 56 | ▶ | if(my $error = $xsub->($engine, $ptr_in, $len, \$ptr)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 1 | Carp::croak("error creating module: " . $error->message); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 55 | bless { ptr => $ptr, engine => $engine }, $class; | |||||||||||||||||||||||||||
| 93 | 22 | }); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | $ffi->attach( [ wasmtime_module_deserialize => 'deserialize' ] => ['wasm_engine_t', 'opaque', 'size_t', 'opaque*'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 97 | 2 | my $xsub = shift; | |||||||||||||||||||||||||||
| 98 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 99 | 2 | my $engine = _engine(\@_); | |||||||||||||||||||||||||||
| 100 | 2 | my $serialized = shift; | |||||||||||||||||||||||||||
| 101 | 2 | my($ptr_in, $len) = FFI::Platypus::Buffer::scalar_to_buffer($serialized); | |||||||||||||||||||||||||||
| 102 | 2 | my $ptr; | |||||||||||||||||||||||||||
| 103 | 2 | ▶ | if(my $error = $xsub->($engine, $ptr_in, $len, \$ptr)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | Carp::croak("error creating module: " . $error->message); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 2 | bless { ptr => $ptr, engine => $engine }, $class; | |||||||||||||||||||||||||||
| 108 | 22 | }); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 8 | ▶ | $ffi->attach( [ wasmtime_module_validate => 'validate' ] => ['wasm_engine_t', 'opaque', 'size_t'] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 112 | 8 | my $xsub = shift; | |||||||||||||||||||||||||||
| 113 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 114 | 8 | my $engine = _engine(\@_); | |||||||||||||||||||||||||||
| 115 | 8 | my $data = _wasm_bytes(@_); | |||||||||||||||||||||||||||
| 116 | 8 | my($ptr_in, $len) = FFI::Platypus::Buffer::scalar_to_buffer($data); | |||||||||||||||||||||||||||
| 117 | 8 | my $error = $xsub->($engine, $ptr_in, $len); | |||||||||||||||||||||||||||
| 118 | wantarray ## no critic (Community::Wantarray) | ||||||||||||||||||||||||||||
| 119 | 8 | ▶ | ? $error ? (0, $error->message) : (1, '') | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 120 | : $error ? 0 : 1; | ||||||||||||||||||||||||||||
| 121 | 22 | }); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub exports | ||||||||||||||||||||||||||||
| 125 | 56 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
exports: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 56 | Wasm::Wasmtime::Module::Exports->new(shift); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 95 | ▶ | $ffi->attach( [ wasmtime_module_exports => '_exports' ]=> [ 'wasmtime_module_t', 'wasm_exporttype_vec_t*' ] => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 95 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 131 | 95 | my $exports = Wasm::Wasmtime::ExportTypeVec->new; | |||||||||||||||||||||||||||
| 132 | 95 | $xsub->($self, $exports); | |||||||||||||||||||||||||||
| 133 | 95 | $exports->to_list; | |||||||||||||||||||||||||||
| 134 | 22 | }); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub imports | ||||||||||||||||||||||||||||
| 138 | 43 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
imports: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 43 | Wasm::Wasmtime::Module::Imports->new(shift); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 91 | ▶ | $ffi->attach( [ wasmtime_module_imports => '_imports' ] => [ 'wasmtime_module_t', 'wasm_importtype_vec_t*' ] => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 91 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 144 | 91 | my $imports = Wasm::Wasmtime::ImportTypeVec->new; | |||||||||||||||||||||||||||
| 145 | 91 | $xsub->($self, $imports); | |||||||||||||||||||||||||||
| 146 | 91 | $imports->to_list; | |||||||||||||||||||||||||||
| 147 | 22 | }); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | ▶ | $ffi->attach( [ 'wasmtime_module_serialize' => 'serialize' ] => [ 'wasmtime_module_t', 'wasm_byte_vec_t*' ] => 'wasmtime_error_t' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 151 | 3 | my($xsub, $self) = @_; | |||||||||||||||||||||||||||
| 152 | 3 | my $s = Wasm::Wasmtime::ByteVec->new; | |||||||||||||||||||||||||||
| 153 | 3 | ▶ | if(my $error = $xsub->($self, $s)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 0 | Carp::croak("error serializing module: " . $error->message); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else | ||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 3 | my $s2 = $s->get; | |||||||||||||||||||||||||||
| 160 | 3 | $s->delete; | |||||||||||||||||||||||||||
| 161 | 3 | return $s2; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 22 | }); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | ▶ | sub engine { shift->{engine} } | ||||||||||||||||||||||||||
|
Subroutine
engine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub to_string | ||||||||||||||||||||||||||||
| 170 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 4 | my($self) = @_; | |||||||||||||||||||||||||||
| 172 | 4 | my @externs = (@{ $self->imports }, @{ $self->exports }); | |||||||||||||||||||||||||||
| 173 | 4 | ▶ | return "(module)\n" unless @externs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | my $string = "(module\n"; | |||||||||||||||||||||||||||
| 175 | 1 | foreach my $extern (@externs) | |||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||
| 177 | 1 | $string .= " " . $extern->to_string . "\n"; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 1 | $string .= ")\n"; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 22 | _generate_destroy('wasmtime_module_delete'); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 22 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||