Esempio n. 1
0
func LuaPushSmartFunctionTable(L *lua.State, ft FunctionTable) {
	// Copy it just in case - I can't imagine someone changing it after passing
	// it to this function, but I don't want to take any chances.
	myft := make(FunctionTable)
	for n, f := range ft {
		myft[n] = f
	}
	names := make([]string, len(myft))[0:0]
	for name := range myft {
		names = append(names, name)
	}
	sort.Strings(names)
	valid_selectors := "["
	for i, name := range names {
		if i > 0 {
			valid_selectors += ", "
		}
		valid_selectors += fmt.Sprintf("'%s'", name)
	}
	valid_selectors += "]."

	L.NewTable()
	L.PushString("__index")
	L.PushGoFunctionAsCFunction(func(L *lua.State) int {
		name := L.ToString(-1)
		if f, ok := myft[name]; ok {
			f()
		} else {
			base.Error().Printf("'%s' is not a valid selector, valid seletors are %s", name, valid_selectors)
			L.PushNil()
		}
		return 1
	})
	L.SetTable(-3)
}