helper_not_unsafe.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 = rv4i(v)
  34. if isnilBitset.isset(byte(rv.Kind())) {
  35. isnil = rv.IsNil()
  36. }
  37. return
  38. }
  39. func rv4i(i interface{}) reflect.Value {
  40. return reflect.ValueOf(i)
  41. }
  42. func rv2i(rv reflect.Value) interface{} {
  43. return rv.Interface()
  44. }
  45. func rvisnil(rv reflect.Value) bool {
  46. return rv.IsNil()
  47. }
  48. func rvssetlen(rv reflect.Value, length int) {
  49. rv.SetLen(length)
  50. }
  51. // func rvzeroaddr(t reflect.Type) reflect.Value {
  52. // return reflect.New(t).Elem()
  53. // }
  54. func rvzeroaddrk(t reflect.Type, k reflect.Kind) reflect.Value {
  55. return reflect.New(t).Elem()
  56. }
  57. func rvconvert(v reflect.Value, t reflect.Type) (rv reflect.Value) {
  58. return v.Convert(t)
  59. }
  60. // func rvisnilref(rv reflect.Value) bool {
  61. // return rv.IsNil()
  62. // }
  63. // func rvslen(rv reflect.Value) int {
  64. // return rv.Len()
  65. // }
  66. // func rv2rtid(rv reflect.Value) uintptr {
  67. // return rv4i(rv.Type()).Pointer()
  68. // }
  69. func rt2id(rt reflect.Type) uintptr {
  70. return rv4i(rt).Pointer()
  71. }
  72. func i2rtid(i interface{}) uintptr {
  73. return rv4i(reflect.TypeOf(i)).Pointer()
  74. }
  75. // --------------------------
  76. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  77. switch v.Kind() {
  78. case reflect.Invalid:
  79. return true
  80. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  81. return v.Len() == 0
  82. case reflect.Bool:
  83. return !v.Bool()
  84. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  85. return v.Int() == 0
  86. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  87. return v.Uint() == 0
  88. case reflect.Float32, reflect.Float64:
  89. return v.Float() == 0
  90. case reflect.Interface, reflect.Ptr:
  91. if deref {
  92. if v.IsNil() {
  93. return true
  94. }
  95. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  96. }
  97. return v.IsNil()
  98. case reflect.Struct:
  99. return isEmptyStruct(v, tinfos, deref, checkStruct)
  100. }
  101. return false
  102. }
  103. // --------------------------
  104. // type ptrToRvMap struct{}
  105. // func (*ptrToRvMap) init() {}
  106. // func (*ptrToRvMap) get(i interface{}) reflect.Value {
  107. // return rv4i(i).Elem()
  108. // }
  109. // --------------------------
  110. type atomicClsErr struct {
  111. v atomic.Value
  112. }
  113. func (x *atomicClsErr) load() (e clsErr) {
  114. if i := x.v.Load(); i != nil {
  115. e = i.(clsErr)
  116. }
  117. return
  118. }
  119. func (x *atomicClsErr) store(p clsErr) {
  120. x.v.Store(p)
  121. }
  122. // --------------------------
  123. type atomicTypeInfoSlice struct { // expected to be 2 words
  124. v atomic.Value
  125. }
  126. func (x *atomicTypeInfoSlice) load() (e []rtid2ti) {
  127. if i := x.v.Load(); i != nil {
  128. e = i.([]rtid2ti)
  129. }
  130. return
  131. }
  132. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  133. x.v.Store(p)
  134. }
  135. // --------------------------
  136. type atomicRtidFnSlice struct { // expected to be 2 words
  137. v atomic.Value
  138. }
  139. func (x *atomicRtidFnSlice) load() (e []codecRtidFn) {
  140. if i := x.v.Load(); i != nil {
  141. e = i.([]codecRtidFn)
  142. }
  143. return
  144. }
  145. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  146. x.v.Store(p)
  147. }
  148. // --------------------------
  149. func (n *decNaked) ru() reflect.Value {
  150. return rv4i(&n.u).Elem()
  151. }
  152. func (n *decNaked) ri() reflect.Value {
  153. return rv4i(&n.i).Elem()
  154. }
  155. func (n *decNaked) rf() reflect.Value {
  156. return rv4i(&n.f).Elem()
  157. }
  158. func (n *decNaked) rl() reflect.Value {
  159. return rv4i(&n.l).Elem()
  160. }
  161. func (n *decNaked) rs() reflect.Value {
  162. return rv4i(&n.s).Elem()
  163. }
  164. func (n *decNaked) rt() reflect.Value {
  165. return rv4i(&n.t).Elem()
  166. }
  167. func (n *decNaked) rb() reflect.Value {
  168. return rv4i(&n.b).Elem()
  169. }
  170. // --------------------------
  171. func rvSetBytes(rv reflect.Value, v []byte) {
  172. rv.SetBytes(v)
  173. }
  174. func rvSetString(rv reflect.Value, v string) {
  175. rv.SetString(v)
  176. }
  177. func rvSetBool(rv reflect.Value, v bool) {
  178. rv.SetBool(v)
  179. }
  180. func rvSetTime(rv reflect.Value, v time.Time) {
  181. rv.Set(rv4i(v))
  182. }
  183. func rvSetFloat32(rv reflect.Value, v float32) {
  184. rv.SetFloat(float64(v))
  185. }
  186. func rvSetFloat64(rv reflect.Value, v float64) {
  187. rv.SetFloat(v)
  188. }
  189. func rvSetInt(rv reflect.Value, v int) {
  190. rv.SetInt(int64(v))
  191. }
  192. func rvSetInt8(rv reflect.Value, v int8) {
  193. rv.SetInt(int64(v))
  194. }
  195. func rvSetInt16(rv reflect.Value, v int16) {
  196. rv.SetInt(int64(v))
  197. }
  198. func rvSetInt32(rv reflect.Value, v int32) {
  199. rv.SetInt(int64(v))
  200. }
  201. func rvSetInt64(rv reflect.Value, v int64) {
  202. rv.SetInt(v)
  203. }
  204. func rvSetUint(rv reflect.Value, v uint) {
  205. rv.SetUint(uint64(v))
  206. }
  207. func rvSetUintptr(rv reflect.Value, v uintptr) {
  208. rv.SetUint(uint64(v))
  209. }
  210. func rvSetUint8(rv reflect.Value, v uint8) {
  211. rv.SetUint(uint64(v))
  212. }
  213. func rvSetUint16(rv reflect.Value, v uint16) {
  214. rv.SetUint(uint64(v))
  215. }
  216. func rvSetUint32(rv reflect.Value, v uint32) {
  217. rv.SetUint(uint64(v))
  218. }
  219. func rvSetUint64(rv reflect.Value, v uint64) {
  220. rv.SetUint(v)
  221. }
  222. // ----------------
  223. // rvSetDirect is rv.Set for all kinds except reflect.Interface
  224. func rvSetDirect(rv reflect.Value, v reflect.Value) {
  225. rv.Set(v)
  226. }
  227. // ----------------
  228. func rvSliceLen(rv reflect.Value) int {
  229. return rv.Len()
  230. }
  231. func rvSliceCap(rv reflect.Value) int {
  232. return rv.Cap()
  233. }
  234. // rvSlice returns a slice of the slice of lenth
  235. func rvSlice(rv reflect.Value, length int) reflect.Value {
  236. return rv.Slice(0, length)
  237. }
  238. // ----------------
  239. func rvGetBool(rv reflect.Value) bool {
  240. return rv.Bool()
  241. }
  242. func rvGetBytes(rv reflect.Value) []byte {
  243. return rv.Bytes()
  244. }
  245. func rvGetTime(rv reflect.Value) time.Time {
  246. return rv2i(rv).(time.Time)
  247. }
  248. func rvGetString(rv reflect.Value) string {
  249. return rv.String()
  250. }
  251. // func rvGetStringToRaw(rv reflect.Value) {
  252. // e.e.EncodeStringBytesRaw(bytesView(rv.String()))
  253. // }
  254. // func rvGetStringEnc(rv reflect.Value) {
  255. // e.e.EncodeStringEnc(cUTF8, rv.String())
  256. // }
  257. func rvGetFloat64(rv reflect.Value) float64 {
  258. return rv.Float()
  259. }
  260. func rvGetFloat32(rv reflect.Value) float32 {
  261. return float32(rv.Float())
  262. }
  263. func rvGetInt(rv reflect.Value) int {
  264. return int(rv.Int())
  265. }
  266. func rvGetInt8(rv reflect.Value) int8 {
  267. return int8(rv.Int())
  268. }
  269. func rvGetInt16(rv reflect.Value) int16 {
  270. return int16(rv.Int())
  271. }
  272. func rvGetInt32(rv reflect.Value) int32 {
  273. return int32(rv.Int())
  274. }
  275. func rvGetInt64(rv reflect.Value) int64 {
  276. return rv.Int()
  277. }
  278. func rvGetUint(rv reflect.Value) uint {
  279. return uint(rv.Uint())
  280. }
  281. func rvGetUint8(rv reflect.Value) uint8 {
  282. return uint8(rv.Uint())
  283. }
  284. func rvGetUint16(rv reflect.Value) uint16 {
  285. return uint16(rv.Uint())
  286. }
  287. func rvGetUint32(rv reflect.Value) uint32 {
  288. return uint32(rv.Uint())
  289. }
  290. func rvGetUint64(rv reflect.Value) uint64 {
  291. return rv.Uint()
  292. }
  293. func rvGetUintptr(rv reflect.Value) uintptr {
  294. return uintptr(rv.Uint())
  295. }
  296. // ------------ map range and map indexing ----------
  297. func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
  298. return m.MapIndex(k)
  299. }
  300. func mapSet(m, k, v reflect.Value) {
  301. m.SetMapIndex(k, v)
  302. }
  303. func mapDelete(m, k reflect.Value) {
  304. m.SetMapIndex(k, reflect.Value{})
  305. }
  306. // return an addressable reflect value that can be used in mapRange and mapGet operations.
  307. //
  308. // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
  309. func mapAddressableRV(t reflect.Type, k reflect.Kind) (r reflect.Value) {
  310. return // reflect.New(t).Elem()
  311. }