function.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright erfenjiao, 630166475@qq.com.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package runtime
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/second-state/WasmEdge-go/wasmedge"
  20. "log"
  21. )
  22. type WasmFunc struct {
  23. symbolName string
  24. reg *PluginMeta
  25. isAgg int
  26. }
  27. func NewWasmFunc(symbolName string, reg *PluginMeta) (*WasmFunc, error) {
  28. // Setup channel and route the data
  29. conf.Log.Infof("Start running wasm function meta %+v", reg)
  30. return &WasmFunc{
  31. symbolName: symbolName,
  32. reg: reg,
  33. }, nil
  34. }
  35. func (f *WasmFunc) Validate(args []interface{}) error {
  36. if len(args) == 0 {
  37. fmt.Println("[plugin][wasm][runtime][Validate] args is null")
  38. }
  39. var err error
  40. return err
  41. }
  42. func (f *WasmFunc) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  43. res := f.ExecWasmFunc(args)
  44. fr := &FuncReply{}
  45. fr.Result = res
  46. fr.State = true
  47. if !fr.State {
  48. if fr.Result != nil {
  49. return fmt.Errorf("%s", fr.Result), false
  50. } else {
  51. return nil, false
  52. }
  53. }
  54. return fr.Result, fr.State
  55. }
  56. func (f *WasmFunc) IsAggregate() bool {
  57. if f.isAgg > 0 {
  58. return f.isAgg > 1
  59. }
  60. return false
  61. }
  62. func (f *WasmFunc) ExecWasmFunc(args []interface{}) []interface{} {
  63. funcname := f.symbolName
  64. WasmFile := f.reg.WasmFile
  65. fmt.Println("[wasm][ExecWasmFunc] WasmFile: ", WasmFile)
  66. conf1 := wasmedge.NewConfigure(wasmedge.WASI)
  67. store := wasmedge.NewStore()
  68. vm := wasmedge.NewVMWithConfigAndStore(conf1, store)
  69. wasi := vm.GetImportModule(wasmedge.WASI)
  70. //step 1: Load WASM file
  71. err := vm.LoadWasmFile(WasmFile)
  72. if err != nil {
  73. fmt.Print("[wasm][ExecWasmFunc] Load WASM from file FAILED: ")
  74. fmt.Errorf(err.Error())
  75. }
  76. //step 2: Validate the WASM module
  77. err = vm.Validate()
  78. if err != nil {
  79. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Validate FAILED: ")
  80. fmt.Errorf(err.Error())
  81. }
  82. //step 3: Instantiate the WASM moudle
  83. err = vm.Instantiate()
  84. if err != nil {
  85. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Instantiate FAILED: ")
  86. fmt.Errorf(err.Error())
  87. }
  88. // step 4: Execute WASM functions.Parameters(1)
  89. var Args []float64
  90. for _, num := range args {
  91. x, ok := (num).(float64)
  92. if !ok {
  93. fmt.Println("Type tranform not to float64!!")
  94. }
  95. Args = append(Args, x)
  96. }
  97. Len := len(args)
  98. var res []interface{}
  99. switch Len {
  100. case 0:
  101. res, err = vm.Execute(funcname)
  102. if err != nil {
  103. log.Fatalln("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Run function failed: ", err.Error())
  104. } else {
  105. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Get res: ")
  106. fmt.Println(res[0].(int32))
  107. }
  108. exitcode := wasi.WasiGetExitCode()
  109. if exitcode != 0 {
  110. fmt.Println("Go: Running wasm failed, exit code:", exitcode)
  111. }
  112. vm.Release()
  113. case 1:
  114. res, err = vm.Execute(funcname, uint32(Args[0]))
  115. if err != nil {
  116. log.Fatalln("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Run function failed: ", err.Error())
  117. } else {
  118. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Get res: ")
  119. fmt.Println(res[0].(int32))
  120. }
  121. exitcode := wasi.WasiGetExitCode()
  122. if exitcode != 0 {
  123. fmt.Println("Go: Running wasm failed, exit code:", exitcode)
  124. }
  125. vm.Release()
  126. case 2:
  127. res, err = vm.Execute(funcname, uint32(Args[0]), uint32(Args[1]))
  128. if err != nil {
  129. log.Fatalln("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Run function failed: ", err.Error())
  130. } else {
  131. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Get res: ")
  132. fmt.Println(res[0].(int32))
  133. }
  134. exitcode := wasi.WasiGetExitCode()
  135. if exitcode != 0 {
  136. fmt.Println("Go: Running wasm failed, exit code:", exitcode)
  137. }
  138. vm.Release()
  139. case 3:
  140. res, err = vm.Execute(funcname, uint32(Args[0]), uint32(Args[1]), uint32(Args[2]))
  141. if err != nil {
  142. log.Fatalln("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Run function failed: ", err.Error())
  143. } else {
  144. fmt.Print("[wasm][manager-AddWasmPlugin-NewWasmPlugin] Get res: ")
  145. fmt.Println(res[0].(int32))
  146. }
  147. exitcode := wasi.WasiGetExitCode()
  148. if exitcode != 0 {
  149. fmt.Println("Go: Running wasm failed, exit code:", exitcode)
  150. }
  151. vm.Release()
  152. }
  153. return res
  154. }