This project is base on the tcr 's lua-colony. It is an awesome project, but the source was removed. I give it a new name "javascript-to-lua", and will proceed to implement and maintain the project on the next days.
This project compiles JavaScript to Lua 5.1 source code, and uses a small runtime library.
$ npm install -g js-to-lua
$ js-to-lua examples/helloworld.js --bundle | lua
Hello world. Welcome to Lua Colony!
To run, put "colony-lib.lua" in your current folder (where you are running the lua
command).
Lua Colony gives the Lua runtime JS's syntax and runtime library. Here's a slightly complicated Hello World in JS:
console.log(["Hello", "world."].concat(["Welcome", "to", "colony"]).join(" ") + '!');
Compiled with Lua Colony:
local _JS = require('colony-js');
local string, math, print = nil, nil, nil;
local this, global, Object, Array, String, Math, require, console = _JS.this, _JS.global, _JS.Object, _JS.Array, _JS.String, _JS.Math, _JS.require, _JS.console;
local _module = {exports={}}; local exports = _module.exports;
console:log((_JS._arr({[0]=("Hello"), ("world.")}):concat(_JS._arr({[0]=("Welcome"), ("to"), ("colony")})):join((" ")) + ("!")));
return _module.exports;
require
is implemented with Node.js/CommonJS semantics. Lua Colony doesn't currently self-host, so you'll have to compile all code before importing it.
Lua Colony compiles to Lua source code, but may not play nice with Lua's ecosystem. Be aware of these caveats:
require(...)
works the same as in Lua.- JavaScript methods compiled to Lua require a
this
argument as the first parameter.- Lua functions which call JavaScript function should pass a
this
object (which may benil
) as the first parameter. - Inversely, JavaScript calling Lua must pass the first argument as the
this
parameter; the most logical way to do this is using the.call()
method:func.call(arg0, arg1, arg2)
object.method(arg0, arg1)
in JavaScript maps toobject:method(arg0, arg1)
in Lua.
- Lua functions which call JavaScript function should pass a
- Arrays in JavaScript are indexed from 0, and Lua arrays are indexed from 1. Make sure to either push a dummy element using
.shift()
when calling Lua from JavaScript, and to explicitly assign the first array element in Lua to the 0 index (eg. in Lua:{[0]='first element', 'second element', 'third...'}
) - Colony uses the debug library to replace the intrinsic metatables of functions, strings, booleans, and numbers. Functions, booleans, and numbers in Lua have no metatables by default, so this will only cause issues for string metatables, which is replaced entirely. The workaround is to ensure all included code explicitly calls the methods of the
string
object (eg.string.len("apples")
vs("apples"):len()
).
miT