NAME
Wasm::Wasmtime::Func - Wasmtime function class
VERSION
version 0.23
SYNOPSIS
# Call a wasm function from Perl
use Wasm::Wasmtime;
my $store = Wasm::Wasmtime::Store->new;
my $module = Wasm::Wasmtime::Module->new( $store->engine, wat => q{
(module
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
)
});
my $instance = Wasm::Wasmtime::Instance->new($module, $store);
my $add = $instance->exports->add;
print $add->call(1,2), "\n"; # 3
# Call Perl from Wasm
use Wasm::Wasmtime;
my $store = Wasm::Wasmtime::Store->new;
my $module = Wasm::Wasmtime::Module->new( $store->engine, wat => q{
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
});
my $hello = Wasm::Wasmtime::Func->new(
$store,
Wasm::Wasmtime::FuncType->new([],[]),
sub { print "hello world!\n" },
);
my $instance = Wasm::Wasmtime::Instance->new($module, $store, [$hello]);
$instance->exports->run->call(); # hello world!
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 function, and can be used to either call a WebAssembly function from Perl, or to create a callback for calling a Perl function from WebAssembly.
CONSTRUCTOR
new
my $func = Wasm::Wasmtime::Func->new(
$store, # Wasm::Wasmtime::Store
\@params, \@results, # array reference for function signature
\&callback, # code reference
);
my $func = Wasm::Wasmtime::Func->new(
$store, # Wasm::Wasmtime::Store
$functype, # Wasm::Wasmtime::FuncType
\&callback, # code reference
);
Creates a function instance, which can be used to call Perl from WebAssembly. See Wasm::Wasmtime::FuncType for details on how to specify the function signature.
METHODS
call
my @results = $func->call(@params);
my @results = $func->(@params);
Calls the function instance. This can be used to call either Perl functions created with new
as above, or call WebAssembly functions from Perl. As a convenience you can call the function by using the function instance like a code reference.
If there is a trap during the call it will throw an exception. In list context all of the results are returned as a list. In scalar context just the first result (if any) is returned.
attach
$func->attach($name);
$func->attach($package, $name);
Attach the function as a Perl subroutine. If $package
is not specified, then the caller's package will be used.
type
my $functype = $func->type;
Returns the Wasm::Wasmtime::FuncType instance which includes the function signature.
param_arity
my $num = $func->param_arity;
Returns the number of arguments the function takes.
result_arity
my $num = $func->param_arity;
Returns the number of results the function returns.
SEE ALSO
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.