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))
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.(*rtype)
72 fl := flag(t.Kind())
73 if ifaceIndir(t) {
74 return Value{t, unsafe_New(t), fl | flagIndir}
75 }
76 return Value{t, nil, fl}
77 }
78
79
80
81
82
83
84 func ToInterface(v Value) (i any) {
85 return valueInterface(v)
86 }
87
88 type EmbedWithUnexpMeth struct{}
89
90 func (EmbedWithUnexpMeth) f() {}
91
92 type pinUnexpMeth interface {
93 f()
94 }
95
96 var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
97
98 func FirstMethodNameBytes(t Type) *byte {
99 _ = pinUnexpMethI
100
101 ut := t.uncommon()
102 if ut == nil {
103 panic("type has no methods")
104 }
105 m := ut.methods()[0]
106 mname := t.(*rtype).nameOff(m.name)
107 if *mname.data(0, "name flag field")&(1<<2) == 0 {
108 panic("method name does not have pkgPath *string")
109 }
110 return mname.bytes
111 }
112
113 type Buffer struct {
114 buf []byte
115 }
116
View as plain text