File Coverage

blib/lib/Wasm/Wasmtime/Global.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Wasm::Wasmtime::Global;
2              
3 16     16   224506 use strict;
  16         38  
  16         548  
4 16     16   83 use warnings;
  16         43  
  16         404  
5 16     16   293 use 5.008004;
  16         58  
6 16     16   110 use base qw( Wasm::Wasmtime::Extern );
  16         31  
  16         2192  
7 16     16   115 use Ref::Util qw( is_ref );
  16         63  
  16         810  
8 16     16   110 use Wasm::Wasmtime::FFI;
  16         34  
  16         1673  
9 16     16   118 use Wasm::Wasmtime::Store;
  16         32  
  16         514  
10 16     16   519 use Wasm::Wasmtime::GlobalType;
  16         51  
  16         584  
11 16     16   105 use constant is_global => 1;
  16         39  
  16         966  
12 16     16   131 use constant kind => 'global';
  16         76  
  16         9120  
13              
14             # ABSTRACT: Wasmtime global class
15             our $VERSION = '0.21'; # VERSION
16              
17              
18             $ffi_prefix = 'wasm_global_';
19             $ffi->load_custom_type('::PtrObject' => 'wasm_global_t' => __PACKAGE__);
20              
21              
22             $ffi->attach( new => ['wasm_store_t', 'wasm_globaltype_t', 'wasm_val_t'] => 'wasm_global_t' => sub {
23             my $xsub = shift;
24             my $class = shift;
25             if(is_ref $_[0])
26             {
27             my($store, $globaltype, $value) = @_;
28             $value = Wasm::Wasmtime::Val->new({
29             kind => $globaltype->content->kind_num,
30             of => { $globaltype->content->kind => $value },
31             });
32             my $self = $xsub->($store, $globaltype, $value);
33             $self->{store} = $store;
34             return $self;
35             }
36             else
37             {
38             my($ptr, $owner) = @_;
39             bless {
40             ptr => $ptr,
41             owner => $owner,
42             }, $class;
43             }
44             });
45              
46              
47             $ffi->attach( type => ['wasm_global_t'] => 'wasm_globaltype_t' => sub {
48             my($xsub, $self) = @_;
49             my $type = $xsub->($self);
50             $type->{owner} = $self->{owner} || $self;
51             $type;
52             });
53              
54              
55             $ffi->attach( get => ['wasm_global_t', 'wasm_val_t'] => sub {
56             my($xsub, $self) = @_;
57             my $value = Wasm::Wasmtime::Val->new;
58             $xsub->($self, $value);
59             $value->to_perl;
60             });
61              
62              
63             $ffi->attach( set => ['wasm_global_t','wasm_val_t'] => sub {
64             my($xsub, $self, $value) = @_;
65             $value = Wasm::Wasmtime::Val->new({
66             kind => $self->type->content->kind_num,
67             of => { $self->type->content->kind => $value },
68             });
69             $xsub->($self, $value);
70             });
71              
72              
73             sub tie
74             {
75 4     4 1 1829 my $self = shift;
76 4         6 my $ref;
77 4         20 tie $ref, __PACKAGE__, $self;
78 4         13 \$ref;
79             }
80              
81 4     4   13 sub TIESCALAR { $_[1] }
82             *FETCH = \&get;
83             *STORE = \&set;
84              
85             __PACKAGE__->_cast(1);
86             _generate_destroy();
87              
88             1;
89              
90             __END__