npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@doridian/luajs

v1.0.8

Published

Bindings for C Lua to JavaScript with Emscripten and WebAssembly

Downloads

1

Readme

LuaJS

Lua VM running in Javascript (using emscripten)

Setup Instructions

Make certain to download your git submodules, run git submodule update --init --recursive.

You'll need emscripten and NodeJS installed. If you're running a system that supports homebrew, you can brew install:

Build instructions

Run npm install && npm run clean && npm run build then you have the compiled files in the dist folder.

Usage of Lua from JavaScript

LuaJS.newState().then(async (L) => {
    let value;
    value = await L.run("return 1+2"); //value == [3]

    value = await L.run("return {a = 1, b = 2}"); //value[0] instanceof Module.Table, value[0] instanceof Module.Reference
    value[0].get("a"); // == 1
    console.log(value);
    console.log(value[0].toObject(true, true)); //converts Module.Table to JavaScript object (will drop all other Module.Reference-s if unrefAll == true)
    value[0].unref();

    let func = await L.run("return function(a,b) return a + b end"); //func[0] instanceof Module.Function, func[0] instanceof Module.Reference
    value = func[0].call(3,4); //value == [7]
    console.log(value);
    func[0].unref();
});

Using .unref() is optional as LuaJS does utilize Finalizers. However, those can be called at any time, so if you are memory constrained, manual unref might help!

Usage of JavaScript from Lua

Lua knows the library js, where the js.global table equals the JavaScript window object

When you call JS functions from Lua, the function parameters will always be automatically converted to JS equivalents (and internally .unref()'d, except functions).

You can convert JS objects/arrays to native Lua tables by using jsObject:toTable(recursive), however you can also directly index JS objects from Lua.

Warning: You need to call all JS functions either like js.global:alert("testmessage") or local alert = js.global.alert; alert(nil, "testmessage"). The first argument will be used as the "this" context in JavaScript.

Using Lua inline in HTML

If you call enableLuaScriptTags(document), you can specify <script type="text/lua"> tags just like JavaScript tags with either inline scripts or a src attribute.

Below is an example HTML document that enables Lua scripts for the entire page:

<!DOCTYPE html>
<html>
    <head>
        <script type="module" src="dist/luajs.mjs"></script>
        <script type="module">
            import emscriptenInit from 'dist/luajs.mjs';
            const LuaJS = await emscriptenInit(LuaJS);

            LuaJS.newState().then(async (L) => {
                await L.enableLuaScriptTags(document);
            });
        </script>
        <script type="text/lua">
            js.global.console:log("Hello world")
        </script>
    </head>
</html>

Using Lua in NodeJS

The compiled module can be used in NodeJS as follows:

// EITHER: Import (ES6 modules, .mjs/.mts)
import { LuaJS } from '@doridian/luaj';
// OR: require (CommonJS, .cjs/.cts)
const { LuaJS } = require('@doridian/luajs');

LuaJS.newState().then(async (L) => {
    console.log(await L.run("return 42 + 69"));
});