Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type unsafeType struct { |
| 9 | safeType |
| 10 | rtype unsafe.Pointer |
| 11 | ptrRType unsafe.Pointer |
| 12 | } |
| 13 | |
| 14 | func newUnsafeType(cfg *frozenConfig, type1 reflect.Type) *unsafeType { |
| 15 | return &unsafeType{ |
| 16 | safeType: safeType{ |
| 17 | Type: type1, |
| 18 | cfg: cfg, |
| 19 | }, |
| 20 | rtype: unpackEFace(type1).data, |
| 21 | ptrRType: unpackEFace(reflect.PtrTo(type1)).data, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func (type2 *unsafeType) Set(obj interface{}, val interface{}) { |
| 26 | objEFace := unpackEFace(obj) |
| 27 | assertType("Type.Set argument 1", type2.ptrRType, objEFace.rtype) |
| 28 | valEFace := unpackEFace(val) |
| 29 | assertType("Type.Set argument 2", type2.ptrRType, valEFace.rtype) |
| 30 | type2.UnsafeSet(objEFace.data, valEFace.data) |
| 31 | } |
| 32 | |
| 33 | func (type2 *unsafeType) UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer) { |
| 34 | typedmemmove(type2.rtype, ptr, val) |
| 35 | } |
| 36 | |
| 37 | func (type2 *unsafeType) IsNil(obj interface{}) bool { |
| 38 | objEFace := unpackEFace(obj) |
| 39 | assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype) |
| 40 | return type2.UnsafeIsNil(objEFace.data) |
| 41 | } |
| 42 | |
| 43 | func (type2 *unsafeType) UnsafeIsNil(ptr unsafe.Pointer) bool { |
| 44 | return ptr == nil |
| 45 | } |
| 46 | |
| 47 | func (type2 *unsafeType) UnsafeNew() unsafe.Pointer { |
| 48 | return unsafe_New(type2.rtype) |
| 49 | } |
| 50 | |
| 51 | func (type2 *unsafeType) New() interface{} { |
| 52 | return packEFace(type2.ptrRType, type2.UnsafeNew()) |
| 53 | } |
| 54 | |
| 55 | func (type2 *unsafeType) PackEFace(ptr unsafe.Pointer) interface{} { |
| 56 | return packEFace(type2.ptrRType, ptr) |
| 57 | } |
| 58 | |
| 59 | func (type2 *unsafeType) RType() uintptr { |
| 60 | return uintptr(type2.rtype) |
| 61 | } |
| 62 | |
| 63 | func (type2 *unsafeType) Indirect(obj interface{}) interface{} { |
| 64 | objEFace := unpackEFace(obj) |
| 65 | assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype) |
| 66 | return type2.UnsafeIndirect(objEFace.data) |
| 67 | } |
| 68 | |
| 69 | func (type2 *unsafeType) UnsafeIndirect(obj unsafe.Pointer) interface{} { |
| 70 | return packEFace(type2.rtype, obj) |
| 71 | } |
| 72 | |
| 73 | func (type2 *unsafeType) LikePtr() bool { |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | func assertType(where string, expectRType unsafe.Pointer, actualRType unsafe.Pointer) { |
| 78 | if expectRType != actualRType { |
| 79 | expectType := reflect.TypeOf(0) |
| 80 | (*iface)(unsafe.Pointer(&expectType)).data = expectRType |
| 81 | actualType := reflect.TypeOf(0) |
| 82 | (*iface)(unsafe.Pointer(&actualType)).data = actualRType |
| 83 | panic(where + ": expect " + expectType.String() + ", actual " + actualType.String()) |
| 84 | } |
| 85 | } |