I am writing a simple bridge for calling lua modules from Go. I have this simple loop
package main
import (
lua "github.com/yuin/gopher-lua"
)
func main() {
L := lua.NewState()
defer L.Close()
L.SetGlobal("someFunction", L.NewFunction(someFunction))
code := `
print("First loop")
local x = someFunction()
for v in x.get() do
print("Value: " .. v)
end
print("Second loop")
for v in someFunction().get() do
print("Value: " .. v)
end
`
if err := L.DoString(code); err != nil {
panic(err)
}
}
func someFunction(L *lua.LState) int {
obj := L.NewTable()
L.SetField(obj, "get", L.NewFunction(getBinding))
L.Push(obj)
return 1
}
func getBinding(L *lua.LState) int {
items := []string{"Gopher", "Lua", "Go"}
index := 0
iterator := func(L *lua.LState) int {
if index < len(items) {
L.Push(lua.LString(items[index]))
index++
return 1
}
return 0
}
L.Push(L.NewFunction(iterator))
return 1
}
But for some reason 1st loop works just fine but the 2nd loop fails with this error🤔
panic: <string>:8: attempt to call a non-function object
stack traceback:
<string>:8: in main chunk
[G]: ?
Does anyone know what might go wrong here? And thank you in advance.