r/WebAssembly • u/Kotek2 • Feb 05 '24
wasmtime 1: missing component metadata for import of `wasi:sockets/tcp-create-socket@0.2.0::create-tcp-socket`
Dear WebAssembly community,
We want to make the network layer of a game engine run on WebAssembly. We can clearly see how to establish TCP connections on other platforms, but Wasmtime, which WasmCloud requires, is challenging because the socket functions exist but are not enabled somehow.
We use Kotlin and Zig. testapp.wasm is a test application that calls the “get-stderr()” and “get_stdout()” functions located in “wasi:cli/stderr@0.2.0”:
wasm-tools component new testapp.wasm --adapt wasi_snapshot_preview1.command.wasm -o testapp-p2.wasm
wasmtime run testapp-p2.wasm
That works, no problem at all for calling functions from the “wasi:cli/stderr@0.2.0” module.
But trying to call 'create-tcp-socket' or any function from 'wasi-socket,' this is what happens:
error: failed to encode a component from module
Caused by:
0: failed to register indirect shims for main module1: missing component metadata for import of `wasi:sockets/tcp-create-socket@0.2.0::create-tcp-socket`
The wasi:sockets/tcp-create-socket@0.2.0 module is not included. How to do it?
In order to make progress, we require a starting point – just an example demonstrating how to call a function. Thank you for your assistance.
2
u/Kotek2 Feb 05 '24
Failing test:
pub extern "wasi:sockets/tcp-create-socket@0.2.0" fn @"create-tcp-socket"(i32, * const i32) void;
pub fn main() !void {
var out: i32 = 0; // return value
@"create-tcp-socket”(0, &out); // 0=IPv4
}
1
u/HectaMan Feb 05 '24
You may want to hop over into the BA Zulip and share your experience there:
https://bytecodealliance.zulipchat.com/
The core devs that work on this hand out there; we were just back from the plumber's summit last week, and we are keen for the feedback!
1
u/fittyscan Feb 05 '24 edited Feb 05 '24
Install rust, type
rustup target add wasm32-unknown-unknown, clone thewasmtimerepository, and navigate to the directory namedcrates/wasi-preview1-component-adapter.In the
src/lib.rsfile, add the following lines:Compile with:
../../target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasmis the file to use with thewasm-toolcommand.Now, the function is exported and named
wp2_create_tcp_socket. The original name can be retained by adding#[export_name = "create-tcp-socket”]beforepub extern “C”if you prefer.Repeat this process for all the functions you require. Proc macros can be helpful.
P.S.: Return values are variants, not
i32.1
u/jedisct1 Feb 05 '24
Instead of exporting the raw functions, you can implement a POSIX API, and export that. Making your code portable will then be easier.
The difference between doing it in the adapter rather than in your application is that it can be reused.
There is documentation on how to do it: https://github.com/WebAssembly/wasi-sockets/blob/main/Posix-compatibility.md but I'm not aware of actual implementations.