khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type UnsafeStructField struct { |
| 9 | reflect.StructField |
| 10 | structType *UnsafeStructType |
| 11 | rtype unsafe.Pointer |
| 12 | ptrRType unsafe.Pointer |
| 13 | } |
| 14 | |
| 15 | func newUnsafeStructField(structType *UnsafeStructType, structField reflect.StructField) *UnsafeStructField { |
| 16 | return &UnsafeStructField{ |
| 17 | StructField: structField, |
| 18 | rtype: unpackEFace(structField.Type).data, |
| 19 | ptrRType: unpackEFace(reflect.PtrTo(structField.Type)).data, |
| 20 | structType: structType, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (field *UnsafeStructField) Offset() uintptr { |
| 25 | return field.StructField.Offset |
| 26 | } |
| 27 | |
| 28 | func (field *UnsafeStructField) Name() string { |
| 29 | return field.StructField.Name |
| 30 | } |
| 31 | |
| 32 | func (field *UnsafeStructField) PkgPath() string { |
| 33 | return field.StructField.PkgPath |
| 34 | } |
| 35 | |
| 36 | func (field *UnsafeStructField) Type() Type { |
| 37 | return field.structType.cfg.Type2(field.StructField.Type) |
| 38 | } |
| 39 | |
| 40 | func (field *UnsafeStructField) Tag() reflect.StructTag { |
| 41 | return field.StructField.Tag |
| 42 | } |
| 43 | |
| 44 | func (field *UnsafeStructField) Index() []int { |
| 45 | return field.StructField.Index |
| 46 | } |
| 47 | |
| 48 | func (field *UnsafeStructField) Anonymous() bool { |
| 49 | return field.StructField.Anonymous |
| 50 | } |
| 51 | |
| 52 | func (field *UnsafeStructField) Set(obj interface{}, value interface{}) { |
| 53 | objEFace := unpackEFace(obj) |
| 54 | assertType("StructField.SetIndex argument 1", field.structType.ptrRType, objEFace.rtype) |
| 55 | valueEFace := unpackEFace(value) |
| 56 | assertType("StructField.SetIndex argument 2", field.ptrRType, valueEFace.rtype) |
| 57 | field.UnsafeSet(objEFace.data, valueEFace.data) |
| 58 | } |
| 59 | |
| 60 | func (field *UnsafeStructField) UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer) { |
| 61 | fieldPtr := add(obj, field.StructField.Offset, "same as non-reflect &v.field") |
| 62 | typedmemmove(field.rtype, fieldPtr, value) |
| 63 | } |
| 64 | |
| 65 | func (field *UnsafeStructField) Get(obj interface{}) interface{} { |
| 66 | objEFace := unpackEFace(obj) |
| 67 | assertType("StructField.GetIndex argument 1", field.structType.ptrRType, objEFace.rtype) |
| 68 | value := field.UnsafeGet(objEFace.data) |
| 69 | return packEFace(field.ptrRType, value) |
| 70 | } |
| 71 | |
| 72 | func (field *UnsafeStructField) UnsafeGet(obj unsafe.Pointer) unsafe.Pointer { |
| 73 | return add(obj, field.StructField.Offset, "same as non-reflect &v.field") |
| 74 | } |