我有以下代码,这些代码在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文件,我怎么能指向它而不是WAT,在->new里面有什么想法?
发布于 2022-04-13 20:43:26
正如基思在他的评论中所暗示的那样,诀窍是只给出一个file论点,而不是给Wasm::Wasmtime::Module->new一个wat论点。这个片段将您提供的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://stackoverflow.com/questions/71850080
复制相似问题