Hello, I am evaluating wasm as an extension to an exstiong game.
Idea beaing that people will be able to write an AI for to control an playr in RTS game. If they create good enough AI it will be considered to be added as an official part of the game. (So am not worried about security or sanboxing, because people will be running their own AI, or reviewed one)
Do you think it is an good solution to this problem?
Can you point me to some examples how to run wasm binaries from rust?
Yep, that's a pretty standard use-case for Wasm.
The docs are a good starting point -- the front page has exactly the "run Wasm from Rust" examples you're looking for: https://docs.rs/wasmtime/latest/wasmtime/
I am either blind, or the example I am looking for is well hidden. ( I looked trough the documentation book too).
Most of the examples use few lines long string as the code that will be compiled to wasm, did not yet found an example that would be loading binary.
You can pass in a .wasm
file's contents, or use Module::deserialize_file
; the constructors accept both wat text format and wasm binary format
To build a .wasm module, you'll want to refer to Rust's documentation and target wasm32-wasi
we have an example in the book for this: https://docs.wasmtime.dev/examples-rust-wasi.html
with the source in-tree in examples/
Also see the example of providing imports here: https://docs.wasmtime.dev/lang-rust.html
:O I tried to run the demo, and and if I read the compiler error corectly, it does not support i686
, I guess it is not planned, so I should look for other way to make languages talk to each other :(
unfortunately we don't have 32-bit support right now. It's a large project, months of work; it's not that we don't want it, it's just that no one has the time/resources to do it full time. If you or others are interested we're always happy to work with contributors!
https://github.com/bytecodealliance/wasmtime/blob/main/crates/runtime/src/trampolines/x86_64.rs :thinking: is this all that is needed? or will it fail on more places, once this one is resolved?
The fundamental issue is that our compiler does not have an x86-32 backend. That's the part that will take several months of work
https://github.com/bytecodealliance/wasmtime/issues/1980 for the tracking issue
:( out of luck then, do not think I can do that, figuring out correct calling convention probaly, but no way I will be able to write whole compiler :(
"just" a compiler backend, not a whole compiler, but yeah it is no small undertaking
@Jakub Bandola did you manage to make it work? I am also trying to use WASM for a modding API for a video game. You can use the wit format to pass around strings or complex structs, but there not many full minimal examples or tutorials on it unfortunately.
No, I was told by 3 or 4 wasm runtimes, that 32 bit is not going to be supported, so I given up on wasm and used HTTP+JSON instead.
@Karel Hrkal (kajacx) For whyt exactly I can/should use "wit"? I am not going to write whole compiler, that is bit too much for me.
@Jakub Bandola wit bindgen (or the component model in general) is for defining an API that the plugins (your AIs) can use to talk to your game and vice versa. This video explains it well, but it's a bit outdated.
From what I understand, you cannot run wasm modules using the wasmtime Rust crate because you have a 32 bit processor and OS? If that's the case, then I cannot help with that.
@Karel Hrkal (kajacx) the game is 32 bit, so running 64 bit code in there would be at best complicated and ugly. (Many say impossible.)
I generate the types for the other languages already with a macro, I do not think a "general" solution would be able to generate the types correctly, specially for something like C++ where I generate also functions to_json
/from_json
to be usable with nlohmann json library.
Ok, I must not be getting something. I thought wasm is 32 bit? Which part of you "code" is 64 bit, such that you cannot run it in your game?
the compiler/interpreter is only 64 bit. It can run 32 bit wasm in 64 bit environment, but can not run in 32 bit environment. :thinking: not sure how better to explain it. @Karel Hrkal (kajacx) you can read https://github.com/bytecodealliance/wasmtime/issues/1980
Last updated: Dec 23 2024 at 14:03 UTC