Luar: Lua reflection bindings for Go
February 28, 2017 ยท View on GitHub
Luar is designed to make using Lua from Go more convenient. Go structs, slices and maps can be automatically converted to Lua tables and vice-versa. The resulting conversion can either be a copy or a proxy. In the latter case, any change made to the result will reflect on the source.
Any Go function can be made available to Lua scripts, without having to write C-style wrappers.
Luar support cyclic structures (map[string]interface{}, lists, etc.).
User-defined types can be made available to Lua as well: their exported methods can be called and usual operations such as indexing or arithmetic can be performed.
See the documentation for usage instructions and examples.
Installation
Install with
go get <repo>/luar
Luar uses Alessandro Arzilli's golua. See golua's homepage for further installation details.
REPL
Version 1.x features an example REPL that is available in the cmd folder.
Changelog
Luar 2
This is a rewrite of 1.x with extended features and a cleaner API. The main differences with the previous version:
-
The function prototypes of
GoToLuaandLuaToGoare simpler and do not require the use of reflection from the callers. Thedontproxifyargument is gone, useGoToLuaProxyto control proxification. -
The
Copy*functions andGoLuaFuncare gone, useGoToLuaandLuaToGoinstead. -
Use
Registerinstead ofRawRegister. -
InitProxiesis gone since it was not needed. -
The
LuaObjectandLuaTableIterstructure fields are unexported. -
LuaObject methods not only work on Lua functions but also on anything with a
__callmetamethods. Idem for tables and the__index/__newindexmetamethods. -
Use
NewLuaObjectFromName(L, "_G")instead ofGlobal. -
LookupandGetigone. Instead theGetandGetObjectfunctions are variadic: each subfield argument can be any valid Lua key (string, integer...). -
Use
(*LuaObject) Callinstead of(*LuaObject) Callf. The protoype of(*LuaObject) Callhas changed in a fashion similar toGoToLuaandLuaToGo.Typesis gone as it is no longer needed. -
Register
ProxyIpairsandProxyPairsinstead of callingLuarSetup. -
Register and use
Unproxifyinstead ofArrayToTable,MapToTable,ProxyRaw,SliceToTableandStructToTable. -
ComplexRealandComplexImaghave been replaced by the proxy attributesrealandimag, respectively. -
SliceSubandSliceAppendhave been replaced by the proxy methodssliceandappend, respectively. Slice proxies have thecapmetamethod alongsideappendandslice. -
String proxies have a
slicemethod just like slice proxies. They can be looped rune-by-rune over withipairs.
The range of supported conversion has been extended:
-
LuaToGo can convert to interfaces and pointers with several levels of indirection.
-
LuaToGo can convert to non-empty maps and structs.