helper_unsafe.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // +build !safe
  2. // +build !appengine
  3. // +build go1.7
  4. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  5. // Use of this source code is governed by a MIT license found in the LICENSE file.
  6. package codec
  7. import (
  8. "reflect"
  9. "sync/atomic"
  10. "time"
  11. "unsafe"
  12. )
  13. // This file has unsafe variants of some helper methods.
  14. // NOTE: See helper_not_unsafe.go for the usage information.
  15. // var zeroRTv [4]uintptr
  16. const safeMode = false
  17. const unsafeFlagIndir = 1 << 7 // keep in sync with GO_ROOT/src/reflect/value.go
  18. type unsafeString struct {
  19. Data unsafe.Pointer
  20. Len int
  21. }
  22. type unsafeSlice struct {
  23. Data unsafe.Pointer
  24. Len int
  25. Cap int
  26. }
  27. type unsafeIntf struct {
  28. typ unsafe.Pointer
  29. word unsafe.Pointer
  30. }
  31. type unsafeReflectValue struct {
  32. typ unsafe.Pointer
  33. ptr unsafe.Pointer
  34. flag uintptr
  35. }
  36. func stringView(v []byte) string {
  37. if len(v) == 0 {
  38. return ""
  39. }
  40. bx := (*unsafeSlice)(unsafe.Pointer(&v))
  41. return *(*string)(unsafe.Pointer(&unsafeString{bx.Data, bx.Len}))
  42. }
  43. func bytesView(v string) []byte {
  44. if len(v) == 0 {
  45. return zeroByteSlice
  46. }
  47. sx := (*unsafeString)(unsafe.Pointer(&v))
  48. return *(*[]byte)(unsafe.Pointer(&unsafeSlice{sx.Data, sx.Len, sx.Len}))
  49. }
  50. func definitelyNil(v interface{}) bool {
  51. // There is no global way of checking if an interface is nil.
  52. // For true references (map, ptr, func, chan), you can just look
  53. // at the word of the interface. However, for slices, you have to dereference
  54. // the word, and get a pointer to the 3-word interface value.
  55. //
  56. // However, the following are cheap calls
  57. // - TypeOf(interface): cheap 2-line call.
  58. // - ValueOf(interface{}): expensive
  59. // - type.Kind: cheap call through an interface
  60. // - Value.Type(): cheap call
  61. // except it's a method value (e.g. r.Read, which implies that it is a Func)
  62. return ((*unsafeIntf)(unsafe.Pointer(&v))).word == nil
  63. }
  64. func rv2i(rv reflect.Value) interface{} {
  65. // TODO: consider a more generally-known optimization for reflect.Value ==> Interface
  66. //
  67. // Currently, we use this fragile method that taps into implememtation details from
  68. // the source go stdlib reflect/value.go, and trims the implementation.
  69. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  70. // true references (map, func, chan, ptr - NOT slice) may be double-referenced as flagIndir
  71. var ptr unsafe.Pointer
  72. if refBitset.isset(byte(urv.flag&(1<<5-1))) && urv.flag&unsafeFlagIndir != 0 {
  73. ptr = *(*unsafe.Pointer)(urv.ptr)
  74. } else {
  75. ptr = urv.ptr
  76. }
  77. return *(*interface{})(unsafe.Pointer(&unsafeIntf{typ: urv.typ, word: ptr}))
  78. }
  79. func rt2id(rt reflect.Type) uintptr {
  80. return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  81. }
  82. func rv2rtid(rv reflect.Value) uintptr {
  83. return uintptr((*unsafeReflectValue)(unsafe.Pointer(&rv)).typ)
  84. }
  85. func i2rtid(i interface{}) uintptr {
  86. return uintptr(((*unsafeIntf)(unsafe.Pointer(&i))).typ)
  87. }
  88. // --------------------------
  89. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  90. urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  91. if urv.flag == 0 {
  92. return true
  93. }
  94. switch v.Kind() {
  95. case reflect.Invalid:
  96. return true
  97. case reflect.String:
  98. return (*unsafeString)(urv.ptr).Len == 0
  99. case reflect.Slice:
  100. return (*unsafeSlice)(urv.ptr).Len == 0
  101. case reflect.Bool:
  102. return !*(*bool)(urv.ptr)
  103. case reflect.Int:
  104. return *(*int)(urv.ptr) == 0
  105. case reflect.Int8:
  106. return *(*int8)(urv.ptr) == 0
  107. case reflect.Int16:
  108. return *(*int16)(urv.ptr) == 0
  109. case reflect.Int32:
  110. return *(*int32)(urv.ptr) == 0
  111. case reflect.Int64:
  112. return *(*int64)(urv.ptr) == 0
  113. case reflect.Uint:
  114. return *(*uint)(urv.ptr) == 0
  115. case reflect.Uint8:
  116. return *(*uint8)(urv.ptr) == 0
  117. case reflect.Uint16:
  118. return *(*uint16)(urv.ptr) == 0
  119. case reflect.Uint32:
  120. return *(*uint32)(urv.ptr) == 0
  121. case reflect.Uint64:
  122. return *(*uint64)(urv.ptr) == 0
  123. case reflect.Uintptr:
  124. return *(*uintptr)(urv.ptr) == 0
  125. case reflect.Float32:
  126. return *(*float32)(urv.ptr) == 0
  127. case reflect.Float64:
  128. return *(*float64)(urv.ptr) == 0
  129. case reflect.Interface:
  130. isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
  131. if deref {
  132. if isnil {
  133. return true
  134. }
  135. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  136. }
  137. return isnil
  138. case reflect.Ptr:
  139. isnil := urv.ptr == nil
  140. if deref {
  141. if isnil {
  142. return true
  143. }
  144. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  145. }
  146. return isnil
  147. case reflect.Struct:
  148. return isEmptyStruct(v, tinfos, deref, checkStruct)
  149. case reflect.Map, reflect.Array, reflect.Chan:
  150. return v.Len() == 0
  151. }
  152. return false
  153. }
  154. // --------------------------
  155. type atomicTypeInfoSlice struct { // expected to be 2 words
  156. l int64 // length of the data array (must be first in struct, for 64-bit alignment necessary for 386)
  157. v unsafe.Pointer // data array - Pointer (not uintptr) to maintain GC reference
  158. }
  159. func (x *atomicTypeInfoSlice) load() []rtid2ti {
  160. l := int(atomic.LoadInt64(&x.l))
  161. if l == 0 {
  162. return nil
  163. }
  164. return *(*[]rtid2ti)(unsafe.Pointer(&unsafeSlice{Data: atomic.LoadPointer(&x.v), Len: l, Cap: l}))
  165. }
  166. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  167. s := (*unsafeSlice)(unsafe.Pointer(&p))
  168. atomic.StorePointer(&x.v, s.Data)
  169. atomic.StoreInt64(&x.l, int64(s.Len))
  170. }
  171. // --------------------------
  172. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  173. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  174. *(*[]byte)(urv.ptr) = d.rawBytes()
  175. }
  176. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  177. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  178. *(*string)(urv.ptr) = d.d.DecodeString()
  179. }
  180. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  181. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  182. *(*bool)(urv.ptr) = d.d.DecodeBool()
  183. }
  184. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  185. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  186. *(*time.Time)(urv.ptr) = d.d.DecodeTime()
  187. }
  188. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  189. fv := d.d.DecodeFloat64()
  190. if chkOvf.Float32(fv) {
  191. d.errorf("float32 overflow: %v", fv)
  192. }
  193. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  194. *(*float32)(urv.ptr) = float32(fv)
  195. }
  196. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  197. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  198. *(*float64)(urv.ptr) = d.d.DecodeFloat64()
  199. }
  200. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  201. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  202. *(*int)(urv.ptr) = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  203. }
  204. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  205. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  206. *(*int8)(urv.ptr) = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
  207. }
  208. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  209. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  210. *(*int16)(urv.ptr) = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
  211. }
  212. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  213. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  214. *(*int32)(urv.ptr) = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
  215. }
  216. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  217. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  218. *(*int64)(urv.ptr) = d.d.DecodeInt64()
  219. }
  220. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  221. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  222. *(*uint)(urv.ptr) = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  223. }
  224. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  225. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  226. *(*uintptr)(urv.ptr) = uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  227. }
  228. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  229. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  230. *(*uint8)(urv.ptr) = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
  231. }
  232. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  233. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  234. *(*uint16)(urv.ptr) = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
  235. }
  236. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  237. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  238. *(*uint32)(urv.ptr) = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
  239. }
  240. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  241. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  242. *(*uint64)(urv.ptr) = d.d.DecodeUint64()
  243. }
  244. // ------------
  245. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  246. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  247. e.e.EncodeBool(*(*bool)(v.ptr))
  248. }
  249. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  250. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  251. e.e.EncodeTime(*(*time.Time)(v.ptr))
  252. }
  253. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  254. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  255. e.e.EncodeString(cUTF8, *(*string)(v.ptr))
  256. }
  257. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  258. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  259. e.e.EncodeFloat64(*(*float64)(v.ptr))
  260. }
  261. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  262. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  263. e.e.EncodeFloat32(*(*float32)(v.ptr))
  264. }
  265. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  266. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  267. e.e.EncodeInt(int64(*(*int)(v.ptr)))
  268. }
  269. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  270. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  271. e.e.EncodeInt(int64(*(*int8)(v.ptr)))
  272. }
  273. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  274. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  275. e.e.EncodeInt(int64(*(*int16)(v.ptr)))
  276. }
  277. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  278. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  279. e.e.EncodeInt(int64(*(*int32)(v.ptr)))
  280. }
  281. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  282. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  283. e.e.EncodeInt(int64(*(*int64)(v.ptr)))
  284. }
  285. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  286. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  287. e.e.EncodeUint(uint64(*(*uint)(v.ptr)))
  288. }
  289. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  290. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  291. e.e.EncodeUint(uint64(*(*uint8)(v.ptr)))
  292. }
  293. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  294. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  295. e.e.EncodeUint(uint64(*(*uint16)(v.ptr)))
  296. }
  297. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  298. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  299. e.e.EncodeUint(uint64(*(*uint32)(v.ptr)))
  300. }
  301. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  302. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  303. e.e.EncodeUint(uint64(*(*uint64)(v.ptr)))
  304. }
  305. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  306. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  307. e.e.EncodeUint(uint64(*(*uintptr)(v.ptr)))
  308. }
  309. // ------------
  310. // func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  311. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  312. // // if urv.flag&unsafeFlagIndir != 0 {
  313. // // urv.ptr = *(*unsafe.Pointer)(urv.ptr)
  314. // // }
  315. // *(*[]byte)(urv.ptr) = d.rawBytes()
  316. // }
  317. // func rv0t(rt reflect.Type) reflect.Value {
  318. // ut := (*unsafeIntf)(unsafe.Pointer(&rt))
  319. // // we need to determine whether ifaceIndir, and then whether to just pass 0 as the ptr
  320. // uv := unsafeReflectValue{ut.word, &zeroRTv, flag(rt.Kind())}
  321. // return *(*reflect.Value)(unsafe.Pointer(&uv})
  322. // }
  323. // func rv2i(rv reflect.Value) interface{} {
  324. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  325. // // true references (map, func, chan, ptr - NOT slice) may be double-referenced as flagIndir
  326. // var ptr unsafe.Pointer
  327. // // kk := reflect.Kind(urv.flag & (1<<5 - 1))
  328. // // if (kk == reflect.Map || kk == reflect.Ptr || kk == reflect.Chan || kk == reflect.Func) && urv.flag&unsafeFlagIndir != 0 {
  329. // if refBitset.isset(byte(urv.flag&(1<<5-1))) && urv.flag&unsafeFlagIndir != 0 {
  330. // ptr = *(*unsafe.Pointer)(urv.ptr)
  331. // } else {
  332. // ptr = urv.ptr
  333. // }
  334. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{typ: urv.typ, word: ptr}))
  335. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  336. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  337. // }
  338. // func definitelyNil(v interface{}) bool {
  339. // var ui *unsafeIntf = (*unsafeIntf)(unsafe.Pointer(&v))
  340. // if ui.word == nil {
  341. // return true
  342. // }
  343. // var tk = reflect.TypeOf(v).Kind()
  344. // return (tk == reflect.Interface || tk == reflect.Slice) && *(*unsafe.Pointer)(ui.word) == nil
  345. // fmt.Printf(">>>> definitely nil: isnil: %v, TYPE: \t%T, word: %v, *word: %v, type: %v, nil: %v\n",
  346. // v == nil, v, word, *((*unsafe.Pointer)(word)), ui.typ, nil)
  347. // }
  348. // func keepAlive4BytesView(v string) {
  349. // runtime.KeepAlive(v)
  350. // }
  351. // func keepAlive4StringView(v []byte) {
  352. // runtime.KeepAlive(v)
  353. // }
  354. // func rt2id(rt reflect.Type) uintptr {
  355. // return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  356. // // var i interface{} = rt
  357. // // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  358. // // return ((*unsafeIntf)(unsafe.Pointer(&i))).word
  359. // }
  360. // func rv2i(rv reflect.Value) interface{} {
  361. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  362. // // non-reference type: already indir
  363. // // reference type: depend on flagIndir property ('cos maybe was double-referenced)
  364. // // const (unsafeRvFlagKindMask = 1<<5 - 1 , unsafeRvFlagIndir = 1 << 7 )
  365. // // rvk := reflect.Kind(urv.flag & (1<<5 - 1))
  366. // // if (rvk == reflect.Chan ||
  367. // // rvk == reflect.Func ||
  368. // // rvk == reflect.Interface ||
  369. // // rvk == reflect.Map ||
  370. // // rvk == reflect.Ptr ||
  371. // // rvk == reflect.UnsafePointer) && urv.flag&(1<<8) != 0 {
  372. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  373. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  374. // // }
  375. // if urv.flag&(1<<5-1) == uintptr(reflect.Map) && urv.flag&(1<<7) != 0 {
  376. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  377. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  378. // }
  379. // // fmt.Printf(">>>>> ++++ direct reference: %v, %v\n", rvk, rv.Type())
  380. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  381. // }
  382. // const (
  383. // unsafeRvFlagKindMask = 1<<5 - 1
  384. // unsafeRvKindDirectIface = 1 << 5
  385. // unsafeRvFlagIndir = 1 << 7
  386. // unsafeRvFlagAddr = 1 << 8
  387. // unsafeRvFlagMethod = 1 << 9
  388. // _USE_RV_INTERFACE bool = false
  389. // _UNSAFE_RV_DEBUG = true
  390. // )
  391. // type unsafeRtype struct {
  392. // _ [2]uintptr
  393. // _ uint32
  394. // _ uint8
  395. // _ uint8
  396. // _ uint8
  397. // kind uint8
  398. // _ [2]uintptr
  399. // _ int32
  400. // }
  401. // func _rv2i(rv reflect.Value) interface{} {
  402. // // Note: From use,
  403. // // - it's never an interface
  404. // // - the only calls here are for ifaceIndir types.
  405. // // (though that conditional is wrong)
  406. // // To know for sure, we need the value of t.kind (which is not exposed).
  407. // //
  408. // // Need to validate the path: type is indirect ==> only value is indirect ==> default (value is direct)
  409. // // - Type indirect, Value indirect: ==> numbers, boolean, slice, struct, array, string
  410. // // - Type Direct, Value indirect: ==> map???
  411. // // - Type Direct, Value direct: ==> pointers, unsafe.Pointer, func, chan, map
  412. // //
  413. // // TRANSLATES TO:
  414. // // if typeIndirect { } else if valueIndirect { } else { }
  415. // //
  416. // // Since we don't deal with funcs, then "flagNethod" is unset, and can be ignored.
  417. // if _USE_RV_INTERFACE {
  418. // return rv.Interface()
  419. // }
  420. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  421. // // if urv.flag&unsafeRvFlagMethod != 0 || urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  422. // // println("***** IS flag method or interface: delegating to rv.Interface()")
  423. // // return rv.Interface()
  424. // // }
  425. // // if urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  426. // // println("***** IS Interface: delegate to rv.Interface")
  427. // // return rv.Interface()
  428. // // }
  429. // // if urv.flag&unsafeRvFlagKindMask&unsafeRvKindDirectIface == 0 {
  430. // // if urv.flag&unsafeRvFlagAddr == 0 {
  431. // // println("***** IS ifaceIndir typ")
  432. // // // ui := unsafeIntf{word: urv.ptr, typ: urv.typ}
  433. // // // return *(*interface{})(unsafe.Pointer(&ui))
  434. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  435. // // }
  436. // // } else if urv.flag&unsafeRvFlagIndir != 0 {
  437. // // println("***** IS flagindir")
  438. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  439. // // } else {
  440. // // println("***** NOT flagindir")
  441. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  442. // // }
  443. // // println("***** default: delegate to rv.Interface")
  444. // urt := (*unsafeRtype)(unsafe.Pointer(urv.typ))
  445. // if _UNSAFE_RV_DEBUG {
  446. // fmt.Printf(">>>> start: %v: ", rv.Type())
  447. // fmt.Printf("%v - %v\n", *urv, *urt)
  448. // }
  449. // if urt.kind&unsafeRvKindDirectIface == 0 {
  450. // if _UNSAFE_RV_DEBUG {
  451. // fmt.Printf("**** +ifaceIndir type: %v\n", rv.Type())
  452. // }
  453. // // println("***** IS ifaceIndir typ")
  454. // // if true || urv.flag&unsafeRvFlagAddr == 0 {
  455. // // // println(" ***** IS NOT addr")
  456. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  457. // // }
  458. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  459. // if _UNSAFE_RV_DEBUG {
  460. // fmt.Printf("**** +flagIndir type: %v\n", rv.Type())
  461. // }
  462. // // println("***** IS flagindir")
  463. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  464. // } else {
  465. // if _UNSAFE_RV_DEBUG {
  466. // fmt.Printf("**** -flagIndir type: %v\n", rv.Type())
  467. // }
  468. // // println("***** NOT flagindir")
  469. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  470. // }
  471. // // println("***** default: delegating to rv.Interface()")
  472. // // return rv.Interface()
  473. // }
  474. // var staticM0 = make(map[string]uint64)
  475. // var staticI0 = (int32)(-5)
  476. // func staticRv2iTest() {
  477. // i0 := (int32)(-5)
  478. // m0 := make(map[string]uint16)
  479. // m0["1"] = 1
  480. // for _, i := range []interface{}{
  481. // (int)(7),
  482. // (uint)(8),
  483. // (int16)(-9),
  484. // (uint16)(19),
  485. // (uintptr)(77),
  486. // (bool)(true),
  487. // float32(-32.7),
  488. // float64(64.9),
  489. // complex(float32(19), 5),
  490. // complex(float64(-32), 7),
  491. // [4]uint64{1, 2, 3, 4},
  492. // (chan<- int)(nil), // chan,
  493. // rv2i, // func
  494. // io.Writer(ioutil.Discard),
  495. // make(map[string]uint),
  496. // (map[string]uint)(nil),
  497. // staticM0,
  498. // m0,
  499. // &m0,
  500. // i0,
  501. // &i0,
  502. // &staticI0,
  503. // &staticM0,
  504. // []uint32{6, 7, 8},
  505. // "abc",
  506. // Raw{},
  507. // RawExt{},
  508. // &Raw{},
  509. // &RawExt{},
  510. // unsafe.Pointer(&i0),
  511. // } {
  512. // i2 := rv2i(reflect.ValueOf(i))
  513. // eq := reflect.DeepEqual(i, i2)
  514. // fmt.Printf(">>>> %v == %v? %v\n", i, i2, eq)
  515. // }
  516. // // os.Exit(0)
  517. // }
  518. // func init() {
  519. // staticRv2iTest()
  520. // }
  521. // func rv2i(rv reflect.Value) interface{} {
  522. // if _USE_RV_INTERFACE || rv.Kind() == reflect.Interface || rv.CanAddr() {
  523. // return rv.Interface()
  524. // }
  525. // // var i interface{}
  526. // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  527. // var ui unsafeIntf
  528. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  529. // // fmt.Printf("urv: flag: %b, typ: %b, ptr: %b\n", urv.flag, uintptr(urv.typ), uintptr(urv.ptr))
  530. // if (urv.flag&unsafeRvFlagKindMask)&unsafeRvKindDirectIface == 0 {
  531. // if urv.flag&unsafeRvFlagAddr != 0 {
  532. // println("***** indirect and addressable! Needs typed move - delegate to rv.Interface()")
  533. // return rv.Interface()
  534. // }
  535. // println("****** indirect type/kind")
  536. // ui.word = urv.ptr
  537. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  538. // println("****** unsafe rv flag indir")
  539. // ui.word = *(*unsafe.Pointer)(urv.ptr)
  540. // } else {
  541. // println("****** default: assign prt to word directly")
  542. // ui.word = urv.ptr
  543. // }
  544. // // ui.word = urv.ptr
  545. // ui.typ = urv.typ
  546. // // fmt.Printf("(pointers) ui.typ: %p, word: %p\n", ui.typ, ui.word)
  547. // // fmt.Printf("(binary) ui.typ: %b, word: %b\n", uintptr(ui.typ), uintptr(ui.word))
  548. // return *(*interface{})(unsafe.Pointer(&ui))
  549. // // return i
  550. // }