-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
js_wasm.go
199 lines (157 loc) · 3.19 KB
/
js_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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package app
import (
"net/url"
"syscall/js"
"github.com/maxence-charriere/go-app/v6/pkg/log"
)
type value struct {
js.Value
}
func (v value) Call(m string, args ...interface{}) Value {
for i, a := range args {
switch a := a.(type) {
case Wrapper:
args[i] = jsval(a.JSValue())
}
}
return val(v.Value.Call(m, args...))
}
func (v value) Get(p string) Value {
return val(v.Value.Get(p))
}
func (v value) Set(p string, x interface{}) {
if wrapper, ok := x.(Wrapper); ok {
x = jsval(wrapper.JSValue())
}
v.Value.Set(p, x)
}
func (v value) Index(i int) Value {
return val(v.Value.Index(i))
}
func (v value) InstanceOf(t Value) bool {
return v.Value.InstanceOf(jsval(t))
}
func (v value) Invoke(args ...interface{}) Value {
return val(v.Value.Invoke(args...))
}
func (v value) JSValue() Value {
return v
}
func (v value) New(args ...interface{}) Value {
return val(v.Value.New(args...))
}
func (v value) Type() Type {
return Type(v.Value.Type())
}
func null() Value {
return val(js.Null())
}
func undefined() Value {
return val(js.Undefined())
}
func valueOf(x interface{}) Value {
switch t := x.(type) {
case value:
x = t.Value
case function:
x = t.fn
case *browserWindow:
x = t.Value
case Event:
return valueOf(t.Value)
}
return val(js.ValueOf(x))
}
type function struct {
value
fn js.Func
}
func (f function) Release() {
f.fn.Release()
}
func funcOf(fn func(this Value, args []Value) interface{}) Func {
f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
wargs := make([]Value, len(args))
for i, a := range args {
wargs[i] = val(a)
}
return fn(val(this), wargs)
})
return function{
value: value{Value: f.Value},
fn: f,
}
}
type browserWindow struct {
value
cursorX int
cursorY int
}
func (w *browserWindow) URL() *url.URL {
rawurl := w.
Get("location").
Get("href").
String()
u, _ := url.Parse(rawurl)
return u
}
func (w *browserWindow) Size() (width int, height int) {
getSize := func(axis string) int {
size := w.Get("inner" + axis)
if !size.Truthy() {
size = w.
Get("document").
Get("documentElement").
Get("client" + axis)
}
if !size.Truthy() {
size = w.
Get("document").
Get("body").
Get("client" + axis)
}
if size.Type() != TypeNumber {
return 0
}
return size.Int()
}
return getSize("Width"), getSize("Height")
}
func (w *browserWindow) CursorPosition() (x, y int) {
return w.cursorX, w.cursorY
}
func (w *browserWindow) setCursorPosition(x, y int) {
w.cursorX = x
w.cursorY = y
}
func val(v js.Value) Value {
return value{Value: v}
}
func jsval(v Value) js.Value {
switch v := v.(type) {
case value:
return v.Value
case function:
return v.Value
case *browserWindow:
return v.Value
case Event:
return jsval(v.Value)
default:
log.Error("converting to js value failed").
T("value-type", v).
Panic()
return js.Undefined()
}
}
// JSValue returns the underlying syscall/js value of the given Javascript
// value.
func JSValue(v Value) js.Value {
return jsval(v)
}
func copyBytesToGo(dst []byte, src Value) int {
return js.CopyBytesToGo(dst, jsval(src))
}
func copyBytesToJS(dst Value, src []byte) int {
return js.CopyBytesToJS(jsval(dst), src)
}