-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathl2.wasm.go
70 lines (61 loc) · 1.77 KB
/
l2.wasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//go:build wasm
package main
import (
"encoding/json"
"fmt"
"syscall/js"
controller "github.com/HexmosTech/lama2/controller"
)
var version string
func init() {
// Initialize version if not set
if len(version) == 0 {
version = "vUnset"
}
}
func main() {
// Set the global JavaScript property "goWebRequestFunc" to the result of wasmLamaPromise
js.Global().Set("goWebRequestFunc", wasmLamaPromise())
js.Global().Set("goCmdConvertFunc", wasmCodeConverter())
// Block the main function to keep the Go WebAssembly running
select {}
}
func wasmLamaPromise() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
laBearerAuthToken := args[2].String()
js.Global().Set("LaBearerAuthToken", laBearerAuthToken)
inputdata := args[0].String()
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
go func() {
result, _, _, _, _, _ := controller.ProcessWasmInput(inputdata)
resultJSON, err := json.Marshal(result)
if err != nil {
fmt.Println("Error:", err)
return
}
resolve.Invoke(js.ValueOf(string(resultJSON)))
}()
return nil
})
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}
func wasmCodeConverter() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
inputdata := args[0].String()
convertLang := args[1].String()
fmt.Println("inputdata", inputdata)
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
go func() {
result, _ := controller.ProcessConverterInput(inputdata, convertLang)
resolve.Invoke(js.ValueOf(result))
}()
return nil
})
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}