From cf8a170133eebb3bc54a761673689afae4162d87 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sun, 25 Aug 2024 12:32:31 +0800 Subject: [PATCH] c/lua:register --- c/lua/_demo/custom-panic/panic.go | 6 ++---- c/lua/lua.go | 7 +++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/c/lua/_demo/custom-panic/panic.go b/c/lua/_demo/custom-panic/panic.go index a63cf26c..e0e732bf 100644 --- a/c/lua/_demo/custom-panic/panic.go +++ b/c/lua/_demo/custom-panic/panic.go @@ -31,10 +31,8 @@ func main() { L.Atpanic(customPanic) - L.Pushcfunction(triggerError) - L.Setglobal(c.Str("trigger_error")) - L.Pushcfunction(triggerFormatError) - L.Setglobal(c.Str("trigger_format_error")) + L.Register(c.Str("trigger_error"), triggerError) + L.Register(c.Str("trigger_format_error"), triggerFormatError) c.Printf(c.Str("1. error (protected):\n")) L.Getglobal(c.Str("trigger_error")) diff --git a/c/lua/lua.go b/c/lua/lua.go index 8a85a205..0e066cae 100644 --- a/c/lua/lua.go +++ b/c/lua/lua.go @@ -91,7 +91,7 @@ type CFunction func(L *State) c.Int // ** Type for continuation functions // */ -// TODO(zzy): KFunction does not currently support +// llgo:type C type KFunction func(L *State, status c.Int, ctx KContext) c.Int // /* @@ -420,7 +420,10 @@ func (L *State) Tointeger(idx c.Int) Integer { return L.Tointegerx(idx, nil) } func (L *State) Pop(n c.Int) { L.Settop(-(n) - 1) } func (L *State) Newtable() { L.Createtable(0, 0) } -// #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) +func (L *State) Register(name *c.Char, f CFunction) { + L.Pushcfunction(f) + L.Setglobal(name) +} func (L *State) Pushcfunction(f CFunction) { L.Pushcclosure(f, 0) } func (L *State) Isfunction(n c.Int) bool { return L.Type(n) == c.Int(FUNCTION) }