blib/lib/Wasm/Wasm3/Module.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran 50.0% 3 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 85.1% 40 / 47 CC 5 SCAR 8.4
CC
5
Cov
83%
_perl_link_wasi
3.8
DESTROY
3.0
BEGIN
1.0
CRAP 2.3
↑ Summary blib/lib/Wasm/Wasm3/Environment.pm »
1 package Wasm::Wasm3::Module;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Wasm::Wasm3::Module
11  
12 =head1 SYNOPSIS
13  
14 See L<Wasm::Wasm3>.
15  
16 =head1 DESCRIPTION
17  
18 This module exposes L<wasm3|https://github.com/wasm3/wasm3>’s
19 module object to Perl.
20  
21 =cut
22  
23 #----------------------------------------------------------------------
24  
25 4 ▶use Wasm::Wasm3;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #----------------------------------------------------------------------
28  
29 =head1 METHODS
30  
31 This class is not directly instantiated; see L<Wasm::Wasm3> for
32 details.
33  
34 =head2 $value = I<OBJ>->get_global( $NAME )
35  
36 Returns the value of the $NAMEd export global.
37  
38 =head2 $type = I<OBJ>->get_global_type( $NAME )
39  
40 Returns the type (e.g., Wasm::Wasm3::TYPE_I32) of the $NAMEd export global.
41  
42 =head2 $obj = I<OBJ>->link_function( $MODULE_NAME, $FUNCTION_NAME, $SIGNATURE, $CODEREF )
43  
44 Sets $CODEREF as $MODULE_NAME.$FUNCTION_NAME’s implementation inside the
45 WebAssembly module. See below for L</$SIGNATURE>.
46  
47 $CODEREF will I<always> be called in list context. $CODEREF B<MUST> return
48 the number of arguments that $SIGNATURE indicates, or you’ll get an error
49 (possibly an unhelpful one).
50  
51 If $CODEREF throws, the exception is C<warn()>ed, and a generic
52 callback-failed error is thrown to the C<link_function()> caller.
53  
54 =head3 WASM Context in Callbacks
55  
56 Your callback may need to reference either the wasm3 runtime or module.
57 When doing this, be sure to use a C<weaken()>ed copy (cf. L<Scalar::Util>)
58 of that object, or you’ll leak memory (and eventually get a C<warn()>ing
59 about it).
60  
61 For example:
62  
63 my $weak_runtime = $runtime;
64 Scalar::Util::weaken($weak_runtime);
65  
66 $module->link_function(
67 mymodule => myfuncname => 'v(ii)',
68 sub {
69 my ($buf_p, $buflen) = @_;
70  
71 my $buf = $weak_runtime->get_memory($buf_p, $buflen);
72  
73 # Now do something cool with $buf.
74  
75 return;
76 },
77 );
78  
79 The distribution’s F<t/wasi_pp.t> shows this technique in action.
80  
81 (An alternative design would be to pass a special context object
82 to every callback, but the weak-reference approach is more efficient.)
83  
84 =head3 $SIGNATURE
85  
86 $SIGNATURE is wasm3’s own convention to describe a function’s inputs &
87 outputs. As of this writing wasm3’s documentation doesn’t describe it very
88 well, so we’ll describe it here.
89  
90 The format is C<$RETURNS($ARGS)>, where $RETURNS and $ARGS are both either:
91  
92 =over
93  
94 =item * C<v>, to indicate empty (C<v> meaning “void”)
95  
96 =item * … a sequence of one or more of: C<i> (i32), C<I> (i64), C<f> (f32),
97 C<F>, (f64)
98  
99 =back
100  
101 Space characters are ignored.
102  
103 For example: C<v(if)> indicates a function that takes i32 and f32 as
104 arguments and returns nothing.
105  
106 =head2 $obj = I<OBJ>->link_wasi_default()
107  
108 A quick helper to link L<WASI|https://wasi.dev> includes via
109 wasm3’s L<uvwasi|https://github.com/nodejs/uvwasi> integration.
110  
111 This uses wasm3’s built-in WASI defaults, e.g., STDIN becomes WASI file
112 descriptor 0.
113  
114 =head2 $obj = I<OBJ>->link_wasi( %OPTS )
115  
116 (NB: Only available if uvwasi is your WASI backend; see L<Wasm::Wasm3>
117 for details.)
118  
119 Like C<link_wasi_default()> but takes a list of key/value pairs that
120 offer the following controls:
121  
122 %OPTS are:
123  
124 =over
125  
126 =item * C<in>, C<out>, C<err> - File handles to the WASI input, output,
127 and error streams. Defaults are file descriptors 0, 1, and 2 respectively.
128  
129 =item * C<env> - A reference to an array of key/value byte-string pairs
130 to pass as the WASI environment.
131  
132 =item * C<preopen> - A reference to a hash of WASI paths to system/real
133 paths.
134  
135 B<IMPORTANT:> WASI paths are character strings, while system paths are
136 B<byte> strings. The discrepancy arises because WASI paths are always
137 character strings, while Perl treats all system paths as byte strings
138 (even on OSes like Windows where paths are character strings).
139  
140 So if, for example, you have directory F</tmp/føø> that you’ll access
141 in WASI as F</épée>, your code might look thus:
142  
143 preopen => {
144 do { use utf8; '/épée' } => do { no utf8; '/tmp/føø' },
145 },
146  
147 =back
148  
149 =cut
150  
151 4 our $WASI_MODULE_STR;
152  
153 1 ▶sub link_wasi_default {
Subroutine link_wasi_default: called CC=1 SCAR=0.0
Pod documented
154 1 my ($self) = @_;
155  
156 1 return $self->_perl_link_wasi('_link_wasi_default');
157 }
158  
159 1 ▶sub link_wasi {
Subroutine link_wasi: called CC=1 SCAR=0.0
Pod documented
160 1 my ($self, @args) = @_;
161  
162 1 return $self->_perl_link_wasi('_link_wasi', @args);
163 }
164  
165 2 ▶sub _perl_link_wasi {
Subroutine _perl_link_wasi: called CC=3 SCAR=13.5
166 2 my ($self, $fn, @args) = @_;
167  
168 2 ▶ if ($WASI_MODULE_STR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Wasm::Wasm3::Module::WASI_MODULE_STR) { } 0 2 2
169 0 ▶ if ($WASI_MODULE_STR ne "$self") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Wasm::Wasm3::Module::WASI_MODULE_STR ne "$self") 0 0 0
170 0 die "$self: WASI is already linked! ($WASI_MODULE_STR)";
171 }
172 }
173 else {
174 2 $self->$fn(@args);
175 2 $WASI_MODULE_STR = "$self";
176 }
177  
178 2 return $self;
179 }
180  
181 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
182 6 my ($self) = @_;
183  
184 6 $self->_destroy_xs();
185  
186 6 ▶ if ($WASI_MODULE_STR && ($WASI_MODULE_STR eq "$self")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Wasm::Wasm3::Module::WASI_MODULE_STR and $Wasm::Wasm3::Module::WASI_MODULE_STR eq "$self") 2 4 6
Condition66%2/3
$Wasm::Wasm3::Module::WASI_MODULE_STR and $Wasm::Wasm3::Module::WASI_MODULE_STR eq "$self"
!ll&&!rl&&r
402
MC/DC50%1/2
$Wasm::Wasm3::Module::WASI_MODULE_STR and $Wasm::Wasm3::Module::WASI_MODULE_STR eq "$self"
$Wasm::Wasm3::Module::WASI_MODULE_STR $Wasm::Wasm3::Module::WASI_MODULE_STR eq "$self"
Truth table66%2/3
A and B
A= $Wasm::Wasm3::Module::WASI_MODULE_STRB= $Wasm::Wasm3::Module::WASI_MODULE_STR eq "$self"
ABresult
0X0
100
111
187 2 undef $WASI_MODULE_STR;
188 }
189  
190 6 return;
191 }
192  
193 4 1;
↑ Summary blib/lib/Wasm/Wasm3/Environment.pm »