The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Wasm::Wasmer::Module

SYNOPSIS

    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.

DESCRIPTION

This class represents a parsed WebAssembly module.

See Wasmer’s documentation for a bit more context.

METHODS

$obj = CLASS->new( $WASM_BIN [, $STORE ] )

Parses a WebAssembly module in binary (.wasm) format and returns a CLASS instance representing that.

(To use text/.wat format instead, see Wasm::Wasmer’s wat2wasm().)

Optionally associates the parse of that module with a Wasm::Wasmer::Store instance.

$instance = OBJ->create_instance( [ \%IMPORTS ] )

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:

    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.

    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.

$instance = OBJ->create_wasi_instance( $WASI, [ \%IMPORTS ] )

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().

The optional %IMPORTS reference (reference!) is as for create_instance(). Note that you can override WASI imports with this, if you so desire.

$global = OBJ->create_global( $VALUE )

Creates a Wasm::Wasmer::Import::Global instance. See that module’s documentation for more details.

$global = OBJ->create_memory()

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.

$bytes = OBJ->serialize()

Serializes the in-memory module for later use. (cf. deserialize() below)

$store = OBJ->store()

Returns OBJ’s underlying Wasm::Wasmer::Store instance.

STATIC FUNCTIONS

$module = deserialize( $SERIALIZED_BIN [, $STORE ] )

Like this class’s new() method but takes a serialized module rather than WASM code.

$yn = validate( $WASM_BIN [, $STORE ] )

Like this class’s new() but just returns a boolean to indicate whether $WASM_BIN represents a valid module.