From 04ef069a20aa851483d4e4faf25f7a52fa717a2c Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sat, 10 Aug 2024 12:29:44 +0800 Subject: [PATCH] c/lua:custom panic --- c/lua/_demo/custom-panic/panic.go | 66 +++++++++++++++++++++++++++++++ c/lua/lauxlib.go | 3 ++ c/lua/lua.go | 8 +++- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 c/lua/_demo/custom-panic/panic.go diff --git a/c/lua/_demo/custom-panic/panic.go b/c/lua/_demo/custom-panic/panic.go new file mode 100644 index 00000000..a63cf26c --- /dev/null +++ b/c/lua/_demo/custom-panic/panic.go @@ -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 +*/ diff --git a/c/lua/lauxlib.go b/c/lua/lauxlib.go index 7df13dd3..4f07e8de 100644 --- a/c/lua/lauxlib.go +++ b/c/lua/lauxlib.go @@ -14,6 +14,9 @@ import ( // /* 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 */ // llgo:link (*State).Loadfilex C.luaL_loadfilex diff --git a/c/lua/lua.go b/c/lua/lua.go index 86f3a532..8a85a205 100644 --- a/c/lua/lua.go +++ b/c/lua/lua.go @@ -152,7 +152,10 @@ func (L *State) Newthread() *State { return nil } // int (lua_closethread) (State *L, State *from); // 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); // /* @@ -389,7 +392,8 @@ const ( // llgo:link (*State).Next C.lua_next 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_len) (State *L, int idx);