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="https://github.com/mherkender/lua.js/raw/master/lua.min.js">
<script type="text/javascript" src="https://github.com/mherkender/lua.js/raw/master/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.
--[=[ ]=]--
) are not supported__mode
and __gc
metamethods are not supporteduints
key in an object can be an array or an object, depending on how it is used. If table.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, use ensure_arraymode()
or ensure_notarraymode()
to require it be available in a certain form.