testlua.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. "github.com/yuin/gopher-lua"
  4. "fmt"
  5. )
  6. func Double(L *lua.LState) int {
  7. lv := L.ToInt(1) /* get argument */
  8. L.Push(lua.LNumber(lv * 2)) /* push result */
  9. L.G.Global.ForEach(func(key, value lua.LValue) {
  10. switch k := key.(type) {
  11. case lua.LString:
  12. fmt.Println("In GO:", k.String(), value.String())
  13. default:
  14. fmt.Println("other type")
  15. }})
  16. return 1 /* number of results */
  17. }
  18. func Commit(L *lua.LState) int{
  19. fmt.Println("commit!")
  20. return 0
  21. }
  22. func main() {
  23. L := lua.NewState()
  24. defer L.Close()
  25. /*
  26. prm := `
  27. t = {10, 20, 30}
  28. for _,element in pairs(t) do
  29. print(element)
  30. end
  31. `
  32. */
  33. L.SetGlobal("MX_APP", lua.LString("hello world"))
  34. L.SetGlobal("double", L.NewFunction(Double))
  35. L.SetGlobal("commit", L.NewFunction(Commit))
  36. prm := `
  37. --keyspace = aaa
  38. --class = xxx
  39. --queue = grok:MX_APP, MX_APP1, MX_APP2
  40. --match = MX_WINDOWS
  41. _app = MX_APP
  42. t = {10, 20, 30}
  43. for _,element in pairs(t) do
  44. print(element)
  45. end
  46. print(_app)
  47. mybiz = function(name)
  48. return "hello" .. name
  49. end
  50. _business = mybiz(MX_APP)
  51. print(_business)
  52. for n,v in pairs(_G) do
  53. print("G:",n,type(v))
  54. end
  55. print("GO Func :",double(55))
  56. commit()
  57. return 200
  58. `
  59. if err := L.DoString(prm); err != nil {
  60. panic(err)
  61. }
  62. ret := L.Get(-1) // returned top value
  63. L.Pop(1)
  64. fmt.Print("RTN from lua : " ,ret)
  65. //for n,v := range L.GetGlobal("double")
  66. }