func register(L *lua.State, table string, values Map, convertFun bool) { pop := true if table == "*" { pop = false } else if len(table) > 0 { L.GetGlobal(table) if L.IsNil(-1) { L.NewTable() L.SetGlobal(table) L.GetGlobal(table) } } else { L.GetGlobal("_G") } for name, val := range values { t := reflect.TypeOf(val) if t.Kind() == reflect.Func { if convertFun { L.PushGoFunction(GoLuaFunc(L, val)) } else { lf := val.(func(*lua.State) int) L.PushGoFunction(lf) } } else { GoToLua(L, t, valueOf(val)) } L.SetField(-2, name) } if pop { L.Pop(1) } }
func callGoMethod(L *lua.State, name string, st reflect.Value) { ret := st.MethodByName(name) if !ret.IsValid() { fmt.Println("whoops") } L.PushGoFunction(GoLuaFunc(L, ret)) }
func slice__index(L *lua.State) int { slice, _ := valueOfProxy(L, 1) if L.IsNumber(2) { idx := L.ToInteger(2) ret := slice.Index(idx - 1) GoToLua(L, ret.Type(), ret) } else { name := L.ToString(2) switch name { case "Slice": L.PushGoFunction(slice_slice) default: fmt.Println("unknown slice method") } } return 1 }
func initializeProxies(L *lua.State) { add_index := func(name string, fun lua.GoFunction) { L.PushGoCallback(fun) L.SetField(-2, name) } flagValue := func() { L.PushBoolean(true) L.SetField(-2, "luago.value") L.Pop(1) } L.NewMetaTable(SLICE_META) add_index("__index", slice__index) add_index("__newindex", slice__newindex) add_index("__len", slicemap__len) flagValue() L.NewMetaTable(MAP_META) add_index("__index", map__index) add_index("__newindex", map__newindex) add_index("__len", slicemap__len) flagValue() L.NewMetaTable(STRUCT_META) add_index("__index", struct__index) add_index("__newindex", struct__newindex) flagValue() L.NewMetaTable(INTERFACE_META) add_index("__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() }
func main() { var L *lua51.State L = lua51.NewState() L.OpenLibs() L.GetField(lua51.LUA_GLOBALSINDEX, "print") L.PushString("Hello World!") L.Call(1, 0) L.PushGoFunction(test) L.PushGoFunction(test) L.PushGoFunction(test) L.PushGoFunction(test) L.PushGoFunction(test2) L.PushInteger(42) L.Call(1, 0) L.Call(0, 0) L.Call(0, 0) L.Call(0, 0) L.Close() }