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

オープン
#15,424 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
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時間
マージ済み PR(30日)
238

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bytecodealliance/wasmtime のほかの issue

bytecodealliance/wasmtime の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。