summaryrefslogtreecommitdiff
path: root/luajit.go
diff options
context:
space:
mode:
Diffstat (limited to 'luajit.go')
-rw-r--r--luajit.go46
1 files changed, 40 insertions, 6 deletions
diff --git a/luajit.go b/luajit.go
index 2ee8b55..1a8b47d 100644
--- a/luajit.go
+++ b/luajit.go
@@ -95,7 +95,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))
}
@@ -105,6 +105,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 {
@@ -116,17 +121,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))
}
@@ -165,6 +179,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)
@@ -182,10 +198,16 @@ func (l *Lua) PushAny(v any) error {
l.PushFloatNumber(v)
case map[string]any:
v, _ := v.(map[string]any)
- l.PushObject(v)
+ err := l.PushObject(v)
+ if err != nil {
+ return fmt.Errorf("object push error: ", err)
+ }
case []any:
v, _ := v.([]any)
- l.PushArray(v)
+ err := l.PushArray(v)
+ if err != nil {
+ return fmt.Errorf("array push error: ", err)
+ }
case time.Time:
v, _ := v.(time.Time)
l.PushString(v.Format(time.DateTime))
@@ -248,6 +270,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
@@ -368,6 +395,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 {
@@ -418,3 +447,8 @@ func (l *Lua) GetGlobal(name string) {
defer C.free(unsafe.Pointer(cstr))
C.lua_getfield(l.l, C.LUA_GLOBALSINDEX, cstr)
}
+
+func (l *Lua) Error(msg string) {
+ l.PushString(msg)
+ C.lua_error(l.l)
+}