Skip to content

A WebAssembly instantiator & javascript polyfill for the lu5 interpreter

License

Notifications You must be signed in to change notification settings

matiasvlevi/lu5-wasm

Repository files navigation

lu5-wasm

A minimal WebAssembly instantiator & javascript polyfill for the lu5 interpreter.


Current Limitations

  • No async
  • No file i/o
  • No matrix transform stack (translate, rotate, scale)
  • No 3D Rendering
  • No setjmp & longjmps
  • No exception handling (we currently use a workaround)

Getting Started

Add the lu5-wasm library from a CDN

<script src="https://unpkg.com/lu5-wasm@latest/dist/lu5-wasm.min.js"></script>

Add a script

<script type="text/lua">
    function setup()
        createWindow(400, 400);
    end

    function draw()
        background('magenta');
    end
</script>

Out of the box, lu5-wasm will search for and execute lu5 scripts in the document.


Run in a specific canvas

Add a canvas with an id

The canvas can be placed anywhere in your document's body

<canvas id="game"></canvas>

Include a lu5 script with a canvas attribute referencing the canvas id

<script type="text/lua" src="sketch.lua" canvas="game"></script>


Use lu5-wasm as a library

For more specialized use cases, you may prefer to use lu5-wasm as an ES Module. This will disable the auto-execution of lua scripts in your document and export api functionality.

Use the lu5-wasm-lib.min.js bundle

<script type="module" src="https://unpkg.com/lu5-wasm@latest/dist/lu5-wasm-lib.min.js"></script>

Library usage

Instantiate lu5 with the wasm binary and execute scripts.

lu5.init()
    .then(vm => vm.execute(`print('Hello from lu5!')`))
    .then(vm => vm.execute(`
        function setup()
            createWindow(400, 400);
        end

        function draw()
            background('purple');
        end
    `));

You can use the state from previous execute calls

lu5.init()
    .then(vm => vm.execute('x = 12'))
    .then(vm => vm.execute('y = 18'))
    .then(vm => vm.execute('print(x + y)'));

Call lu5.reset to clear state

lu5.init()
    .then(vm => vm.execute('x = 12'))
    .then(vm => vm.reset())
    .then(vm => vm.execute('print(x)')); // nil

See DOCUMENTATION.md.



About

A WebAssembly instantiator & javascript polyfill for the lu5 interpreter

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published