Calling into C/C++ Code
Lets assume I have a C library compiled to wasm with emscripten. With javascript I can call into C ABI using Module.ccall('function_name'). I did not test wrapping hat into a js! macro but It I guess that this would be unnecessarily slow.
-
Is there a better way to make a wasm to wasm call, which is cross source language? More specific: How do I call into a C wasm module using stdweb?
-
Can I still use the wasm32-unknown-unknown target for Rust when the C code is compiled with emscripten? Compiling C with the native target is kind of hard because emscripten has browser implementations of common libs (libc++, pthreads).
Is there a better way to make a wasm to wasm call, which is cross source language? More specific: How do I call into a C wasm module using stdweb?
Currently you probably have to go through JS to do that. In theory I think it should be possible to link multiple .wasm files together, but I'm not sure if any such tooling exists, and even if it did then emscripten-style .wasm modules expect a different environment than cargo-web-style .wasm modules and use their own allocators so it would most likely not work. (Dynamic linking support is supposedly a planned feature.)
Can I still use the wasm32-unknown-unknown target for Rust when the C code is compiled with emscripten?
Yes. The generated .js module is completely encapsulated.
I agree. emscripten has a huge *.js glue file that does a lot of environment setup. I guess go trough javascript for now. Thanks for your answers.
You might also want to take a look at wasm-to-rust, which can supposedly (I haven't tried it myself) compile down WASM bytecode to Rust. See here for more details.
Very interesting project. Could be used to import any library into rust if it works. But the problem is that the generated Rust code still expects an emscripten environment which probably won't work with the cargo web env. I guess I go through javascript for a while. I do not have the background to make this work.
Perhaps you can use wasmtime: https://github.com/bytecodealliance/wasmtime
They use .wat for simplicity but I'm sure you can use .wasm too
https://docs.wasmtime.dev/examples-rust-gcd.html