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 }
func initializeProxies(L *lua.State) { flagValue := func() { L.PushBoolean(true) L.SetField(-2, "luago.value") L.Pop(1) } L.NewMetaTable(SLICE_META) L.SetMetaMethod("__index", slice__index) L.SetMetaMethod("__newindex", slice__newindex) L.SetMetaMethod("__len", slicemap__len) flagValue() L.NewMetaTable(MAP_META) L.SetMetaMethod("__index", map__index) L.SetMetaMethod("__newindex", map__newindex) L.SetMetaMethod("__len", slicemap__len) flagValue() L.NewMetaTable(STRUCT_META) L.SetMetaMethod("__index", struct__index) L.SetMetaMethod("__newindex", struct__newindex) flagValue() L.NewMetaTable(INTERFACE_META) L.SetMetaMethod("__index", interface__index) flagValue() L.NewMetaTable(CHANNEL_META) //~ RegisterFunctions(L,"*",FMap { //~ "Send":channel_send, //~ "Recv":channel_recv, //~ }) L.NewTable() L.PushGoFunction(channel_send) L.SetField(-2, "Send") L.PushGoFunction(channel_recv) L.SetField(-2, "Recv") L.SetField(-2, "__index") flagValue() }
// 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. func GoToLua(L *lua.State, t reflect.Type, val reflect.Value) { proxify := true if !val.IsValid() || val.IsNil() { L.PushNil() return } if t == nil { t = val.Type() if t.Kind() == reflect.Interface { // unbox interfaces! val = valueOf(val.Interface()) t = val.Type() } proxify = false } if t.Kind() == reflect.Ptr { t = t.Elem() } switch t.Kind() { case reflect.Float64: case reflect.Float32: { L.PushNumber(val.Float()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: { L.PushNumber(float64(val.Int())) } case reflect.Uint, reflect.Uint8: { L.PushNumber(float64(val.Uint())) } case reflect.String: { L.PushString(val.String()) } case reflect.Bool: { L.PushBoolean(val.Bool()) } case reflect.Slice: { if proxify { makeValueProxy(L, val, SLICE_META) } else { CopySliceToTable(L, val) } } case reflect.Map: { if proxify { makeValueProxy(L, val, MAP_META) } else { CopyMapToTable(L, val) } } case reflect.Struct: { if v, ok := val.Interface().(error); ok { L.PushString(v.Error()) } else { makeValueProxy(L, val, STRUCT_META) } } default: { if v, ok := val.Interface().(error); ok { L.PushString(v.Error()) } else if val.IsNil() { L.PushNil() } else { makeValueProxy(L, val, INTERFACE_META) } } } }