Stream: wasmtime

Topic: ✔ Creating a TypedFunc for a component


view this post on Zulip i509VCB (Oct 02 2023 at 17:39):

I'm not sure why I am getting errors about ComponentNamedList.

For reference I am trying to get a TypedFunc for this: https://github.com/i509VCB/aerugo/blob/ab79099ba124da6f57a021d1f7357e00f45ffce6/wm.wit#L12-L16

And I have this so far:

struct WmFuncs {
    new_toplevel: TypedFunc<Resource<Toplevel>, ()>,
    closed_toplevel: TypedFunc<ToplevelId, ()>,
    update_toplevel: TypedFunc<(ToplevelId, ToplevelUpdates), ()>,
    ack_toplevel: TypedFunc<(ToplevelId, u32), ()>,
    committed_toplevel: TypedFunc<(ToplevelId, Option<Resource<Snapshot>>), ()>,
    key: TypedFunc<(u32, u32, Option<String>, KeyStatus), KeyFilter>,
    key_modifiers: TypedFunc<KeyModifiers, ()>,
    new_output: TypedFunc<Resource<Output>, ()>,
    disconnect_output: TypedFunc<OutputId, ()>,
}

My suspicion is that I'm forgetting something here?

view this post on Zulip i509VCB (Oct 02 2023 at 17:40):

The compiler error:

error[E0277]: the trait bound `wasmtime::component::Resource<host::aerugo::wm::types::Toplevel>: ComponentNamedList` is not satisfied
   --> crates/wm-runtime/src/lib.rs:300:36
    |
300 |             new_toplevel: instance.get_typed_func(store, "new-toplevel")?,
    |                                    ^^^^^^^^^^^^^^ the trait `ComponentNamedList` is not implemented for `wasmtime::component::Resource<host::aerugo::wm::types::Toplevel>`
    |

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:41):

are you using wasmtime::component::bindgen?

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:41):

change new_toplevel: TypedFunc<Resource<Toplevel>, ()>, to new_toplevel: TypedFunc<(Resource<Toplevel>,), ()>, I think

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:41):

or yeah use bindgen! if you can

view this post on Zulip i509VCB (Oct 02 2023 at 17:41):

Pat Hickey said:

are you using wasmtime::component::bindgen?

yes

view this post on Zulip i509VCB (Oct 02 2023 at 17:42):

Although I get nothing from bindgen to load those typed funcs from my scan of the expanded macro?

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:42):

actually alex got it, its the 1-tuple thing

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:42):

is the output of bindgen! what's failing to compile?

view this post on Zulip i509VCB (Oct 02 2023 at 17:42):

Alex Crichton said:

is the output of bindgen! what's failing to compile?

No

view this post on Zulip i509VCB (Oct 02 2023 at 17:42):

Given the mention of bindgen! I'm wondering if I skipped over something that already has the types of the TypedFunc I am trying to load

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:43):

ah ok, if you're looking for features in the generated code please feel free to file issues for those

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:43):

bindgen will generate a WmTypes::Wm trait that you will need to impl to provide each of those funcs.

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:43):

it depends on the structure of the WIT and also what you're doing specifically, but if you find that bindgen! doesn't work for you then we should soup up bindgen! on our end to be more featureful most likely

view this post on Zulip i509VCB (Oct 02 2023 at 17:44):

Alex Crichton said:

it depends on the structure of the WIT and also what you're doing specifically, but if you find that bindgen! doesn't work for you then we should soup up bindgen! on our end to be more featureful most likely

Yeah ideally bindgen! would generate a type containing all the TypedFuncs for the resource returned from wasm like I have done

view this post on Zulip i509VCB (Oct 02 2023 at 17:45):

Pat Hickey said:

bindgen will generate a WmTypes::Wm trait that you will need to impl to provide each of those funcs.

And there is a Wm type but that does not contain anything from the resource.

pub struct WmTypes {
          get_info:wasmtime::component::Func,create_wm:wasmtime::component::Func,
        }

view this post on Zulip i509VCB (Oct 02 2023 at 17:45):

It's just the two functions from the interface that that contains

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:45):

why do you want it to work that way? can you instead define a struct MyWm and impl wm_types::Wm for MyWm ?

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:45):

oh I believe these are exports instead of imports

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:45):

imports get traits but exports are stored as funcs

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:46):

@i509VCB unfortunately given lifetimes it's not possible to store those as TypedFunc, but within each call_* method you'll see that the first thing it does is cast to a typed func.

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:46):

there will be a wm_types::Host trait that requires an impl of any functions in wm-types.

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:46):

but we could add accessors such as bindings.func_* which returns &TypedFunc<...>

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:47):

@Pat Hickey that's only if wm-types was an import though? Given the Func I think these are exports?

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:47):

wm_types::Host should just contain fn create_wm(...) ...

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:47):

oh! of course.

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:47):

sorry, i read these wrong.

view this post on Zulip i509VCB (Oct 02 2023 at 17:47):

There is a HostWm trait that's generated but it's entirely empty lol

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:48):

which part of this are you trying to implement on the host, and which part in a wasm component

view this post on Zulip i509VCB (Oct 02 2023 at 17:48):

The wasm component implements the wm resource and I call it from the host.

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:48):

ok, got it.

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:49):

so there should be somestruct AerugoWm defined by bindgen!.

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:49):

is there a reason the call_* methods won't work for you? e.g. how come you want access to the TypedFunc?

view this post on Zulip Pat Hickey (Oct 02 2023 at 17:50):

and that will have a fn aerugo_wm_wm_types(&mut store) which should then be a struct WmTypes and WmTypes should have a fn create_wm

view this post on Zulip i509VCB (Oct 02 2023 at 17:50):

Alex Crichton said:

is there a reason the call_* methods won't work for you? e.g. how come you want access to the TypedFunc?

No call methods exist for the functions on the resource wm: This is what the expanded macro generates: https://gist.github.com/i509VCB/13ebc08130e7b190c9b699d7102a7184

GitHub Gist: instantly share code, notes, and snippets.

view this post on Zulip i509VCB (Oct 02 2023 at 17:50):

Unless this was added recently and I need to update wasmtime again

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:53):

Are you using main or are you using 13.0.0?

view this post on Zulip i509VCB (Oct 02 2023 at 17:53):

13.0.0

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:53):

ah ok try using main, and I think you should see a new accessor for the resource methods

view this post on Zulip Alex Crichton (Oct 02 2023 at 17:54):

support for exported resources didn't make it into 13.0.0

view this post on Zulip i509VCB (Oct 02 2023 at 17:54):

Okay I'll give a look at main, this is still bleeding edge so it's not unexpected

view this post on Zulip i509VCB (Oct 02 2023 at 18:14):

Yeah main generates the stuff. Thank you!

view this post on Zulip Notification Bot (Oct 02 2023 at 18:16):

i509VCB has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC