...
1
2
3
4
5 package reflectlite
6
7 import (
8 "unsafe"
9 )
10
11
12
13 func Field(v Value, i int) Value {
14 if v.kind() != Struct {
15 panic(&ValueError{"reflect.Value.Field", v.kind()})
16 }
17 tt := (*structType)(unsafe.Pointer(v.typ()))
18 if uint(i) >= uint(len(tt.Fields)) {
19 panic("reflect: Field index out of range")
20 }
21 field := &tt.Fields[i]
22 typ := field.Typ
23
24
25 fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
26
27 if !field.Name.IsExported() {
28 if field.Embedded() {
29 fl |= flagEmbedRO
30 } else {
31 fl |= flagStickyRO
32 }
33 }
34
35
36
37
38
39 ptr := add(v.ptr, field.Offset, "same as non-reflect &v.field")
40 return Value{typ, ptr, fl}
41 }
42
43 func TField(typ Type, i int) Type {
44 t := typ.(rtype)
45 if t.Kind() != Struct {
46 panic("reflect: Field of non-struct type")
47 }
48 tt := (*structType)(unsafe.Pointer(t.Type))
49
50 return StructFieldType(tt, i)
51 }
52
53
54 func StructFieldType(t *structType, i int) Type {
55 if i < 0 || i >= len(t.Fields) {
56 panic("reflect: Field index out of bounds")
57 }
58 p := &t.Fields[i]
59 return toType(p.Typ)
60 }
61
62
63
64
65
66
67 func Zero(typ Type) Value {
68 if typ == nil {
69 panic("reflect: Zero(nil)")
70 }
71 t := typ.common()
72 fl := flag(t.Kind())
73 if t.IfaceIndir() {
74 return Value{t, unsafe_New(t), fl | flagIndir}
75 }
76 return Value{t, nil, fl}
77 }
78
79
80
81
82
83
84
85
86 func ToInterface(v Value) (i any) {
87 return valueInterface(v)
88 }
89
90 type EmbedWithUnexpMeth struct{}
91
92 func (EmbedWithUnexpMeth) f() {}
93
94 type pinUnexpMeth interface {
95 f()
96 }
97
98 var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
99
100 func FirstMethodNameBytes(t Type) *byte {
101 _ = pinUnexpMethI
102
103 ut := t.uncommon()
104 if ut == nil {
105 panic("type has no methods")
106 }
107 m := ut.Methods()[0]
108 mname := t.(rtype).nameOff(m.Name)
109 if *mname.DataChecked(0, "name flag field")&(1<<2) == 0 {
110 panic("method name does not have pkgPath *string")
111 }
112 return mname.Bytes
113 }
114
115 type Buffer struct {
116 buf []byte
117 }
118
View as plain text