http urls monitor.

helper_unsafe.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 (not sufficient, as a pointer value encodes the type)
  140. isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
  141. if deref {
  142. if isnil {
  143. return true
  144. }
  145. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  146. }
  147. return isnil
  148. case reflect.Struct:
  149. return isEmptyStruct(v, tinfos, deref, checkStruct)
  150. case reflect.Map, reflect.Array, reflect.Chan:
  151. return v.Len() == 0
  152. }
  153. return false
  154. }
  155. // --------------------------
  156. // atomicTypeInfoSlice contains length and pointer to the array for a slice.
  157. // It is expected to be 2 words.
  158. //
  159. // Previously, we atomically loaded and stored the length and array pointer separately,
  160. // which could lead to some races.
  161. // We now just atomically store and load the pointer to the value directly.
  162. type atomicTypeInfoSlice struct { // expected to be 2 words
  163. l int // length of the data array (must be first in struct, for 64-bit alignment necessary for 386)
  164. v unsafe.Pointer // data array - Pointer (not uintptr) to maintain GC reference
  165. }
  166. func (x *atomicTypeInfoSlice) load() []rtid2ti {
  167. xp := unsafe.Pointer(x)
  168. x2 := *(*atomicTypeInfoSlice)(atomic.LoadPointer(&xp))
  169. if x2.l == 0 {
  170. return nil
  171. }
  172. return *(*[]rtid2ti)(unsafe.Pointer(&unsafeSlice{Data: x2.v, Len: x2.l, Cap: x2.l}))
  173. }
  174. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  175. s := (*unsafeSlice)(unsafe.Pointer(&p))
  176. xp := unsafe.Pointer(x)
  177. atomic.StorePointer(&xp, unsafe.Pointer(&atomicTypeInfoSlice{l: s.Len, v: s.Data}))
  178. }
  179. // --------------------------
  180. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  181. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  182. *(*[]byte)(urv.ptr) = d.rawBytes()
  183. }
  184. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  185. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  186. *(*string)(urv.ptr) = d.d.DecodeString()
  187. }
  188. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  189. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  190. *(*bool)(urv.ptr) = d.d.DecodeBool()
  191. }
  192. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  193. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  194. *(*time.Time)(urv.ptr) = d.d.DecodeTime()
  195. }
  196. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  197. fv := d.d.DecodeFloat64()
  198. if chkOvf.Float32(fv) {
  199. d.errorf("float32 overflow: %v", fv)
  200. }
  201. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  202. *(*float32)(urv.ptr) = float32(fv)
  203. }
  204. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  205. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  206. *(*float64)(urv.ptr) = d.d.DecodeFloat64()
  207. }
  208. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  209. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  210. *(*int)(urv.ptr) = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  211. }
  212. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  213. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  214. *(*int8)(urv.ptr) = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
  215. }
  216. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  217. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  218. *(*int16)(urv.ptr) = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
  219. }
  220. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  221. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  222. *(*int32)(urv.ptr) = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
  223. }
  224. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  225. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  226. *(*int64)(urv.ptr) = d.d.DecodeInt64()
  227. }
  228. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  229. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  230. *(*uint)(urv.ptr) = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  231. }
  232. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  233. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  234. *(*uintptr)(urv.ptr) = uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  235. }
  236. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  237. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  238. *(*uint8)(urv.ptr) = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
  239. }
  240. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  241. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  242. *(*uint16)(urv.ptr) = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
  243. }
  244. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  245. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  246. *(*uint32)(urv.ptr) = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
  247. }
  248. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  249. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  250. *(*uint64)(urv.ptr) = d.d.DecodeUint64()
  251. }
  252. // ------------
  253. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  254. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  255. e.e.EncodeBool(*(*bool)(v.ptr))
  256. }
  257. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  258. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  259. e.e.EncodeTime(*(*time.Time)(v.ptr))
  260. }
  261. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  262. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  263. e.e.EncodeString(cUTF8, *(*string)(v.ptr))
  264. }
  265. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  266. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  267. e.e.EncodeFloat64(*(*float64)(v.ptr))
  268. }
  269. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  270. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  271. e.e.EncodeFloat32(*(*float32)(v.ptr))
  272. }
  273. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  274. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  275. e.e.EncodeInt(int64(*(*int)(v.ptr)))
  276. }
  277. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  278. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  279. e.e.EncodeInt(int64(*(*int8)(v.ptr)))
  280. }
  281. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  282. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  283. e.e.EncodeInt(int64(*(*int16)(v.ptr)))
  284. }
  285. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  286. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  287. e.e.EncodeInt(int64(*(*int32)(v.ptr)))
  288. }
  289. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  290. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  291. e.e.EncodeInt(int64(*(*int64)(v.ptr)))
  292. }
  293. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  294. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  295. e.e.EncodeUint(uint64(*(*uint)(v.ptr)))
  296. }
  297. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  298. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  299. e.e.EncodeUint(uint64(*(*uint8)(v.ptr)))
  300. }
  301. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  302. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  303. e.e.EncodeUint(uint64(*(*uint16)(v.ptr)))
  304. }
  305. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  306. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  307. e.e.EncodeUint(uint64(*(*uint32)(v.ptr)))
  308. }
  309. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  310. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  311. e.e.EncodeUint(uint64(*(*uint64)(v.ptr)))
  312. }
  313. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  314. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  315. e.e.EncodeUint(uint64(*(*uintptr)(v.ptr)))
  316. }
  317. // ------------
  318. // func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  319. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  320. // // if urv.flag&unsafeFlagIndir != 0 {
  321. // // urv.ptr = *(*unsafe.Pointer)(urv.ptr)
  322. // // }
  323. // *(*[]byte)(urv.ptr) = d.rawBytes()
  324. // }
  325. // func rv0t(rt reflect.Type) reflect.Value {
  326. // ut := (*unsafeIntf)(unsafe.Pointer(&rt))
  327. // // we need to determine whether ifaceIndir, and then whether to just pass 0 as the ptr
  328. // uv := unsafeReflectValue{ut.word, &zeroRTv, flag(rt.Kind())}
  329. // return *(*reflect.Value)(unsafe.Pointer(&uv})
  330. // }
  331. // func rv2i(rv reflect.Value) interface{} {
  332. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  333. // // true references (map, func, chan, ptr - NOT slice) may be double-referenced as flagIndir
  334. // var ptr unsafe.Pointer
  335. // // kk := reflect.Kind(urv.flag & (1<<5 - 1))
  336. // // if (kk == reflect.Map || kk == reflect.Ptr || kk == reflect.Chan || kk == reflect.Func) && urv.flag&unsafeFlagIndir != 0 {
  337. // if refBitset.isset(byte(urv.flag&(1<<5-1))) && urv.flag&unsafeFlagIndir != 0 {
  338. // ptr = *(*unsafe.Pointer)(urv.ptr)
  339. // } else {
  340. // ptr = urv.ptr
  341. // }
  342. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{typ: urv.typ, word: ptr}))
  343. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  344. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  345. // }
  346. // func definitelyNil(v interface{}) bool {
  347. // var ui *unsafeIntf = (*unsafeIntf)(unsafe.Pointer(&v))
  348. // if ui.word == nil {
  349. // return true
  350. // }
  351. // var tk = reflect.TypeOf(v).Kind()
  352. // return (tk == reflect.Interface || tk == reflect.Slice) && *(*unsafe.Pointer)(ui.word) == nil
  353. // fmt.Printf(">>>> definitely nil: isnil: %v, TYPE: \t%T, word: %v, *word: %v, type: %v, nil: %v\n",
  354. // v == nil, v, word, *((*unsafe.Pointer)(word)), ui.typ, nil)
  355. // }
  356. // func keepAlive4BytesView(v string) {
  357. // runtime.KeepAlive(v)
  358. // }
  359. // func keepAlive4StringView(v []byte) {
  360. // runtime.KeepAlive(v)
  361. // }
  362. // func rt2id(rt reflect.Type) uintptr {
  363. // return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  364. // // var i interface{} = rt
  365. // // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  366. // // return ((*unsafeIntf)(unsafe.Pointer(&i))).word
  367. // }
  368. // func rv2i(rv reflect.Value) interface{} {
  369. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  370. // // non-reference type: already indir
  371. // // reference type: depend on flagIndir property ('cos maybe was double-referenced)
  372. // // const (unsafeRvFlagKindMask = 1<<5 - 1 , unsafeRvFlagIndir = 1 << 7 )
  373. // // rvk := reflect.Kind(urv.flag & (1<<5 - 1))
  374. // // if (rvk == reflect.Chan ||
  375. // // rvk == reflect.Func ||
  376. // // rvk == reflect.Interface ||
  377. // // rvk == reflect.Map ||
  378. // // rvk == reflect.Ptr ||
  379. // // rvk == reflect.UnsafePointer) && urv.flag&(1<<8) != 0 {
  380. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  381. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  382. // // }
  383. // if urv.flag&(1<<5-1) == uintptr(reflect.Map) && urv.flag&(1<<7) != 0 {
  384. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  385. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  386. // }
  387. // // fmt.Printf(">>>>> ++++ direct reference: %v, %v\n", rvk, rv.Type())
  388. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  389. // }
  390. // const (
  391. // unsafeRvFlagKindMask = 1<<5 - 1
  392. // unsafeRvKindDirectIface = 1 << 5
  393. // unsafeRvFlagIndir = 1 << 7
  394. // unsafeRvFlagAddr = 1 << 8
  395. // unsafeRvFlagMethod = 1 << 9
  396. // _USE_RV_INTERFACE bool = false
  397. // _UNSAFE_RV_DEBUG = true
  398. // )
  399. // type unsafeRtype struct {
  400. // _ [2]uintptr
  401. // _ uint32
  402. // _ uint8
  403. // _ uint8
  404. // _ uint8
  405. // kind uint8
  406. // _ [2]uintptr
  407. // _ int32
  408. // }
  409. // func _rv2i(rv reflect.Value) interface{} {
  410. // // Note: From use,
  411. // // - it's never an interface
  412. // // - the only calls here are for ifaceIndir types.
  413. // // (though that conditional is wrong)
  414. // // To know for sure, we need the value of t.kind (which is not exposed).
  415. // //
  416. // // Need to validate the path: type is indirect ==> only value is indirect ==> default (value is direct)
  417. // // - Type indirect, Value indirect: ==> numbers, boolean, slice, struct, array, string
  418. // // - Type Direct, Value indirect: ==> map???
  419. // // - Type Direct, Value direct: ==> pointers, unsafe.Pointer, func, chan, map
  420. // //
  421. // // TRANSLATES TO:
  422. // // if typeIndirect { } else if valueIndirect { } else { }
  423. // //
  424. // // Since we don't deal with funcs, then "flagNethod" is unset, and can be ignored.
  425. // if _USE_RV_INTERFACE {
  426. // return rv.Interface()
  427. // }
  428. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  429. // // if urv.flag&unsafeRvFlagMethod != 0 || urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  430. // // println("***** IS flag method or interface: delegating to rv.Interface()")
  431. // // return rv.Interface()
  432. // // }
  433. // // if urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  434. // // println("***** IS Interface: delegate to rv.Interface")
  435. // // return rv.Interface()
  436. // // }
  437. // // if urv.flag&unsafeRvFlagKindMask&unsafeRvKindDirectIface == 0 {
  438. // // if urv.flag&unsafeRvFlagAddr == 0 {
  439. // // println("***** IS ifaceIndir typ")
  440. // // // ui := unsafeIntf{word: urv.ptr, typ: urv.typ}
  441. // // // return *(*interface{})(unsafe.Pointer(&ui))
  442. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  443. // // }
  444. // // } else if urv.flag&unsafeRvFlagIndir != 0 {
  445. // // println("***** IS flagindir")
  446. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  447. // // } else {
  448. // // println("***** NOT flagindir")
  449. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  450. // // }
  451. // // println("***** default: delegate to rv.Interface")
  452. // urt := (*unsafeRtype)(unsafe.Pointer(urv.typ))
  453. // if _UNSAFE_RV_DEBUG {
  454. // fmt.Printf(">>>> start: %v: ", rv.Type())
  455. // fmt.Printf("%v - %v\n", *urv, *urt)
  456. // }
  457. // if urt.kind&unsafeRvKindDirectIface == 0 {
  458. // if _UNSAFE_RV_DEBUG {
  459. // fmt.Printf("**** +ifaceIndir type: %v\n", rv.Type())
  460. // }
  461. // // println("***** IS ifaceIndir typ")
  462. // // if true || urv.flag&unsafeRvFlagAddr == 0 {
  463. // // // println(" ***** IS NOT addr")
  464. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  465. // // }
  466. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  467. // if _UNSAFE_RV_DEBUG {
  468. // fmt.Printf("**** +flagIndir type: %v\n", rv.Type())
  469. // }
  470. // // println("***** IS flagindir")
  471. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  472. // } else {
  473. // if _UNSAFE_RV_DEBUG {
  474. // fmt.Printf("**** -flagIndir type: %v\n", rv.Type())
  475. // }
  476. // // println("***** NOT flagindir")
  477. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  478. // }
  479. // // println("***** default: delegating to rv.Interface()")
  480. // // return rv.Interface()
  481. // }
  482. // var staticM0 = make(map[string]uint64)
  483. // var staticI0 = (int32)(-5)
  484. // func staticRv2iTest() {
  485. // i0 := (int32)(-5)
  486. // m0 := make(map[string]uint16)
  487. // m0["1"] = 1
  488. // for _, i := range []interface{}{
  489. // (int)(7),
  490. // (uint)(8),
  491. // (int16)(-9),
  492. // (uint16)(19),
  493. // (uintptr)(77),
  494. // (bool)(true),
  495. // float32(-32.7),
  496. // float64(64.9),
  497. // complex(float32(19), 5),
  498. // complex(float64(-32), 7),
  499. // [4]uint64{1, 2, 3, 4},
  500. // (chan<- int)(nil), // chan,
  501. // rv2i, // func
  502. // io.Writer(ioutil.Discard),
  503. // make(map[string]uint),
  504. // (map[string]uint)(nil),
  505. // staticM0,
  506. // m0,
  507. // &m0,
  508. // i0,
  509. // &i0,
  510. // &staticI0,
  511. // &staticM0,
  512. // []uint32{6, 7, 8},
  513. // "abc",
  514. // Raw{},
  515. // RawExt{},
  516. // &Raw{},
  517. // &RawExt{},
  518. // unsafe.Pointer(&i0),
  519. // } {
  520. // i2 := rv2i(reflect.ValueOf(i))
  521. // eq := reflect.DeepEqual(i, i2)
  522. // fmt.Printf(">>>> %v == %v? %v\n", i, i2, eq)
  523. // }
  524. // // os.Exit(0)
  525. // }
  526. // func init() {
  527. // staticRv2iTest()
  528. // }
  529. // func rv2i(rv reflect.Value) interface{} {
  530. // if _USE_RV_INTERFACE || rv.Kind() == reflect.Interface || rv.CanAddr() {
  531. // return rv.Interface()
  532. // }
  533. // // var i interface{}
  534. // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  535. // var ui unsafeIntf
  536. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  537. // // fmt.Printf("urv: flag: %b, typ: %b, ptr: %b\n", urv.flag, uintptr(urv.typ), uintptr(urv.ptr))
  538. // if (urv.flag&unsafeRvFlagKindMask)&unsafeRvKindDirectIface == 0 {
  539. // if urv.flag&unsafeRvFlagAddr != 0 {
  540. // println("***** indirect and addressable! Needs typed move - delegate to rv.Interface()")
  541. // return rv.Interface()
  542. // }
  543. // println("****** indirect type/kind")
  544. // ui.word = urv.ptr
  545. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  546. // println("****** unsafe rv flag indir")
  547. // ui.word = *(*unsafe.Pointer)(urv.ptr)
  548. // } else {
  549. // println("****** default: assign prt to word directly")
  550. // ui.word = urv.ptr
  551. // }
  552. // // ui.word = urv.ptr
  553. // ui.typ = urv.typ
  554. // // fmt.Printf("(pointers) ui.typ: %p, word: %p\n", ui.typ, ui.word)
  555. // // fmt.Printf("(binary) ui.typ: %b, word: %b\n", uintptr(ui.typ), uintptr(ui.word))
  556. // return *(*interface{})(unsafe.Pointer(&ui))
  557. // // return i
  558. // }