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) fmt.Println("error", emsg) } ch, t := valueOfProxy(LT, -2) if LT.ToBoolean(-1) { // send on a channel val := valueOf(LuaToGo(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) LT.PushBoolean(ok) res = LT.Resume(2) } } }() return 0 }
// 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 } }
func channel_send(L *lua.State) int { fmt.Println("yield send") 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 { fmt.Println("yield recv") 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 }
// create a new LuaObject using the given state and stack index. func NewLuaObject(L *lua.State, idx int) *LuaObject { tp := lua.LTypename(L, idx) L.PushValue(idx) ref := L.Ref(lua.LUA_REGISTRYINDEX) return &LuaObject{L, ref, tp} }