summaryrefslogtreecommitdiff
path: root/lua.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-08-23 21:14:30 +0600
committerunwox <me@unwox.com>2024-08-23 21:14:30 +0600
commit751e381230762caa6937e9c516de82fd27c59250 (patch)
tree10a4ded95e673133d9e8303524a585469e805786 /lua.go
parent4003417f7b94648a7726995a49ab56c4bc8aad55 (diff)
pass a single response object instead of multiple arguments to lua
Diffstat (limited to 'lua.go')
-rw-r--r--lua.go35
1 files changed, 29 insertions, 6 deletions
diff --git a/lua.go b/lua.go
index a150e27..875aac0 100644
--- a/lua.go
+++ b/lua.go
@@ -151,13 +151,36 @@ func (l *Lua) SetTableItem(key string) {
C.lua_setfield(l.l, -2, C.CString(key))
}
-// PushStringTable pushes string->string table onto the stack.
-func (l *Lua) PushStringTable(table map[string]string) {
- l.CreateTable(len(table))
- for k, v := range table {
- l.PushString(v)
- C.lua_setfield(l.l, -2, C.CString(k))
+// PushTable pushes string->any table onto the stack.
+func (l *Lua) PushObject(table map[string]any) error {
+ var pushTable func(t map[string]any) error
+ pushTable = func (t map[string]any) error {
+ l.CreateTable(len(t))
+ for k, v := range t {
+ switch v.(type) {
+ case string:
+ v, _ := v.(string)
+ l.PushString(v)
+ case func (l *Lua) int:
+ v, _ := v.(func (l *Lua) int)
+ l.PushGoFunction(v)
+ case int:
+ v, _ := v.(int)
+ l.PushNumber(v)
+ case map[string]any:
+ v, _ := v.(map[string]any)
+ pushTable(v)
+ default:
+ return fmt.Errorf(
+ "unsupported value type: %T",
+ v,
+ )
+ }
+ l.SetTableItem(k)
+ }
+ return nil
}
+ return pushTable(table)
}
// PushFromRef pushes a value from registry ref onto the stack.