c/lua:custom panic
This commit is contained in:
66
c/lua/_demo/custom-panic/panic.go
Normal file
66
c/lua/_demo/custom-panic/panic.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
"github.com/goplus/llgo/c/lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
func triggerError(L *lua.State) c.Int {
|
||||||
|
L.Pushstring(c.Str("This is an error triggered"))
|
||||||
|
return L.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func triggerFormatError(L *lua.State) c.Int {
|
||||||
|
return L.LError(c.Str("This is an error code:(%d)"), 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
func customPanic(L *lua.State) c.Int {
|
||||||
|
msg := L.Tostring(-1)
|
||||||
|
c.Printf(c.Str("Pani'c: %s\n"), msg)
|
||||||
|
os.Exit(1)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
L := lua.Newstate()
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
L.Openlibs()
|
||||||
|
|
||||||
|
L.Atpanic(customPanic)
|
||||||
|
|
||||||
|
L.Pushcfunction(triggerError)
|
||||||
|
L.Setglobal(c.Str("trigger_error"))
|
||||||
|
L.Pushcfunction(triggerFormatError)
|
||||||
|
L.Setglobal(c.Str("trigger_format_error"))
|
||||||
|
|
||||||
|
c.Printf(c.Str("1. error (protected):\n"))
|
||||||
|
L.Getglobal(c.Str("trigger_error"))
|
||||||
|
if L.Pcall(0, 0, 0) != lua.OK {
|
||||||
|
c.Printf(c.Str("Error: %s\n"), L.Tostring(-1))
|
||||||
|
L.Pop(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Printf(c.Str("2. format_error (protected):\n"))
|
||||||
|
L.Getglobal(c.Str("trigger_format_error"))
|
||||||
|
if L.Pcall(0, 0, 0) != lua.OK {
|
||||||
|
c.Printf(c.Str("Error: %s\n"), L.Tostring(-1))
|
||||||
|
L.Pop(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Printf(c.Str("3. Unprotected call (panic):\n"))
|
||||||
|
L.Getglobal(c.Str("trigger_error"))
|
||||||
|
// This will trigger unprotected panic and catch by customPanic
|
||||||
|
L.Call(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
1. error (protected):
|
||||||
|
Error: This is an error triggered
|
||||||
|
2. format_error (protected):
|
||||||
|
Error: This is an error code:(42)
|
||||||
|
3. Unprotected call (panic):
|
||||||
|
Panic: This is an error triggered
|
||||||
|
*/
|
||||||
@@ -14,6 +14,9 @@ import (
|
|||||||
|
|
||||||
// /* key, in the registry, for table of preloaded loaders */
|
// /* key, in the registry, for table of preloaded loaders */
|
||||||
|
|
||||||
|
// llgo:link (*State).LError C.luaL_error
|
||||||
|
func (L *State) LError(format *c.Char, __llgo_va_list ...any) c.Int { return 0 }
|
||||||
|
|
||||||
// /* predefined references */
|
// /* predefined references */
|
||||||
|
|
||||||
// llgo:link (*State).Loadfilex C.luaL_loadfilex
|
// llgo:link (*State).Loadfilex C.luaL_loadfilex
|
||||||
|
|||||||
@@ -152,7 +152,10 @@ func (L *State) Newthread() *State { return nil }
|
|||||||
|
|
||||||
// int (lua_closethread) (State *L, State *from);
|
// int (lua_closethread) (State *L, State *from);
|
||||||
// int (lua_resetthread) (State *L); /* Deprecated! */
|
// int (lua_resetthread) (State *L); /* Deprecated! */
|
||||||
// lua_CFunction (lua_atpanic) (State *L, lua_CFunction panicf);
|
|
||||||
|
// llgo:link (*State).Atpanic C.lua_atpanic
|
||||||
|
func (L *State) Atpanic(panicf CFunction) CFunction { return nil }
|
||||||
|
|
||||||
// lua_Number (lua_version) (State *L);
|
// lua_Number (lua_version) (State *L);
|
||||||
|
|
||||||
// /*
|
// /*
|
||||||
@@ -389,7 +392,8 @@ const (
|
|||||||
// llgo:link (*State).Next C.lua_next
|
// llgo:link (*State).Next C.lua_next
|
||||||
func (L *State) Next(idx c.Int) c.Int { return 0 }
|
func (L *State) Next(idx c.Int) c.Int { return 0 }
|
||||||
|
|
||||||
// LUA_API int (lua_error) (State *L);
|
// llgo:link (*State).Error C.lua_error
|
||||||
|
func (L *State) Error() c.Int { return 0 }
|
||||||
|
|
||||||
// LUA_API void (lua_concat) (State *L, int n);
|
// LUA_API void (lua_concat) (State *L, int n);
|
||||||
// LUA_API void (lua_len) (State *L, int idx);
|
// LUA_API void (lua_len) (State *L, int idx);
|
||||||
|
|||||||
Reference in New Issue
Block a user