From 46a5b492b3396a879fb1fec985d906686a4266ed Mon Sep 17 00:00:00 2001 From: unwox Date: Tue, 15 Oct 2024 14:32:09 +0600 Subject: standardize responses from luna modules --- lua.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'lua.go') diff --git a/lua.go b/lua.go index a400420..50c6fc5 100644 --- a/lua.go +++ b/lua.go @@ -99,7 +99,7 @@ func (l *Lua) StackLen() int { return int(C.lua_gettop(l.l)) } -// ToInt converts a stack value with stack index into a string. +// ToString converts a stack value with stack index into a string. func (l *Lua) ToString(index int) string { return C.GoString(C.lua_tolstring(l.l, C.int(index), nil)) } @@ -109,6 +109,11 @@ func (l *Lua) ToInt(index int) int { return int(C.lua_tonumberx(l.l, C.int(index), nil)) } +// ToBoolean converts a stack value with stack index -1 into a boolean. +func (l *Lua) ToBoolean(index int) bool { + return int(C.lua_toboolean(l.l, C.int(index))) == 1 +} + // PopToRef pops the item (stack offset -1) from the stack and then stores it // in the registry. func (l *Lua) PopToRef() LuaRef { @@ -120,17 +125,26 @@ func (l *Lua) Pop(n int) { C.lua_settop(l.l, C.int(-n - 1)) } -// PushNumber pushes tnil onto the Lua stack. +// PushNil pushes tnil onto the Lua stack. func (l *Lua) PushNil() { C.lua_pushnil(l.l) } -// PushNumber pushes the number onto the Lua stack. +// PushBoolean pushes a boolean onto the Lua stack. +func (l *Lua) PushBoolean(b bool) { + if b { + C.lua_pushboolean(l.l, 1) + } else { + C.lua_pushboolean(l.l, 0) + } +} + +// PushNumber pushes a number onto the Lua stack. func (l *Lua) PushNumber(num int) { C.lua_pushnumber(l.l, C.double(num)) } -// PushNumber pushes the number onto the Lua stack. +// PushFloatNumber pushes a float number onto the Lua stack. func (l *Lua) PushFloatNumber(num float64) { C.lua_pushnumber(l.l, C.double(num)) } @@ -169,6 +183,8 @@ func (l *Lua) SetTableItem(key string) { // PushAny pushes value v onto the stack. func (l *Lua) PushAny(v any) error { switch v.(type) { + case nil: + l.PushNil() case string: v, _ := v.(string) l.PushString(v) @@ -186,10 +202,16 @@ func (l *Lua) PushAny(v any) error { l.PushFloatNumber(v) case map[string]any: v, _ := v.(map[string]any) - return l.PushObject(v) + err := l.PushObject(v) + if err != nil { + return fmt.Errorf("array push errro: ", err) + } case []any: v, _ := v.([]any) - return l.PushArray(v) + err := l.PushArray(v) + if err != nil { + return fmt.Errorf("array push errro: ", err) + } case time.Time: v, _ := v.(time.Time) l.PushString(v.Format(time.DateTime)) @@ -252,6 +274,11 @@ func (l *Lua) IsNumber(index int) bool { return C.lua_isnumber(l.l, C.int(index)) == 1 } +// IsBoolean checks if the stack contains a boolean under the given index. +func (l *Lua) IsBoolean(index int) bool { + return C.lua_type(l.l, C.int(index)) == C.LUA_TBOOLEAN +} + // IsFunction checks if the stack contains a function under the given index. func (l *Lua) IsFunction(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TFUNCTION @@ -263,6 +290,7 @@ func (l *Lua) IsTable(index int) bool { } // Next advances a table iterator at the stack index -2. +// IsTable checks if the stack contains a table under the given index. func (l *Lua) Next() bool { return C.lua_next(l.l, -2) != 0 } @@ -372,6 +400,8 @@ func (l *Lua) Scan(vars ...any) error { v = l.ToString(-1) } else if l.IsNumber(-1) { v = l.ToInt(-1) + } else if l.IsBoolean(-1) { + v = l.ToBoolean(-1) } else if l.IsNil(-1) { v = nil } else { @@ -422,3 +452,8 @@ func (l *Lua) GetGlobal(name string) { defer C.free(unsafe.Pointer(cstr)) C.lua_getglobal(l.l, cstr) } + +func (l *Lua) Error(msg string) { + l.PushString(msg) + C.lua_error(l.l) +} -- cgit v1.2.3