-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
87 lines (71 loc) · 1.55 KB
/
value.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
package runtime
import (
"fmt"
"reflect"
)
type CompareResult int
const (
CompareResultNotEqual CompareResult = 0
CompareResultEqual CompareResult = 1
CompareResultLess CompareResult = 2
CompareResultGreater CompareResult = 4
)
type Value interface {
GetOwner() Value
SetOwner(Value)
Type() ValueType
CompareTo(Value, *Context) CompareResult
ToString(*Context) string
ToGoValue(*Context) interface{}
GoType() reflect.Type
IsTrue() bool
GetIndex(int, *Context) Value
GetMember(string, *Context) Value
}
type ValueBase struct {
Owner Value
}
func (v *ValueBase) GetOwner() Value {
if v.Owner == nil {
return constUndefined
}
return v.Owner
}
func (v *ValueBase) SetOwner(owner Value) {
v.Owner = owner
}
func makeMember(owner, member Value, c *Context) Value {
if callable, ok := member.(ValueCallable); ok {
if _, isObj := member.(ValueObject); !isObj {
return NewBoundMethod(owner, callable)
}
}
return member
}
var MakeMember = makeMember
func getExtMember(owner Value, name string, c *Context) Value {
tid := owner.Type().TypeId
extName := fmt.Sprintf("%d#%s", tid, name)
if ext, found := c.FindValue(extName); found {
return makeMember(owner, ext, c)
}
return constUndefined
}
type CanLen interface {
Len() int
}
type CanSetMember interface {
SetMember(string, Value, *Context)
}
type CanSetIndex interface {
SetIndex(int, Value, *Context)
}
type CanHash interface {
Hash() int64
}
type Container interface {
Contains(c *Context, v Value) bool
}
type CanSlice interface {
Slice(c *Context, begin, end int64) Value
}