summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-06-13 13:11:01 +0600
committerunwox <me@unwox.com>2024-06-13 13:11:01 +0600
commitc1ac633d9fa5da9fb312e4a990ee48d0fa7c0c4e (patch)
tree9ce19fcc4c63b63d839e44396daccd524c30e14f
parentff448a271d8b10684bec49aaa5d59d8952431f75 (diff)
add LoadString method for evaluating strings in lua context
-rw-r--r--lua.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/lua.go b/lua.go
index 1c23126..f13a8cb 100644
--- a/lua.go
+++ b/lua.go
@@ -121,6 +121,23 @@ func (l *Lua) Next() bool {
return C.lua_next(l.l, -2) != 0
}
+func (l *Lua) LoadString(code string) error {
+ cstr := C.CString(code)
+ defer C.free(unsafe.Pointer(cstr))
+ if C.luaL_loadstring(l.l, cstr) != C.LUA_OK {
+ errMsg := l.ToString(-1)
+ l.Pop(1)
+ return errors.New(errMsg)
+ }
+ if !l.PCall(0, 0) {
+ errMsg := l.ToString(-1)
+ l.Pop(1)
+ return errors.New(errMsg)
+ }
+ l.Pop(l.StackLen())
+ return nil
+}
+
func (l *Lua) PushTableItem(key string) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))