我有以下在 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
但是我有二進制 wasm 檔案,我如何在 ->new 里面指向它而不是 WAT 文本?
uj5u.com熱心網友回復:
正如基思在他的評論中提到的那樣,訣竅是只給出一個file
論點而不是一個wat
論點Wasm::Wasmtime::Module->new
。此代碼段將您提供的 WAT 轉換為磁盤.wasm
檔案,然后加載并運行它。如果您已經擁有該.wasm
檔案,那么顯然您不需要使用wat2file
所示的小功能:
use Wasm::Wasmtime;
my $filename = 'myfile.wasm';
# this is just to make your WAT text into a disk WASM file, making this self-contained
# don't use it if you already have a .wasm file already!
my $wat = q{
(module
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
)
};
wat2file($filename, $wat);
my $store = Wasm::Wasmtime::Store->new;
my $module = Wasm::Wasmtime::Module->new($store->engine, file => $filename);
my $instance = Wasm::Wasmtime::Instance->new($module, $store);
my $add = $instance->exports->add;
print $add->call(1,2), "\n"; # 3
sub wat2file {
my ($filename, $wat) = @_;
require Wasm::Wasmtime::Wat2Wasm;
open my $fh, '>', $filename;
print $fh Wasm::Wasmtime::Wat2Wasm::wat2wasm($wat);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464652.html