1
0
Fork
You've already forked libwasmoon
0
A real lua VM with JS bindings made with webassembly (library version for embeddable lua bundles) https://www.npmjs.com/package/libwasmoon
  • TypeScript 59.5%
  • Makefile 40.5%
2026年05月29日 14:09:54 +02:00
src Initial support for interacting with ServiceWorker contexts 2026年05月29日 13:56:01 +02:00
wasmoon @4cf40cb6dc Initial Commit 2025年05月01日 12:47:00 +02:00
.gitignore Add .gitignore 2025年05月01日 12:48:06 +02:00
.gitmodules Initial Commit 2025年05月01日 12:47:00 +02:00
changelog.md Release v0.4.0 2026年05月29日 14:09:54 +02:00
LICENSE Add license 2025年05月01日 15:29:29 +02:00
makefile Support await() in functions called from Javascript (wasmoon!135) 2025年06月12日 16:02:33 +02:00
package.json Release v0.4.0 2026年05月29日 14:09:54 +02:00
readme.md readme.md: Fix usage 2025年05月08日 11:57:40 +02:00

Based on ❤️ wasmoon

libwasmoon

A real lua VM with JS bindings made with webassembly (library version for embeddable lua bundles)

About

Libwasmoon is a javascript template which allows to run Lua code in the browser.

It is based on the wasmoon project and works by loading base64-encoded versions of custom-tailored Lua binaries. This allows to embed compiled Lua bundles and native Lua modules (e.g. written in C) via WebAssembly.

The result is a single javascript file which runs Lua code that can interact with the DOM / javascript APIs and vice versa – meaning that other <script>-tags can interact with variables and functions provided by Lua (see Interacting with Javascript APIs).

Feature Additions

  • Compile modules into a single javascript file
  • Support for native Lua modules (e.g. written in C)
    • Provides a system interface for most POSIX features via WASI

Usage

The template is designed to work together with lrc (the LRocket-compiler) which targets WebAssembly.

Installing the WebAssembly target for the LRocket compiler:

> luarocks install lrocket-build-wasm

The compiler provides three targets for WebAssembly:

You can compile to Javascript, to include your code as javascript module:

> lrc --otype wasm-js main.lua -o output-lib.js

You can compile directly to HTML, to write your complete page in Lua (starting off with window.document.body.innerHTML = [[...]]):

> lrc --otype wasm-html main.lua -o index.html

Or compile to .wasm to handle the embedding into the libwasmoon template yourself:

> lrc --otype wasm main.lua -o lua-module.wasm

If you would like to handle the compilation yourself (e.g. use a different compiler than lrc) virtually any compiler that targets wasm32 can be used to build your own Lua binary script.

For details see Using a Different Compiler.

Interacting with Javascript APIs

Libwasmoon makes the window-object available inside the global Lua environment – This allows to use any desired Javascript API via Lua syntax!

All is made possible by the fantastic work of the wasmoon project!

Examples

Button Callback

window.document.innerHTML = '<button>Click me!</button>'
-- for syntactic convenience we can export all contents of 'window.<...>' to the global environment
setmetatable(_G, { __index = window })
local button = document.querySelector 'button'
button.addEventListener('click', function()
 alert('Lua function called!')
end)

Lua Function in Javascript

function window.myLuaFunction()
 return math.random(1, 42)
end
// in javascript
alert('The answer to everything is: ' + myLuaFunction());

Geo Location

window.navigator.geolocation.getCurrentPosition(function(loc)
 -- prints to console.log()
 print('You are here', loc.coords.latitude, loc.coords.longitude)
end)

Using a Different Compiler

Any compiler that targets wasm32 (ideally wasm32-wasi / wasm32-wasi-threads) can be used to build the binary that will be embedded into the libwasmoon template.

The compiled binary should export a symbol named void __lr_entrypoint(lua_State *L) (called by libwasmoon after the Lua-state has been initialized).

We recommend linking against lua-wasm32, which is a static lua library (the one lrc links against when compiling bundles for WebAssembly). It is available via luarocks (note that the rock also ships a copy of the libwasmoon template):

> luarocks install lua-wasm32

For example, the clang-compiler could be used to compile a custom lua interpreter which can then be embedded into the libwasmoon template:

> clang --target=wasm32-wasi-threads -o lua-module.wasm my-custom-interpreter.c \
 <path-to-lua-wasm32>/lib/liblua-5.4.a

Turining lua-module.wasm into Javascript:

> sed dist/loader.js.tpl "s/{{libwasmoon:wasm_bytes}}/$(base64 -w0 lua-module.wasm)/g" > \
 output-lib.js

Using the module in HTML:

<!DOCTYPE html>
<body></body>
<script src="output-lib.js"></script>

You may also embed the script between an inline <script>...</script>-tag.

License

Just like wasmoon, libwasmoon is licensed under the MIT License.


Created by Lesosoftware in 2025