func GoLua(L *lua.State) int { go func() { LT := L.NewThread() L.PushValue(1) lua.XMove(L, LT, 1) res := LT.Resume(0) for res != 0 { if res == 2 { emsg := LT.ToString(-1) RaiseError(LT, emsg) } ch, t := valueOfProxy(LT, -2) if LT.ToBoolean(-1) { // send on a channel val := luaToGoValue(LT, t.Elem(), -3) ch.Send(val) res = LT.Resume(0) } else { // receive on a channel val, ok := ch.Recv() GoToLua(LT, t.Elem(), val, false) LT.PushBoolean(ok) res = LT.Resume(2) } } }() return 0 }
func channel_send(L *lua.State) int { L.PushValue(2) L.PushValue(1) L.PushBoolean(true) return L.Yield(3) //~ ch,t := valueOfProxy(L,1) //~ val := valueOf(LuaToGo(L, t.Elem(),2)) //~ ch.Send(val) //~ return 0 }
func channel_recv(L *lua.State) int { L.PushValue(1) L.PushBoolean(false) return L.Yield(2) //~ ch,t := valueOfProxy(L,1) //~ L.Yield(0) //~ val,ok := ch.Recv() //~ GoToLua(L,t.Elem(),val) //~ L.PushBoolean(ok) //~ L.Resume(0) //~ return 2 }
// 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 func Lookup(L *lua.State, path string, idx int) { parts := strings.Split(path, ".") if idx != 0 { L.PushValue(idx) } else { L.GetGlobal("_G") } for _, field := range parts { L.GetField(-1, field) L.Remove(-2) // remove table } }
// ProxyPairs implements Lua 5.2 'pairs' functions. // It respects the __pairs metamethod. // // It is only useful for compatibility with Lua 5.1. func ProxyPairs(L *lua.State) int { // See Lua >=5.2 source code. if L.GetMetaField(1, "__pairs") { L.PushValue(1) L.Call(1, 3) return 3 } L.CheckType(1, lua.LUA_TTABLE) L.PushGoFunction(pairsAux) L.PushValue(1) L.PushNil() return 3 }
// A new LuaObject from stack index. func NewLuaObject(L *lua.State, idx int) *LuaObject { tp := L.LTypename(idx) L.PushValue(idx) ref := L.Ref(lua.LUA_REGISTRYINDEX) return &LuaObject{L, ref, tp} }