Skip to content

c-api: component-model: Resource table, WASI #11055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

MangoPeachGrape
Copy link
Contributor

I've gotten quite stuck on how to do resources, so I decided to try WASI first.

Some things I'm unsure about:

  • Should the context be inside an Option and then expect() in the WASI traits, or is the default context fine?
  • How to specify your own context, perhaps wasmtime_context_set_wasi_p2()?

On resources, I think I'm getting stuck on ResourceTypeKind::Host using TypeId.

Would all the resources use a CApiResource type on the rust side, and then use a string discriminant, or something like that?

Do you have any other ideas?

@MangoPeachGrape MangoPeachGrape requested a review from a team as a code owner June 16, 2025 21:27
@MangoPeachGrape MangoPeachGrape requested review from alexcrichton and removed request for a team June 16, 2025 21:27
@github-actions github-actions bot added the wasmtime:c-api Issues pertaining to the C API. label Jun 16, 2025
@alexcrichton
Copy link
Member

Should the context be inside an Option and then expect() in the WASI traits, or is the default context fine?

For ResourceTable I think it's ok to leave it there on-by-default, but for wasmtime_wasi::p2::WasiCtx yeah let's have that be an option which panics if it's not set.

How to specify your own context, perhaps wasmtime_context_set_wasi_p2()?

Right now wasi.h is a binding to the wasip1 builder API, and I think we'll want a new API for the wasip2 builder API (e.g. bindings for wasmtime_wasi::p2::WasiCtx::builder). Once that structure is built yeah a new API of wasmtime_context_set_wasi_p2 would take the builder and then insert it into the store. Then afterwards the Option in the store would be Some and everything should work.

On resources, I think I'm getting stuck on ResourceTypeKind::Host using TypeId.

This has tripped me up in the past as well, I think it might be reasonable to add ResourceTypeKind::HostId(u64) where the host just provides a 64-bit integer. That could be added for the C API and that way the C API could take integers to differentiate resource types?

@MangoPeachGrape
Copy link
Contributor Author

we'll want a new API for the wasip2 builder API (e.g. bindings for wasmtime_wasi::p2::WasiCtx::builder)

Where should I place this? Maybe include/wasmtime/wasi/p2.h? Or should I place it next to the include/wasi.h, i.e. include/wasi_p2.h?

And how should the naming convention be? Similar to how it is in wasi.h, e.g. wasi_p2_config_inherit_stdout(), or something more similar to the rust side, maybe wasmtime_wasi_p2_builder_inherit_stdout()?

I think it might be reasonable to add ResourceTypeKind::HostId(u64) where the host just provides a 64-bit integer. That could be added for the C API and that way the C API could take integers to differentiate resource types?

Seems like a nice solution, I think I'll try that in a next PR?

@alexcrichton
Copy link
Member

I've long wanted to move wasi.h and scope it under the "wasmtime" namespace. Additionally we try to consider "wasipN" a unit as oppose to "wasi pN", so how about include/wasmtime/wasip2.h and wasmtime_wasip2_config_*?

@MangoPeachGrape
Copy link
Contributor Author

Does that look correct?

I've long wanted to move wasi.h and scope it under the "wasmtime" namespace

Should I do that also here? Would that include adding wasmtime_wasip1_ prefix to everything?

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Does that look correct?

Looks good! Just one minor rename, and with some comments in the headers for the new functions looks good. Mind adding a test or two as well? (doesn't need to be too too fancy)

I've long wanted to move wasi.h and scope it under the "wasmtime" namespace

Should I do that also here? Would that include adding wasmtime_wasip1_ prefix to everything?

Nah let's save that for later. At this point it's probably best to just not touch it and achieve these goals with the updates going forward. I don't think there's any real need to rename the preexisting APIs at this time.

@alexcrichton alexcrichton added this pull request to the merge queue Jun 18, 2025
Merged via the queue into bytecodealliance:main with commit 69c01c5 Jun 18, 2025
160 checks passed
@MangoPeachGrape MangoPeachGrape deleted the c-api/component-model/resource-table branch July 1, 2025 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:c-api Issues pertaining to the C API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants