-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreader.go
466 lines (402 loc) · 13.6 KB
/
reader.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package jspb
import "github.com/gopherjs/gopherjs/js"
// Reader encapsulates the jspb.BinaryReader.
type Reader interface {
Next() bool
Err() error
GetFieldNumber() int
SkipField()
// Scalars
ReadInt32() int32
ReadInt64() int64
ReadUint32() uint32
ReadUint64() uint64
ReadSint32() int32
ReadSint64() int64
ReadFixed32() uint32
ReadFixed64() uint64
ReadSfixed32() int32
ReadSfixed64() int64
ReadFloat32() float32
ReadFloat64() float64
ReadEnum() int
ReadBool() bool
ReadString() string
ReadBytes() []byte
// Scalar Slices
ReadInt32Slice() []int32
ReadInt64Slice() []int64
ReadUint32Slice() []uint32
ReadUint64Slice() []uint64
ReadSint32Slice() []int32
ReadSint64Slice() []int64
ReadFixed32Slice() []uint32
ReadFixed64Slice() []uint64
ReadSfixed32Slice() []int32
ReadSfixed64Slice() []int64
ReadFloat32Slice() []float32
ReadFloat64Slice() []float64
ReadEnumSlice() []int
ReadBoolSlice() []bool
// Specials
ReadMessage(func())
}
type decoder struct {
*js.Object
}
// ReadInt64 reads a signed 64 bit varint
func (d decoder) ReadInt64() int64 {
d.Call("readSplitVarint64_")
high := d.Get("tempHigh_").Uint64()
low := d.Get("tempLow_").Uint64()
return d.mergeSignedInteger(low, high)
}
// ReadZigzag64 reads a signed 64 bit Zigzag encoded varint
func (d decoder) ReadZigzag64() int64 {
v := d.ReadUint64()
// https://github.com/gogo/protobuf/blob/1ef32a8b9fc3f8ec940126907cedb5998f6318e4/proto/decode.go#L254
return int64((v >> 1) ^ uint64((int64(v&1)<<63)>>63))
}
// ReadUint64 reads a unsigned 64 bit varint
func (d decoder) ReadUint64() uint64 {
d.Call("readSplitVarint64_")
high := d.Get("tempHigh_").Uint64()
low := d.Get("tempLow_").Uint64()
return high<<32 | low
}
// ReadFixed64 reads a 64 bit unsigned integer
func (d decoder) ReadFixed64() uint64 {
low := d.Call("readUint32").Uint64()
high := d.Call("readUint32").Uint64()
return high<<32 | low
}
// ReadSignedFixed64 reads a 64 bit signed integer
func (d decoder) ReadSignedFixed64() int64 {
low := d.Call("readUint32").Uint64()
high := d.Call("readUint32").Uint64()
return d.mergeSignedInteger(low, high)
}
func (d decoder) mergeSignedInteger(low, high uint64) int64 {
// Adapted from google-protobuf
// https://github.com/google/protobuf/blob/25625b956a2f0d432582009c16553a9fd21c3cea/js/binary/utils.js#L521
negative := (high&0x8 == 1)
if negative {
low = (^low + 1)
var carry uint64
if low == 0 {
carry = 1
}
high = (^high + carry)
}
return int64(high<<32 | low)
}
// NewReader returns a new Reader ready for writing
func NewReader(data []byte) Reader {
r := &reader{
Object: js.Global.Get("BinaryReader").New(data),
}
r.decoder = &decoder{
Object: r.Get("decoder_"),
}
return r
}
// reader implements the Reader interface
type reader struct {
*js.Object
decoder *decoder
err error
}
// Reads the next field header in the stream if there is one, returns true if
// we saw a valid field header or false if we've read the whole stream.
// Sets err if we encountered a deprecated START_GROUP/END_GROUP fielr.decoder.
func (r *reader) Next() bool {
defer catchException(&r.err)
return r.err == nil && r.Call("nextField").Bool() && !r.Call("isEndGroup").Bool()
}
// Err returns the error state of the Reader. It must
// be called after Next() has returned false.
func (r reader) Err() error {
return r.err
}
// The field number of the next field in the buffer, or
// -1 if there is no next fielr.decoder.
func (r reader) GetFieldNumber() int {
return r.Call("getFieldNumber").Int()
}
// Skips over the next field in the binary stream.
func (r reader) SkipField() {
r.Call("skipField")
}
// ReadInt32 reads a signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadInt32() int32 {
defer catchException(&r.err)
return int32(r.Call("readInt32").Int())
}
// ReadInt64 reads a signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadInt64() int64 {
defer catchException(&r.err)
return r.decoder.ReadInt64()
}
// ReadUint32 reads an unsigned 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadUint32() uint32 {
defer catchException(&r.err)
return uint32(r.Call("readUint32").Int())
}
// ReadUint64 reads an unsigned 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadUint64() uint64 {
defer catchException(&r.err)
return r.decoder.ReadUint64()
}
// ReadSint32 reads a signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSint32() int32 {
defer catchException(&r.err)
return int32(r.Call("readSint32").Int())
}
// ReadSint64 reads a signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSint64() int64 {
defer catchException(&r.err)
return r.decoder.ReadZigzag64()
}
// ReadFixed32 reads an unsigned 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFixed32() uint32 {
defer catchException(&r.err)
return uint32(r.Call("readFixed32").Int())
}
// ReadFixed64 reads an unsigned 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFixed64() uint64 {
defer catchException(&r.err)
return r.decoder.ReadFixed64()
}
// ReadSfixed32 reads a signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSfixed32() int32 {
defer catchException(&r.err)
return int32(r.Call("readSfixed32").Int())
}
// ReadSfixed64 reads a signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSfixed64() int64 {
defer catchException(&r.err)
return r.decoder.ReadSignedFixed64()
}
// ReadFloat32 reads a 32-bit floating point field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFloat32() float32 {
defer catchException(&r.err)
return float32(r.Call("readFloat").Float())
}
// ReadFloat64 reads a 64-bit floating point field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFloat64() float64 {
defer catchException(&r.err)
return r.Call("readDouble").Float()
}
// ReadEnum reads an enum field from the binary stream,
// or sets err if the next field in the stream
// is not of the correct wire type.
func (r *reader) ReadEnum() int {
defer catchException(&r.err)
return r.Call("readEnum").Int()
}
// ReadBool reads a bool field from the binary stream, or sets err
// if the next field in the stream is not of the correct wire type.
func (r *reader) ReadBool() bool {
defer catchException(&r.err)
return r.Call("readBool").Bool()
}
// ReadString reads a string field from the binary stream, or sets err
// if the next field in the stream is not of the correct wire type.
func (r *reader) ReadString() string {
defer catchException(&r.err)
return r.Call("readString").String()
}
// ReadBytes reads a bytes field from the binary stream, or sets err
// if the next field in the stream is not of the correct wire type.
func (r *reader) ReadBytes() []byte {
defer catchException(&r.err)
return r.Call("readBytes").Interface().([]byte)
}
// ReadMessage deserializes a proto using
// the provided reader function.
func (r *reader) ReadMessage(readFunc func()) {
defer catchException(&r.err)
r.Call("readMessage", js.Undefined /* Unused */, readFunc)
}
// ReadInt32Slice reads a repeated signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadInt32Slice() (ret []int32) {
defer catchException(&r.err)
values := r.Call("readPackedInt32").Interface().([]interface{})
for _, value := range values {
ret = append(ret, int32(value.(float64)))
}
return ret
}
// ReadInt64Slice reads a repeated signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadInt64Slice() (ret []int64) {
defer catchException(&r.err)
cb := js.MakeFunc(func(*js.Object, []*js.Object) interface{} {
ret = append(ret, r.decoder.ReadInt64())
return js.Undefined
})
r.Call("readPackedField_", cb)
return ret
}
// ReadUint32Slice reads a repeated unsigned 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadUint32Slice() (ret []uint32) {
defer catchException(&r.err)
values := r.Call("readPackedUint32").Interface().([]interface{})
for _, value := range values {
ret = append(ret, uint32(value.(float64)))
}
return ret
}
// ReadUint64Slice reads a repeated unsigned 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadUint64Slice() (ret []uint64) {
defer catchException(&r.err)
cb := js.MakeFunc(func(*js.Object, []*js.Object) interface{} {
ret = append(ret, r.decoder.ReadUint64())
return js.Undefined
})
r.Call("readPackedField_", cb)
return ret
}
// ReadSint32Slice reads a repeated signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSint32Slice() (ret []int32) {
defer catchException(&r.err)
values := r.Call("readPackedSint32").Interface().([]interface{})
for _, value := range values {
ret = append(ret, int32(value.(float64)))
}
return ret
}
// ReadSint64Slice reads a repeated signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSint64Slice() (ret []int64) {
defer catchException(&r.err)
cb := js.MakeFunc(func(*js.Object, []*js.Object) interface{} {
ret = append(ret, r.decoder.ReadZigzag64())
return js.Undefined
})
r.Call("readPackedField_", cb)
return ret
}
// ReadFixed32Slice reads a repeated unsigned 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFixed32Slice() (ret []uint32) {
defer catchException(&r.err)
values := r.Call("readPackedFixed32").Interface().([]interface{})
for _, value := range values {
ret = append(ret, uint32(value.(float64)))
}
return ret
}
// ReadFixed64Slice reads a repeated unsigned 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFixed64Slice() (ret []uint64) {
defer catchException(&r.err)
cb := js.MakeFunc(func(*js.Object, []*js.Object) interface{} {
ret = append(ret, r.decoder.ReadFixed64())
return js.Undefined
})
r.Call("readPackedField_", cb)
return ret
}
// ReadSfixed32Slice reads a repeated signed 32-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSfixed32Slice() (ret []int32) {
defer catchException(&r.err)
values := r.Call("readPackedSfixed32").Interface().([]interface{})
for _, value := range values {
ret = append(ret, int32(value.(float64)))
}
return ret
}
// ReadSfixed64Slice reads a repeated signed 64-bit integer field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadSfixed64Slice() (ret []int64) {
defer catchException(&r.err)
cb := js.MakeFunc(func(*js.Object, []*js.Object) interface{} {
ret = append(ret, r.decoder.ReadSignedFixed64())
return js.Undefined
})
r.Call("readPackedField_", cb)
return ret
}
// ReadFloat32Slice reads a repeated 32-bit floating point field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFloat32Slice() (ret []float32) {
defer catchException(&r.err)
values := r.Call("readPackedFloat").Interface().([]interface{})
for _, value := range values {
ret = append(ret, float32(value.(float64)))
}
return ret
}
// ReadFloat64Slice reads a repeated 64-bit floating point field from the binary
// stream, or sets err if the next field in the
// stream is not of the correct wire type.
func (r *reader) ReadFloat64Slice() (ret []float64) {
defer catchException(&r.err)
values := r.Call("readPackedDouble").Interface().([]interface{})
for _, value := range values {
ret = append(ret, value.(float64))
}
return ret
}
// ReadEnumSlice reads a repeated enum field from the binary stream,
// or sets err if the next field in the stream
// is not of the correct wire type.
func (r *reader) ReadEnumSlice() (ret []int) {
defer catchException(&r.err)
values := r.Call("readPackedEnum").Interface().([]interface{})
for _, value := range values {
ret = append(ret, int(value.(float64)))
}
return ret
}
// ReadBoolSlice reads a repeated bool field from the binary stream, or sets err
// if the next field in the stream is not of the correct wire type.
func (r *reader) ReadBoolSlice() (ret []bool) {
defer catchException(&r.err)
values := r.Call("readPackedBool").Interface().([]interface{})
for _, value := range values {
ret = append(ret, value.(bool))
}
return ret
}