| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Wasm::Wasm3; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 433264 | use strict; | 
|  | 4 |  |  |  |  | 34 |  | 
|  | 4 |  |  |  |  | 96 |  | 
| 4 | 4 |  |  | 4 |  | 17 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 127 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | =encoding utf-8 | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | Wasm::Wasm3 - Self-contained L via L | 
| 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: | 
| 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 is the function’s signature; see L for | 
| 45 |  |  |  |  |  |  | details.) | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | Well-known WebAssembly runtimes like L, | 
| 50 |  |  |  |  |  |  | L, or L | 
| 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 takes a different tactic from | 
| 56 |  |  |  |  |  |  | the aforementioned “big dogs”: whereas those are all JIT compilers, | 
| 57 |  |  |  |  |  |  | wasm3 is a WebAssembly I. 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 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 exports tables. | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 L SUPPORT | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | wasm3 implements WASI via either of (as of this writing) two backends: | 
| 77 |  |  |  |  |  |  | a wrapper around L, and a | 
| 78 |  |  |  |  |  |  | less-complete original implementation. The former needs | 
| 79 |  |  |  |  |  |  | L, which doesn’t compile on all platforms, | 
| 80 |  |  |  |  |  |  | while the latter should compile everywhere this module can run. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | This distribution’s F 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 for an | 
| 88 |  |  |  |  |  |  | example. | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 MEMORY LEAK DETECTION | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | To help you avoid memory leaks, instances of all classes C | 
| 93 |  |  |  |  |  |  | if their C method runs at global destruction time. | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | This necessitates extra care when linking Perl functions to WASM; | 
| 96 |  |  |  |  |  |  | see L for details, and the distribution’s | 
| 97 |  |  |  |  |  |  | F 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 |  |  | 4 |  | 16 | use XSLoader; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 158 |  | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | our $VERSION; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | BEGIN { | 
| 114 | 4 |  |  | 4 |  | 9 | $VERSION = '0.01_92'; | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 4 |  |  |  |  | 3258 | XSLoader::load( __PACKAGE__, $VERSION ); | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 4 |  |  | 4 |  | 1483 | use Wasm::Wasm3::Module (); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 66 |  | 
| 120 | 4 |  |  | 4 |  | 1358 | use Wasm::Wasm3::Runtime (); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 119 |  | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 4 |  |  | 4 |  | 21 | use constant M3_VERSION => (_M3_VERSION_MAJOR, _M3_VERSION_MINOR, _M3_VERSION_REV); | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 381 |  | 
| 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, C, C, C | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | Numeric constants that indicate the corresponding WebAssembly type. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head2 $YN = WASI_BACKEND | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Either C or C. See above about WASI support for | 
| 143 |  |  |  |  |  |  | details. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head1 METHODS | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =head2 $OBJ = I->new() | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Instantiates I. | 
| 150 |  |  |  |  |  |  | Creates a new wasm3 environment and binds it to the returned object. | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head2 $RUNTIME = I->create_runtime( $STACKSIZE ) | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | Creates a new wasm3 runtime from I. | 
| 155 |  |  |  |  |  |  | Returns a L instance. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 $MODULE = I->parse_module( $WASM_BINARY ) | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Loads a WebAssembly module from I (F<*.wasm>) format. | 
| 160 |  |  |  |  |  |  | Returns a L 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 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. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =cut | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | 1; |