File Coverage

blib/lib/Wasm/Wasmtime/Linker.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Wasm::Wasmtime::Linker;
2              
3 9     9   3209 use strict;
  9         21  
  9         304  
4 9     9   47 use warnings;
  9         20  
  9         212  
5 9     9   150 use 5.008004;
  9         33  
6 9     9   508 use Wasm::Wasmtime::FFI;
  9         21  
  9         1134  
7 9     9   624 use Wasm::Wasmtime::Store;
  9         33  
  9         266  
8 9     9   514 use Wasm::Wasmtime::Extern;
  9         25  
  9         244  
9 9     9   538 use Wasm::Wasmtime::Instance;
  9         27  
  9         274  
10 9     9   3967 use Wasm::Wasmtime::WasiInstance;
  9         29  
  9         303  
11 9     9   65 use Wasm::Wasmtime::Func;
  9         17  
  9         194  
12 9     9   50 use Wasm::Wasmtime::Trap;
  9         18  
  9         198  
13 9     9   46 use Ref::Util qw( is_blessed_ref );
  9         17  
  9         389  
14 9     9   52 use Carp ();
  9         24  
  9         8360  
15              
16             # ABSTRACT: Wasmtime linker class
17             our $VERSION = '0.22'; # VERSION
18              
19              
20             $ffi_prefix = 'wasmtime_linker_';
21             $ffi->load_custom_type('::PtrObject' => 'wasmtime_linker_t' => __PACKAGE__);
22              
23              
24             $ffi->attach( new => ['wasm_store_t'] => 'wasmtime_linker_t' => sub {
25             my($xsub, $class, $store) = @_;
26             my $self = $xsub->($store);
27             $self->{store} = $store;
28             $self;
29             });
30              
31              
32             $ffi->attach( allow_shadowing => [ 'wasmtime_linker_t', 'bool' ] => sub {
33             my($xsub, $self, $value) = @_;
34             $xsub->($self, $value);
35             $self;
36             });
37              
38              
39             $ffi->attach( define => ['wasmtime_linker_t', 'wasm_byte_vec_t*', 'wasm_byte_vec_t*', 'opaque'] => 'wasmtime_error_t' => sub {
40             my $xsub = shift;
41             my $self = shift;
42             my $module = Wasm::Wasmtime::ByteVec->new(shift);
43             my $name = Wasm::Wasmtime::ByteVec->new(shift);
44             my $extern = shift;
45              
46             # Fix this sillyness when/if ::Extern becomes a base class for extern classes
47             if(is_blessed_ref($extern) && ( $extern->isa('Wasm::Wasmtime::Extern')
48             || $extern->isa('Wasm::Wasmtime::Func')
49             || $extern->isa('Wasm::Wasmtime::Memory')
50             || $extern->isa('Wasm::Wasmtime::Global')
51             || $extern->isa('Wasm::Wasmtime::Table')))
52             {
53             my $error = $xsub->($self, $module, $name, $extern->{ptr});
54             Carp::croak($error->message) if $error;
55             return $self;
56             }
57             else
58             {
59             Carp::croak("not an extern: $extern");
60             }
61             });
62              
63              
64             $ffi->attach( define_wasi => ['wasmtime_linker_t', 'wasi_instance_t'] => 'wasmtime_error_t' => sub {
65             my($xsub, $self, $wasi) = @_;
66             my $error = $xsub->($self, $wasi);
67             Carp::croak($error->message) if $error;
68             $self;
69             });
70              
71              
72             $ffi->attach( define_instance => ['wasmtime_linker_t', 'wasm_byte_vec_t*', 'wasm_instance_t'] => 'wasmtime_error_t' => sub {
73             my($xsub, $self, $name, $instance) = @_;
74             my $vname = Wasm::Wasmtime::ByteVec->new($name);
75             my $error = $xsub->($self, $vname, $instance);
76             Carp::croak($error->message) if $error;
77             $self;
78             });
79              
80              
81             $ffi->attach( instantiate => ['wasmtime_linker_t','wasm_module_t','opaque*','opaque*'] => 'wasmtime_error_t' => sub {
82             my($xsub, $self, $module) = @_;
83             my $trap;
84             my $ptr;
85             my $error = $xsub->($self, $module, \$ptr, \$trap);
86             Carp::croak($error->message) if $error;
87             if($trap)
88             {
89             $trap = Wasm::Wasmtime::Trap->new($trap);
90             die $trap;
91             }
92             elsif($ptr)
93             {
94             return Wasm::Wasmtime::Instance->new(
95             $module, $self->store, $ptr,
96             );
97             }
98             else
99             {
100             Carp::croak("unknown instantiate error");
101             }
102             });
103              
104              
105             $ffi->attach( get_one_by_name => ['wasmtime_linker_t','wasm_byte_vec_t*','wasm_byte_vec_t*','opaque*'] => 'wasmtime_error_t' => sub {
106             my($xsub, $self, $module, $name) = @_;
107             my $vmodule = Wasm::Wasmtime::ByteVec->new($module);
108             my $vname = Wasm::Wasmtime::ByteVec->new($name);
109             my $ptr;
110             if(my $error = $xsub->($self, $vmodule, $vname, \$ptr))
111             {
112             Carp::croak($error->message);
113             }
114             else
115             {
116             $ptr
117             ? $ffi->cast('opaque','wasm_extern_t',$ptr)
118             : undef;
119             }
120             });
121              
122              
123             $ffi->attach( get_default => ['wasmtime_linker_t','wasm_byte_vec_t*','opaque*'] => 'wasmtime_error_t' => sub {
124             my($xsub, $self, $name) = @_;
125             my $vname = Wasm::Wasmtime::ByteVec->new($name);
126             my $ptr;
127             if(my $error = $xsub->($self, $vname, \$ptr))
128             {
129             Carp::croak($error->message);
130             }
131             else
132             {
133             return $ptr ? Wasm::Wasmtime::Func->new($ptr, $self) : undef;
134             }
135             });
136              
137              
138 46     46 1 475 sub store { shift->{store} }
139              
140             _generate_destroy();
141              
142             1;
143              
144             __END__