Documentation ¶
Overview ¶
luar provides a more convenient way to access Lua from Go, using Alessandro Arzilli's golua (https://github.com/vifino/golua). Plain Go functions can be registered with luar and they will be called by reflection; methods on Go structs likewise. package main import "fmt" import "github.com/vifino/luar" type MyStruct struct { Name string Age int } const test = ` for i = 1,5 do Print(MSG,i) end Print(ST) print(ST.Name,ST.Age) --// slices! for i,v in pairs(S) do print(i,v) end ` func main() { L := luar.Init() defer L.Close() S := []string {"alfred","alice","bob","frodo"} ST := &MyStruct{"Dolly",46} luar.Register(L,"",luar.Map{ "Print":fmt.Println, "MSG":"hello", // can also register constants "ST":ST, // and other values "S":S, }) L.DoString(test) } Go types like slices, maps and structs are passed over as Lua proxy objects, or alternatively copied as tables.
Copyright Steve Donovan 2013
Index ¶
- func CopyMapToTable(L *lua.State, vmap reflect.Value) int
- func CopySliceToTable(L *lua.State, vslice reflect.Value) int
- func CopyTableToMap(L *lua.State, t reflect.Type, idx int) interface{}
- func CopyTableToSlice(L *lua.State, t reflect.Type, idx int) interface{}
- func CopyTableToStruct(L *lua.State, t reflect.Type, idx int) interface{}
- func GoLua(L *lua.State) int
- func GoLuaFunc(L *lua.State, fun interface{}) lua.LuaGoFunction
- func GoToLua(L *lua.State, t reflect.Type, val reflect.Value, dontproxify bool)
- func Init() *lua.State
- func Lookup(L *lua.State, path string, idx int)
- func LuaToGo(L *lua.State, t reflect.Type, idx int) interface{}
- func MakeChannel(L *lua.State) int
- func RaiseError(L *lua.State, msg string)
- func RawRegister(L *lua.State, table string, values Map)
- func Register(L *lua.State, table string, values Map)
- func Types(values ...interface{}) []reflect.Type
- type LuaObject
- func (lo *LuaObject) Call(args ...interface{}) (res interface{}, err error)
- func (lo *LuaObject) Callf(rtypes []reflect.Type, args ...interface{}) (res []interface{}, err error)
- func (lo *LuaObject) Close()
- func (lo *LuaObject) Get(key string) interface{}
- func (lo *LuaObject) GetObject(key string) *LuaObject
- func (lo *LuaObject) Geti(idx int64) interface{}
- func (lo *LuaObject) Iter() *LuaTableIter
- func (lo *LuaObject) Push()
- func (lo *LuaObject) Set(idx interface{}, val interface{}) interface{}
- func (lo *LuaObject) Setv(src *LuaObject, keys ...string)
- type LuaTableIter
- type Map
- type Null
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyMapToTable ¶
Copy a Go map to a Lua table, Defines luar.map2table
func CopySliceToTable ¶
Copy a Go slice to a Lua table. nils in both slices and structs are represented as luar.null. Defines luar.slice2table
func CopyTableToMap ¶
Return the Lua table at 'idx' as a copied Go map. If 't' is nil then the map type is map[string]interface{}
func CopyTableToSlice ¶
Return the Lua table at 'idx' as a copied Go slice. If 't' is nil then the slice type is []interface{}
func CopyTableToStruct ¶
Copy matching Lua table entries to a struct, given the struct type and the index on the Lua stack.
func GoLuaFunc ¶
func GoLuaFunc(L *lua.State, fun interface{}) lua.LuaGoFunction
GoLuaFunc converts an arbitrary Go function into a Lua-compatible GoFunction. There are special optimized cases for functions that go from strings to strings, and doubles to doubles, but otherwise Go reflection is used to provide a generic wrapper function
func GoToLua ¶
Push a Go value 'val' of type 't' on the Lua stack. If we haven't been given a concrete type, use the type of the value and unbox any interfaces. You can force slices and maps to be copied over as tables by setting 'dontproxify' to true.
func Init ¶
Make and initialize a new Lua state. Populates luar table with five functions: map2table, slice2table, map, slice, type, and value. This makes customized pairs/ipairs // functions available so that __pairs/__ipairs can be used, Lua 5.2 style.
func Lookup ¶
Look up a Lua value by its full name. If idx is 0, then this name is assumed to start in the global table, e.g. "string.gsub". With non-zero idx, can be used to look up subfields of a table. It terminates with a nil value if we cannot continue the lookup...
func LuaToGo ¶
Convert a Lua value 'idx' on the stack to the Go value of desired type 't'. Handles numerical and string types in a straightforward way, and will convert tables to either map or slice types.
func MakeChannel ¶
func RawRegister ¶
Make a number of 'raw' Go functions or values available in Lua code. (Raw Go functions access the Lua state directly and have signature (L *lua.State) int.)
Types ¶
type LuaObject ¶
Encapsulates a Lua object like a table or a function
func NewLuaObject ¶
A new LuaObject from stack index.
func NewLuaObjectFromName ¶
A new LuaObject from global qualified name, using Lookup.
func NewLuaObjectFromValue ¶
A new LuaObject from a Go value. Note that this _will_ convert any slices or maps into Lua tables.
func (*LuaObject) Call ¶
Call a Lua function, and return a single value, converted in a default way.
func (*LuaObject) Callf ¶
func (lo *LuaObject) Callf(rtypes []reflect.Type, args ...interface{}) (res []interface{}, err error)
Call a Lua function, given the desired return types and the arguments.
type LuaTableIter ¶
type LuaTableIter struct { Key interface{} Value interface{} // contains filtered or unexported fields }
func (*LuaTableIter) Next ¶
func (ti *LuaTableIter) Next() bool
Get next key/value pair from table
iter := lo.Iter() keys := []string{} for iter.Next() { keys = append(keys, iter.Key.(string)) }