forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
89 lines (74 loc) · 2.02 KB
/
handler.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
package otto
import (
"encoding/json"
"reflect"
)
type GoObjectHandler interface {
GetValue(name string) interface{}
GetProperty(name string) (interface{}, bool)
Enumerate(all bool, each func(string) bool)
}
type GoObjectSetter interface {
SetValue(name string, value interface{}) error
CanSetValue(name string) bool
}
type GoObjectExtender interface {
}
type goHandlerObject struct {
value GoObjectHandler
}
type GoObjectComparable interface {
CompareWith(b interface{}) lessThanResult
}
func goHandlerIsCompareAble(self *object) bool {
object := self.value.(goHandlerObject)
if _, ok := object.value.(GoObjectComparable); ok {
return true
}
return false
}
func goHandlerCompareWith(self *object, y Value) lessThanResult {
object := self.value.(goHandlerObject)
if c, ok := object.value.(GoObjectComparable); ok {
if y.object() != nil {
return c.CompareWith(y.object().value)
}
return c.CompareWith(y.value)
}
return lessThanUndefined
}
func goHandlerGetOwnProperty(self *object, name string) *property {
object := self.value.(goHandlerObject)
value := object.value.GetValue(name)
var rv = reflect.ValueOf(value)
if rv.IsValid() {
return &property{self.runtime.toValue(value), 0110}
}
return objectGetOwnProperty(self, name)
}
func goHandlerCanPut(self *object, name string) bool {
object := self.value.(goHandlerObject)
if setter, ok := object.value.(GoObjectSetter); ok {
return setter.CanSetValue(name)
}
return false
}
func goHandlerPut(self *object, name string, value Value, throw bool) {
object := self.value.(goHandlerObject)
if setter, ok := object.value.(GoObjectSetter); ok {
setter.SetValue(name, value.value)
}
}
func goHandlerEnumerate(self *object, all bool, each func(string) bool) {
object := self.value.(goHandlerObject)
object.value.Enumerate(all, each)
objectEnumerate(self, all, each)
}
func goHandlerMarshalJSON(self *object) json.Marshaler {
object := self.value.(*goHandlerObject)
switch marshaler := object.value.(type) {
case json.Marshaler:
return marshaler
}
return nil
}