diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 50fc367d..0ea487b4 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.18" -const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1" -const NUMBER = 157 +const REVISION = "e0fff32b787ec10d8c27b655f546a8a43e090b61+1" +const NUMBER = 158 func getRevision() string { return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER) diff --git a/patch/adapter_go1.17_18_19_20_21.go b/patch/adapter_go1.17_18_19_20_21.go index bc58a9e4..80c458b2 100644 --- a/patch/adapter_go1.17_18_19_20_21.go +++ b/patch/adapter_go1.17_18_19_20_21.go @@ -81,19 +81,19 @@ func takeAddrs(fn *ir.Func, t *types.Type, nameOnly bool) ir.Expr { return wrapListType(ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, typeNode(intfSlice), paramList)) } -func getFieldNames(fn *ir.Func, t *types.Type) ir.Expr { +func getFieldNames(pos src.XPos, fn *ir.Func, t *types.Type) ir.Expr { if t.NumFields() == 0 { - return NewNilExpr(fn.Pos(), strSlice) + return NewNilExpr(pos, strSlice) } paramList := make([]ir.Node, t.NumFields()) i := 0 ForEachField(t, func(field *types.Field) bool { fieldName := getFieldName(fn, field) - paramList[i] = NewStringLit(fn.Pos(), fieldName) + paramList[i] = NewStringLit(pos, fieldName) i++ return true }) - return wrapListType(ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, typeNode(strSlice), paramList)) + return wrapListType(ir.NewCompLitExpr(pos, ir.OCOMPLIT, typeNode(strSlice), paramList)) } func getTypeNames(params *types.Type) []ir.Node { diff --git a/patch/adapter_go1.22.go b/patch/adapter_go1.22.go index 33f5b21c..2fba1a6f 100644 --- a/patch/adapter_go1.22.go +++ b/patch/adapter_go1.22.go @@ -65,19 +65,19 @@ func takeAddrs(fn *ir.Func, t []*types.Field, nameOnly bool) ir.Expr { } // NOTE: []*types.Field instead of *types.Type(go1.21) -func getFieldNames(fn *ir.Func, t []*types.Field) ir.Expr { +func getFieldNames(pos src.XPos, fn *ir.Func, t []*types.Field) ir.Expr { if len(t) == 0 { - return NewNilExpr(fn.Pos(), strSlice) + return NewNilExpr(pos, strSlice) } paramList := make([]ir.Node, len(t)) i := 0 ForEachField(t, func(field *types.Field) bool { fieldName := getFieldName(fn, field) - paramList[i] = NewStringLit(fn.Pos(), fieldName) + paramList[i] = NewStringLit(pos, fieldName) i++ return true }) - return ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, strSlice, paramList) + return ir.NewCompLitExpr(pos, ir.OCOMPLIT, strSlice, paramList) } func getTypeNames(params []*types.Field) []ir.Node { diff --git a/patch/link_name.go b/patch/link_name.go index 6d781260..8672af8b 100644 --- a/patch/link_name.go +++ b/patch/link_name.go @@ -1,11 +1,13 @@ package patch import ( + "cmd/compile/internal/base" "cmd/compile/internal/ir" "cmd/compile/internal/typecheck" xgo_ctxt "cmd/compile/internal/xgo_rewrite_internal/patch/ctxt" xgo_record "cmd/compile/internal/xgo_rewrite_internal/patch/record" xgo_syntax "cmd/compile/internal/xgo_rewrite_internal/patch/syntax" + "cmd/internal/src" "strings" ) @@ -137,14 +139,18 @@ func replaceWithRuntimeCall(fn *ir.Func, name string) { // callNode = ir.NewAssignListStmt(fnPos, ir.OAS2, resNames, []ir.Node{callNode}) // } } - replaceFuncBody(fn, []ir.Node{ + replaceFuncBodyWithPos(base.AutogeneratedPos, fn, []ir.Node{ // debugPrint("debug getg"), callNode, }) } func replaceFuncBody(fn *ir.Func, nodes []ir.Node) { - node := ifConstant(fn.Pos(), true, nodes, fn.Body) + replaceFuncBodyWithPos(fn.Pos(), fn, nodes) +} + +func replaceFuncBodyWithPos(pos src.XPos, fn *ir.Func, nodes []ir.Node) { + node := ifConstant(pos, true, nodes, fn.Body) fn.Body = []ir.Node{node} xgo_record.SetRewrittenBody(fn, fn.Body) diff --git a/patch/trap.go b/patch/trap.go index a9d6e521..40f8674a 100644 --- a/patch/trap.go +++ b/patch/trap.go @@ -392,17 +392,18 @@ func initClosureRegs() { trueLit := NewBoolLit(base.AutogeneratedPos, true) emptyStrLit := NewStringLit(base.AutogeneratedPos, "") for _, fn := range trappedClosures { - pos := fn.Pos() + pos := base.AutogeneratedPos + fnPos := fn.Pos() - posInfo := getPosInfo(pos) + posInfo := getPosInfo(fnPos) fileName := posInfo.AbsFilename() adjustFile := getAdjustedFile(fileName) if adjustFile != "" { fileName = adjustFile } fnType := fn.Type() - argNames := getFieldNames(fn, fnType.Params()) - resNames := getFieldNames(fn, fnType.Results()) + argNames := getFieldNames(pos, fn, fnType.Params()) + resNames := getFieldNames(pos, fn, fnType.Results()) // TODO: why fn.Name() always returns nil? var fnRef ir.Node if !closureMayBeEliminatedDueToIfConst { diff --git a/runtime/core/version.go b/runtime/core/version.go index b9c7d40f..090cfe92 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.18" -const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1" -const NUMBER = 157 +const REVISION = "e0fff32b787ec10d8c27b655f546a8a43e090b61+1" +const NUMBER = 158 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/runtime/test/debug/debug_test.go b/runtime/test/debug/debug_test.go index 94aa7caf..2bfc2c3d 100644 --- a/runtime/test/debug/debug_test.go +++ b/runtime/test/debug/debug_test.go @@ -6,35 +6,18 @@ package debug import ( - "context" "testing" - "time" - "github.com/xhd2015/xgo/runtime/core" "github.com/xhd2015/xgo/runtime/mock" ) -func TestMockTimeSleep(t *testing.T) { - begin := time.Now() - sleepDur := 1 * time.Second - var haveCalledMock bool - var mockArg time.Duration - mock.Mock(time.Sleep, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error { - haveCalledMock = true - mockArg = args.GetFieldIndex(0).Value().(time.Duration) - return nil +func TestDebug(t *testing.T) { + mock.Patch(greet, func(s string) string { + return "mock " + s }) - time.Sleep(sleepDur) - - cost := time.Since(begin) + greet("world") +} - if !haveCalledMock { - t.Fatalf("expect haveCalledMock, actually not") - } - if mockArg != sleepDur { - t.Fatalf("expect mockArg to be %v, actual: %v", sleepDur, mockArg) - } - if cost > 100*time.Millisecond { - t.Fatalf("expect time.Sleep mocked, actual cost: %v", cost) - } +func greet(s string) string { + return "hello " + s }