🌻 📖 Wasm::Wasmtime::Module

NAME

Wasm::Wasmtime::Module - Wasmtime module class

VERSION

version 0.23

SYNOPSIS

 use Wasm::Wasmtime;
 
 my $module = Wasm::Wasmtime::Module->new( wat => '(module)' );

DESCRIPTION

WARNING: WebAssembly and Wasmtime are a moving target and the interface for these modules is under active development. Use with caution.

This class represents a WebAssembly module.

CONSTRUCTORS

new

 my $module = Wasm::Wasmtime::Module->new(
   $engine,       # Wasm::Wasmtime::Engine
   wat => $wat,   # WebAssembly Text
 );
 my $module = Wasm::Wasmtime::Module->new(
   $engine,       # Wasm::Wasmtime::Engine
   wasm => $wasm, # WebAssembly binary
 );
 my $module = Wasm::Wasmtime::Module->new(
   $engine,       # Wasm::Wasmtime::Engine
   file => $path, # Filename containing WebAssembly binary (.wasm) or WebAssembly Text (.wat)
 );
 my $module = Wasm::Wasmtime::Module->new(
   wat => $wat,   # WebAssembly Text
 );
 my $module = Wasm::Wasmtime::Module->new(
   wasm => $wasm, # WebAssembly binary
 );
 my $module = Wasm::Wasmtime::Module->new(
   file => $path, # Filename containing WebAssembly binary (.wasm) or WebAssembly Text (.wat)
 );

Create a new WebAssembly module object. You must provide either WebAssembly Text (WAT), WebAssembly binary (Wasm), or a filename of a file that contains WebAssembly binary (Wasm). If the optional Wasm::Wasmtime::Engine object is not provided one will be created for you.

[Deprecated]

 my $module = Wasm::Wasmtime::Module->new(
   $store,        # Wasm::Wasmtime::Store
   wat => $wat,   # WebAssembly Text
 );
 my $module = Wasm::Wasmtime::Module->new(
   $store,        # Wasm::Wasmtime::Store
   wasm => $wasm, # WebAssembly binary
 );
 my $module = Wasm::Wasmtime::Module->new(
   $store,        # Wasm::Wasmtime::Store
   file => $path, # Filename containing WebAssembly binary (.wasm) or WebAssembly Text (.wat)
 );

You can provide a Wasm::Wasmtime::Store instance instead of a Wasm::Wasmtime::Engine. Although the store instance is no longer required internally to create a module instance, the engine object which is needed can be found from the store. This form will be removed in a future version.

deserialize

 my $module = Wasm::Wasmtime::Module->deserialize(
   $engine,       # Wasm::Wasmtime::Engine
   $serialized,   # serialized module
 );
 my $module = Wasm::Wasmtime::Module->deserialize(
   $serialized,   # serialized module
 );

Build a module from serialized data. The serialized data can be gotten from the serialize method documented below.

METHODS

validate

 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   $store,        # Wasm::Wasmtime::Store
   wat => $wat,   # WebAssembly Text
 );
 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   $store,        # Wasm::Wasmtime::Store
   wasm => $wasm, # WebAssembly binary
 );
 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   $store,        # Wasm::Wasmtime::Store
   file => $path, # Filename containing WebAssembly binary (.wasm)
 );
 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   wat => $wat,   # WebAssembly Text
 );
 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   wasm => $wasm, # WebAssembly binary
 );
 my($ok, $mssage) = Wasm::Wasmtime::Module->validate(
   file => $path, # Filename containing WebAssembly binary (.wasm)
 );

Takes the same arguments as new, but validates the module without creating a module object. Returns $ok, which is true if the WebAssembly is valid, and false otherwise. For invalid WebAssembly $message may contain a useful diagnostic for why it was invalid.

exports

 my $exports = $module->exports;

Returns a Wasm::Wasmtime::Module::Exports object that can be used to query the module exports.

imports

 my $imports = $module->imports;

Returns a list of Wasm::Wasmtime::ImportType objects for the objects imported by the WebAssembly module.

serialize

 my $serialized = $module->serialize;

This function serializes compiled module artifacts as blob data. This data can be reconstituted with the deserialize constructor method documented above.

engine

 my $engine = $module->engine;

Returns the Wasm::Wasmtime::Engine object used by this module.

to_string

 my $string = $module->to_string;

Converts the module imports and exports into a string for diagnostics.

SEE ALSO

Wasm
Wasm::Wasmtime

AUTHOR

Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020-2022 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.