-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
Add support for wasm32-unknown-unknown
#23
Comments
The panic is generated by the |
There seem to be 3 main projects to get lua working on wasm with https://github.com/ceifa/wasmoon seeming to be the only one that recently had changes |
I would love to see this implemented! Could somebody summarise the necessary tasks needing to be done for this to run on wassm ? |
Step 1: get Lua to compile to WASM or use one of the existing projects for that. Step 2: find a way to correctly compile said lua VM to wasm together with mlua. Presumably you want them to get compiled in such a way that they share the same WASM memory (So... probably need to end up with 1 wasm file?) to reduce the overhead of FFI. Step 3: Find some way to get mlua to communicate with the bundled lua. (I am assuming that the normal API's won't work, or that at least some extra plumbing is required.) Also, there are JS implementations of Lua. Those might be easier to get working but are probably slower than a version compiled in wasm, especially if the wasm version can use the same memory as the rest of the program. https://github.com/fengari-lua/fengari-web is used by the teal playground. Also, assuming you indeed want to reduce FFI overhead as much as possible and thus make the luavm use the same memory as the rest of the program, then I would imagine that at most only 1 instance of the VM is running. As a result, the functions to create a new Lua instance should probably be disabled and instead replaced with one to get a global instance. |
Thanks @lenscas! Hmm would having many lua vm's be very bad in terms of efficiency? ideally the API stays the same between targets, sharing VM's between scripts is very messy and foot-gunny, unless it's possible to somehow blackbox each script within the vm |
I would expect that if you want to spawn new VM's that they all have their own memory. Which means that the only way to "share" memory is by copying it over. I'm also not sure if 2 WASM modules can communicate directly with each other. If they can't, you are looking at going through JS. Which means that every value needs to be copied twice. Lua itself has ways to do sandboxing. Though doing that properly is of course not the easiest. |
I will try to add some info. anyway, both of them are useless for our usecase. if you separate out the lua vm from the main rust app as two individual wasm modules, there's nothing for mlua to do. both modules need to share memory and communicate with each other smh, but its more like a lua app and rust app talking to each other and not "embedding" lua into rust app. I cannot know for sure, but you should be able to embed a lua vm inside rust wasm (no JIt though). there's two ways: Second, use a C lua VM (bindings) and compile it to wasm. like i said, now, my C/C++ (or cmake) skills are absolute garbage, so, dealing with lua54's building process (by copying from wasmoon) is too hard for me. BUT, fortunately,
use mlua::Lua;
fn main() {
println!("inside main");
for i in 0..5 {
Lua::new()
.load(&format!("print('hello lua, times: {i}')"))
.exec()
.expect("failed to execute lua");
}
}
<script src="./target/wasm32-unknown-emscripten/debug/mlua.js"></script>
<h1>Hello World</h1>
now, technically, lua54 is also doable because wasmoon can do it. but i will leave that task to someone who can understand what's happening in https://github.com/ceifa/wasmoon/blob/main/build.sh . just to point out again, this issue is about compilation to PS: #40 seems to be a duplicate of this. |
a lot of crates usually only support |
@coderedart fantastic and thorough exploration, thank you! winit emscripten support seems to be a no-go, and so for engines like bevy I imagine it's an unlikely solution, so it looks like the only way for this to be useful in engines like |
reply in that thread
i guess this issue will go back to hibernation. i will just keep my eye on hematita. |
I plan soon to start porting Lua 5.3 from C to Rust which should bring
hematita is mostly a POC than a real implementation. It even does not has GC support. |
I am happy to get anything, but just wanted to know why lua 5.3 instead of 5.1 (which works on luau / luajit)? the whole internet including apps like mpv seem to stick to 5.1 / 5.2 because of backwards compatibility. |
I rather see 5.3 on the Web than 5.1. There js a compatible library that brings 5.1 and luaJIT to 5.3 compatibility wise. I don't think something similar exists for 5.3 to 5.1. |
That would be interesting, but also sounds like a big project, Are there any attempts at bringing lua to pure rust which are somewhat complete? It's probably worth reaching out to people who have tried this before. |
https://github.com/cjneidhart/lua-in-rust Seems to be a wip lua impl |
There are many attempts to implement Lua on pure Rust but unfortunately all of them are not finished and/or ready for production use. Unfortunately Lua is not very easy to port/rewrite. There are lots of macros in the source code and tricks. |
tracking issue for C ABI and Wasm ABI merge. rust-lang/rust#83788 it seems the work is primarily blocked by clang's multi value return types support in C ABI for wasm. |
I was eager enough to get this fixed I started trying to translate lua 5.4.4 to rust before seeing this. Do you welcome PRs to help with the drudgery where possible? |
FWIW, I was able to get both sdl2 working along with mlua on the emscripten target :) a very minimal example https://github.com/coderedart/rust-sdl2-wasm is now live at https://coderedart.github.io/rust-sdl2-wasm/
sdl2 basically works without us doing much. I used |
Finally, It is done. Untitled.mp4you can play with it too. live version @ https://coderedart.github.io/rust-sdl2-wasm/ the next step is to wait for mlua 0.9 when we can actually script egui with lua live inside a browser. |
mlua-rs/lua-src-rs#7 made |
Does rust-lang/rust#117919 |
@Andrewp2 any luck with this? Seems like it should work but I can't work out how to integrate it |
I would only expect the WASM C ABI to be relevant for bindings. There are two issues, I'd guess.
|
At the moment, attempting to build for
wasm32-unknown-unknown
causes the following panic:don't know how to build Lua for wasm32-unknown-unknown
.I know running Lua in wasm isn't the most efficient thing in the world, but I feel like the functionality should be added.
I'd be happy to look into this, I just want to get the OK before I go and implement it.
The text was updated successfully, but these errors were encountered: