component::bindgen: Impossible to call async functions on host in imported async functions

未关闭
#15,424 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
12/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
活跃
技术栈
rust, wasm

调研方向

The question centres on the bindgen! macro's imports block with async | trappable and the HostWithStore impl for HasSelf<PluginHost>, where evaluate_expression2 receives an &Accessor<T, Self>. Start with the Accessor::with and Accessor::spawn methods, and with the _7_async bindgen example the issue links to. Done would mean either a supported way for an async host function to await async host work, or a documented answer to that question. The issue is unresolved and needs a maintainer decision on the API first.

由索引模型根据 Issue 内容生成。

描述

wasm-proposal:component-model wasm-proposal:component-model-async wasmtime:api

component::bindgen! supports async for imported functions in two ways. First, in that it allows async implementations for any Wasm function. Second, in that it supports implementations of async Wasm functions.

I'm having a hard time with the latter because it requires the use of a WithStore interface, which in turn allows access to the host object via an Accessor. The Accessor deliberately only allows blocking access to the host, but I need async access to it. Ironically, perhaps, this is not a problem with blocking Wasm functions. But from what I can tell, it is simply impossible with async Wasm functions.

Here's an example.

From the WIT file:

...
interface host {
    evaluate-expression1: func(expression: string) -> result<option<string> string>;
    evaluate-expression2: async func(expression: string) -> result<option<string>, string>;
}

Generating the bindings:

wasmtime::component::bindgen!({
    ...
    imports: {
        "test:plugins/host.evaluate-expression1": async | trappable,
        "test:plugins/host.evaluate-expression2": async | trappable,
    }
});

Implementation:

impl imported::Host for PluginHost
{
    async fn evaluate_expression1(
        &mut self,
        expression: String,
    ) -> wasmtime::Result<Result<Option<String>, String>> {
        // No problem calling an async function on self here!
        Ok(self.evaluate_expression_impl(expression).await)
    }
}

impl imported::HostWithStore<T> for HasSelf<PluginHost>
{
    async fn evaluate_expression2(
        accessor: &Accessor<T, Self>,
        expression: imported::String,
    ) -> wasmtime::Result<Result<Option<String>, String>> {
        // How do I call evaluate_expression_impl here???
        todo!()
    }
}

I'm stuck. Accessor::with only supports blocking code and Accessor::spawn doesn't change that. The example code doesn't help, because it only shows blocking code. (I'm confused about this example. What is the use case for a blocking implementation of an async Wasm function?)

I think I understand the technical limitation. Imported async Wasm functions are being "driven" by the async implementation of the component, which the implementation does not control.

What can I do? Is this really impossible? My use case seems like it would be quite common. It seems to me that there should be a way to "hand over" my host async function to the component's async implementation to "drive". Or maybe I'm misunderstanding the situation entirely. :)

I would humbly suggest that if there is a solution, it should be shown in the example I linked above.

主要语言
Rust
星标
18.9k
派生
2k
平均合并
1 天 6 小时
30 天内合并 PR
238

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wasmtime 的其他 Issue

查看 bytecodealliance/wasmtime 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。