Exemple #1
0
func (self *Lua) CallFunction(name string, args ...interface{}) {
	defer func() {
		if r := recover(); r != nil {
			if self.PrintTraceback { //NOCOVER
				print("============ start lua traceback ============\n")
				self.RunString(`print(debug.traceback())`)
				print("============ end lua traceback ==============\n")
			}
			panic(r)
		}
	}()
	cName := cstr(name)
	C.setup_message_handler(self.State)
	C.lua_getglobal(self.State, cName)
	for _, arg := range args {
		self.PushGoValue(reflect.ValueOf(arg))
	}
	ret := C.lua_pcallk(self.State, C.int(len(args)), 0, C.lua_gettop(self.State)-C.int(len(args)+2), 0, nil)
	if ret != C.int(0) {
		self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
	}
}
Exemple #2
0
func (self *Lua) RunString(code string) {
	defer func() {
		if r := recover(); r != nil {
			if self.PrintTraceback { //NOCOVER
				print("============ start lua traceback ============\n")
				self.RunString(`print(debug.traceback())`)
				print("============ end lua traceback ==============\n")
			}
			panic(r)
		}
	}()
	cCode := cstr(code)
	C.setup_message_handler(self.State)
	if ret := C.luaL_loadstring(self.State, cCode); ret != C.int(0) {
		self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
	}
	ret := C.lua_pcallk(self.State, 0, 0, C.lua_gettop(self.State)-C.int(1), 0, nil)
	if ret != C.int(0) {
		self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
	}
	C.lua_settop(self.State, 0)
}