-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcollection.go
265 lines (234 loc) · 4.95 KB
/
collection.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
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
package quickjs
import (
"errors"
)
//
// Array
// @Description: simply implement the array structure of js
type Array struct {
arrayValue Value
ctx *Context
}
func NewQjsArray(value Value, ctx *Context) *Array {
return &Array{
arrayValue: value,
ctx: ctx,
}
}
// Push
//
// @Description: add one or more elements after the array,returns the new array length
// @receiver a :
// @param elements :
// @return int64
func (a Array) Push(elements ...Value) int64 {
ret := a.arrayValue.Call("push", elements...)
//defer ret.Free()
return ret.Int64()
}
// Get
//
// @Description: get the specific value by subscript
// @receiver a :
// @param index :
// @return Value
func (a Array) Get(index int64) (Value, error) {
if index < 0 {
return Value{}, errors.New("the input index value is a negative number")
}
if index >= a.arrayValue.Len() {
return Value{}, errors.New("index subscript out of range")
}
return a.arrayValue.GetIdx(index), nil
}
// Set
//
// @Description:
// @receiver a :
// @param index :
// @param value :
// @return error
func (a Array) Set(index int64, value Value) error {
if index < 0 {
return errors.New("the input index value is a negative number")
}
if index >= a.arrayValue.Len() {
return errors.New("index subscript out of range")
}
a.arrayValue.SetIdx(index, value)
return nil
}
func (a Array) Delete(index int64) (bool, error) {
if index < 0 {
return false, errors.New("the input index value is a negative number")
}
if index >= a.arrayValue.Len() {
return false, errors.New("index subscript out of range")
}
removeList := a.arrayValue.Call("splice", a.ctx.Int64(index), a.ctx.Int64(1))
defer removeList.Free()
return removeList.IsArray(), nil
}
// Len
//
// @Description: get the length of the array
// @receiver a :
// @return int64
func (a Array) Len() int64 {
return a.arrayValue.Len()
}
// HasIdx
//
// @Description: Determine whether there is data at the current subscript position
// @receiver a :
// @param i :
// @return bool
func (a Array) HasIdx(i int64) bool {
return a.arrayValue.HasIdx(i)
}
// ToValue
//
// @Description: get the value object of qjs
// @receiver a :
// @return Value
func (a Array) ToValue() Value {
return a.arrayValue
}
func (a Array) Free() {
a.arrayValue.Free()
}
//
// Map
// @Description: simply implement the map structure of js
type Map struct {
mapValue Value
ctx *Context
}
func NewQjsMap(value Value, ctx *Context) *Map {
return &Map{
mapValue: value,
ctx: ctx,
}
}
// Get
//
// @Description: get the value by key
// @receiver m :
// @param key :
// @return Value
func (m Map) Get(key Value) Value {
return m.mapValue.Call("get", key)
}
// Put
//
// @Description:
// @receiver m :
// @param key :
// @param value :
func (m Map) Put(key Value, value Value) {
m.mapValue.Call("set", key, value).Free()
}
// Delete
//
// @Description:delete the value of an element by key
// @receiver m :
// @param key :
func (m Map) Delete(key Value) {
m.mapValue.Call("delete", key).Free()
}
// Has
//
// @Description:determine whether an element exists
// @receiver m :
// @param key :
func (m Map) Has(key Value) bool {
boolValue := m.mapValue.Call("has", key)
defer boolValue.Free()
return boolValue.Bool()
}
// ForEach
//
// @Description: iterate map
// @receiver m :
func (m Map) ForEach(forFn func(key Value, value Value)) {
forEachFn := m.ctx.Function(func(ctx *Context, this Value, args []Value) Value {
forFn(args[1], args[0])
return ctx.Null()
})
value := m.mapValue.Call("forEach", forEachFn)
forEachFn.Free()
defer value.Free()
}
func (m Map) Free() {
m.mapValue.Free()
}
func (m Map) ToValue() Value {
return m.mapValue
}
// Call
//
// @Description: call some internal methods of js
// @receiver a :
// @param funcName :
// @param values :
// @return Value
func (m Map) Call(funcName string, values []Value) Value {
return m.mapValue.Call(funcName, values...)
}
type Set struct {
setValue Value
ctx *Context
}
func NewQjsSet(value Value, ctx *Context) *Set {
return &Set{
setValue: value,
ctx: ctx,
}
}
// Add
//
// @Description: add element
// @receiver s :
// @param value :
func (s Set) Add(value Value) {
v := s.setValue.Call("add", value)
defer v.Free()
}
// Delete
//
// @Description: add element
// @receiver s :
// @param value :
func (s Set) Delete(value Value) {
v := s.setValue.Call("delete", value)
defer v.Free()
}
// Has
//
// @Description: determine whether an element exists in the set
// @receiver s :
// @param value :
// @return bool
func (s Set) Has(value Value) bool {
v := s.setValue.Call("has", value)
return v.Bool()
}
// ForEach
//
// @Description: iterate set
// @receiver m :
func (s Set) ForEach(forFn func(value Value)) {
forEachFn := s.ctx.Function(func(ctx *Context, this Value, args []Value) Value {
forFn(args[0])
return ctx.Null()
})
value := s.setValue.Call("forEach", forEachFn)
forEachFn.Free()
defer value.Free()
}
func (s Set) Free() {
s.setValue.Free()
}
func (s Set) ToValue() Value {
return s.setValue
}