Lua2JS

raw JSON →
0.0.11 verified Fri May 01 auth: no javascript abandoned

A Lua parser and transpiler to JavaScript that generates Mozilla Parser API ASTs. The current version is 0.0.11, with no recent releases or active development evident. It targets Lua 5.1 with partial support and aims to run unmodified Lua programs in JavaScript environments. Unlike other Lua transpilers, it provides options for Lua-JS interop such as automatic conversion of LuaTable to arrays/objects and method call syntax remapping. It requires no runtime dependencies and is distributed as a single minified file. The project has significant gaps: many standard library functions (e.g., string.format, pattern matching, coroutines, debug) are missing, and certain Lua syntax features (expression table keys, goto) are unsupported. The repository appears to be in maintenance or abandoned state.

error Error: Lua parse error at line 1: unexpected symbol near '['
cause Using expression table keys which are unsupported.
fix
Replace {[expr]=value} with a string key or compute the value after table creation.
error TypeError: __lua is not defined
cause Using luaCalls or luaOperators option without including the runtime support.
fix
Include the lua2js runtime library (dist/lua2js.runtime.js) in your project.
error lua2js.parse is not a function
cause Importing the default export incorrectly (e.g., using require('lua2js').parse when default is the parser).
fix
Use import lua2js from 'lua2js' or require('lua2js') and then call lua2js.parse.
breaking Lua standard library functions like string.format, pattern matching, coroutines, and debug are missing.
fix Implement missing functionality or avoid using those functions in Lua code.
gotcha Expression table keys (e.g., {[expr]=value}) are not supported.
fix Use string or numeric keys only in table literals.
breaking Long form strings with internal ]] may cause parse errors even with [==[ syntax.
fix Avoid long form strings with ]] sequences; use regular strings with escaped quotes if possible.
deprecated The project has no recent releases since 2014 and may be abandoned.
fix Consider alternative Lua-to-JS transpilers such as lua.js, moonscript, or fengari.
gotcha The global environment _ENV and _G are not supported; globals are defined as properties of the global object.
fix Do not rely on _ENV or _G; assign globals directly.
npm install lua2js
yarn add lua2js
pnpm add lua2js

Parses a Lua Fibonacci function and translates it to JavaScript, demonstrating basic usage of parse and translate.

import lua2js from 'lua2js';

const luaCode = `
function fib(n)
    if n < 2 then
        return n
    else
        return fib(n-1) + fib(n-2)
    end
end
print(fib(10))
`;

const ast = lua2js.parse(luaCode);
const jsCode = lua2js.translate(ast);
console.log(jsCode);
// Output:
// var fib = function(n) {
//   if (n < 2) {
//     return n;
//   } else {
//     return fib(n - 1) + fib(n - 2);
//   }
// };
// console.log(fib(10));