Click here to download precompiled versions of lua.js
To build these files yourself, use make in the root of the project, for example...
make CLOSURE_COMPILER=/path/to/closure/compiler.jar
(the CLOSURE_COMPILER variable only needs to be set if you're building the minified versions of the scripts)
You can use the lua2js tool to translate Lua code into Javascript.
./lua2js input.lua output.js
(node is required to run lua2js)
lua(.min).js must also be embedded on the same page as generated Javascript code. It contains all the functions necessary to keep the code working.
When a script generated by lua2js is run, the variable lua_script
will be set to the global variable (_G) of the script. You can use this variable to interact with the script using the functions in lua.js/lua.as.
<script type="text/javascript" src="lua.min.js">
<script type="text/javascript" src="generated_code.js">
<script type="test/javascript">
// this is equivalent to: init(1, 2, 3)
lua_call(lua_tableget(lua_script, "init"), [1, 2, 3]);
// this is equivalent to: local numActive = activeChars.count
var numActive = lua_tableget(lua_tableget(lua_script, "activeChars"), "count")[0];
// this is equivalent to: local distFromCenter = get_distance({x=1, y=2})
var distFromCenter = lua_call(lua_tableget(lua_script, "get_distance"), [lua_newtable(null, "x", 1, "y", 2)])[0];
</script>
If you are dealing with multiple Lua scripts, modules should be used. They can be used normally for inter-Lua interactions, and the lua_module()
function can be used by Javascript to retrieve modules.
lua2js also generates valid ActionScript code as well. Include lua.as (not lua.js) and the generated file to make it work.
You can also use the function lua_load()
to parse Lua code in the browser. lua_load()
is only available when the lua+parser.js version of lua.js is used. lua_load()
will parse the script, and return a function that will execute the script when called and returning the global variable of that script.
- There are missing library functions and incomplete implementations of some functions. They should all be marked with TODO comments, or throw "Not supported" errors.
- The global table does not currently support metatables.
- Some Lua functions do not handle error conditions in the same way as standard Lua does.
- Nested comments (e.g.
--[=[ ]=]--
) are not supported __mode
and__gc
metamethods are not supported
- This project works by converting Lua code directly to ECMAscript. An alternative solution would be to emulate a Lua VM to better maintain compatibility, but this would be much slower.
- Lua functions in ECMAscript always return arrays. This is to support the multiple return values that Lua uses.
- Tables are normal objects with sub-objects (str, uints, etc) that seperate different kinds of keys from each other.
- The
uints
key in an object can be an array or an object, depending on how it is used. Iftable.insert
is used for example, then it will be converted to an array if it is not already one. To force uints to be one thing or another, useensure_arraymode()
orensure_notarraymode()
to require it be available in a certain form. - lua.js can be compiled using Closure Compiler's ADVANCED_OPTIMIZATIONS mode for a dramatically smaller total file size. This is more complicated than traditional minifying so you'll need to use variable maps or combine Lua-related scripts together.