NAME

Wasm::Wasmtime - Perl interface to Wasmtime

VERSION

version 0.18

SYNOPSIS

 use Wasm::Wasmtime;
 use PeekPoke::FFI qw( poke );
 
 my $store = Wasm::Wasmtime::Store->new;
 
 my $module = Wasm::Wasmtime::Module->new($store, wat => q{
   (module
 
     ;; callback we can make back into perl space
     (func $hello (import "" "hello"))
     (func (export "call_hello") (call $hello))
 
     ;; plain WebAssembly function that we can call from Perl
     (func (export "gcd") (param i32 i32) (result i32)
       (local i32)
       block  ;; label = @1
         block  ;; label = @2
           local.get 0
           br_if 0 (;@2;)
           local.get 1
           local.set 2
           br 1 (;@1;)
         end
         loop  ;; label = @2
           local.get 1
           local.get 0
           local.tee 2
           i32.rem_u
           local.set 0
           local.get 2
           local.set 1
           local.get 0
           br_if 0 (;@2;)
         end
       end
       local.get 2
     )
 
     ;; memory region that can be accessed from
     ;; either Perl or WebAssembly
     (memory (export "memory") 2 3)
     (func (export "load") (param i32) (result i32)
       (i32.load8_s (local.get 0))
     )
 
   )
 });
 
 sub hello
 {
   print "hello world!\n";
 }
 
 my $instance = Wasm::Wasmtime::Instance->new( $module, $store, [\&hello] );
 
 # call a WebAssembly function that calls back into Perl space
 $instance->exports->call_hello;
 
 # call plain WebAssembly function
 my $gcd = $instance->exports->gcd;
 print $gcd->(6,27), "\n";      # 3
 
 # write to memory from Perl and read it from WebAssembly
 my $memory = $instance->exports->memory;
 poke($memory->data + 10, 42);  # set offset 10 to 42
 my $load = $instance->exports->load;
 print $load->(10), "\n";       # 42
 poke($memory->data + 10, 52);  # set offset 10 to 52
 print $load->(10), "\n";       # 52

DESCRIPTION

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

This module pre-loads all the relevant Wasmtime modules so that you can just start using the appropriate classes.

If you are just getting your feet wet with WebAssembly and Perl then you probably want to take a look at Wasm, which is a simple interface that automatically imports functions from Wasm space into Perl space.

ENVIRONMENT

PERL_WASM_WASMTIME_MEMORY

This environment variable, if set, should be a colon separated list of values for static_memory_maximum_size, static_memory_guard_size and dynamic_memory_guard_size. See Wasm::Wasmtime::Config for more details on these limits.

SEE ALSO

Wasm

Simplified interface to WebAssembly that imports WebAssembly functions into Perl space.

Wasm::Wasmtime::Module

Interface to WebAssembly module.

Wasm::Wasmtime::Instance

Interface to a WebAssembly module instance.

Wasm::Wasmtime::Func

Interface to WebAssembly function.

Wasm::Wasmtime::Linker

Link together multiple WebAssembly modules into one program.

Wasm::Wasmtime::Wat2Wasm

Tool to convert WebAssembly Text (WAT) to WebAssembly binary (Wasm).

Wasm::Wasmtime::WasiInstance

WebAssembly System Interface (WASI).

https://github.com/bytecodealliance/wasmtime

The rust library used by this module, via its C API, via FFI.

https://github.com/bytecodealliance/wasmtime-py

These bindings here heavily influenced by the Python Wasmtime bindings.

AUTHOR

Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020 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.