mmaarouf / lua-cjson-wasm

Project to demo executing lua code that dynamically loads a shared library (cjson.so) in the browser using WebAssembly.

Home Page:https://mmaarouf.github.io/lua-cjson-wasm/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lua-cjson-wasm pipeline

Project to demo executing lua code that dynamically loads a dynamically loads a shared library (lua-cjson) in the browser using WebAssembly.

demo

Try it out here.

How it works

  • When play button is clicked, the lua script is passed to lua_interop
  • lua_interop will use runner.lua to execute the script
  • runner.lua will wrap the script's return value in a "result" table, serialise it JSON using the lua-cjson library and then return it back to lua_interop
  • lua_interop will pass the result back to the webpage
  • The webpage will parse the JSON and display the result on the screen with green-ish/red-ish highlighting based on the result status

Compiling the WebAssembly modules

Using the cjson library in the lua code dynamically loads it at runtime. To get this to work in WASM, it needs to be compiled as a SIDE_MODULE.

Compilation steps

  1. Compile lua as a WASM side module
  2. Compile lua-cjson into a WASM side module & rename atrifact to cjson.so to allow the lua runtime to dynamically load it at runtime
  3. Compile lua-interop.c into a WASM main module

Development

Tools

  • bash on linux-like environment (for Windows use WSL2)
  • Docker
  • Docker Compose
  • An .editorconfig compliant editor

Commands

Main project commands:

  • ./bin/build - builds the project
  • ./bin/run - runs the project. Webpage accessible on port 8080
  • ./bin/shell - shells into the development environment
  • ./bin/clean - deletes compiled artifacts

To run the project: ./bin/build && ./bin/run

Development environment commands

When running commands under ./bin/ a new ephemeral docker container is spun up to execute the command.

Alternatively you can start single container and have it run in the background using ./bin/dev-env/start. That allows you to do things such as have the project running in one terminal, and be shelled into the development environment in another.

To stop, use: ./bin/dev-env/stop.

About

Project to demo executing lua code that dynamically loads a shared library (cjson.so) in the browser using WebAssembly.

https://mmaarouf.github.io/lua-cjson-wasm/


Languages

Language:Shell 24.4%Language:Lua 19.8%Language:JavaScript 16.8%Language:CSS 11.4%Language:HTML 8.1%Language:Makefile 7.9%Language:C 6.3%Language:Dockerfile 5.3%