Source file
test/escape_reflect.go
1
2
3
4
5
6
7
8
9 package escape
10
11 import (
12 "reflect"
13 "unsafe"
14 )
15
16 var sink interface{}
17
18 func typ(x int) any {
19 v := reflect.ValueOf(x)
20 return v.Type()
21 }
22
23 func kind(x int) reflect.Kind {
24 v := reflect.ValueOf(x)
25 return v.Kind()
26 }
27
28 func int1(x int) int {
29 v := reflect.ValueOf(x)
30 return int(v.Int())
31 }
32
33 func ptr(x *int) *int {
34 v := reflect.ValueOf(x)
35 return (*int)(v.UnsafePointer())
36 }
37
38 func bytes1(x []byte) byte {
39 v := reflect.ValueOf(x)
40 return v.Bytes()[0]
41 }
42
43
44 func bytes2(x []byte) []byte {
45 v := reflect.ValueOf(x)
46 return v.Bytes()
47 }
48
49 func string1(x string) string {
50 v := reflect.ValueOf(x)
51 return v.String()
52 }
53
54 func string2(x int) string {
55 v := reflect.ValueOf(x)
56 return v.String()
57 }
58
59 func interface1(x any) any {
60 v := reflect.ValueOf(x)
61 return v.Interface()
62 }
63
64 func interface2(x int) any {
65 v := reflect.ValueOf(x)
66 return v.Interface()
67 }
68
69 func interface3(x int) int {
70 v := reflect.ValueOf(x)
71 return v.Interface().(int)
72 }
73
74 func interface4(x *int) any {
75 v := reflect.ValueOf(x)
76 return v.Interface()
77 }
78
79 func addr(x *int) reflect.Value {
80 v := reflect.ValueOf(x).Elem()
81 return v.Addr()
82 }
83
84
85 func uintptr1(x *int) uintptr {
86 v := reflect.ValueOf(x)
87 return v.Pointer()
88 }
89
90 func unsafeaddr(x *int) uintptr {
91 v := reflect.ValueOf(x).Elem()
92 return v.UnsafeAddr()
93 }
94
95 func ifacedata(x any) [2]uintptr {
96 v := reflect.ValueOf(&x).Elem()
97 return v.InterfaceData()
98 }
99
100 func can(x int) bool {
101 v := reflect.ValueOf(x)
102 return v.CanAddr() || v.CanInt() || v.CanSet() || v.CanInterface()
103 }
104
105 func is(x int) bool {
106 v := reflect.ValueOf(x)
107 return v.IsValid() || v.IsNil() || v.IsZero()
108 }
109
110 func is2(x [2]int) bool {
111 v := reflect.ValueOf(x)
112 return v.IsValid() || v.IsNil() || v.IsZero()
113 }
114
115 func is3(x struct{ a, b int }) bool {
116 v := reflect.ValueOf(x)
117 return v.IsValid() || v.IsNil() || v.IsZero()
118 }
119
120 func overflow(x int) bool {
121 v := reflect.ValueOf(x)
122 return v.OverflowInt(1 << 62)
123 }
124
125 func len1(x []int) int {
126 v := reflect.ValueOf(x)
127 return v.Len()
128 }
129
130 func len2(x [3]int) int {
131 v := reflect.ValueOf(x)
132 return v.Len()
133 }
134
135 func len3(x string) int {
136 v := reflect.ValueOf(x)
137 return v.Len()
138 }
139
140 func len4(x map[int]int) int {
141 v := reflect.ValueOf(x)
142 return v.Len()
143 }
144
145 func len5(x chan int) int {
146 v := reflect.ValueOf(x)
147 return v.Len()
148 }
149
150 func cap1(x []int) int {
151 v := reflect.ValueOf(x)
152 return v.Cap()
153 }
154
155 func cap2(x [3]int) int {
156 v := reflect.ValueOf(x)
157 return v.Cap()
158 }
159
160 func cap3(x chan int) int {
161 v := reflect.ValueOf(x)
162 return v.Cap()
163 }
164
165 func setlen(x *[]int, n int) {
166 v := reflect.ValueOf(x).Elem()
167 v.SetLen(n)
168 }
169
170 func setcap(x *[]int, n int) {
171 v := reflect.ValueOf(x).Elem()
172 v.SetCap(n)
173 }
174
175
176 func slice1(x []byte) []byte {
177 v := reflect.ValueOf(x)
178 return v.Slice(1, 2).Bytes()
179 }
180
181
182 func slice2(x string) string {
183 v := reflect.ValueOf(x)
184 return v.Slice(1, 2).String()
185 }
186
187 func slice3(x [10]byte) []byte {
188 v := reflect.ValueOf(x)
189 return v.Slice(1, 2).Bytes()
190 }
191
192 func elem1(x *int) int {
193 v := reflect.ValueOf(x)
194 return int(v.Elem().Int())
195 }
196
197 func elem2(x *string) string {
198 v := reflect.ValueOf(x)
199 return string(v.Elem().String())
200 }
201
202 type S struct {
203 A int
204 B *int
205 C string
206 }
207
208 func (S) M() {}
209
210 func field1(x S) int {
211 v := reflect.ValueOf(x)
212 return int(v.Field(0).Int())
213 }
214
215 func field2(x S) string {
216 v := reflect.ValueOf(x)
217 return v.Field(2).String()
218 }
219
220 func numfield(x S) int {
221 v := reflect.ValueOf(x)
222 return v.NumField()
223 }
224
225 func index1(x []int) int {
226 v := reflect.ValueOf(x)
227 return int(v.Index(0).Int())
228 }
229
230
231 func index2(x []string) string {
232 v := reflect.ValueOf(x)
233 return v.Index(0).String()
234 }
235
236 func index3(x [3]int) int {
237 v := reflect.ValueOf(x)
238 return int(v.Index(0).Int())
239 }
240
241 func index4(x [3]string) string {
242 v := reflect.ValueOf(x)
243 return v.Index(0).String()
244 }
245
246 func index5(x string) byte {
247 v := reflect.ValueOf(x)
248 return byte(v.Index(0).Uint())
249 }
250
251
252 func call1(f func(int), x int) {
253 fv := reflect.ValueOf(f)
254 v := reflect.ValueOf(x)
255 fv.Call([]reflect.Value{v})
256 }
257
258 func call2(f func(*int), x *int) {
259 fv := reflect.ValueOf(f)
260 v := reflect.ValueOf(x)
261 fv.Call([]reflect.Value{v})
262 }
263
264 func method(x S) reflect.Value {
265 v := reflect.ValueOf(x)
266 return v.Method(0)
267 }
268
269 func nummethod(x S) int {
270 v := reflect.ValueOf(x)
271 return v.NumMethod()
272 }
273
274
275 func mapindex(m map[string]string, k string) string {
276 mv := reflect.ValueOf(m)
277 kv := reflect.ValueOf(k)
278 return mv.MapIndex(kv).String()
279 }
280
281 func mapkeys(m map[string]string) []reflect.Value {
282 mv := reflect.ValueOf(m)
283 return mv.MapKeys()
284 }
285
286 func mapiter1(m map[string]string) *reflect.MapIter {
287 mv := reflect.ValueOf(m)
288 return mv.MapRange()
289 }
290
291 func mapiter2(m map[string]string) string {
292 mv := reflect.ValueOf(m)
293 it := mv.MapRange()
294 if it.Next() {
295 return it.Key().String()
296 }
297 return ""
298 }
299
300 func mapiter3(m map[string]string, it *reflect.MapIter) {
301 mv := reflect.ValueOf(m)
302 it.Reset(mv)
303 }
304
305 func recv1(ch chan string) string {
306 v := reflect.ValueOf(ch)
307 r, _ := v.Recv()
308 return r.String()
309 }
310
311 func recv2(ch chan string) string {
312 v := reflect.ValueOf(ch)
313 r, _ := v.TryRecv()
314 return r.String()
315 }
316
317
318 func send1(ch chan string, x string) {
319 vc := reflect.ValueOf(ch)
320 vx := reflect.ValueOf(x)
321 vc.Send(vx)
322 }
323
324
325 func send2(ch chan string, x string) bool {
326 vc := reflect.ValueOf(ch)
327 vx := reflect.ValueOf(x)
328 return vc.TrySend(vx)
329 }
330
331 func close1(ch chan string) {
332 v := reflect.ValueOf(ch)
333 v.Close()
334 }
335
336 func select1(ch chan string) string {
337 v := reflect.ValueOf(ch)
338 cas := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: v}
339 _, r, _ := reflect.Select([]reflect.SelectCase{cas})
340 return r.String()
341 }
342
343
344 func select2(ch chan string, x string) {
345 vc := reflect.ValueOf(ch)
346 vx := reflect.ValueOf(x)
347 cas := reflect.SelectCase{Dir: reflect.SelectSend, Chan: vc, Send: vx}
348 reflect.Select([]reflect.SelectCase{cas})
349 }
350
351 var (
352 intTyp = reflect.TypeOf(int(0))
353 uintTyp = reflect.TypeOf(uint(0))
354 stringTyp = reflect.TypeOf(string(""))
355 bytesTyp = reflect.TypeOf([]byte{})
356 )
357
358
359 func convert1(x int) uint {
360 v := reflect.ValueOf(x)
361 return uint(v.Convert(uintTyp).Uint())
362 }
363
364
365 func convert2(x []byte) string {
366 v := reflect.ValueOf(x)
367 return v.Convert(stringTyp).String()
368 }
369
370
371 func set1(v reflect.Value, x int) {
372 vx := reflect.ValueOf(x)
373 v.Set(vx)
374 }
375
376
377 func set2(x int) int64 {
378 var a int
379 v := reflect.ValueOf(&a).Elem()
380 vx := reflect.ValueOf(x)
381 v.Set(vx)
382 return v.Int()
383 }
384
385 func set3(v reflect.Value, x int) {
386 v.SetInt(int64(x))
387 }
388
389 func set4(x int) int {
390 var a int
391 v := reflect.ValueOf(&a).Elem()
392 v.SetInt(int64(x))
393 return int(v.Int())
394 }
395
396 func set5(v any, x reflect.Value) {
397 reflect.ValueOf(v).Elem().Set(x)
398 }
399
400 func setstring(v reflect.Value, x string) {
401 v.SetString(x)
402 }
403
404 func setbytes(v reflect.Value, x []byte) {
405 v.SetBytes(x)
406 }
407
408 func setpointer(v reflect.Value, x unsafe.Pointer) {
409 v.SetPointer(x)
410 }
411
412 func setmapindex(m map[string]string, k, e string) {
413 mv := reflect.ValueOf(m)
414 kv := reflect.ValueOf(k)
415 ev := reflect.ValueOf(e)
416 mv.SetMapIndex(kv, ev)
417 }
418
419
420 func mapdelete(m map[string]string, k string) {
421 mv := reflect.ValueOf(m)
422 kv := reflect.ValueOf(k)
423 mv.SetMapIndex(kv, reflect.Value{})
424 }
425
426
427 func setiterkey1(v reflect.Value, it *reflect.MapIter) {
428 v.SetIterKey(it)
429 }
430
431
432 func setiterkey2(v reflect.Value, m map[string]string) {
433 it := reflect.ValueOf(m).MapRange()
434 v.SetIterKey(it)
435 }
436
437
438 func setitervalue1(v reflect.Value, it *reflect.MapIter) {
439 v.SetIterValue(it)
440 }
441
442
443 func setitervalue2(v reflect.Value, m map[string]string) {
444 it := reflect.ValueOf(m).MapRange()
445 v.SetIterValue(it)
446 }
447
448
449
450 func append1(s []int, x int) []int {
451 sv := reflect.ValueOf(s)
452 xv := reflect.ValueOf(x)
453 rv := reflect.Append(sv, xv)
454 return rv.Interface().([]int)
455 }
456
457
458 func append2(s, x []int) []int {
459 sv := reflect.ValueOf(s)
460 xv := reflect.ValueOf(x)
461 rv := reflect.AppendSlice(sv, xv)
462 return rv.Interface().([]int)
463 }
464
View as plain text