You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This one is related to #10 and how to handle errors from Go functions in Lua.
I have a program, which registers new Lua types from Go as providers, which return some result and an error.
As I don't want to do the error handling in Lua, I thought I'd use a closure and close the Go provider function where I could do the error handling and raise errors in Lua in case I have an error from my provider function. That way I only return the result from the Go function to Lua and do the error handling in the wrapper function.
What I've noticed though is that whenever I register my providers in Lua the wrapper function is always the same, resulting in overwriting the wrappers for previous providers.
Here's an example code:
package main
import (
"fmt""github.com/layeh/gopher-luar""github.com/yuin/gopher-lua"
)
// provider typetypeproviderfunc(namestring) (string, error)
// registry contains all registered providersvarregistry=map[string]provider{
"provider1": provider_1,
"provider2": provider_2,
}
funcprovider_1(namestring) (string, error) {
return"provider_1 result", nil
}
funcprovider_2(namestring) (string, error) {
return"provider_2 results", nil
}
funcmain() {
L:=lua.NewState()
deferL.Close()
// Register the providers in Luafortyp, provider:=rangeregistry {
wrapper:=func(L*lua.LState) int {
result, err:=provider(L.CheckString(1))
iferr!=nil {
L.RaiseError(err.Error()) // Do the error handling here, so that we don't have to return the error to Lua
}
L.Push(luar.New(L, result))
return1// Number of arguments
}
tbl:=L.NewTable()
tbl.RawSetH(lua.LString("new"), L.NewFunction(wrapper)) // New wrapper overwrites all previous onesL.SetGlobal(typ, tbl)
fmt.Printf("type %s -> wrapper %s\n", typ, wrapper)
}
// Run some Lua codecode:=` print(provider1.new("some input for provider1")) print(provider2.new("some input for provider2")) `iferr:=L.DoString(code); err!=nil {
panic(err)
}
}
And here's the output it generates.
type provider1 -> wrapper %!s(func(*lua.LState) int=0x401a90)
type provider2 -> wrapper %!s(func(*lua.LState) int=0x401a90)
provider_2 results
provider_2 results
As you can see the wrapper function is always the same one, but I'd expect them to be different on each iteration. As a result the last wrapper function overwrites all previous ones, and the returned result is always from the last registered provider (in this case provider2).
Is this something I've overlooked or a bug in gopher-luar?
Thanks,
Marin
The text was updated successfully, but these errors were encountered:
This one is related to #10 and how to handle errors from Go functions in Lua.
I have a program, which registers new Lua types from Go as providers, which return some result and an error.
As I don't want to do the error handling in Lua, I thought I'd use a closure and close the Go provider function where I could do the error handling and raise errors in Lua in case I have an error from my provider function. That way I only return the result from the Go function to Lua and do the error handling in the wrapper function.
What I've noticed though is that whenever I register my providers in Lua the wrapper function is always the same, resulting in overwriting the wrappers for previous providers.
Here's an example code:
And here's the output it generates.
As you can see the wrapper function is always the same one, but I'd expect them to be different on each iteration. As a result the last wrapper function overwrites all previous ones, and the returned result is always from the last registered provider (in this case provider2).
Is this something I've overlooked or a bug in gopher-luar?
Thanks,
Marin
The text was updated successfully, but these errors were encountered: