Wasm::Wasmer::Module
my $module = Wasm::Wasmer::Module->new( $wasm_bin );
… or, to use a pre-built Wasm::Wasmer::Store instance:
my $module = Wasm::Wasmer::Module->new( $wasm_bin, $store );
… then:
my $instance = $module->create_instance();
… or, for WASI:
my $wasi = $module->store()->create_wasi( .. ); my $instance = $module->create_wasi_instance($wasi);
You can also specify imports; see below.
This class represents a parsed WebAssembly module.
See Wasmer’s documentation for a bit more context.
Parses a WebAssembly module in binary (.wasm) format and returns a CLASS instance representing that.
.wasm
(To use text/.wat format instead, see Wasm::Wasmer’s wat2wasm().)
.wat
wat2wasm()
Optionally associates the parse of that module with a Wasm::Wasmer::Store instance.
Creates a Wasm::Wasmer::Instance instance from OBJ with the (optional) given %IMPORTS. (NB: %IMPORTS is given via reference.)
%IMPORTS is an optional hash-of-hashrefs that describes the set of imports to give to the new instance.
Here’s a simple example that gives a function ns.give2 to WebAssembly that just returns the number 2:
ns
give2
my $instance = $module->create_instance( { ns => { give2 => sub { 2 }, }, }, );
Other import types are rather more complex because they’re interactive; thus, you have to create them prior to calling create_instance() and include your import objects in %IMPORTS.
create_instance()
my $const = $module->store()->create_i32_const( 42 ); my $var = $module->store()->create_f64_mut( 2.718281828 ); my $memory = $module->store()->create_memory( initial => 3 );
(Tables are currently unsupported.)
So, if we alter our above example to import our constants and memory as well as the function, we have:
my $instance = $module->create_instance( { ns => { give2 => sub { 2 }, # These values are all pre-created objects: constvar => $const, mutvar => $mut, memory => $memory, }, }, );
NB: Instances can share imports, even if they’re instances of different WASM modules.
Creates a Wasm::Wasmer::Instance instance from OBJ. That object’s WebAssembly imports will include the WASI interface.
$WASI argument is either undef or a Wasm::Wasmer::WASI instance. Undef is equivalent to $self->store()->create_wasi().
$self->store()->create_wasi()
The optional %IMPORTS reference (reference!) is as for create_instance(). Note that you can override WASI imports with this, if you so desire.
Creates a Wasm::Wasmer::Import::Global instance. See that module’s documentation for more details.
Creates a Wasm::Wasmer::Import::Memory instance. See that module’s documentation for more details. Currently this accepts no parameters; instead it conforms to the WASM module’s needs.
Serializes the in-memory module for later use. (cf. deserialize() below)
deserialize()
Returns OBJ’s underlying Wasm::Wasmer::Store instance.
Like this class’s new() method but takes a serialized module rather than WASM code.
new()
Like this class’s new() but just returns a boolean to indicate whether $WASM_BIN represents a valid module.
To install Wasm::Wasmer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Wasm::Wasmer
CPAN shell
perl -MCPAN -e shell install Wasm::Wasmer
For more information on module installation, please visit the detailed CPAN module installation guide.