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

Đang mở
#15,424 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, wasm
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
18.9k
Fork
2k
Merge trung bình
1 ngày 6 giờ
Pull request đã merge (30 ngày)
238

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bytecodealliance/wasmtime

Tất cả issue của bytecodealliance/wasmtime

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.