Skip to content
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

Revamp wasi example and related docs #9788

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

ifsheldon
Copy link

@ifsheldon ifsheldon commented Dec 11, 2024

Closes #9777

For now, this is a draft PR because there's a runtime issue in the revamp example. Please see the FIXME in examples/wasi/main.rs. There I want to invoke the exported wasi:cli/[email protected] function, which is the main function in examples/wasi/wasm/wasi.rs. Probably @alexcrichton or @fitzgen can help a bit? Thanks!

When the runtime issue is fixed, I will try to revamp more sections in the related doc in following commits

Updated:

This PR revamps the examples wasi and wasi-async with latest updates from wasmtime and wasmtime-wasi and uses WASIp2 APIs by default. Related documentation is also updated.

@github-actions github-actions bot added the wasmtime:docs Issues related to Wasmtime's documentation label Dec 11, 2024
docs/examples-rust-wasi.md Outdated Show resolved Hide resolved
examples/wasi/main.rs Outdated Show resolved Hide resolved
@ifsheldon
Copy link
Author

But now there comes another issue: this hello-world example is just too specific to wasi:cli, since we used a specific wasmtime_wasi::bindings::sync::Command to run a wasi:cli/command. For a more general component, we need to add another example or at least give a pointer.

BTW, I still don't know why I could not get a Func to execute the run function in wasi:cli/run.

@alexcrichton
Copy link
Member

Personally I think it's reasonable to start with hello-world like this and then graduate up to other examples. For example this page could link to the bindgen_examples page and that could get expanded with a custom world for example (IIRC there may already be one there?)

BTW, I still don't know why I could not get a Func to execute the run function in wasi:cli/run.

You needed another invocation of get_export. You loaded the exported instance but you'll need to load the exported function of that instance as well.

@ifsheldon ifsheldon marked this pull request as ready for review December 14, 2024 12:59
@ifsheldon ifsheldon requested review from a team as code owners December 14, 2024 12:59
@ifsheldon ifsheldon requested review from dicej and removed request for a team December 14, 2024 12:59
@ifsheldon
Copy link
Author

OK, I think this draft PR seems good to me now, so I turned it into a formal PR.

@ifsheldon
Copy link
Author

ifsheldon commented Dec 14, 2024

You needed another invocation of get_export. You loaded the exported instance but you'll need to load the exported function of that instance as well.

OK, got that. I also added related code in the example to demonstrate how to do that.

BTW, do you know why ComponentNamedList is implemented for tuple (A1,) but not A1? Should I file another issue for this? This is bit counter-intuitive when I want to write something like func.typed::<(), Result<(), ()>>(&store), which rustc complains about not implementing ComponentNamedList. I really didn't know how to fix this error until I lookup Implementations on Foreign Types section of ComponentNamedList.

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.

Thanks!

This is bit counter-intuitive when I want to write something like func.typed::<(), Result<(), ()>>(&store),

This is currently intentional in the sense that it's in theory preferred to use the output of bindgen! where you don't have to deal with this. In that sense the raw component APIs weren't designed to be the most ergonomic and easy-to-use since that's where bindgen! comes in to close the gap.

IIRC there were coherence with that trait impl, but I could be misremembering as well.

docs/examples-rust-wasi.md Outdated Show resolved Hide resolved
docs/examples-rust-wasi.md Outdated Show resolved Hide resolved
docs/examples-rust-wasi.md Outdated Show resolved Hide resolved
examples/wasi/main.rs Outdated Show resolved Hide resolved
@alexcrichton alexcrichton added this pull request to the merge queue Dec 16, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 16, 2024
@ifsheldon
Copy link
Author

ifsheldon commented Dec 17, 2024

Oops. CI complains about file not found target/wasm32-wasip2/debug/wasi.wasm, so I guess CMakeLists also needs to be updated.

Furthermore, I digged into examples/wasi/main.c and related c-api crate. It seems c-api crate also needs some renovation.........

For example, c-api exposes this function, which links wasip1 interfaces, but no other functions link wasip2 interfaces.

pub extern "C" fn wasmtime_linker_define_wasi(

I think removing preview1 feature from this line and fixing whatever breaks should be enough. This introduces API breaking changes, though.

wasmtime-wasi = { workspace = true, optional = true, features = ["preview1"] }

I'd rather like to skip this error first and open another tracking issue.

@alexcrichton alexcrichton added this pull request to the merge queue Dec 17, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 17, 2024
@ifsheldon
Copy link
Author

Can you take a look at the CI error Test C-API macos-latest? I think I made a right correction on CMakeList.txt but I don't know what else needs to be fixed.

@alexcrichton
Copy link
Member

Have you tried running the example locally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:docs Issues related to Wasmtime's documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renovate the WASI example
2 participants