🌻 📖 Wasm::Wasmtime::Caller

NAME

Wasm::Wasmtime::Caller - Wasmtime caller interface

VERSION

version 0.23

SYNOPSIS

 use Wasm::Wasmtime;
 use Wasm::Wasmtime::Caller qw( wasmtime_caller );
 
 {
   # this just uses Platypus to create a utility function
   # to convert a pointer to a C string into a Perl string.
   use FFI::Platypus 1.00;
   my $ffi = FFI::Platypus->new( api => 1 );
   $ffi->attach_cast( 'cstring' => 'opaque' => 'string' );
 }
 
 sub print_wasm_string
 {
   my $ptr = shift;
   my $caller = wasmtime_caller;
   my $memory = $caller->export_get('memory');
   print cstring($ptr + $memory->data);
 }
 
 my $store = Wasm::Wasmtime::Store->new;
 my $instance = Wasm::Wasmtime::Instance->new(
   Wasm::Wasmtime::Module->new($store->engine, wat => q{
     (module
       (import "" "print_wasm_string" (func $print_wasm_string (param i32)))
       (func (export "run")
         i32.const 0
         call $print_wasm_string
       )
       (memory (export "memory") 1)
       (data (i32.const 0) "Hello, world!\n\00")
     )
   }),
   $store,
   [\&print_wasm_string],
 );
 
 $instance->exports->run->();

DESCRIPTION

This class represents the caller's context when calling a Perl Wasm::Wasmtime::Func from WebAssembly. The primary purpose of this structure is to provide access to the caller's exported memory. This allows functions which take pointers as arguments to easily read the memory the pointers point into.

This is intended to be a pretty temporary mechanism for accessing the Caller's memory until interface types has been fully standardized and implemented.

FUNCTIONS

wasmtime_caller

 my $caller = wasmtime_caller;
 my $caller = wasmtime_caller $index;

This returns the current caller context (an instance of Wasm::Wasmtime::Caller). If the current Perl code wasn't called from WebAssembly, then it will return undef. If $index is given, then that indicates how many WebAssembly call frames to go back before the current one. (This is vaguely similar to how the Perl caller function works).

This function is exported by default using Exporter.

METHODS

export_get

 my $extern = $caller->export_get($name);

Returns the Wasm::Wasmtime::Extern for the named export $name. As of this writing, only Wasm::Wasmtime::Memory types are supported.

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.