Skip to content
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

Request: Add Lua Language Support to Wasmer #5335

Open
JustKira opened this issue Jan 11, 2025 · 4 comments
Open

Request: Add Lua Language Support to Wasmer #5335

JustKira opened this issue Jan 11, 2025 · 4 comments
Labels
🎉 enhancement New feature!

Comments

@JustKira
Copy link

Thanks for proposing a new feature!

Motivation

The motivation behind this request stems from my ongoing project to build a custom game engine where the core is developed in Rust, and the UI layer is implemented in Lua alongside my custom language. The idea is to allow developers to contribute to the engine using different languages, enabling the creation of specific features in their preferred language while maintaining seamless integration within the same engine.

Wasmer appears to be the ideal foundation for this project due to its support for multiple languages. However, Lua—an essential component of my project—is currently not supported. This feature would fill a critical gap in Wasmer’s otherwise robust language support and make it an even more versatile runtime.

Proposed solution

Add support for the Lua language in Wasmer. This feature would allow developers to compile Lua code to WebAssembly and execute it via Wasmer, similar to the existing support for other languages. Ideally, this could include:

A Lua-to-WASM compiler or integration with existing compilers.
Runtime compatibility with Lua-specific features, such as its lightweight nature and efficient scripting capabilities.

Alternatives

I’ve explored existing Lua-WASM and Lua-Rust interpreters, but they don't fully meet the needs of this project, particularly when it comes to the ability to seamlessly integrate multiple languages within a single engine runtime. By integrating Lua support directly into Wasmer, the ecosystem gains flexibility and scalability, aligning with Wasmer's goal of being a universal WebAssembly runtime.

Additional context

This feature would open doors for projects that require multi-language integration, particularly in game engines, scripting, or similar domains. Lua’s lightweight design and widespread use make it a strong candidate for inclusion in Wasmer’s supported languages.

For reference, there was a related issue (#2612) that was previously closed without a solution. Given the importance of Lua for projects like mine, I believe this deserves reconsideration.

@JustKira JustKira added the 🎉 enhancement New feature! label Jan 11, 2025
@syrusakbary
Copy link
Member

Thanks for opening the issue. It seems the first step would be to compile Lua to Wasm/WASIX and then use Wasmer for that.

Lua community could probably help on this as well (compiling Lua to WASIX), but we would be more than keen to help you on that if you encounter any issues

@JustKira
Copy link
Author

To ensure I understand correctly: Does Wasmer load WebAssembly (Wasm) files? If so, do I just need to make sure I compile Lua properly into the Wasm or WASIX format? I'm new to WebAssembly and exploring it—it's such an interesting technology!

@xdoardo
Copy link
Contributor

xdoardo commented Jan 23, 2025

Hello @JustKira!

Yes, Wasmer is a Wasm runtime. It can thus "understand" Wasm files, in the sense that it can load and run them. Therefore yes, in principle, you should just make your Lua code compile to Wasm to be able to run them in Wasmer.

In practice, however, and depending on what your program does, it might not be so simple. If your program needs to interact with the filesystem, create sockets or shows other specific behaviours - you can sort of think OS calls - in order to have a Wasm file (in jargon, a module) that you can use with any runtime, one needs:

  1. to have all the runtimes agree on a standard API to translate the OS calls to; and
    2a. have your Lua-to-Wasm transpiler/compiler translate your script to Wasm respecting that standard API; or
    2b. have your Lua interpreter compiled to Wasm respecting that standard API.

WASI and its extension WASIX are specifications of the standard API, which Wasmer implements. The next step, therefore, would be 2a or 2b: this is the endeavour where the Lua community could help on.

After a quick search online, it seems that there are unmaintained Lua-to-Wasm compilers (1, 2) and maintained Lua-to-Wasm compilers that don't compile to WASI (3); for (2b), one could try to compile Lua interpreters in a language that can target Wasm to it; an example of this mechanism can be seen in our efforts for php.

@Arshia001
Copy link
Member

To expand on @xdoardo's answer:

The official lua interpreter seems to be written exclusively in C. Getting it working in WASIX should be relatively simple. The steps involved would be:

  • Grab a copy of the WASI SDK
  • Grab a build of the WASIX sysroot
  • Instruct the build process to use those things together.

You can take a look at the build setup we have for PHP to see how it's done; TLDR:

and then you let it run normally. I'm not super-familiar with lua, so I don't know if it does any sort of JIT'ing; if it does, that needs to be disabled as dynamic code loading is not supported in WASM (yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature!
Projects
None yet
Development

No branches or pull requests

4 participants