You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add component model support to the c-api (particularly the ability run a component from a c-api host).
Benefit
Much of the WebAssembly world is currently (re)tooling around the component model as it approaches MVP. The ability to run components will quickly become more critical with the finalization of the wasi preview 2 abi. Currently C/C++ is the largest fully-supported language with a clear path to producing component guests (through wit-bindgen) that does not have a path to run from a host in the same language.
Implementation
I can take a first pass at this modeled roughly around the Rust crate with the current c-api idioms (for example wasmtime_config_wasm_component_model(conf, true);, a component aware linker (perhaps wasmtime_linker_define_component_model(linker);), etc. I haven't looked deep enough into the implementation yet to see if there is significant core functionality to enable this past the standard glue code and lifecycle management for c objects.
Alternatives
Eventually this will become necessary - it's a matter of timing. I suspect that this has just not yet been implemented due to the instability of wasmtime::component.
CC: Discussed with @sunfishcode at the BA hackathon
Thanks for opening an issue for this! You're right in that this hasn't quite bubbled to the top of any priority list yet which is why it isn't done, but it's something that definitely should get done!
My prediction for the main difficulty of this feature will be how to transfer values back and forth. The type grammar of core wasm is quite simple which made this a non-issue for core wasm's C API support, but for components it's significantly different. The easiest route is probably going to be to mirror Val into C more-or-less. This will not be really all that efficient in terms of calls because datatypes like lists will be copied in/out and everything will be dynamically type-checked. For example I don't know how wasmtime_func_call_unchecked could get a C/C++ equivalent that skips all the type-checks. That's not a showstopper for any sort of initial support, however. Still though there's tricky questions around how exactly memory management will work with values. I'm not sure the best answer here.
More minor it might be worth bikeshedding a bit to avoid a wasmtime_component_* prefix on everything which is quite long. Not that I have much of an idea about a better name...
I'm going to close this issue in favor of more specific open issues since thanks to @MangoPeachGrape the component model is pretty well supported in the C API now that I think it's best to focus on specific issues rather than the general concept.
@planetbeing - at the time I wrote those docs for C support in the component model, no progress had been made on this work here. We switched to the Rust crate a couple of years ago and I haven't kept up with it.
It looks like no examples were added to the Wasmtime C API docs for @MangoPeachGrape 's component model work. Looking quickly through the autogenerated doxygen docs here (wasmtime_component*), the support looks fairly complete, and analogous to the Component Model API in the Rust crate.
I'd suggest following along with the Rust crate docs, substituting the C API types above and see how far that gets you.
I'd be willing to update the Component Model API docs this month, but will need to try it and build some minimal examples first.
The bigger lift of course would be having a c wasmtime-bindgen generate host-side bindings, and other wasmtime sugar, so host.c could look somewhat more like the python version. I like the python version 🙂
#include<stdio.h>#include<stdlib.h>#include<wasmtime.h>#include<wit/adder.h>intmain(intargc, char*argv[]) {
if (argc!=4) {
fprintf(stderr, "Usage: %s <x> <y> <component.wasm>\n", argv[0]);
return1;
}
uint32_tx= (uint32_t)atoi(argv[1]);
uint32_ty= (uint32_t)atoi(argv[2]);
constchar*path=argv[3];
wasmtime_store_t*store=wasmtime_wasip2_engine_with_store((wasmtime_simple_cfg){});
wit_docs_adder_t*adder=wit_docs_adder(
(wit__instantiate){.store=store, .load_file=path}
);
// The sauce. `adder.add->add(adder,x,y)` if you *really* have to.uint32_tresult=adder.add->add(x,y);
printf("%u + %u = %u\n", x, y, result);
wit__free_instance(adder);
wasmtime_free_everything(store);
}
though of course retaining a imports_docs_adder_add_add(&instance, context, x, y) variant whose only runtime indirection/polymorphism is the Val stuff, which some future wasmtime could theoretically JIT away.
Activity
pchickey commentedon Sep 10, 2023
The Nginx Unit team is also interested in this
github-actions commentedon Sep 11, 2023
Subscribe to Label Action
cc @peterhuene
Details
This issue or pull request has been labeled: "wasmtime:c-api"Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the
.github/subscribe-to-label.jsonconfiguration file.Learn more.
alexcrichton commentedon Sep 11, 2023
Thanks for opening an issue for this! You're right in that this hasn't quite bubbled to the top of any priority list yet which is why it isn't done, but it's something that definitely should get done!
My prediction for the main difficulty of this feature will be how to transfer values back and forth. The type grammar of core wasm is quite simple which made this a non-issue for core wasm's C API support, but for components it's significantly different. The easiest route is probably going to be to mirror
Valinto C more-or-less. This will not be really all that efficient in terms of calls because datatypes like lists will be copied in/out and everything will be dynamically type-checked. For example I don't know howwasmtime_func_call_uncheckedcould get a C/C++ equivalent that skips all the type-checks. That's not a showstopper for any sort of initial support, however. Still though there's tricky questions around how exactly memory management will work with values. I'm not sure the best answer here.More minor it might be worth bikeshedding a bit to avoid a
wasmtime_component_*prefix on everything which is quite long. Not that I have much of an idea about a better name...rajsite commentedon Sep 4, 2024
related #8036
alexcrichton commentedon Aug 21, 2025
I'm going to close this issue in favor of more specific open issues since thanks to @MangoPeachGrape the component model is pretty well supported in the C API now that I think it's best to focus on specific issues rather than the general concept.
planetbeing commentedon Jan 4, 2026
I was linked to this issue by https://github.com/bytecodealliance/component-docs/blob/4e906915e9a1020657f1cf7b193cb9fd2847d7e6/component-model/src/language-support/building-a-simple-component/c.md?plain=1#L329 but since this issue is closed, the trail is cold for me to discover how to host components with C/C++. The comment that "thanks to @MangoPeachGrape the component model is pretty well supported in the C API now" is heartening but I'm at a loss as to how to use that support or even what code changes those are referring to so I can start investigating. Where can I start looking for information on this?
seanisom commentedon Jan 4, 2026
@planetbeing - at the time I wrote those docs for C support in the component model, no progress had been made on this work here. We switched to the Rust crate a couple of years ago and I haven't kept up with it.
It looks like no examples were added to the Wasmtime C API docs for @MangoPeachGrape 's component model work. Looking quickly through the autogenerated doxygen docs here (
wasmtime_component*), the support looks fairly complete, and analogous to the Component Model API in the Rust crate.I'd suggest following along with the Rust crate docs, substituting the C API types above and see how far that gets you.
I'd be willing to update the Component Model API docs this month, but will need to try it and build some minimal examples first.
ohAitch commentedon Feb 27, 2026
Here's a starting point, if you'll permit me the use of artificial intelligence technologies to wrangle boilerplate:
host.c
Dockerfile (optional)
Note the docs describe the checked-in add.wasm https://github.com/bytecodealliance/component-docs/blob/main@{2026-2-26}/component-model/examples/example-host/add.wasm
which is out of date with the versioned/namespaced
docs:adder/add@0.1.0 {add}expected by the example hostHowever,
tutorial/c/adderandtutorial/adder(rust) both work when built.ohAitch commentedon Feb 28, 2026
The bigger lift of course would be having a c
wasmtime-bindgengenerate host-side bindings, and other wasmtime sugar, sohost.ccould look somewhat more like the python version. I like the python version 🙂Spitballing some API here
though of course retaining a
imports_docs_adder_add_add(&instance, context, x, y)variant whose only runtime indirection/polymorphism is the Val stuff, which some future wasmtime could theoretically JIT away.