helper_not_unsafe.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // +build !go1.7 safe appengine
  2. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. import (
  6. "reflect"
  7. "sync/atomic"
  8. "time"
  9. )
  10. const safeMode = true
  11. // stringView returns a view of the []byte as a string.
  12. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  13. // In regular safe mode, it is an allocation and copy.
  14. //
  15. // Usage: Always maintain a reference to v while result of this call is in use,
  16. // and call keepAlive4BytesView(v) at point where done with view.
  17. func stringView(v []byte) string {
  18. return string(v)
  19. }
  20. // bytesView returns a view of the string as a []byte.
  21. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  22. // In regular safe mode, it is an allocation and copy.
  23. //
  24. // Usage: Always maintain a reference to v while result of this call is in use,
  25. // and call keepAlive4BytesView(v) at point where done with view.
  26. func bytesView(v string) []byte {
  27. return []byte(v)
  28. }
  29. // isNil says whether the value v is nil.
  30. // This applies to references like map/ptr/unsafepointer/chan/func,
  31. // and non-reference values like interface/slice.
  32. func isNil(v interface{}) (rv reflect.Value, isnil bool) {
  33. rv = reflect.ValueOf(v)
  34. if isnilBitset.isset(byte(rv.Kind())) {
  35. isnil = rv.IsNil()
  36. }
  37. return
  38. }
  39. func rv2i(rv reflect.Value) interface{} {
  40. return rv.Interface()
  41. }
  42. func rvisnil(rv reflect.Value) bool {
  43. return rv.IsNil()
  44. }
  45. func rvssetlen(rv reflect.Value, length int) {
  46. rv.SetLen(length)
  47. }
  48. // func rvisnilref(rv reflect.Value) bool {
  49. // return rv.IsNil()
  50. // }
  51. // func rvslen(rv reflect.Value) int {
  52. // return rv.Len()
  53. // }
  54. // func rv2rtid(rv reflect.Value) uintptr {
  55. // return reflect.ValueOf(rv.Type()).Pointer()
  56. // }
  57. func rt2id(rt reflect.Type) uintptr {
  58. return reflect.ValueOf(rt).Pointer()
  59. }
  60. func i2rtid(i interface{}) uintptr {
  61. return reflect.ValueOf(reflect.TypeOf(i)).Pointer()
  62. }
  63. // --------------------------
  64. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  65. switch v.Kind() {
  66. case reflect.Invalid:
  67. return true
  68. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  69. return v.Len() == 0
  70. case reflect.Bool:
  71. return !v.Bool()
  72. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  73. return v.Int() == 0
  74. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  75. return v.Uint() == 0
  76. case reflect.Float32, reflect.Float64:
  77. return v.Float() == 0
  78. case reflect.Interface, reflect.Ptr:
  79. if deref {
  80. if v.IsNil() {
  81. return true
  82. }
  83. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  84. }
  85. return v.IsNil()
  86. case reflect.Struct:
  87. return isEmptyStruct(v, tinfos, deref, checkStruct)
  88. }
  89. return false
  90. }
  91. // --------------------------
  92. // type ptrToRvMap struct{}
  93. // func (*ptrToRvMap) init() {}
  94. // func (*ptrToRvMap) get(i interface{}) reflect.Value {
  95. // return reflect.ValueOf(i).Elem()
  96. // }
  97. // --------------------------
  98. type atomicClsErr struct {
  99. v atomic.Value
  100. }
  101. func (x *atomicClsErr) load() (e clsErr) {
  102. if i := x.v.Load(); i != nil {
  103. e = i.(clsErr)
  104. }
  105. return
  106. }
  107. func (x *atomicClsErr) store(p clsErr) {
  108. x.v.Store(p)
  109. }
  110. // --------------------------
  111. type atomicTypeInfoSlice struct { // expected to be 2 words
  112. v atomic.Value
  113. }
  114. func (x *atomicTypeInfoSlice) load() (e []rtid2ti) {
  115. if i := x.v.Load(); i != nil {
  116. e = i.([]rtid2ti)
  117. }
  118. return
  119. }
  120. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  121. x.v.Store(p)
  122. }
  123. // --------------------------
  124. type atomicRtidFnSlice struct { // expected to be 2 words
  125. v atomic.Value
  126. }
  127. func (x *atomicRtidFnSlice) load() (e []codecRtidFn) {
  128. if i := x.v.Load(); i != nil {
  129. e = i.([]codecRtidFn)
  130. }
  131. return
  132. }
  133. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  134. x.v.Store(p)
  135. }
  136. // --------------------------
  137. func (n *decNaked) ru() reflect.Value {
  138. return reflect.ValueOf(&n.u).Elem()
  139. }
  140. func (n *decNaked) ri() reflect.Value {
  141. return reflect.ValueOf(&n.i).Elem()
  142. }
  143. func (n *decNaked) rf() reflect.Value {
  144. return reflect.ValueOf(&n.f).Elem()
  145. }
  146. func (n *decNaked) rl() reflect.Value {
  147. return reflect.ValueOf(&n.l).Elem()
  148. }
  149. func (n *decNaked) rs() reflect.Value {
  150. return reflect.ValueOf(&n.s).Elem()
  151. }
  152. func (n *decNaked) rt() reflect.Value {
  153. return reflect.ValueOf(&n.t).Elem()
  154. }
  155. func (n *decNaked) rb() reflect.Value {
  156. return reflect.ValueOf(&n.b).Elem()
  157. }
  158. // --------------------------
  159. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  160. rv.SetBytes(d.rawBytes())
  161. }
  162. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  163. rv.SetString(string(d.d.DecodeStringAsBytes()))
  164. }
  165. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  166. rv.SetBool(d.d.DecodeBool())
  167. }
  168. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  169. rv.Set(reflect.ValueOf(d.d.DecodeTime()))
  170. }
  171. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  172. rv.SetFloat(float64(d.decodeFloat32()))
  173. }
  174. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  175. rv.SetFloat(d.d.DecodeFloat64())
  176. }
  177. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  178. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  179. }
  180. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  181. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
  182. }
  183. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  184. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
  185. }
  186. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  187. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
  188. }
  189. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  190. rv.SetInt(d.d.DecodeInt64())
  191. }
  192. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  193. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  194. }
  195. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  196. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  197. }
  198. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  199. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
  200. }
  201. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  202. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
  203. }
  204. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  205. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
  206. }
  207. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  208. rv.SetUint(d.d.DecodeUint64())
  209. }
  210. // ----------------
  211. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  212. e.e.EncodeBool(rv.Bool())
  213. }
  214. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  215. e.e.EncodeTime(rv2i(rv).(time.Time))
  216. }
  217. func (e *Encoder) kStringToRaw(f *codecFnInfo, rv reflect.Value) {
  218. s := rv.String()
  219. e.e.EncodeStringBytesRaw(bytesView(s))
  220. }
  221. func (e *Encoder) kStringEnc(f *codecFnInfo, rv reflect.Value) {
  222. s := rv.String()
  223. e.e.EncodeStringEnc(cUTF8, s)
  224. }
  225. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  226. e.e.EncodeFloat64(rv.Float())
  227. }
  228. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  229. e.e.EncodeFloat32(float32(rv.Float()))
  230. }
  231. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  232. e.e.EncodeInt(rv.Int())
  233. }
  234. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  235. e.e.EncodeInt(rv.Int())
  236. }
  237. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  238. e.e.EncodeInt(rv.Int())
  239. }
  240. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  241. e.e.EncodeInt(rv.Int())
  242. }
  243. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  244. e.e.EncodeInt(rv.Int())
  245. }
  246. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  247. e.e.EncodeUint(rv.Uint())
  248. }
  249. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  250. e.e.EncodeUint(rv.Uint())
  251. }
  252. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  253. e.e.EncodeUint(rv.Uint())
  254. }
  255. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  256. e.e.EncodeUint(rv.Uint())
  257. }
  258. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  259. e.e.EncodeUint(rv.Uint())
  260. }
  261. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  262. e.e.EncodeUint(rv.Uint())
  263. }
  264. // ------------ map range and map indexing ----------
  265. func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
  266. return m.MapIndex(k)
  267. }
  268. func mapSet(m, k, v reflect.Value) {
  269. m.SetMapIndex(k, v)
  270. }
  271. func mapDelete(m, k reflect.Value) {
  272. m.SetMapIndex(k, reflect.Value{})
  273. }
  274. // return an addressable reflect value that can be used in mapRange and mapGet operations.
  275. //
  276. // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
  277. func mapAddressableRV(t reflect.Type) (r reflect.Value) {
  278. return // reflect.New(t).Elem()
  279. }