diff options
| author | unwox <me@unwox.com> | 2024-06-13 13:11:01 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-06-13 13:11:01 +0600 |
| commit | c1ac633d9fa5da9fb312e4a990ee48d0fa7c0c4e (patch) | |
| tree | 9ce19fcc4c63b63d839e44396daccd524c30e14f | |
| parent | ff448a271d8b10684bec49aaa5d59d8952431f75 (diff) | |
add LoadString method for evaluating strings in lua context
| -rw-r--r-- | lua.go | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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)) |
