Skip to content

Commit ebe943a

Browse files
committed
fix #241, support 32bit platform
1 parent 414d030 commit ebe943a

File tree

4 files changed

+49
-88
lines changed

4 files changed

+49
-88
lines changed

any.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"unsafe"
99
"github.com/v2pro/plz/reflect2"
10+
"strconv"
1011
)
1112

1213
// Any generic object representation.
@@ -101,6 +102,9 @@ func Wrap(val interface{}) Any {
101102
case reflect.String:
102103
return WrapString(val.(string))
103104
case reflect.Int:
105+
if strconv.IntSize == 32 {
106+
return WrapInt32(int32(val.(int)))
107+
}
104108
return WrapInt64(int64(val.(int)))
105109
case reflect.Int8:
106110
return WrapInt32(int32(val.(int8)))
@@ -111,7 +115,15 @@ func Wrap(val interface{}) Any {
111115
case reflect.Int64:
112116
return WrapInt64(val.(int64))
113117
case reflect.Uint:
118+
if strconv.IntSize == 32 {
119+
return WrapUint32(uint32(val.(uint)))
120+
}
114121
return WrapUint64(uint64(val.(uint)))
122+
case reflect.Uintptr:
123+
if ptrSize == 32 {
124+
return WrapUint32(uint32(val.(uintptr)))
125+
}
126+
return WrapUint64(uint64(val.(uintptr)))
115127
case reflect.Uint8:
116128
return WrapUint32(uint32(val.(uint8)))
117129
case reflect.Uint16:

iter_int.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ func init() {
2222

2323
// ReadUint read uint
2424
func (iter *Iterator) ReadUint() uint {
25+
if strconv.IntSize == 32 {
26+
return uint(iter.ReadUint32())
27+
}
2528
return uint(iter.ReadUint64())
2629
}
2730

2831
// ReadInt read int
2932
func (iter *Iterator) ReadInt() int {
33+
if strconv.IntSize == 32 {
34+
return int(iter.ReadInt32())
35+
}
3036
return int(iter.ReadInt64())
3137
}
3238

reflect_native.go

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"reflect"
66
"unsafe"
77
"github.com/v2pro/plz/reflect2"
8+
"strconv"
89
)
910

11+
const ptrSize = 32 << uintptr(^uintptr(0)>>63)
12+
1013
func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
1114
if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
1215
sliceDecoder := decoderOfSlice(ctx, typ)
@@ -24,7 +27,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
2427
if typeName != "int" {
2528
return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
2629
}
27-
return &intCodec{}
30+
if strconv.IntSize == 32 {
31+
return &int32Codec{}
32+
}
33+
return &int64Codec{}
2834
case reflect.Int8:
2935
if typeName != "int8" {
3036
return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
@@ -49,7 +55,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
4955
if typeName != "uint" {
5056
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
5157
}
52-
return &uintCodec{}
58+
if strconv.IntSize == 32 {
59+
return &uint32Codec{}
60+
}
61+
return &uint64Codec{}
5362
case reflect.Uint8:
5463
if typeName != "uint8" {
5564
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
@@ -69,7 +78,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
6978
if typeName != "uintptr" {
7079
return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
7180
}
72-
return &uintptrCodec{}
81+
if ptrSize == 32 {
82+
return &uint32Codec{}
83+
}
84+
return &uint64Codec{}
7385
case reflect.Uint64:
7486
if typeName != "uint64" {
7587
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
@@ -110,7 +122,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
110122
if typeName != "int" {
111123
return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
112124
}
113-
return &intCodec{}
125+
if strconv.IntSize == 32 {
126+
return &int32Codec{}
127+
}
128+
return &int64Codec{}
114129
case reflect.Int8:
115130
if typeName != "int8" {
116131
return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
@@ -135,7 +150,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
135150
if typeName != "uint" {
136151
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
137152
}
138-
return &uintCodec{}
153+
if strconv.IntSize == 32 {
154+
return &uint32Codec{}
155+
}
156+
return &uint64Codec{}
139157
case reflect.Uint8:
140158
if typeName != "uint8" {
141159
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
@@ -155,7 +173,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
155173
if typeName != "uintptr" {
156174
return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
157175
}
158-
return &uintptrCodec{}
176+
if ptrSize == 32 {
177+
return &uint32Codec{}
178+
}
179+
return &uint64Codec{}
159180
case reflect.Uint64:
160181
if typeName != "uint64" {
161182
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
@@ -196,40 +217,6 @@ func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
196217
return *((*string)(ptr)) == ""
197218
}
198219

199-
type intCodec struct {
200-
}
201-
202-
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
203-
if !iter.ReadNil() {
204-
*((*int)(ptr)) = iter.ReadInt()
205-
}
206-
}
207-
208-
func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
209-
stream.WriteInt(*((*int)(ptr)))
210-
}
211-
212-
func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
213-
return *((*int)(ptr)) == 0
214-
}
215-
216-
type uintptrCodec struct {
217-
}
218-
219-
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
220-
if !iter.ReadNil() {
221-
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
222-
}
223-
}
224-
225-
func (codec *uintptrCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
226-
stream.WriteUint64(uint64(*((*uintptr)(ptr))))
227-
}
228-
229-
func (codec *uintptrCodec) IsEmpty(ptr unsafe.Pointer) bool {
230-
return *((*uintptr)(ptr)) == 0
231-
}
232-
233220
type int8Codec struct {
234221
}
235222

@@ -298,24 +285,6 @@ func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
298285
return *((*int64)(ptr)) == 0
299286
}
300287

301-
type uintCodec struct {
302-
}
303-
304-
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
305-
if !iter.ReadNil() {
306-
*((*uint)(ptr)) = iter.ReadUint()
307-
return
308-
}
309-
}
310-
311-
func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
312-
stream.WriteUint(*((*uint)(ptr)))
313-
}
314-
315-
func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
316-
return *((*uint)(ptr)) == 0
317-
}
318-
319288
type uint8Codec struct {
320289
}
321290

reflect_struct_encoder.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,12 @@ func encoderOfStruct(ctx *ctx, typ reflect2.Type) ValEncoder {
4747
}
4848

4949
func createCheckIsEmpty(ctx *ctx, typ reflect2.Type) checkIsEmpty {
50+
encoder := createEncoderOfNative(ctx, typ)
51+
if encoder != nil {
52+
return encoder
53+
}
5054
kind := typ.Kind()
5155
switch kind {
52-
case reflect.String:
53-
return &stringCodec{}
54-
case reflect.Int:
55-
return &intCodec{}
56-
case reflect.Int8:
57-
return &int8Codec{}
58-
case reflect.Int16:
59-
return &int16Codec{}
60-
case reflect.Int32:
61-
return &int32Codec{}
62-
case reflect.Int64:
63-
return &int64Codec{}
64-
case reflect.Uint:
65-
return &uintCodec{}
66-
case reflect.Uint8:
67-
return &uint8Codec{}
68-
case reflect.Uint16:
69-
return &uint16Codec{}
70-
case reflect.Uint32:
71-
return &uint32Codec{}
72-
case reflect.Uintptr:
73-
return &uintptrCodec{}
74-
case reflect.Uint64:
75-
return &uint64Codec{}
76-
case reflect.Float32:
77-
return &float32Codec{}
78-
case reflect.Float64:
79-
return &float64Codec{}
80-
case reflect.Bool:
81-
return &boolCodec{}
8256
case reflect.Interface:
8357
return &dynamicEncoder{typ}
8458
case reflect.Struct:

0 commit comments

Comments
 (0)