Skip to content

Commit aefc220

Browse files
committed
feat: add reflect caller
1 parent 99bc53f commit aefc220

File tree

3 files changed

+172
-71
lines changed

3 files changed

+172
-71
lines changed

src/reflect/all_test.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -3988,8 +3988,8 @@ func TestValuePanic(t *testing.T) {
39883988
shouldPanic("reflect.Value.Addr of unaddressable value", func() { vo(0).Addr() })
39893989
shouldPanic("call of reflect.Value.Bool on float64 Value", func() { vo(0.0).Bool() })
39903990
shouldPanic("call of reflect.Value.Bytes on string Value", func() { vo("").Bytes() })
3991-
shouldPanic("call of reflect.Value.Call on bool Value", func() { vo(true).Call(nil) })
3992-
shouldPanic("call of reflect.Value.CallSlice on int Value", func() { vo(0).CallSlice(nil) })
3991+
shouldPanic("call of reflect.Value.Caller on bool Value", func() { vo(true).Call(nil) })
3992+
shouldPanic("call of reflect.Value.Caller on int Value", func() { vo(0).CallSlice(nil) })
39933993
shouldPanic("call of reflect.Value.Close on string Value", func() { vo("").Close() })
39943994
shouldPanic("call of reflect.Value.Complex on float64 Value", func() { vo(0.0).Complex() })
39953995
shouldPanic("call of reflect.Value.Elem on bool Value", func() { vo(false).Elem() })
@@ -8408,3 +8408,19 @@ func TestClear(t *testing.T) {
84088408
})
84098409
}
84108410
}
8411+
8412+
func TestCaller_CallReusesOutValues(t *testing.T) {
8413+
fn := func() int {
8414+
return 27
8415+
}
8416+
8417+
var i int
8418+
v := ValueOf(&i).Elem()
8419+
out := []Value{v}
8420+
8421+
ValueOf(fn).Caller().Call([]Value{}, out)
8422+
8423+
if i != 27 {
8424+
t.Errorf("unexpected result for output value; got %d, want 27", i)
8425+
}
8426+
}

src/reflect/benchmark_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ func BenchmarkCall(b *testing.B) {
197197
})
198198
}
199199

200+
func BenchmarkCallerCall(b *testing.B) {
201+
fv := ValueOf(func(a, b string) {})
202+
c := fv.Caller()
203+
out := make([]Value, c.Type().NumOut())
204+
b.ReportAllocs()
205+
b.RunParallel(func(pb *testing.PB) {
206+
args := []Value{ValueOf("a"), ValueOf("b")}
207+
for pb.Next() {
208+
c.Call(args, out)
209+
}
210+
})
211+
}
212+
200213
type myint int64
201214

202215
func (i *myint) inc() {
@@ -213,6 +226,17 @@ func BenchmarkCallMethod(b *testing.B) {
213226
}
214227
}
215228

229+
func BenchmarkCallerCallMethod(b *testing.B) {
230+
b.ReportAllocs()
231+
z := new(myint)
232+
233+
v := ValueOf(z.inc)
234+
c := v.Caller()
235+
for i := 0; i < b.N; i++ {
236+
c.Call(nil, nil)
237+
}
238+
}
239+
216240
func BenchmarkCallArgCopy(b *testing.B) {
217241
byteArray := func(n int) Value {
218242
return Zero(ArrayOf(n, TypeOf(byte(0))))

0 commit comments

Comments
 (0)