示例#1
0
文件: luar.go 项目: kdar/morphgen
// 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
	}
}
示例#2
0
文件: luar.go 项目: kdar/morphgen
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)
	}
}
示例#3
0
文件: ui.go 项目: kdar/morphgen
// Add a callback to the lua state.
func addCallback(L *lua.State, name string, args []interface{}) {
	L.GetGlobal("callbacks")
	luar.GoToLua(L, reflect.TypeOf(args), reflect.ValueOf(args))
	L.SetField(-2, name)
}