1 // +build !go1.7 safe appengine
3 // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
4 // Use of this source code is governed by a MIT license found in the LICENSE file.
16 // stringView returns a view of the []byte as a string.
17 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
18 // In regular safe mode, it is an allocation and copy.
20 // Usage: Always maintain a reference to v while result of this call is in use,
21 // and call keepAlive4BytesView(v) at point where done with view.
22 func stringView(v []byte) string {
26 // bytesView returns a view of the string as a []byte.
27 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
28 // In regular safe mode, it is an allocation and copy.
30 // Usage: Always maintain a reference to v while result of this call is in use,
31 // and call keepAlive4BytesView(v) at point where done with view.
32 func bytesView(v string) []byte {
36 func definitelyNil(v interface{}) bool {
37 // this is a best-effort option.
38 // We just return false, so we don't unnecessarily incur the cost of reflection this early.
42 func rv2i(rv reflect.Value) interface{} {
46 func rt2id(rt reflect.Type) uintptr {
47 return reflect.ValueOf(rt).Pointer()
50 func rv2rtid(rv reflect.Value) uintptr {
51 return reflect.ValueOf(rv.Type()).Pointer()
54 func i2rtid(i interface{}) uintptr {
55 return reflect.ValueOf(reflect.TypeOf(i)).Pointer()
58 // --------------------------
60 func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
64 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
68 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
70 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
72 case reflect.Float32, reflect.Float64:
74 case reflect.Interface, reflect.Ptr:
79 return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
83 return isEmptyStruct(v, tinfos, deref, checkStruct)
88 // --------------------------
89 // type ptrToRvMap struct{}
91 // func (*ptrToRvMap) init() {}
92 // func (*ptrToRvMap) get(i interface{}) reflect.Value {
93 // return reflect.ValueOf(i).Elem()
96 // --------------------------
97 type atomicClsErr struct {
101 func (x *atomicClsErr) load() clsErr {
109 func (x *atomicClsErr) store(p clsErr) {
113 // --------------------------
114 type atomicTypeInfoSlice struct { // expected to be 2 words
118 func (x *atomicTypeInfoSlice) load() []rtid2ti {
126 func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
130 // --------------------------
131 func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
132 rv.SetBytes(d.rawBytes())
135 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
136 rv.SetString(d.d.DecodeString())
139 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
140 rv.SetBool(d.d.DecodeBool())
143 func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
144 rv.Set(reflect.ValueOf(d.d.DecodeTime()))
147 func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
148 fv := d.d.DecodeFloat64()
149 if chkOvf.Float32(fv) {
150 d.errorf("float32 overflow: %v", fv)
155 func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
156 rv.SetFloat(d.d.DecodeFloat64())
159 func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
160 rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
163 func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
164 rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
167 func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
168 rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
171 func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
172 rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
175 func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
176 rv.SetInt(d.d.DecodeInt64())
179 func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
180 rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
183 func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
184 rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
187 func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
188 rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
191 func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
192 rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
195 func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
196 rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
199 func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
200 rv.SetUint(d.d.DecodeUint64())
205 func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
206 e.e.EncodeBool(rv.Bool())
209 func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
210 e.e.EncodeTime(rv2i(rv).(time.Time))
213 func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
214 e.e.EncodeString(cUTF8, rv.String())
217 func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
218 e.e.EncodeFloat64(rv.Float())
221 func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
222 e.e.EncodeFloat32(float32(rv.Float()))
225 func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
226 e.e.EncodeInt(rv.Int())
229 func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
230 e.e.EncodeInt(rv.Int())
233 func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
234 e.e.EncodeInt(rv.Int())
237 func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
238 e.e.EncodeInt(rv.Int())
241 func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
242 e.e.EncodeInt(rv.Int())
245 func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
246 e.e.EncodeUint(rv.Uint())
249 func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
250 e.e.EncodeUint(rv.Uint())
253 func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
254 e.e.EncodeUint(rv.Uint())
257 func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
258 e.e.EncodeUint(rv.Uint())
261 func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
262 e.e.EncodeUint(rv.Uint())
265 func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
266 e.e.EncodeUint(rv.Uint())
269 // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
271 // // Usage: call this at point where done with the bytes view.
272 // func keepAlive4BytesView(v string) {}
274 // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
276 // // Usage: call this at point where done with the string view.
277 // func keepAlive4StringView(v []byte) {}
279 // func definitelyNil(v interface{}) bool {
280 // rv := reflect.ValueOf(v)
281 // switch rv.Kind() {
282 // case reflect.Invalid:
284 // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func: