Skip to content

Commit 05e2121

Browse files
committed
fix method with ctx
1 parent ee229bd commit 05e2121

5 files changed

Lines changed: 57 additions & 12 deletions

File tree

cmd/xgo/version.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import "fmt"
44

5-
const VERSION = "1.0.14"
6-
const REVISION = "649cbfce18b726d457516babe9f10a82e07c023d+1"
7-
const NUMBER = 149
5+
const VERSION = "1.0.15"
6+
const REVISION = "ee229bd12d0d0fdb24f26602651b01dbd76c7cf1+1"
7+
const NUMBER = 150
88

99
func getRevision() string {
1010
return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER)

runtime/core/version.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"os"
77
)
88

9-
const VERSION = "1.0.14"
10-
const REVISION = "649cbfce18b726d457516babe9f10a82e07c023d+1"
11-
const NUMBER = 149
9+
const VERSION = "1.0.15"
10+
const REVISION = "ee229bd12d0d0fdb24f26602651b01dbd76c7cf1+1"
11+
const NUMBER = 150
1212

1313
// these fields will be filled by compiler
1414
const XGO_VERSION = ""

runtime/mock/patch.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ func buildInterceptorFromPatch(recvPtr interface{}, replacer interface{}) func(c
3636
callArgs := make([]reflect.Value, nIn)
3737
src := 0
3838
dst := 0
39-
if fn.RecvType != "" && recvPtr != nil {
40-
// patching an instance method
41-
src++
39+
if fn.RecvType != "" {
40+
if recvPtr != nil {
41+
// patching an instance method
42+
src++
43+
} else {
44+
// set receiver
45+
callArgs[dst] = reflect.ValueOf(args.GetFieldIndex(0).Value())
46+
dst++
47+
src++
48+
}
4249
}
4350
if fn.FirstArgCtx {
44-
callArgs[0] = reflect.ValueOf(ctx)
51+
callArgs[dst] = reflect.ValueOf(ctx)
4552
dst++
4653
}
4754
for i := 0; i < nIn-dst; i++ {

runtime/test/patch/patch_type_method_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package patch
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/xhd2015/xgo/runtime/mock"
78
)
89

9-
func TestPatchTypeMethod(t *testing.T) {
10+
func TestPatchTypeMethodNoArg(t *testing.T) {
1011
ins := &struct_{
1112
s: "world",
1213
}
@@ -19,3 +20,35 @@ func TestPatchTypeMethod(t *testing.T) {
1920
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
2021
}
2122
}
23+
24+
func (c *struct_) greetCtx(ctx context.Context) string {
25+
return "hello " + c.s
26+
}
27+
28+
func TestPatchTypeMethodCtxArg(t *testing.T) {
29+
ins := &struct_{
30+
s: "world",
31+
}
32+
mock.Patch((*struct_).greetCtx, func(ins *struct_, ctx context.Context) string {
33+
return "mock " + ins.s
34+
})
35+
36+
res := ins.greetCtx(context.Background())
37+
if res != "mock world" {
38+
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
39+
}
40+
}
41+
42+
func TestPatchInstanceMethodCtxArg(t *testing.T) {
43+
ins := &struct_{
44+
s: "world",
45+
}
46+
mock.Patch(ins.greetCtx, func(ctx context.Context) string {
47+
return "mock " + ins.s
48+
})
49+
50+
res := ins.greetCtx(context.Background())
51+
if res != "mock world" {
52+
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
53+
}
54+
}

runtime/trap/trap.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ func trapImpl(pkgPath string, identityName string, generic bool, pc uintptr, rec
9696
var ctx context.Context
9797
if f.FirstArgCtx {
9898
// TODO: is *HttpRequest a *Context?
99-
ctx = reflect.ValueOf(args[0]).Elem().Interface().(context.Context)
99+
100+
// NOTE: ctx can be nil when doing InspectPC
101+
argCtx := reflect.ValueOf(args[0]).Elem().Interface()
102+
if argCtx != nil {
103+
ctx = argCtx.(context.Context)
104+
}
100105
// ctx = *(args[0].(*context.Context))
101106
} else if f.Closure {
102107
if len(args) > 0 {

0 commit comments

Comments
 (0)