-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetter.go
36 lines (32 loc) · 1.15 KB
/
getter.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
// Cr. https://github.com/nlepage/golang-wasm/tree/master/js/bind
package citizenfx
import (
"reflect"
"syscall/js"
)
func isGetter(t string, ft reflect.Type) bool {
return isProperty(t) && ft.NumIn() == 0 && ft.NumOut() == 1
}
func bindGetter(name string, t reflect.Type, parent func() js.Value) reflect.Value {
switch t.Kind() {
case reflect.Float64:
return reflect.ValueOf(func() float64 { return parent().Get(name).Float() })
//TODO manage Float32
case reflect.Int:
return reflect.ValueOf(func() int { return parent().Get(name).Int() })
//TODO manage other sizes of ints
case reflect.Bool:
return reflect.ValueOf(func() bool { return parent().Get(name).Bool() })
case reflect.String:
return reflect.ValueOf(func() string { return parent().Get(name).String() })
case reflect.Struct:
ft := reflect.FuncOf([]reflect.Type{}, []reflect.Type{t}, false)
return reflect.MakeFunc(ft, func(args []reflect.Value) []reflect.Value {
v := reflect.New(t)
//TODO store parent reference ?
Bind(v.Interface(), func() js.Value { return parent().Get(name) })
return []reflect.Value{reflect.ValueOf(v.Elem().Interface())}
})
}
panic("FIXME") //FIXME
}