New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing LTable to CallByParam #257
Comments
I tried to recreate you code and and hadn't any errors or unexpected behaviour. go.mod
main.go package main
import (
"fmt"
lua "github.com/yuin/gopher-lua"
)
func main() {
vm := lua.NewState(lua.Options{
RegistrySize: 1024 * 20,
RegistryMaxSize: 1024 * 80,
RegistryGrowStep: 32,
})
ltbl := vm.NewTable()
ltbl.RawSetString("varTest", lua.LString("Test!"))
vm.DoString(`
function Test(tableTest)
print("tableTest:", tableTest["varTest"])
end
`)
err := vm.CallByParam(lua.P{
Protect: true,
NRet: 0,
Fn: vm.GetGlobal("Test"),
}, ltbl)
if err != nil {
fmt.Println(err)
return
}
} |
I still don't understand why my code isn't working, but yours is... Thanks for your help anyway, I'll dig deeper ! |
Ok, my problem is because I use |
package testLua1
import (
lua "github.com/yuin/gopher-lua"
)
func TestLua1() {
L := lua.NewState(lua.Options{
RegistrySize: 1024 * 20,
RegistryMaxSize: 1024 * 80,
RegistryGrowStep: 32,
})
ltbl := L.NewTable()
ltbl.RawSetString("varTest", lua.LString("Test!"))
ftest := func(L *lua.LState) int {
funk := L.CheckFunction(1)
L.CallByParam(lua.P{
Fn: L,
NRet: 0,
Protect: true,
Handler: funk,
}, ltbl)
return 0
}
var test = map[string]lua.LGFunction{
"testGo": ftest,
}
mod := L.RegisterModule("luaTest", test)
L.Push(mod)
L.DoString(`
function Test(tableTest)
print("tableTest:", tableTest["varTest"])
end
luaTest.testGo(Test)
`)
} Output = I could use |
@Julien2313 It's a bit hard to say why your code didn't work since you posted only some parts of it. My guess is, that the problem was that you passed the variable with an ampersand
You will get a different output. For information about gopher-lua module just look at other modules. Maybe this code will help you: https://github.com/epikur-io/glua-email To load the module f.e. "glua-email" you just say: L.PreloadModule("email", email.Loader) I think this would be the way to go if you want to develope modules for gopher lua. |
Thanks for your help ! This is the test code, you juste have to call de function, or change it to main(). |
I guess my problem is because I don't use well the parameter handler |
With the PreloadModule() : package main
import (
lua "github.com/yuin/gopher-lua"
)
func main() {
L := lua.NewState(lua.Options{
RegistrySize: 1024 * 20,
RegistryMaxSize: 1024 * 80,
RegistryGrowStep: 32,
})
defer L.Close()
ltbl := L.NewTable()
ltbl.RawSetString("varTest", lua.LString("Test!"))
ftest := func(L *lua.LState) int {
funk := L.CheckFunction(1)
_ = L.CallByParam(lua.P{
Fn: L,
NRet: 0,
Protect: true,
Handler: funk,
}, ltbl)
return 0
}
L.PreloadModule("luaTest", ftest)
if err := L.DoString(`
function Test(tableTest)
print("yho")
print("tableTest:", tableTest["varTest"])
end
luaTest(Test)
`); err != nil {
panic(err)
}
} Output :
|
I see there is an error I over looked:
"Fn" Should be the the lua function not the VM (L) itself. Also you don't need to set the "Handler" for L.CallByParam, just leave it blank. Edit: If you don't get your code working you can also contact me. |
Ok, I tried a different way, and this is working pretty well : package main
import (
lua "github.com/yuin/gopher-lua"
)
func main() {
L := lua.NewState(lua.Options{
RegistrySize: 1024 * 20,
RegistryMaxSize: 1024 * 80,
RegistryGrowStep: 32,
})
defer L.Close()
ltbl := L.NewTable()
ltbl.RawSetString("varTest", lua.LString("Test!"))
ftest := func(L *lua.LState) int {
funk := L.CheckAny(1)
if err := L.CallByParam(lua.P{
Fn: funk,
NRet: 0,
Protect: true,
}, ltbl); err != nil {
panic(err)
}
return 0
}
var test = map[string]lua.LGFunction{
"testGo": ftest,
}
mod := L.RegisterModule("luaTest", test)
L.Push(mod)
if err := L.DoString(`
function Test(tableTest)
print("tableTest:", tableTest["varTest"])
end
luaTest.testGo(Test)
`); err != nil {
panic(err)
}
} I don't know if I use in a bad way Thanks for your time @epikur-io ! |
Ok, if I just change CheckAny to CheckFunction, it's working too. |
Please answer the following before submitting your issue:
1cd887cd7036
go version go1.13.3
linux/amd64
Exactly like this issue :
[Question][Help] How do I Passing a lua.LTable to a CallByParam #178
When I print my lTable in Go, I get something like that :
{<nil> [] map[] map[varTest:test] [varTest] map[varTest:0]}
And in my test function in LUA :
(I tried .Metatable() too, in case)
The keys and values in my lTable
I get a nil
The text was updated successfully, but these errors were encountered: