blib/lib/Wasm/Wasm3.pm

filter: Statementstmt 100% 22 / 22 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 29 / 29 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« easyxs/easyxs_perlcall.h ↑ Summary blib/lib/Wasm/Wasm3/Runtime.pm »
1 package Wasm::Wasm3;
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 - Self-contained L<WebAssembly|https://webassembly.org/> via L<wasm3|https://github.com/wasm3/wasm3>
11  
12 =head1 SYNOPSIS
13  
14 Basic setup:
15  
16 my $env = Wasm::Wasm3->new();
17 my $module = $env->parse_module($wasm_binary);
18 my $runtime = $env->create_runtime(1024)->load_module($module);
19  
20 Run L<WASI|https://wasi.dev>:
21  
22 my $exit_code = $runtime->run_wasi('arg1', 'arg2');
23  
24 WebAssembly-exported globals:
25  
26 my $global = $module->get_global('some-value');
27  
28 $module->set_global('some-value', 1234);
29  
30 WebAssembly-exported memory:
31  
32 $runtime->set_memory( $offset, $bytes );
33  
34 my $from_wasm = $runtime->get_memory( $offset, $length );
35  
36 Call a WebAssembly-exported function:
37  
38 my @out = $runtime->call('some-func', @args);
39  
40 Implement a WebAssembly-imported function in Perl:
41  
42 $runtime->link_function('mod-name', 'func-name', 'v(ii)', $coderef);
43  
44 (C<v(ii)> is the function’s signature; see L<Wasm::Wasm3::Runtime> for
45 details.)
46  
47 =head1 DESCRIPTION
48  
49 Well-known WebAssembly runtimes like L<Wasmer|https://wasmer.io>,
50 L<Wasmtime|https://wasmtime.dev>, or L<WAVM|https://github.com/wavm/wavm>
51 often require nonstandard dependencies/toolchains (e.g., LLVM or Rust).
52 Their builds can take a while, especially on slow machines, and only
53 the most popular platforms may enjoy support.
54  
55 L<wasm3|https://github.com/wasm3/wasm3> takes a different tactic from
56 the aforementioned “big dogs”: whereas those are all JIT compilers,
57 wasm3 is a WebAssembly I<interpreter>. This makes it quite small and
58 fast/simple to build, which lets you run WebAssembly in environments
59 that something bigger may not support. Runtime performance lags the
60 “big dogs” significantly, but startup latency will likely be lower, and
61 memory usage is B<much> lower.
62  
63 This distribution includes wasm3, so you don’t need to build it yourself.
64  
65 =head1 STATUS
66  
67 This Perl library is EXPERIMENTAL.
68  
69 Additionally, wasm3 is, as of this writing, rather less complete than
70 Wasmer et al. wasm3 only exports a single WebAssembly memory, for
71 example. It can’t import memories or globals, and it neither imports
72 I<nor> exports tables.
73  
74 =head1 L<WASI|https://wasi.dev> SUPPORT
75  
76 wasm3 implements WASI via either of (as of this writing) two backends:
77 a wrapper around L<uvwasi|https://github.com/nodejs/uvwasi>, and a
78 less-complete original implementation. The former needs
79 L<libuv|https://libuv.org>, which doesn’t compile on all platforms,
80 while the latter should compile everywhere this module can run.
81  
82 This distribution’s F<Makefile.PL> implements logic to determine which
83 backend to use.
84  
85 You’re free, of course, to implement your own WASI imports rather than to
86 use wasm3’s. Depending on how much of WASI you actually need that may not
87 be as onerous as it sounds; see the distribution’s F<t/wasi_pp.t> for an
88 example.
89  
90 =head1 MEMORY LEAK DETECTION
91  
92 To help you avoid memory leaks, instances of all classes C<warn()>
93 if their C<DESTROY()> method runs at global destruction time.
94  
95 This necessitates extra care when linking Perl functions to WASM;
96 see L<Wasm::Wasm3::Module> for details, and the distribution’s
97 F<t/wasi_pp.t> for an example.
98  
99 =head1 DOCUMENTATION
100  
101 This module generally documents only those aspects of its usage that
102 are germane to this module specifically. For more details, see
103 wasm3’s documentation.
104  
105 =cut
106  
107 #----------------------------------------------------------------------
108  
109 4 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 4 our $VERSION;
112  
113 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
114 4 $VERSION = '0.02';
115  
116 4 XSLoader::load( __PACKAGE__, $VERSION );
117 }
118  
119 4 ▶use Wasm::Wasm3::Module ();
Subroutine BEGIN: called CC=1 SCAR=0.0
120 4 ▶use Wasm::Wasm3::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 4 ▶use constant M3_VERSION => (_M3_VERSION_MAJOR, _M3_VERSION_MINOR, _M3_VERSION_REV);
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 #----------------------------------------------------------------------
125  
126 =head1 STATIC FUNCTIONS & CONSTANTS
127  
128 =head2 ($MAJOR, $MINOR, $REV) = M3_VERSION
129  
130 Returns wasm3’s version as 3 integers.
131  
132 =head2 $STRING = M3_VERSION_STRING
133  
134 Returns wasm3’s version as a string.
135  
136 =head2 C<TYPE_I32>, C<TYPE_I64>, C<TYPE_F32>, C<TYPE_F64>
137  
138 Numeric constants that indicate the corresponding WebAssembly type.
139  
140 =head2 $YN = WASI_BACKEND
141  
142 Either C<uvwasi> or C<simple>. See above about WASI support for
143 details.
144  
145 =head1 METHODS
146  
147 =head2 $OBJ = I<CLASS>->new()
148  
149 Instantiates I<CLASS>.
150 Creates a new wasm3 environment and binds it to the returned object.
151  
152 =head2 $RUNTIME = I<OBJ>->create_runtime( $STACKSIZE )
153  
154 Creates a new wasm3 runtime from I<OBJ>.
155 Returns a L<Wasm::Wasm3::Runtime> instance.
156  
157 =head2 $MODULE = I<OBJ>->parse_module( $WASM_BINARY )
158  
159 Loads a WebAssembly module from I<binary> (F<*.wasm>) format.
160 Returns a L<Wasm::Wasm3::Module> instance.
161  
162 If your WebAssembly module is in text format rather than binary,
163 you’ll need to convert it first. Try
164 L<wabt|https://github.com/webassembly/wabt> if you need such a tool.
165  
166 =cut
167  
168 =head1 LICENSE & COPYRIGHT
169  
170 Copyright 2022 Gasper Software Consulting. All rights reserved.
171  
172 This library is licensed under the same terms as Perl itself.
173 See L<perlartistic>.
174  
175 =cut
176  
177 4 1;
« easyxs/easyxs_perlcall.h ↑ Summary blib/lib/Wasm/Wasm3/Runtime.pm »