This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
bytearray.go
195 lines (175 loc) · 5.46 KB
/
bytearray.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grumpy
import (
"bytes"
"fmt"
"reflect"
"sync"
)
var (
// ByteArrayType is the object representing the Python 'bytearray' type.
ByteArrayType = newBasisType("bytearray", reflect.TypeOf(ByteArray{}), toByteArrayUnsafe, ObjectType)
)
// ByteArray represents Python 'bytearray' objects.
type ByteArray struct {
Object
mutex sync.RWMutex
value []byte
}
func toByteArrayUnsafe(o *Object) *ByteArray {
return (*ByteArray)(o.toPointer())
}
// ToObject upcasts a to an Object.
func (a *ByteArray) ToObject() *Object {
return &a.Object
}
// Value returns the underlying bytes held by a.
func (a *ByteArray) Value() []byte {
return a.value
}
func byteArrayEq(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, False, True, False), nil
}
func byteArrayGE(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, False, True, True), nil
}
func byteArrayGetItem(f *Frame, o, key *Object) (result *Object, raised *BaseException) {
a := toByteArrayUnsafe(o)
if key.typ.slots.Index != nil {
index, raised := IndexInt(f, key)
if raised != nil {
return nil, raised
}
a.mutex.RLock()
elems := a.Value()
if index, raised = seqCheckedIndex(f, len(elems), index); raised == nil {
result = NewInt(int(elems[index])).ToObject()
}
a.mutex.RUnlock()
return result, raised
}
if key.isInstance(SliceType) {
a.mutex.RLock()
elems := a.Value()
start, stop, step, sliceLen, raised := toSliceUnsafe(key).calcSlice(f, len(elems))
if raised == nil {
value := make([]byte, sliceLen)
if step == 1 {
copy(value, elems[start:stop])
} else {
i := 0
for j := start; j != stop; j += step {
value[i] = elems[j]
i++
}
}
result = (&ByteArray{Object: Object{typ: ByteArrayType}, value: value}).ToObject()
}
a.mutex.RUnlock()
return result, raised
}
return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("bytearray indices must be integers or slice, not %s", key.typ.Name()))
}
func byteArrayGT(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, False, False, True), nil
}
func byteArrayInit(f *Frame, o *Object, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "__init__", args, IntType); raised != nil {
return nil, raised
}
a := toByteArrayUnsafe(o)
a.mutex.Lock()
a.value = make([]byte, toIntUnsafe(args[0]).Value())
a.mutex.Unlock()
return None, nil
}
func byteArrayLE(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, True, True, False), nil
}
func byteArrayLT(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, True, False, False), nil
}
func byteArrayNative(f *Frame, o *Object) (reflect.Value, *BaseException) {
a := toByteArrayUnsafe(o)
a.mutex.RLock()
result := reflect.ValueOf(a.Value())
a.mutex.RUnlock()
return result, nil
}
func byteArrayNE(f *Frame, v, w *Object) (*Object, *BaseException) {
return byteArrayCompare(v, w, True, False, True), nil
}
func byteArrayRepr(f *Frame, o *Object) (*Object, *BaseException) {
a := toByteArrayUnsafe(o)
a.mutex.RLock()
s, raised := Repr(f, NewStr(string(a.Value())).ToObject())
a.mutex.RUnlock()
if raised != nil {
return nil, raised
}
return NewStr(fmt.Sprintf("bytearray(b%s)", s.Value())).ToObject(), nil
}
func byteArrayStr(f *Frame, o *Object) (*Object, *BaseException) {
a := toByteArrayUnsafe(o)
a.mutex.RLock()
s := string(a.Value())
a.mutex.RUnlock()
return NewStr(s).ToObject(), nil
}
func initByteArrayType(dict map[string]*Object) {
ByteArrayType.slots.Eq = &binaryOpSlot{byteArrayEq}
ByteArrayType.slots.GE = &binaryOpSlot{byteArrayGE}
ByteArrayType.slots.GetItem = &binaryOpSlot{byteArrayGetItem}
ByteArrayType.slots.GT = &binaryOpSlot{byteArrayGT}
ByteArrayType.slots.Init = &initSlot{byteArrayInit}
ByteArrayType.slots.LE = &binaryOpSlot{byteArrayLE}
ByteArrayType.slots.LT = &binaryOpSlot{byteArrayLT}
ByteArrayType.slots.Native = &nativeSlot{byteArrayNative}
ByteArrayType.slots.NE = &binaryOpSlot{byteArrayNE}
ByteArrayType.slots.Repr = &unaryOpSlot{byteArrayRepr}
ByteArrayType.slots.Str = &unaryOpSlot{byteArrayStr}
}
func byteArrayCompare(v, w *Object, ltResult, eqResult, gtResult *Int) *Object {
if v == w {
return eqResult.ToObject()
}
// For simplicity we make a copy of w if it's a str or bytearray. This
// is inefficient and it may be useful to optimize.
var data []byte
switch {
case w.isInstance(StrType):
data = []byte(toStrUnsafe(w).Value())
case w.isInstance(ByteArrayType):
a := toByteArrayUnsafe(w)
a.mutex.RLock()
data = make([]byte, len(a.value))
copy(data, a.value)
a.mutex.RUnlock()
default:
return NotImplemented
}
a := toByteArrayUnsafe(v)
a.mutex.RLock()
cmp := bytes.Compare(a.value, data)
a.mutex.RUnlock()
switch cmp {
case -1:
return ltResult.ToObject()
case 0:
return eqResult.ToObject()
default:
return gtResult.ToObject()
}
}