Skip to content
MultiSliceCrewPublic

About

Haxe/hxcpp @:native bindings for LuaJIT.

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Repository files navigation

hxluajit

Haxe/hxcpp @:native bindings for LuaJIT.

Installation

You can install it through Haxelib

haxelib install hxluajit

Or through Git, if you want the latest updates

haxelib git hxluajit https://github.com/MAJigsaw77/hxluajit.git

Usage Example

Check out the Examples Folder for examples on how to use this library.

Licensing

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.

MultiSliceCrew additions

Compatibility helpers

LuaJIT follows the Lua 5.1 API. These helpers bring the most used functions from newer versions:

  • Lua.absindex, Lua.isinteger and Lua.rawlen
  • LuaL.len
  • LuaJIT.on, LuaJIT.off and LuaJIT.flush

LuaState

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.
  • timeLimit stops 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, setJit and checkSyntax are also available.

hxluajit.wrapper.LuaConverter converts between Haxe and Lua values: arrays, anonymous structures, StringMap, IntMap, numbers, strings, booleans and functions.

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 and callMulti(args) returns all of them.
  • A reference keeps the Lua function alive until release() is called or the state is closed. alive tells if it can still be called. Set LuaConverter.functionReferences = false to get null instead.
  • References can be passed back to the same state, and turn into nil in other states.

More helpers

  • callMulti(name, args) returns every value a global function returns.
  • getFunction(name) returns a global function as a LuaFunction.
  • setPath('game.player.score', 10) and getPath('game.player.score') read and write nested tables, creating them when needed.
  • addModule(name, value) and preload(name, code) make modules available to require.
  • onPrint receives what scripts print.
  • Errors include a stack traceback with the file and line of every call. Set traceback = false to get only the message.

Haxe objects in Lua

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 or set.
  • 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() and obj.method(obj).
  • tostring, ==, # and calling function objects are supported.
  • canRead, canWrite and blockedFields limit what scripts can touch, and onError receives errors.
  • Set LuaConverter.objectProxies = true to send every class instance as an object.
  • pushHandler and readHandler let projects with their own value converter use the same objects.

About

Haxe/hxcpp @:native bindings for LuaJIT.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages