/
jsutil.go
executable file
·174 lines (158 loc) · 4.92 KB
/
jsutil.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// +build js,wasm
// Package jsutil contains various utility functions for working with
// JavaScript and WebAssembly
package jsutil
import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime/debug"
"syscall/js"
)
func CopyBytesToJS(bytes []byte) (jsBytes js.Value, err error) {
defer func() {
if r := recover(); r != nil {
err = RecoverError(r)
}
}()
jsBytes = js.Global().Get("Uint8Array").New(len(bytes))
copied := js.CopyBytesToJS(jsBytes, bytes)
if copied != len(bytes) {
return js.Undefined(), fmt.Errorf("should have copied %d bytes to JS but only copied %d", len(bytes), copied)
}
return jsBytes, nil
}
func CopyBytesToGo(jsBytes js.Value) (bytes []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = RecoverError(r)
}
}()
jsBytesLength := jsBytes.Get("length").Int()
bytes = make([]byte, jsBytesLength)
copied := js.CopyBytesToGo(bytes, jsBytes)
if copied != jsBytesLength {
return nil, fmt.Errorf("should have copied %d bytes to Go but only copied %d", jsBytesLength, copied)
}
return bytes, nil
}
// ErrorToJS converts a Go error to a JavaScript Error.
func ErrorToJS(err error) js.Value {
return js.Global().Get("Error").New(err.Error())
}
// IsNullOrUndefined returns true if the given JavaScript value is either null
// or undefined.
func IsNullOrUndefined(value js.Value) bool {
return value.IsNull() || value.IsUndefined()
}
// WrapInPromise converts a potentially blocking Go function to a non-blocking
// JavaScript Promise. If the function returns an error, the promise will reject
// with that error. Otherwise, the promise will resolve with the first return
// value.
func WrapInPromise(f func() (interface{}, error)) js.Value {
var executor js.Func
executor = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
reject := args[1]
go func() {
defer executor.Release()
if result, err := f(); err != nil {
reject.Invoke(ErrorToJS(err))
} else {
resolve.Invoke(result)
}
}()
return nil
})
return js.Global().Get("Promise").New(executor)
}
// AwaitPromiseContext is like AwaitPromise but accepts a context. If the context
// is canceled or times out before the promise resolves, it will return
// (js.Undefined, ctx.Error).
func AwaitPromiseContext(ctx context.Context, promise js.Value) (result js.Value, err error) {
resultsChan := make(chan js.Value)
errChan := make(chan js.Error)
thenFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go func() {
resultsChan <- args[0]
}()
return js.Undefined()
})
defer thenFunc.Release()
catchFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go func() {
errChan <- js.Error{Value: args[0]}
}()
return js.Undefined()
})
defer catchFunc.Release()
promise.Call("then", thenFunc).Call("catch", catchFunc)
select {
case <-ctx.Done():
return js.Undefined(), ctx.Err()
case result := <-resultsChan:
return result, nil
case err := <-errChan:
return js.Undefined(), err
}
}
// AwaitPromise accepts a js.Value representing a Promise. If the promise
// resolves, it returns (result, nil). If the promise rejects, it returns
// (js.Undefined, error). AwaitPromise has a synchronous-like API but does not
// block the JavaScript event loop.
func AwaitPromise(promise js.Value) (result js.Value, err error) {
return AwaitPromiseContext(context.Background(), promise)
}
// InefficientlyConvertToJS converts the given Go value to a JS value by
// encoding to JSON and then decoding it. This function is not very efficient
// and its use should be phased out over time as much as possible.
func InefficientlyConvertToJS(value interface{}) (result js.Value, err error) {
defer func() {
if e := recover(); e != nil {
switch e := e.(type) {
case error:
err = e
default:
err = fmt.Errorf("unexpected error: (%T) %s", e, e)
}
}
}()
jsonBytes, err := json.Marshal(value)
if err != nil {
return js.Undefined(), err
}
return js.Global().Get("JSON").Call("parse", string(jsonBytes)), nil
}
// InefficientlyConvertFromJS converts the given JS value to a Go value and sets
// it. This function is not very efficient and its use should be phased out over
// time as much as possible.
func InefficientlyConvertFromJS(jsValue js.Value, value interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
switch e := e.(type) {
case error:
err = e
default:
err = fmt.Errorf("unexpected error: (%T) %s", e, e)
}
}
}()
jsonString := js.Global().Get("JSON").Call("stringify", jsValue)
return json.Unmarshal([]byte(jsonString.String()), value)
}
// RecoverError allows a function to recover from a thrown Javascript error if
// called inside of a deferred function with a recover statement.
func RecoverError(e interface{}) error {
if e != nil {
debug.PrintStack()
}
switch e := e.(type) {
case error:
return e
case string:
return errors.New(e)
default:
return fmt.Errorf("unexpected JavaScript error: (%T) %v", e, e)
}
}