-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1.23 KB
/
main.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
//go:build wasm
// +build wasm
package main
import (
"fmt"
"syscall/js"
"github.com/luthersystems/elps/lisp"
"github.com/luthersystems/libmxf"
)
func JSMxfDecode(this js.Value, vals []js.Value) interface{} {
if len(vals) != 2 {
libmxf.Log(fmt.Sprintf("invalid args: [%d] != 2", len(vals)))
return nil
}
encMsg := vals[0].String()
callback := vals[1]
lval := libmxf.MxfDecode(func(dsid string) string {
return callback.Call("getKey", js.ValueOf(dsid)).String()
}, encMsg)
err := lisp.GoError(lval)
if err != nil {
callback.Call("reject", js.ValueOf(err.Error()))
} else {
callback.Call("resolve", js.ValueOf(string(lval.Bytes())))
}
return nil
}
func main() {
// hook up "exported" functions
done := make(chan struct{})
var exportCallbacks []js.Func
export := func(name string, fn func(js.Value, []js.Value) interface{}) js.Func {
cb := js.FuncOf(fn)
js.Global().Set(name, cb)
exportCallbacks = append(exportCallbacks, cb)
return cb
}
export("MxfDecode", JSMxfDecode)
export("GoKill", func(js.Value, []js.Value) interface{} {
close(done)
return nil
})
// initialization complete
libmxf.Log("loaded wasm")
<-done
for i := range exportCallbacks {
exportCallbacks[i].Release()
}
exportCallbacks = nil
}