如何停止从 wee8 中导入的主机函数执行嵌入式代码?
How to stop embedded code execution from imported host function in wee8?
wee8(v8 的 wasm api)是否有办法停止从导入的宿主函数中执行嵌入式访客函数?
示例:
auto hostFunc(const wasm::Val args[], wasm::Val results[]) -> wasm::own<wasm::Trap> {
std::cout << "host called!" << std::endl;
if (<some condition>) {
// Stop the execution of the guest code, return control to host.
}
return nullptr;
}
嵌入代码取自 v8 的示例,减去一些检查和打印(以缩短代码段)。
void embedWasmCode(wasm::vec<byte_t> binary) {
auto store_ = wasm::Store::make(engine.get());
auto store = store_.get();
auto module = wasm::Module::make(store, binary);
auto func_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make()
);
auto callback = wasm::Func::make(store, func_type.get(), hostFunc);
wasm::Extern* imports[] = {callback.get()};
auto instance = wasm::Instance::make(store, module.get(), imports);
auto exports = instance->exports();
auto run_func = exports[0]->func();
auto res = run_func->call();
// When the execution of run_func gets to the host function, the host function
// stops run_func's execution if some condition is met and execution returns
// to the host, here.
// ... more code
}
例如,Wasmer 具有记录在案的此功能 here。
是有办法的,当然可以在示例中找到。
来自trap.cc:
auto fail_callback(
void* env, const wasm::Val args[], wasm::Val results[]
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl;
auto store = reinterpret_cast<wasm::Store*>(env);
auto message = wasm::Name::make(std::string("callback abort"));
return wasm::Trap::make(store, message);
}
通过 return 从主机函数中获取陷阱对象,来宾代码(调用导入的主机函数)的执行将停止,控制权将 return 交给主机。
wee8(v8 的 wasm api)是否有办法停止从导入的宿主函数中执行嵌入式访客函数?
示例:
auto hostFunc(const wasm::Val args[], wasm::Val results[]) -> wasm::own<wasm::Trap> {
std::cout << "host called!" << std::endl;
if (<some condition>) {
// Stop the execution of the guest code, return control to host.
}
return nullptr;
}
嵌入代码取自 v8 的示例,减去一些检查和打印(以缩短代码段)。
void embedWasmCode(wasm::vec<byte_t> binary) {
auto store_ = wasm::Store::make(engine.get());
auto store = store_.get();
auto module = wasm::Module::make(store, binary);
auto func_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make()
);
auto callback = wasm::Func::make(store, func_type.get(), hostFunc);
wasm::Extern* imports[] = {callback.get()};
auto instance = wasm::Instance::make(store, module.get(), imports);
auto exports = instance->exports();
auto run_func = exports[0]->func();
auto res = run_func->call();
// When the execution of run_func gets to the host function, the host function
// stops run_func's execution if some condition is met and execution returns
// to the host, here.
// ... more code
}
例如,Wasmer 具有记录在案的此功能 here。
是有办法的,当然可以在示例中找到。
来自trap.cc:
auto fail_callback(
void* env, const wasm::Val args[], wasm::Val results[]
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl;
auto store = reinterpret_cast<wasm::Store*>(env);
auto message = wasm::Name::make(std::string("callback abort"));
return wasm::Trap::make(store, message);
}
通过 return 从主机函数中获取陷阱对象,来宾代码(调用导入的主机函数)的执行将停止,控制权将 return 交给主机。