helper_not_unsafe.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. func definitelyNil(v interface{}) bool {
  30. // this is a best-effort option.
  31. // We just return false, so we don't unnecessarily incur the cost of reflection this early.
  32. return false
  33. }
  34. func rv2i(rv reflect.Value) interface{} {
  35. return rv.Interface()
  36. }
  37. func rvisnil(rv reflect.Value) bool {
  38. return rv.IsNil()
  39. }
  40. func rvssetlen(rv reflect.Value, length int) {
  41. rv.SetLen(length)
  42. }
  43. // func rvisnilref(rv reflect.Value) bool {
  44. // return rv.IsNil()
  45. // }
  46. // func rvslen(rv reflect.Value) int {
  47. // return rv.Len()
  48. // }
  49. // func rv2rtid(rv reflect.Value) uintptr {
  50. // return reflect.ValueOf(rv.Type()).Pointer()
  51. // }
  52. func rt2id(rt reflect.Type) uintptr {
  53. return reflect.ValueOf(rt).Pointer()
  54. }
  55. func i2rtid(i interface{}) uintptr {
  56. return reflect.ValueOf(reflect.TypeOf(i)).Pointer()
  57. }
  58. // --------------------------
  59. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  60. switch v.Kind() {
  61. case reflect.Invalid:
  62. return true
  63. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  64. return v.Len() == 0
  65. case reflect.Bool:
  66. return !v.Bool()
  67. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  68. return v.Int() == 0
  69. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  70. return v.Uint() == 0
  71. case reflect.Float32, reflect.Float64:
  72. return v.Float() == 0
  73. case reflect.Interface, reflect.Ptr:
  74. if deref {
  75. if v.IsNil() {
  76. return true
  77. }
  78. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  79. }
  80. return v.IsNil()
  81. case reflect.Struct:
  82. return isEmptyStruct(v, tinfos, deref, checkStruct)
  83. }
  84. return false
  85. }
  86. // --------------------------
  87. // type ptrToRvMap struct{}
  88. // func (*ptrToRvMap) init() {}
  89. // func (*ptrToRvMap) get(i interface{}) reflect.Value {
  90. // return reflect.ValueOf(i).Elem()
  91. // }
  92. // --------------------------
  93. type atomicClsErr struct {
  94. v atomic.Value
  95. }
  96. func (x *atomicClsErr) load() (e clsErr) {
  97. if i := x.v.Load(); i != nil {
  98. e = i.(clsErr)
  99. }
  100. return
  101. }
  102. func (x *atomicClsErr) store(p clsErr) {
  103. x.v.Store(p)
  104. }
  105. // --------------------------
  106. type atomicTypeInfoSlice struct { // expected to be 2 words
  107. v atomic.Value
  108. }
  109. func (x *atomicTypeInfoSlice) load() (e []rtid2ti) {
  110. if i := x.v.Load(); i != nil {
  111. e = i.([]rtid2ti)
  112. }
  113. return
  114. }
  115. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  116. x.v.Store(p)
  117. }
  118. // --------------------------
  119. type atomicRtidFnSlice struct { // expected to be 2 words
  120. v atomic.Value
  121. }
  122. func (x *atomicRtidFnSlice) load() (e []codecRtidFn) {
  123. if i := x.v.Load(); i != nil {
  124. e = i.([]codecRtidFn)
  125. }
  126. return
  127. }
  128. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  129. x.v.Store(p)
  130. }
  131. // --------------------------
  132. func (n *decNaked) ru() reflect.Value {
  133. return reflect.ValueOf(&n.u).Elem()
  134. }
  135. func (n *decNaked) ri() reflect.Value {
  136. return reflect.ValueOf(&n.i).Elem()
  137. }
  138. func (n *decNaked) rf() reflect.Value {
  139. return reflect.ValueOf(&n.f).Elem()
  140. }
  141. func (n *decNaked) rl() reflect.Value {
  142. return reflect.ValueOf(&n.l).Elem()
  143. }
  144. func (n *decNaked) rs() reflect.Value {
  145. return reflect.ValueOf(&n.s).Elem()
  146. }
  147. func (n *decNaked) rt() reflect.Value {
  148. return reflect.ValueOf(&n.t).Elem()
  149. }
  150. func (n *decNaked) rb() reflect.Value {
  151. return reflect.ValueOf(&n.b).Elem()
  152. }
  153. // --------------------------
  154. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  155. rv.SetBytes(d.rawBytes())
  156. }
  157. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  158. rv.SetString(string(d.d.DecodeStringAsBytes()))
  159. }
  160. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  161. rv.SetBool(d.d.DecodeBool())
  162. }
  163. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  164. rv.Set(reflect.ValueOf(d.d.DecodeTime()))
  165. }
  166. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  167. rv.SetFloat(float64(d.decodeFloat32()))
  168. }
  169. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  170. rv.SetFloat(d.d.DecodeFloat64())
  171. }
  172. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  173. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  174. }
  175. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  176. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
  177. }
  178. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  179. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
  180. }
  181. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  182. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
  183. }
  184. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  185. rv.SetInt(d.d.DecodeInt64())
  186. }
  187. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  188. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  189. }
  190. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  191. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  192. }
  193. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  194. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
  195. }
  196. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  197. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
  198. }
  199. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  200. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
  201. }
  202. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  203. rv.SetUint(d.d.DecodeUint64())
  204. }
  205. // ----------------
  206. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  207. e.e.EncodeBool(rv.Bool())
  208. }
  209. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  210. e.e.EncodeTime(rv2i(rv).(time.Time))
  211. }
  212. func (e *Encoder) kStringToRaw(f *codecFnInfo, rv reflect.Value) {
  213. s := rv.String()
  214. e.e.EncodeStringBytesRaw(bytesView(s))
  215. }
  216. func (e *Encoder) kStringEnc(f *codecFnInfo, rv reflect.Value) {
  217. s := rv.String()
  218. e.e.EncodeStringEnc(cUTF8, s)
  219. }
  220. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  221. e.e.EncodeFloat64(rv.Float())
  222. }
  223. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  224. e.e.EncodeFloat32(float32(rv.Float()))
  225. }
  226. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  227. e.e.EncodeInt(rv.Int())
  228. }
  229. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  230. e.e.EncodeInt(rv.Int())
  231. }
  232. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  233. e.e.EncodeInt(rv.Int())
  234. }
  235. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  236. e.e.EncodeInt(rv.Int())
  237. }
  238. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  239. e.e.EncodeInt(rv.Int())
  240. }
  241. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  242. e.e.EncodeUint(rv.Uint())
  243. }
  244. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  245. e.e.EncodeUint(rv.Uint())
  246. }
  247. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  248. e.e.EncodeUint(rv.Uint())
  249. }
  250. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  251. e.e.EncodeUint(rv.Uint())
  252. }
  253. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  254. e.e.EncodeUint(rv.Uint())
  255. }
  256. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  257. e.e.EncodeUint(rv.Uint())
  258. }
  259. // ------------ map range and map indexing ----------
  260. func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
  261. return m.MapIndex(k)
  262. }
  263. func mapSet(m, k, v reflect.Value) {
  264. m.SetMapIndex(k, v)
  265. }
  266. func mapDelete(m, k reflect.Value) {
  267. m.SetMapIndex(k, reflect.Value{})
  268. }
  269. // return an addressable reflect value that can be used in mapRange and mapGet operations.
  270. //
  271. // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
  272. func mapAddressableRV(t reflect.Type) (r reflect.Value) {
  273. return // reflect.New(t).Elem()
  274. }
  275. // func definitelyNil(v interface{}) bool {
  276. // rv := reflect.ValueOf(v)
  277. // switch rv.Kind() {
  278. // case reflect.Invalid:
  279. // return true
  280. // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
  281. // return rv.IsNil()
  282. // default:
  283. // return false
  284. // }
  285. // }