example_packages_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package vm_test
  2. import (
  3. "log"
  4. "github.com/mattn/anko/env"
  5. _ "github.com/mattn/anko/packages"
  6. "github.com/mattn/anko/vm"
  7. )
  8. func Example_vmSort() {
  9. // _ "github.com/mattn/anko/packages"
  10. e := env.NewEnv()
  11. script := `
  12. fmt = import("fmt")
  13. sort = import("sort")
  14. a = [5, 1.1, 3, "f", "2", "4.4"]
  15. sortFuncs = make(sort.SortFuncsStruct)
  16. sortFuncs.LenFunc = func() { return len(a) }
  17. sortFuncs.LessFunc = func(i, j) { return a[i] < a[j] }
  18. sortFuncs.SwapFunc = func(i, j) { temp = a[i]; a[i] = a[j]; a[j] = temp }
  19. sort.Sort(sortFuncs)
  20. fmt.Println(a)
  21. `
  22. _, err := vm.Execute(e, nil, script)
  23. if err != nil {
  24. log.Fatalf("execute error: %v\n", err)
  25. }
  26. // output:
  27. // [f 1.1 2 3 4.4 5]
  28. }
  29. func Example_vmRegexp() {
  30. // _ "github.com/mattn/anko/packages"
  31. e := env.NewEnv()
  32. script := `
  33. fmt = import("fmt")
  34. regexp = import("regexp")
  35. re = regexp.MustCompile("^simple$")
  36. result = re.MatchString("simple")
  37. fmt.Println(result)
  38. fmt.Println("")
  39. re = regexp.MustCompile("simple")
  40. result = re.FindString("This is a simple sentence")
  41. fmt.Println(result)
  42. fmt.Println("")
  43. re = regexp.MustCompile(",")
  44. result = re.Split("a,b,c", -1)
  45. fmt.Println(result)
  46. fmt.Println("")
  47. re = regexp.MustCompile("foo")
  48. result = re.ReplaceAllString("foo", "bar")
  49. fmt.Println(result)
  50. `
  51. _, err := vm.Execute(e, nil, script)
  52. if err != nil {
  53. log.Fatalf("execute error: %v\n", err)
  54. }
  55. // output:
  56. // true
  57. //
  58. // simple
  59. //
  60. // [a b c]
  61. //
  62. // bar
  63. }
  64. func Example_vmHttp() {
  65. // _ "github.com/mattn/anko/packages"
  66. e := env.NewEnv()
  67. script := `
  68. fmt = import("fmt")
  69. io = import("io")
  70. ioutil = import("io/ioutil")
  71. net = import("net")
  72. http = import("net/http")
  73. time = import("time")
  74. func handlerRoot(responseWriter, request) {
  75. io.WriteString(responseWriter, "Hello World :)")
  76. }
  77. serveMux = http.NewServeMux()
  78. serveMux.HandleFunc("/", handlerRoot)
  79. listener, err = net.Listen("tcp", ":8080")
  80. if err != nil {
  81. fmt.Println(err)
  82. return
  83. }
  84. go http.Serve(listener, serveMux)
  85. client = http.DefaultClient
  86. response, err = client.Get("http://localhost:8080/")
  87. if err != nil {
  88. fmt.Println(err)
  89. return
  90. }
  91. body, err = ioutil.ReadAll(response.Body)
  92. if err != nil {
  93. fmt.Println(err)
  94. }
  95. response.Body.Close()
  96. fmt.Printf("%s\n", body)
  97. `
  98. _, err := vm.Execute(e, nil, script)
  99. if err != nil {
  100. log.Fatalf("execute error: %v\n", err)
  101. }
  102. // output:
  103. // Hello World :)
  104. }