helper_not_unsafe.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 rvSetSliceLen(rv reflect.Value, length int) {
  49. rv.SetLen(length)
  50. }
  51. func rvZeroAddrK(t reflect.Type, k reflect.Kind) reflect.Value {
  52. return reflect.New(t).Elem()
  53. }
  54. func rvConvert(v reflect.Value, t reflect.Type) (rv reflect.Value) {
  55. return v.Convert(t)
  56. }
  57. func rt2id(rt reflect.Type) uintptr {
  58. return rv4i(rt).Pointer()
  59. }
  60. func i2rtid(i interface{}) uintptr {
  61. return rv4i(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 atomicClsErr struct {
  93. v atomic.Value
  94. }
  95. func (x *atomicClsErr) load() (e clsErr) {
  96. if i := x.v.Load(); i != nil {
  97. e = i.(clsErr)
  98. }
  99. return
  100. }
  101. func (x *atomicClsErr) store(p clsErr) {
  102. x.v.Store(p)
  103. }
  104. // --------------------------
  105. type atomicTypeInfoSlice struct { // expected to be 2 words
  106. v atomic.Value
  107. }
  108. func (x *atomicTypeInfoSlice) load() (e []rtid2ti) {
  109. if i := x.v.Load(); i != nil {
  110. e = i.([]rtid2ti)
  111. }
  112. return
  113. }
  114. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  115. x.v.Store(p)
  116. }
  117. // --------------------------
  118. type atomicRtidFnSlice struct { // expected to be 2 words
  119. v atomic.Value
  120. }
  121. func (x *atomicRtidFnSlice) load() (e []codecRtidFn) {
  122. if i := x.v.Load(); i != nil {
  123. e = i.([]codecRtidFn)
  124. }
  125. return
  126. }
  127. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  128. x.v.Store(p)
  129. }
  130. // --------------------------
  131. func (n *fauxUnion) ru() reflect.Value {
  132. return rv4i(&n.u).Elem()
  133. }
  134. func (n *fauxUnion) ri() reflect.Value {
  135. return rv4i(&n.i).Elem()
  136. }
  137. func (n *fauxUnion) rf() reflect.Value {
  138. return rv4i(&n.f).Elem()
  139. }
  140. func (n *fauxUnion) rl() reflect.Value {
  141. return rv4i(&n.l).Elem()
  142. }
  143. func (n *fauxUnion) rs() reflect.Value {
  144. return rv4i(&n.s).Elem()
  145. }
  146. func (n *fauxUnion) rt() reflect.Value {
  147. return rv4i(&n.t).Elem()
  148. }
  149. func (n *fauxUnion) rb() reflect.Value {
  150. return rv4i(&n.b).Elem()
  151. }
  152. // --------------------------
  153. func rvSetBytes(rv reflect.Value, v []byte) {
  154. rv.SetBytes(v)
  155. }
  156. func rvSetString(rv reflect.Value, v string) {
  157. rv.SetString(v)
  158. }
  159. func rvSetBool(rv reflect.Value, v bool) {
  160. rv.SetBool(v)
  161. }
  162. func rvSetTime(rv reflect.Value, v time.Time) {
  163. rv.Set(rv4i(v))
  164. }
  165. func rvSetFloat32(rv reflect.Value, v float32) {
  166. rv.SetFloat(float64(v))
  167. }
  168. func rvSetFloat64(rv reflect.Value, v float64) {
  169. rv.SetFloat(v)
  170. }
  171. func rvSetInt(rv reflect.Value, v int) {
  172. rv.SetInt(int64(v))
  173. }
  174. func rvSetInt8(rv reflect.Value, v int8) {
  175. rv.SetInt(int64(v))
  176. }
  177. func rvSetInt16(rv reflect.Value, v int16) {
  178. rv.SetInt(int64(v))
  179. }
  180. func rvSetInt32(rv reflect.Value, v int32) {
  181. rv.SetInt(int64(v))
  182. }
  183. func rvSetInt64(rv reflect.Value, v int64) {
  184. rv.SetInt(v)
  185. }
  186. func rvSetUint(rv reflect.Value, v uint) {
  187. rv.SetUint(uint64(v))
  188. }
  189. func rvSetUintptr(rv reflect.Value, v uintptr) {
  190. rv.SetUint(uint64(v))
  191. }
  192. func rvSetUint8(rv reflect.Value, v uint8) {
  193. rv.SetUint(uint64(v))
  194. }
  195. func rvSetUint16(rv reflect.Value, v uint16) {
  196. rv.SetUint(uint64(v))
  197. }
  198. func rvSetUint32(rv reflect.Value, v uint32) {
  199. rv.SetUint(uint64(v))
  200. }
  201. func rvSetUint64(rv reflect.Value, v uint64) {
  202. rv.SetUint(v)
  203. }
  204. // ----------------
  205. // rvSetDirect is rv.Set for all kinds except reflect.Interface
  206. func rvSetDirect(rv reflect.Value, v reflect.Value) {
  207. rv.Set(v)
  208. }
  209. // rvSlice returns a slice of the slice of lenth
  210. func rvSlice(rv reflect.Value, length int) reflect.Value {
  211. return rv.Slice(0, length)
  212. }
  213. // ----------------
  214. func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) reflect.Value {
  215. return rv.Index(i)
  216. }
  217. func rvGetSliceLen(rv reflect.Value) int {
  218. return rv.Len()
  219. }
  220. func rvGetSliceCap(rv reflect.Value) int {
  221. return rv.Cap()
  222. }
  223. func rvGetArrayBytesRO(rv reflect.Value, scratch []byte) (bs []byte) {
  224. l := rv.Len()
  225. if rv.CanAddr() {
  226. return rvGetBytes(rv.Slice(0, l))
  227. }
  228. if l <= cap(scratch) {
  229. bs = scratch[:l]
  230. } else {
  231. bs = make([]byte, l)
  232. }
  233. reflect.Copy(rv4i(bs), rv)
  234. return
  235. }
  236. func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) {
  237. v = rvZeroAddrK(reflectArrayOf(rvGetSliceLen(rv), rv.Type().Elem()), reflect.Array)
  238. reflect.Copy(v, rv)
  239. return
  240. }
  241. func rvGetSlice4Array(rv reflect.Value, tslice reflect.Type) (v reflect.Value) {
  242. return rv.Slice(0, rv.Len())
  243. }
  244. func rvCopySlice(dest, src reflect.Value) {
  245. reflect.Copy(dest, src)
  246. }
  247. // ------------
  248. func rvGetBool(rv reflect.Value) bool {
  249. return rv.Bool()
  250. }
  251. func rvGetBytes(rv reflect.Value) []byte {
  252. return rv.Bytes()
  253. }
  254. func rvGetTime(rv reflect.Value) time.Time {
  255. return rv2i(rv).(time.Time)
  256. }
  257. func rvGetString(rv reflect.Value) string {
  258. return rv.String()
  259. }
  260. func rvGetFloat64(rv reflect.Value) float64 {
  261. return rv.Float()
  262. }
  263. func rvGetFloat32(rv reflect.Value) float32 {
  264. return float32(rv.Float())
  265. }
  266. func rvGetInt(rv reflect.Value) int {
  267. return int(rv.Int())
  268. }
  269. func rvGetInt8(rv reflect.Value) int8 {
  270. return int8(rv.Int())
  271. }
  272. func rvGetInt16(rv reflect.Value) int16 {
  273. return int16(rv.Int())
  274. }
  275. func rvGetInt32(rv reflect.Value) int32 {
  276. return int32(rv.Int())
  277. }
  278. func rvGetInt64(rv reflect.Value) int64 {
  279. return rv.Int()
  280. }
  281. func rvGetUint(rv reflect.Value) uint {
  282. return uint(rv.Uint())
  283. }
  284. func rvGetUint8(rv reflect.Value) uint8 {
  285. return uint8(rv.Uint())
  286. }
  287. func rvGetUint16(rv reflect.Value) uint16 {
  288. return uint16(rv.Uint())
  289. }
  290. func rvGetUint32(rv reflect.Value) uint32 {
  291. return uint32(rv.Uint())
  292. }
  293. func rvGetUint64(rv reflect.Value) uint64 {
  294. return rv.Uint()
  295. }
  296. func rvGetUintptr(rv reflect.Value) uintptr {
  297. return uintptr(rv.Uint())
  298. }
  299. // ------------ map range and map indexing ----------
  300. func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
  301. return m.MapIndex(k)
  302. }
  303. func mapSet(m, k, v reflect.Value) {
  304. m.SetMapIndex(k, v)
  305. }
  306. func mapDelete(m, k reflect.Value) {
  307. m.SetMapIndex(k, reflect.Value{})
  308. }
  309. // return an addressable reflect value that can be used in mapRange and mapGet operations.
  310. //
  311. // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
  312. func mapAddressableRV(t reflect.Type, k reflect.Kind) (r reflect.Value) {
  313. return // reflect.New(t).Elem()
  314. }
  315. // ---------- ENCODER optimized ---------------
  316. func (e *Encoder) jsondriver() *jsonEncDriver {
  317. return e.e.(*jsonEncDriver)
  318. }
  319. // ---------- DECODER optimized ---------------
  320. func (d *Decoder) checkBreak() bool {
  321. return d.d.CheckBreak()
  322. }
  323. func (d *Decoder) jsondriver() *jsonDecDriver {
  324. return d.d.(*jsonDecDriver)
  325. }