nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32

Home Page:https://nodemcu.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebAssembly target for the luac.cross

Alfrederson opened this issue · comments

Not really a missing feature, but something which would be really cool

In 2023, there could be an option to generate a browser-compatible version of the luac.cross tool. Lua is really easy to compile from source into wasm, e.g:

https://alfrederson.github.io/lua-web/

Justification

We can already upload a compiled firmware to the ESP32/8266 using the WebSerial API.
The Lua vm can easily be compiled to WebAssembly by replacing gcc with emcc in the Makefile within the /lua/luac_cross directory and commenting out two lines in luac.c, i.e:

CC := $(WRAPCC) emcc
...
# and also this line

IMAGE := ../../../luac.cross.html

And

// maybe add a || to also do this typedef when compiling with EMCC
// #if defined(_MSC_VER) || defined(__MINGW32__)
typedef unsigned int uint;
// #endif

I don't remember what else needs to be changed but it required very little effort (installing some dependencies and 1 or 2 compiler flags) to actually get luac.cross running within a html with the default emscripten shell. Just compiling it without changing anything else in the source file is enough to get the usage message on the browser's console. If the luac.c file is tweaked, it can be made to run lua code, e.g:

The implication of this is that there can be an online tool to generate everything else the luac.cross compiler generates, but without a dedicated server/containerized service. Since it's static, it could even be served from the ESP itself.

Question

One major problem I stumbled upon is that I couldn't get the luac.cross from the esp32 branch to compile without building the entire thing. Is the binary generated by the esp8266 branch of the luac.cross compatible with the ESP32 lua vm?

Sounds interesting. We would probably also need different versions for int and 64 Bit builds.
A PR would certainly be welcome.

Sounds interesting. We would probably also need different versions for int and 64 Bit builds. A PR would certainly be welcome.

It didn't work at all. I could get it to run but it produce non-viable bytecode. It was also impossible to make it generate the LFS image. It was easier to just dockerize the thing and create a simple API in go, e.g:

https://github.com/Alfrederson/luac.web

No need to run it on raspberry pi servers!