-
Notifications
You must be signed in to change notification settings - Fork 1
/
method.go
175 lines (153 loc) · 6.11 KB
/
method.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
package atom
import (
tps "github.com/1set/starlet/dataconv/types"
"go.starlark.net/starlark"
)
// for integer
var (
intMethods = map[string]*starlark.Builtin{
"get": starlark.NewBuiltin("get", intGet),
"set": starlark.NewBuiltin("set", intSet),
"cas": starlark.NewBuiltin("cas", intCAS),
"add": starlark.NewBuiltin("add", intAdd),
"sub": starlark.NewBuiltin("sub", intSub),
"inc": starlark.NewBuiltin("inc", intInc),
"dec": starlark.NewBuiltin("dec", intDec),
}
)
func intGet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.MakeInt64(recv.val.Load()), nil
}
func intSet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var value int64
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "value", &value); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
recv.val.Store(value)
return starlark.None, nil
}
func intCAS(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var oldVal, newVal int64
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "old", &oldVal, "new", &newVal); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.Bool(recv.val.CAS(oldVal, newVal)), nil
}
func intAdd(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var delta int64
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "delta", &delta); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.MakeInt64(recv.val.Add(delta)), nil
}
func intSub(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var delta int64
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "delta", &delta); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.MakeInt64(recv.val.Sub(delta)), nil
}
func intInc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.MakeInt64(recv.val.Inc()), nil
}
func intDec(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicInt)
return starlark.MakeInt64(recv.val.Dec()), nil
}
// for float
var (
floatMethods = map[string]*starlark.Builtin{
"get": starlark.NewBuiltin("get", floatGet),
"set": starlark.NewBuiltin("set", floatSet),
"cas": starlark.NewBuiltin("cas", floatCAS),
"add": starlark.NewBuiltin("add", floatAdd),
"sub": starlark.NewBuiltin("sub", floatSub),
}
)
func floatGet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicFloat)
return starlark.Float(recv.val.Load()), nil
}
func floatSet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var value tps.FloatOrInt
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "value", &value); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicFloat)
recv.val.Store(value.GoFloat())
return starlark.None, nil
}
func floatCAS(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var oldVal, newVal tps.FloatOrInt
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "old", &oldVal, "new", &newVal); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicFloat)
return starlark.Bool(recv.val.CAS(oldVal.GoFloat(), newVal.GoFloat())), nil
}
func floatAdd(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var delta tps.FloatOrInt
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "delta", &delta); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicFloat)
return starlark.Float(recv.val.Add(delta.GoFloat())), nil
}
func floatSub(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var delta tps.FloatOrInt
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "delta", &delta); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicFloat)
return starlark.Float(recv.val.Sub(delta.GoFloat())), nil
}
// for string
var (
stringMethods = map[string]*starlark.Builtin{
"get": starlark.NewBuiltin("get", stringGet),
"set": starlark.NewBuiltin("set", stringSet),
"cas": starlark.NewBuiltin("cas", stringCAS),
}
)
func stringGet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicString)
return starlark.String(recv.val.Load()), nil
}
func stringSet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var value string
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "value", &value); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicString)
recv.val.Store(value)
return starlark.None, nil
}
func stringCAS(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var oldVal, newVal string
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "old", &oldVal, "new", &newVal); err != nil {
return nil, err
}
recv := b.Receiver().(*AtomicString)
return starlark.Bool(recv.val.CompareAndSwap(oldVal, newVal)), nil
}