Stream: wasmtime

Topic: wasm as extension for a game


view this post on Zulip Jakub Bandola (Oct 06 2023 at 16:19):

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?

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:28):

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/

view this post on Zulip Jakub Bandola (Oct 06 2023 at 16:36):

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.

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:41):

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

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:41):

To build a .wasm module, you'll want to refer to Rust's documentation and target wasm32-wasi

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:42):

we have an example in the book for this: https://docs.wasmtime.dev/examples-rust-wasi.html

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:42):

with the source in-tree in examples/

view this post on Zulip Chris Fallin (Oct 06 2023 at 16:44):

Also see the example of providing imports here: https://docs.wasmtime.dev/lang-rust.html

view this post on Zulip Jakub Bandola (Oct 06 2023 at 17:08):

: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 :(

view this post on Zulip Chris Fallin (Oct 06 2023 at 17:12):

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!

view this post on Zulip Jakub Bandola (Oct 06 2023 at 17:15):

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?

view this post on Zulip Chris Fallin (Oct 06 2023 at 17:17):

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

view this post on Zulip Chris Fallin (Oct 06 2023 at 17:18):

https://github.com/bytecodealliance/wasmtime/issues/1980 for the tracking issue

We don't currently have fully working x86 support, but we have a backend in-tree that's largely completely. However, it uses the old instructions selection framework, and as described in #1936, Cra...

view this post on Zulip Jakub Bandola (Oct 06 2023 at 17:19):

:( 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 :(

view this post on Zulip fitzgen (he/him) (Oct 06 2023 at 19:59):

"just" a compiler backend, not a whole compiler, but yeah it is no small undertaking

view this post on Zulip Karel Hrkal (kajacx) (Jan 02 2024 at 14:23):

@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.

view this post on Zulip Jakub Bandola (Jan 02 2024 at 14:32):

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.

view this post on Zulip Jakub Bandola (Jan 02 2024 at 14:40):

@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.

view this post on Zulip Karel Hrkal (kajacx) (Jan 02 2024 at 15:05):

@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.

view this post on Zulip Jakub Bandola (Jan 02 2024 at 15:15):

@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.

GitLab.com

view this post on Zulip Karel Hrkal (kajacx) (Jan 03 2024 at 11:07):

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?

view this post on Zulip Jakub Bandola (Jan 03 2024 at 11:13):

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

We don't currently have fully working x86 support, but we have a backend in-tree that's largely completely. However, it uses the old instructions selection framework, and as described in #1936, Cra...

Last updated: Oct 23 2024 at 20:03 UTC