Haxe/hxcpp @:native bindings for LuaJIT.
You can install it through Haxelib
haxelib install hxluajitOr through Git, if you want the latest updates
haxelib git hxluajit https://github.com/MAJigsaw77/hxluajit.gitCheck out the Examples Folder for examples on how to use this library.
hxluajit is made available under the MIT License. Check LICENSE for more information.
LuaJIT is made available under the MIT License. Check LuaJIT's Licence for more information.
LuaJIT follows the Lua 5.1 API. These helpers bring the most used functions from newer versions:
Lua.absindex,Lua.isintegerandLua.rawlenLuaL.lenLuaJIT.on,LuaJIT.offandLuaJIT.flush
hxluajit.wrapper.LuaState is a ready to use Lua state:
import hxluajit.wrapper.LuaState;
final lua = new LuaState(true, true);
lua.onError = message -> Sys.println(message);
lua.addCallback('add', args -> args[0] + args[1]);
lua.set('player', {name: 'bf', health: 2});
lua.doString('function double(x) return add(x, x) end');
trace(lua.call('double', [21]));
lua.close();- The second constructor argument turns on the sandbox, which removes
os.execute,io.popen,debug,ffi, file writing and native module loading. timeLimitstops calls that run for longer than the given seconds, so an endless loop in a script does not freeze the game. The JIT is turned off while a limit is set, because compiled code skips the hook that checks the time.- Callbacks can return
new LuaMulti([a, b])to return more than one value. memoryKB,collect,setJitandcheckSyntaxare also available.
hxluajit.wrapper.LuaConverter converts between Haxe and Lua values: arrays, anonymous structures, StringMap, IntMap, numbers, strings, booleans and functions.
Lua functions reach Haxe as hxluajit.wrapper.LuaFunction references, and Haxe functions can be given to Lua:
var onDone:LuaFunction = null;
lua.addCallback('later', args -> onDone = args[0]);
lua.doString('later(function(score) print("done", score) end)');
onDone.call([1200]);
onDone.release();
lua.set('double', (x:Int) -> x * 2);
lua.doString('print(double(21))');call(args)returns the first result andcallMulti(args)returns all of them.- A reference keeps the Lua function alive until
release()is called or the state is closed.alivetells if it can still be called. SetLuaConverter.functionReferences = falseto getnullinstead. - References can be passed back to the same state, and turn into
nilin other states.
callMulti(name, args)returns every value a global function returns.getFunction(name)returns a global function as aLuaFunction.setPath('game.player.score', 10)andgetPath('game.player.score')read and write nested tables, creating them when needed.addModule(name, value)andpreload(name, code)make modules available torequire.onPrintreceives what scripts print.- Errors include a stack traceback with the file and line of every call. Set
traceback = falseto get only the message.
LuaObject lets scripts use live Haxe objects. Reading and writing fields, calling methods and indexing arrays all go straight to the Haxe object.
lua.setObject('player', player);
lua.doString("player.x = 100; player:jump(2); print(player.items[1], #player.items)");LuaObject.wrap(object)marks a value to be sent as an object, from callbacks orset.- Fields that hold other objects or arrays are sent as objects too.
- Objects passed back to Haxe come back as the original object.
- Methods work with both
obj:method()andobj.method(obj). tostring,==,#and calling function objects are supported.canRead,canWriteandblockedFieldslimit what scripts can touch, andonErrorreceives errors.- Set
LuaConverter.objectProxies = trueto send every class instance as an object. pushHandlerandreadHandlerlet projects with their own value converter use the same objects.