Repository navigation
Expand file tree
/
Copy pathipc.go
More file actions
519 lines (480 loc) · 14.1 KB
/
Copy pathipc.go
File metadata and controls
519 lines (480 loc) · 14.1 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//go:build !js
package mygo
import (
"context"
jsonv1 "encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"log"
"reflect"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"unsafe"
"github.com/egoist/mygo/internal/tsgen"
)
// jsonOptions are used for every value crossing the IPC boundary.
var jsonOptions = json.JoinOptions(
jsonv1.FormatDurationAsNano(true),
jsontext.EscapeForJS(true),
)
type service struct {
name string
typ reflect.Type
methods []*method
internal bool
}
type method struct {
service string
name string
fn reflect.Value
ctx bool
params []reflect.Type
variadic bool
// chans marks the parameters that are channels (*Channel[T]), if any
// is (streams).
chans []bool
streams bool
result reflect.Type
hasErr bool
pc uintptr
}
type eventInfo struct {
name string
typ reflect.Type
pc uintptr
file string
line int
}
var ipc struct {
sync.RWMutex
services []*service
methods map[string]*method
events []*eventInfo
}
var (
contextType = reflect.TypeFor[context.Context]()
errorType = reflect.TypeFor[error]()
)
// Bind exposes the exported methods of each service to the frontend. The
// generated TypeScript client (see `mygo generate`) turns
//
// type Greeter struct{}
//
// // Greet returns a greeting.
// func (Greeter) Greet(name string) (string, error)
//
// into a typed `Greeter.greet(name: string): Promise<string>` function.
//
// Arguments and results are encoded as JSON. A method may take a
// context.Context as its first parameter: it is canceled when the calling
// page navigates away or its window closes, and CallerWindow returns the
// calling window from it. A method may return nothing, a value, an error, or
// a value and an error; a non-nil error rejects the promise on the
// JavaScript side. Calls run on their own goroutine.
//
// The service is named after its type; Bind panics if the name is taken or a
// method uses a type that cannot be encoded as JSON (channels, functions).
func Bind(services ...any) {
for _, s := range services {
if err := bind("", s, false); err != nil {
panic(err)
}
}
}
// BindAs is Bind with an explicit service name, for services whose type
// names collide.
func BindAs(name string, service any) {
if err := bind(name, service, false); err != nil {
panic(err)
}
}
func bind(name string, svc any, internal bool) error {
v := reflect.ValueOf(svc)
if !v.IsValid() {
return errors.New("mygo: cannot bind nil")
}
t := v.Type()
base := t
if base.Kind() == reflect.Pointer {
base = base.Elem()
}
if name == "" {
name = base.Name()
}
if name == "" {
return fmt.Errorf("mygo: cannot bind %s: type has no name, use BindAs", t)
}
if !internal && !tsgen.IsIdentifier(name) {
return fmt.Errorf("mygo: service name %q is not a valid identifier", name)
}
s := &service{name: name, typ: t, internal: internal}
for i := range t.NumMethod() {
m, err := newMethod(name, t.Method(i), v)
if err != nil {
return err
}
s.methods = append(s.methods, m)
}
if len(s.methods) == 0 {
return fmt.Errorf("mygo: %s has no exported methods to bind", t)
}
ipc.Lock()
defer ipc.Unlock()
for _, x := range ipc.services {
if x.name == name {
return fmt.Errorf("mygo: a service named %s is already bound", name)
}
}
if ipc.methods == nil {
ipc.methods = map[string]*method{}
}
ipc.services = append(ipc.services, s)
for _, m := range s.methods {
ipc.methods[name+"."+m.name] = m
}
return nil
}
func newMethod(service string, m reflect.Method, recv reflect.Value) (*method, error) {
ft := m.Type
mi := &method{service: service, name: m.Name, fn: recv.Method(m.Index), variadic: ft.IsVariadic()}
where := service + "." + m.Name
start := 1 // skip the receiver
if ft.NumIn() > 1 && ft.In(1) == contextType {
mi.ctx = true
start = 2
}
for i := start; i < ft.NumIn(); i++ {
p := ft.In(i)
isChan := p.Implements(channelParamType)
t := p
if isChan {
t = reflect.Zero(p).Interface().(channelParam).valueType()
}
if err := validateType(t); err != nil {
return nil, fmt.Errorf("mygo: %s: parameter %d: %w", where, i-start+1, err)
}
mi.params = append(mi.params, p)
mi.chans = append(mi.chans, isChan)
mi.streams = mi.streams || isChan
}
switch ft.NumOut() {
case 0:
case 1:
if ft.Out(0) == errorType {
mi.hasErr = true
} else {
mi.result = ft.Out(0)
}
case 2:
if ft.Out(1) != errorType {
return nil, fmt.Errorf("mygo: %s: the second result must be an error", where)
}
mi.result, mi.hasErr = ft.Out(0), true
default:
return nil, fmt.Errorf("mygo: %s: methods may return at most a value and an error", where)
}
if mi.result != nil {
if err := validateType(mi.result); err != nil {
return nil, fmt.Errorf("mygo: %s: result: %w", where, err)
}
}
mi.pc = methodPC(recv.Type(), m.Name)
return mi, nil
}
// validateType reports an error if values of type t cannot cross the IPC
// boundary.
func validateType(t reflect.Type) error {
if hasChannel(t, map[reflect.Type]bool{}) {
return fmt.Errorf("type %s holds a Channel, which can only be a parameter", t)
}
return tsgen.Validate(t)
}
// methodPC returns the entry of the method's own code rather than the
// wrapper Go generates for value receivers called through a pointer.
func methodPC(t reflect.Type, name string) uintptr {
if t.Kind() == reflect.Pointer {
if m, ok := t.Elem().MethodByName(name); ok {
return m.Func.Pointer()
}
}
if m, ok := t.MethodByName(name); ok {
return m.Func.Pointer()
}
return 0
}
func lookupMethod(name string) *method {
ipc.RLock()
defer ipc.RUnlock()
return ipc.methods[name]
}
// call calls the method for the page of w whose context is page and whose
// token is token.
func (m *method) call(w *Window, page context.Context, token string, args []rawValue) (result any, err error) {
ctx, cancel := page, context.CancelFunc(nil)
var chans []*channel
if m.streams {
// The page closing a channel cancels the call. The channels close
// when it returns, before its result is sent.
ctx, cancel = context.WithCancel(page)
defer func() {
for _, c := range chans {
c.close(closedByGo)
}
cancel()
}()
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
log.Printf("mygo: panic in %s.%s: %v\n%s", m.service, m.name, r, debug.Stack())
}
}()
in := make([]reflect.Value, 0, len(m.params)+1)
if m.ctx {
in = append(in, reflect.ValueOf(ctx))
}
last := len(m.params) - 1
for i, t := range m.params {
if m.chans[i] {
// The page passes the id of its channel.
var id int64
if i < len(args) {
_ = json.Unmarshal(args[i], &id)
}
if id <= 0 {
return nil, fmt.Errorf("invalid argument %d: not a Channel", i+1)
}
c := w.newChannel(page, ctx, cancel, id, token)
chans = append(chans, c)
v := reflect.New(t.Elem())
v.Interface().(channelParam).init(c)
in = append(in, v)
continue
}
if m.variadic && i == last {
for j := i; j < len(args); j++ {
v, err := decodeArg(args[j], t.Elem(), j)
if err != nil {
return nil, err
}
in = append(in, v)
}
break
}
if i >= len(args) {
in = append(in, reflect.Zero(t))
continue
}
v, err := decodeArg(args[i], t, i)
if err != nil {
return nil, err
}
in = append(in, v)
}
out := m.fn.Call(in)
if m.hasErr {
if e := out[len(out)-1]; !e.IsNil() {
return nil, e.Interface().(error)
}
}
if m.result != nil {
return out[0].Interface(), nil
}
return nil, nil
}
func decodeArg(raw rawValue, t reflect.Type, i int) (reflect.Value, error) {
v := reflect.New(t)
if err := json.Unmarshal(raw, v.Interface(), jsonOptions); err != nil {
return reflect.Value{}, fmt.Errorf("invalid argument %d: %w", i+1, err)
}
return v.Elem(), nil
}
// rawValue is a JSON value that shares the bytes it was decoded from,
// which must outlive it unchanged: arguments, which can be large, go from
// the message to their parameters without being copied.
type rawValue []byte
// UnmarshalJSONFrom keeps the value in place. json.Unmarshal decodes from
// the slice it is given, which it never modifies.
func (v *rawValue) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
raw, err := dec.ReadValue()
*v = rawValue(raw)
return err
}
// stringBytes returns the bytes of s without copying them. They must not
// be modified.
func stringBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) }
// handleCall decodes and runs a call from the page on the current
// goroutine, then queues the reply. Calls from untrusted pages are
// rejected.
func handleCall(w *Window, ctx context.Context, raw string, trusted bool) {
var m struct {
ID int64 `json:"id"`
K string `json:"k"`
M string `json:"m"`
A []rawValue `json:"a"` // shares raw
}
if err := json.Unmarshal(stringBytes(raw), &m); err != nil {
log.Printf("mygo: malformed call from window %d: %v", w.id, err)
return
}
var result any
var err error
if !trusted {
err = fmt.Errorf("this page is not allowed to call Go methods (see PageOptions.TrustedOrigins)")
} else if mi := lookupMethod(m.M); mi == nil {
err = notBound(m.M)
} else {
result, err = mi.call(w, ctx, m.K, m.A)
}
w.enqueue(encodeReply(m.ID, m.K, result, err), false)
}
var closeBrace = []byte("}")
func encodeReply(id int64, token string, result any, err error) message {
b := make([]byte, 0, 96)
b = append(b, `{"t":"reply","id":`...)
b = strconv.AppendInt(b, id, 10)
b = append(b, `,"k":`...)
b, _ = jsontext.AppendQuote(b, token)
if err == nil {
v, merr := json.Marshal(result, jsonOptions)
if merr == nil {
return message{append(b, `,"ok":true,"v":`...), v, closeBrace}
}
err = fmt.Errorf("cannot encode result: %w", merr)
}
b = append(b, `,"ok":false,"e":`...)
b, _ = jsontext.AppendQuote(b, err.Error())
return message{head: append(b, '}')}
}
type callerKey struct{}
// CallerWindow returns the window whose page called a bound method, given
// the context.Context passed to that method: for the page of a web view
// (WebView), the window it is in. It returns nil for other contexts.
func CallerWindow(ctx context.Context) *Window {
w, _ := ctx.Value(callerKey{}).(*Window)
return w
}
// pageKey holds the calling page in the context of a call.
type pageKey struct{}
// CallerPage returns the page that called a bound method, given the
// context.Context passed to that method: the page of the calling window,
// or of a web view in it (WebView), whose window CallerWindow returns. It
// returns nil for other contexts.
func CallerPage(ctx context.Context) *Page {
p, _ := ctx.Value(pageKey{}).(*Page)
return p
}
// Event is a typed event the Go side sends to pages. Declare events at
// package level so `mygo generate` includes them in the TypeScript client:
//
// var Progress = mygo.NewEvent[ProgressInfo]("progress")
//
// Progress.Emit(win, ProgressInfo{Done: 1, Total: 3})
//
// and subscribe in the frontend with `events.progress.on(p => ...)`.
type Event[T any] struct {
name string
}
// NewEvent declares an event with payload type T. Names must be unique;
// those starting with "mygo:" are reserved.
func NewEvent[T any](name string) *Event[T] {
if strings.HasPrefix(name, "mygo:") {
panic(fmt.Sprintf("mygo: event %q: names starting with \"mygo:\" are reserved", name))
}
t := reflect.TypeFor[T]()
if err := validateType(t); err != nil {
panic(fmt.Sprintf("mygo: event %q: %v", name, err))
}
pc, file, line, _ := runtime.Caller(1)
ipc.Lock()
defer ipc.Unlock()
for _, e := range ipc.events {
if e.name == name {
panic(fmt.Sprintf("mygo: event %q is already declared", name))
}
}
ipc.events = append(ipc.events, &eventInfo{name: name, typ: t, pc: pc, file: file, line: line})
return &Event[T]{name: name}
}
// Name returns the event name.
func (e *Event[T]) Name() string { return e.name }
// Emit sends the event to the page shown in w, or to the pages of the web
// views of a window showing native UI (WebView). Events sent before the
// page's DOM is ready are delivered once it is.
func (e *Event[T]) Emit(w *Window, payload T) error {
msg, err := encodeEvent(e.name, payload)
if err != nil {
return err
}
if w.IsDestroyed() {
return errDestroyed
}
if w.content != nil {
windows.Lock()
views := append([]*Window(nil), w.webViews...)
windows.Unlock()
for _, v := range views {
v.enqueue(msg, true)
}
return nil
}
w.enqueue(msg, true)
return nil
}
// EmitPage sends the event to a page: of a window, or of a web view
// (WebView.Page).
func (e *Event[T]) EmitPage(p *Page, payload T) error {
msg, err := encodeEvent(e.name, payload)
if err != nil {
return err
}
if p == nil || p.w.IsDestroyed() {
return errDestroyed
}
p.w.enqueue(msg, true)
return nil
}
// Broadcast sends the event to every window, and the web views in them.
func (e *Event[T]) Broadcast(payload T) error {
msg, err := encodeEvent(e.name, payload)
if err != nil {
return err
}
for _, w := range pageWindows() {
w.enqueue(msg, true)
}
return nil
}
func encodeEvent(name string, payload any) (message, error) {
p, err := json.Marshal(payload, jsonOptions)
if err != nil {
return message{}, fmt.Errorf("mygo: cannot encode event %q: %w", name, err)
}
b := make([]byte, 0, len(name)+32)
b = append(b, `{"t":"event","n":`...)
b, _ = jsontext.AppendQuote(b, name)
b = append(b, `,"p":`...)
return message{b, p, closeBrace}, nil
}
// windowControls backs window.mygo.window in the page.
type windowControls struct{}
func (windowControls) Minimize(ctx context.Context) { CallerWindow(ctx).Minimize() }
func (windowControls) Maximize(ctx context.Context) { CallerWindow(ctx).Maximize() }
func (windowControls) Unmaximize(ctx context.Context) { CallerWindow(ctx).Unmaximize() }
func (windowControls) ToggleMaximize(ctx context.Context) { CallerWindow(ctx).ToggleMaximize() }
func (windowControls) IsMaximized(ctx context.Context) bool { return CallerWindow(ctx).IsMaximized() }
func (windowControls) ToggleFullScreen(ctx context.Context) { CallerWindow(ctx).ToggleFullScreen() }
func (windowControls) Close(ctx context.Context) { CallerWindow(ctx).Close() }
func (windowControls) SetTitle(ctx context.Context, title string) {
CallerWindow(ctx).SetTitle(title)
}
func init() {
if err := bind("mygo:window", windowControls{}, true); err != nil {
panic(err)
}
}