Skip to content

Component host functions#597

Open
william-stacken wants to merge 4 commits into
bytecodealliance:mainfrom
william-stacken:host-functions
Open

Component host functions#597
william-stacken wants to merge 4 commits into
bytecodealliance:mainfrom
william-stacken:host-functions

Conversation

@william-stacken
Copy link
Copy Markdown
Contributor

@william-stacken william-stacken commented May 8, 2026

Fixes #433

This PR introduces host functions for components, with strict type validation such that if the function signatures do not match between what the component expects and the host function, an error would be raised. Let me know what you think about the direction!

@saulecabrera
Copy link
Copy Markdown
Member

I am taking a look at this, it might take a while, since this is a considerable chunk of code to go through.

@jbourassa
Copy link
Copy Markdown
Collaborator

Let me know what you think about the direction!

Not really feedback, but a thought: wasmex uses the wit file when defining a component, instead of having a DSL for defining signatures in Elixir. Documented here.

I think it's worth considering, but haven't given it much thought recently though; maybe there's a valid reason not to go with that approach. Feel free to push back!

Copy link
Copy Markdown
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the approach looks reasonable, however, I am concerned about the maintainability and performance aspects of the type system: we are in a sense duplicating some of the type checks that normally should be handed to Wasmtime.

To say: I am not fully convinced that the UX gains justify the complexity and the potential performance hit.

I think that a reasonable alternative to explore is a variant of Option A, as described in #433, which is somewhat similar to what wasmtime-py does (IIUC) lazy value coercion checks rather than eager type checks. Trade-offs that I can think of: (i) errors will surface at function call time rather than instantiation time (ii) we lose the homogeneous nature of the Component/Core APIs.

My assumption is that it shouldn't be too hard to try out the Option A variant on top of the changes present here, IIUC, it will be mostly a matter of removing the type check that is happening at instantiation time. Having some performance numbers will probably help us finalize the decision.

I looked a bit at Wasmex's approach, and my understanding is that their API is philosophically different to ours: they do not try to stay as close to Wasmtime's API, which is why I think the approach of using a WIT file suits their needs best. Personally I was not able to convince myself that we could adopt a similar approach.

Comment thread ext/src/ruby_api/component/linker.rs Outdated
Comment thread ext/src/ruby_api/component/linker.rs Outdated
Comment thread ext/src/ruby_api/component/linker.rs Outdated
Comment thread spec/unit/component/linker_spec.rb Outdated
Comment thread ext/src/ruby_api/component/linker.rs Outdated
@william-stacken william-stacken force-pushed the host-functions branch 2 times, most recently from 4b6d60b to 2bf92f3 Compare May 19, 2026 12:34
@william-stacken
Copy link
Copy Markdown
Contributor Author

I have now removed the instantiation-time type validations. I also added a "type wrapper" class for return values of the host functions as described in Option A of the issue, which allows wasmtime to perform type checks. With this approach, the host functions can perform type validations and raise an exception if they wish.

Comment thread spec/unit/component/linker_spec.rb
Comment thread spec/unit/component/linker_spec.rb Outdated
Comment thread ext/src/ruby_api/component/linker.rs Outdated
Comment thread spec/unit/component/linker_spec.rb
Comment thread ext/src/ruby_api/component/linker.rs Outdated
Copy link
Copy Markdown
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is looking good, thanks for iterating on this. One last thing: I think we should add a tests that exercises a void return function invocation? I do not see one for the integration tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Component host functions

3 participants