Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix method with ctx #26

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import "fmt"

const VERSION = "1.0.14"
const REVISION = "649cbfce18b726d457516babe9f10a82e07c023d+1"
const NUMBER = 149
const VERSION = "1.0.15"
const REVISION = "ee229bd12d0d0fdb24f26602651b01dbd76c7cf1+1"
const NUMBER = 150

func getRevision() string {
return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER)
Expand Down
6 changes: 3 additions & 3 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

const VERSION = "1.0.14"
const REVISION = "649cbfce18b726d457516babe9f10a82e07c023d+1"
const NUMBER = 149
const VERSION = "1.0.15"
const REVISION = "ee229bd12d0d0fdb24f26602651b01dbd76c7cf1+1"
const NUMBER = 150

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
15 changes: 11 additions & 4 deletions runtime/mock/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ func buildInterceptorFromPatch(recvPtr interface{}, replacer interface{}) func(c
callArgs := make([]reflect.Value, nIn)
src := 0
dst := 0
if fn.RecvType != "" && recvPtr != nil {
// patching an instance method
src++
if fn.RecvType != "" {
if recvPtr != nil {
// patching an instance method
src++
} else {
// set receiver
callArgs[dst] = reflect.ValueOf(args.GetFieldIndex(0).Value())
dst++
src++
}
}
if fn.FirstArgCtx {
callArgs[0] = reflect.ValueOf(ctx)
callArgs[dst] = reflect.ValueOf(ctx)
dst++
}
for i := 0; i < nIn-dst; i++ {
Expand Down
35 changes: 34 additions & 1 deletion runtime/test/patch/patch_type_method_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package patch

import (
"context"
"testing"

"github.com/xhd2015/xgo/runtime/mock"
)

func TestPatchTypeMethod(t *testing.T) {
func TestPatchTypeMethodNoArg(t *testing.T) {
ins := &struct_{
s: "world",
}
Expand All @@ -19,3 +20,35 @@ func TestPatchTypeMethod(t *testing.T) {
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
}
}

func (c *struct_) greetCtx(ctx context.Context) string {
return "hello " + c.s
}

func TestPatchTypeMethodCtxArg(t *testing.T) {
ins := &struct_{
s: "world",
}
mock.Patch((*struct_).greetCtx, func(ins *struct_, ctx context.Context) string {
return "mock " + ins.s
})

res := ins.greetCtx(context.Background())
if res != "mock world" {
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
}
}

func TestPatchInstanceMethodCtxArg(t *testing.T) {
ins := &struct_{
s: "world",
}
mock.Patch(ins.greetCtx, func(ctx context.Context) string {
return "mock " + ins.s
})

res := ins.greetCtx(context.Background())
if res != "mock world" {
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
}
}
7 changes: 6 additions & 1 deletion runtime/trap/trap.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ func trapImpl(pkgPath string, identityName string, generic bool, pc uintptr, rec
var ctx context.Context
if f.FirstArgCtx {
// TODO: is *HttpRequest a *Context?
ctx = reflect.ValueOf(args[0]).Elem().Interface().(context.Context)

// NOTE: ctx can be nil when doing InspectPC
argCtx := reflect.ValueOf(args[0]).Elem().Interface()
if argCtx != nil {
ctx = argCtx.(context.Context)
}
// ctx = *(args[0].(*context.Context))
} else if f.Closure {
if len(args) > 0 {
Expand Down
Loading