我如何用gopher-lua定义一个Lua函数,它有一个预定义的表作为参数,Lua脚本可以访问该函数中的表?

o4tp2gmn  于 2023-11-14  发布在  Go
关注(0)|答案(2)|浏览(144)

我知道如何定义Go函数并将其设置为全局函数(文档中的Double示例)。但是,如果这个函数的参数应该是一个预定义的表呢?

  1. function calling_this_function_would_be_required(predefined_table)
  2. print(predefined_table["something"])
  3. end

字符串
IMAP服务器Dovecot确实提供了类似上面的内容:https://doc.dovecot.org/configuration_manual/authentication/lua_based_authentication/#examples
我也想提供预定义的功能表(甚至用户数据)。但我真的不知道如何实现这一点。
将表设置为全局很容易(L.SetGlobal(...)),但如何将其添加到预期的函数中?
在Go中添加函数

  1. func CallMe(L *lua.LState) {
  2. // How do I add a table as argument??
  3. }
  4. func Foo() {
  5. L := NewState()
  6. defer L.Close()
  7. t := L.NewTable()
  8. t.RawSetString("example", lua.LString("some_value"))
  9. // I do not want a global table. I would like an expected Lua function that has _this_ table as argument
  10. L.SetGlobal("predefined_table", t)
  11. // Not even sure with his...
  12. L.SetGlobal("calling_this_function_is_required", L.NewFunction©llMe))
  13. }


如果有人能给我一点启发,那就太好了:-)提前感谢

uidvcgyl

uidvcgyl1#

基于@koyaanisqatsi的回答,我发现了如何在Go中工作。
Go代码示例:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/yuin/gopher-lua"
  5. )
  6. type Person struct {
  7. Name string
  8. GivenName string
  9. Street string
  10. PostalCode string
  11. City string
  12. }
  13. func main() {
  14. p := &Person{
  15. Name: "Mustermann",
  16. GivenName: "Max",
  17. Street: "Sackgasse 19",
  18. PostalCode: "36304",
  19. City: "Alsfeld",
  20. }
  21. L := lua.NewState()
  22. defer L.Close()
  23. if err := L.DoFile("sample.lua"); err != nil {
  24. panic(err)
  25. }
  26. t := L.NewTable()
  27. t.RawSetString("name", lua.LString(p.Name))
  28. t.RawSetString("given_name", lua.LString(p.GivenName))
  29. t.RawSetString("street", lua.LString(p.Street))
  30. t.RawSetString("postal_code", lua.LString(p.PostalCode))
  31. t.RawSetString("city", lua.LString(p.City))
  32. if err := L.CallByParam(lua.P{
  33. Fn: L.GetGlobal("call_me"),
  34. NRet: 1,
  35. Protect: true,
  36. }, t); err != nil {
  37. panic(err)
  38. }
  39. ret := L.Get(-1) // returned value
  40. L.Pop(1) // remove received value
  41. fmt.Println("The result of the Lua function is:", ret)
  42. }

字符串
sample.lua文件:

  1. function call_me(tbl)
  2. print(tbl.name)
  3. print(tbl.given_name)
  4. print(tbl.street)
  5. print(tbl.postal_code)
  6. print(tbl.city)
  7. return 0
  8. end


测试结果:

  1. Mustermann
  2. Max
  3. Sackgasse 19
  4. 36304
  5. Alsfeld
  6. The result of the Lua function is: 0

展开查看全部
c86crjj0

c86crjj02#

如果省略参数,则可以返回预定义的表。
例如

  1. -- pdtfunc.lua
  2. local function pdtfunc(pdt)
  3. local pdt = pdt and setmetatable(pdt, {__index = table}) or setmetatable({"Exampe Value (Index Key 1)", ["Example"] = "Example Value by named Key"}, {__index = table})
  4. return(pdt)
  5. end
  6. return(pdtfunc)

字符串
返回表的构造函数将所有表函数作为方法附加。
使用示例(Lua 5.1 Standalone)

  1. /bin/lua
  2. Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
  3. > pdt = require("pdtfunc")
  4. > print(pdt())
  5. table: 0x565b2bf0
  6. > print(pdt():concat()) -- Attached Method to get string or number values
  7. Exampe Value (Index Key 1)
  8. > print(pdt()[1])
  9. Exampe Value (Index Key 1)
  10. > print(pdt()["Example"])
  11. Example Value by named Key
  12. > print(pdt().Example)
  13. Example Value by named Key
  14. > mytab = pdt()
  15. > print(mytab:concat())
  16. Exampe Value (Index Key 1)
  17. > print(mytab.Example)
  18. Example Value by named Key
  19. > mytab:insert("New Value with insert()")
  20. > print(#mytab)
  21. 2
  22. > print(mytab:concat("\n"))
  23. Exampe Value (Index Key 1)
  24. New Value with insert()
  25. > -- Now give a table as argument
  26. > mytab = require("pdtfunc")({"With a table as argument"})
  27. > print(mytab:concat())
  28. With a table as argument

展开查看全部

相关问题