diff --git a/cmd/xgo/edit.go b/cmd/xgo/edit.go index ed613b82..dc8f36ef 100644 --- a/cmd/xgo/edit.go +++ b/cmd/xgo/edit.go @@ -24,6 +24,7 @@ func editFile(file string, callback func(content string) (string, error)) error return ioutil.WriteFile(file, []byte(newContent), 0755) } +// Deprecated: use addContentAtIndex instead func addCodeAfterImports(code string, beginMark string, endMark string, contents []string) string { idx := indexSeq(code, []string{"import", "(", "\n"}, false) if idx < 0 { @@ -32,14 +33,17 @@ func addCodeAfterImports(code string, beginMark string, endMark string, contents return insertContentNoDuplicate(code, beginMark, endMark, idx, strings.Join(contents, "\n")+"\n") } +// Deprecated: use addContentAtIndex instead func addContentBefore(content string, beginMark string, endMark string, seq []string, addContent string) string { return addContentAt(content, beginMark, endMark, seq, addContent, true) } +// Deprecated: use addContentAtIndex instead func addContentAfter(content string, beginMark string, endMark string, seq []string, addContent string) string { return addContentAt(content, beginMark, endMark, seq, addContent, false) } +// Deprecated: use addContentAtIndex instead func addContentAt(content string, beginMark string, endMark string, seq []string, addContent string, begin bool) string { idx := indexSeq(content, seq, begin) if idx < 0 { diff --git a/cmd/xgo/patch_compiler.go b/cmd/xgo/patch_compiler.go index dd5c3243..b33c11dc 100644 --- a/cmd/xgo/patch_compiler.go +++ b/cmd/xgo/patch_compiler.go @@ -317,6 +317,31 @@ func patchCompilerNoder(goroot string, goVersion *goinfo.GoVersion) error { } content = addContentAfter(content, "/**/", "/**/", anchors, noderFiles) + + // expose the trimFilename func for recording + if goVersion.Major == 1 && goVersion.Minor <= 17 { + content = addContentAtIndex(content, + "/**/", "/**/", + []string{ + `func absFilename(name string) string {`, + }, + 0, + true, + "func init(){ xgo_syntax.AbsFilename = absFilename;}\n", + ) + } else { + content = addContentAtIndex(content, + "/**/", "/**/", + []string{ + `func trimFilename(b *syntax.PosBase) string {`, + }, + 0, + true, + "func init(){ xgo_syntax.TrimFilename = trimFilename;}\n", + ) + } + + // func trimFilename(b *syntax.PosBase) string { return content, nil }) } diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 1e26512d..27d9af79 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.25" -const REVISION = "965c202f092256db5171d680d92030aa720cca4d+1" -const NUMBER = 188 +const REVISION = "9028d0e560172d8b5c5ccba73491eed2886ddd6c+1" +const NUMBER = 189 func getRevision() string { revSuffix := "" diff --git a/patch/syntax/syntax.go b/patch/syntax/syntax.go index 3a352616..aa17ee2e 100644 --- a/patch/syntax/syntax.go +++ b/patch/syntax/syntax.go @@ -422,9 +422,11 @@ type DeclInfo struct { FileSyntax *syntax.File FileIndex int - File string - FileRef string - Line int + + // this is file name after applied -trimpath + File string + FileRef string + Line int } func (c *DeclInfo) RefName() string { @@ -481,11 +483,20 @@ func fillName(field *syntax.Field, namePrefix string) { // collect funcs from files, register each of them by // calling to __xgo_reg_func with names and func pointer +var AbsFilename func(name string) string +var TrimFilename func(b *syntax.PosBase) string + func getFuncDecls(files []*syntax.File, varTrap bool) []*DeclInfo { // fileInfos := make([]*FileDecl, 0, len(files)) var declFuncs []*DeclInfo for i, f := range files { - file := f.Pos().RelFilename() + var file string + if TrimFilename != nil { + // >= go1.18 + file = TrimFilename(f.Pos().Base()) + } else { + file = AbsFilename(f.Pos().Base().Filename()) + } for _, decl := range f.DeclList { fnDecls := extractFuncDecls(i, f, file, decl, varTrap) declFuncs = append(declFuncs, fnDecls...) diff --git a/runtime/core/version.go b/runtime/core/version.go index 63e7b7c4..406ee7ec 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.25" -const REVISION = "965c202f092256db5171d680d92030aa720cca4d+1" -const NUMBER = 188 +const REVISION = "9028d0e560172d8b5c5ccba73491eed2886ddd6c+1" +const NUMBER = 189 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/runtime/test/trap_with_overlay/replace_test.go.txt b/runtime/test/trap_with_overlay/replace_test.go.txt new file mode 100644 index 00000000..fc3d1eb0 --- /dev/null +++ b/runtime/test/trap_with_overlay/replace_test.go.txt @@ -0,0 +1,33 @@ +package with_overlay + +import ( + "context" + "testing" + + "github.com/xhd2015/xgo/runtime/core" + "github.com/xhd2015/xgo/runtime/functab" + "github.com/xhd2015/xgo/runtime/trap" +) + +func TestTrapWithOverlay(t *testing.T) { + funcInfo := functab.GetFuncByPkg("github.com/xhd2015/xgo/runtime/test/trap_with_overlay", "A") + if funcInfo == nil { + t.Fatalf("cannot get function A") + } + var haveCalled bool + trap.AddFuncInfoInterceptor(funcInfo, &trap.Interceptor{ + Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (data interface{}, err error) { + haveCalled = true + return + }, + }) + // do the call + A() + if !haveCalled { + t.Fatalf("expect have called, actually not") + } +} + +func A() { + +} diff --git a/runtime/test/trap_with_overlay/run/run_test.go b/runtime/test/trap_with_overlay/run/run_test.go new file mode 100644 index 00000000..e3b602cd --- /dev/null +++ b/runtime/test/trap_with_overlay/run/run_test.go @@ -0,0 +1,51 @@ +package run + +import ( + "encoding/json" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "testing" +) + +func TestOverlay(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + wd = filepath.Dir(wd) + replace := map[string]string{ + filepath.Join(wd, "trap_with_overlay_test.go"): filepath.Join(wd, "replace_test.go.txt"), + } + overlayFile := filepath.Join(wd, "overlay.json") + overlay := map[string]interface{}{ + "Replace": replace, + } + overlayJSON, err := json.Marshal(overlay) + if err != nil { + t.Fatal(err) + } + err = ioutil.WriteFile(overlayFile, overlayJSON, 0755) + if err != nil { + t.Fatal(err) + } + + projectRoot := wd + for i := 0; i < 3; i++ { + projectRoot = filepath.Dir(projectRoot) + } + + cmd := exec.Command("go", "run", "-tags", "dev", "./cmd/xgo", "test", + "--project-dir", wd, + "-overlay", overlayFile, + "-v", + ) + cmd.Dir = projectRoot + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + err = cmd.Run() + if err != nil { + t.Fatal(err) + } +} diff --git a/runtime/test/trap_with_overlay/trap_with_overlay_test.go b/runtime/test/trap_with_overlay/trap_with_overlay_test.go new file mode 100644 index 00000000..aeea9ed7 --- /dev/null +++ b/runtime/test/trap_with_overlay/trap_with_overlay_test.go @@ -0,0 +1,29 @@ +package with_overlay + +import ( + "context" + "testing" + + "github.com/xhd2015/xgo/runtime/core" + "github.com/xhd2015/xgo/runtime/functab" + "github.com/xhd2015/xgo/runtime/trap" +) + +func TestTrapWithOverlay(t *testing.T) { + funcInfo := functab.GetFuncByPkg("github.com/xhd2015/xgo/runtime/test/trap_with_overlay", "A") + if funcInfo == nil { + t.Fatalf("cannot get function A") + } + var haveCalled bool + trap.AddFuncInfoInterceptor(funcInfo, &trap.Interceptor{ + Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (data interface{}, err error) { + haveCalled = true + return + }, + }) + // do the call + // padding + if !haveCalled { + t.Fatalf("expect have called, actually not") + } +} diff --git a/script/run-test/main.go b/script/run-test/main.go index e0aa8b43..9dbd970d 100644 --- a/script/run-test/main.go +++ b/script/run-test/main.go @@ -51,6 +51,9 @@ var runtimeSubTests = []string{ "patch", "patch_const", } +var runtimeSubFileTests = []string{ + "trap_with_overlay/run", +} func main() { args := os.Args[1:] @@ -407,6 +410,9 @@ func doRunTest(goroot string, kind testKind, args []string, tests []string) erro for _, runtimeTest := range runtimeSubTests { testArgs = append(testArgs, "./"+runtimeTest+"/...") } + for _, runtimeSubFileTest := range runtimeSubFileTests { + testArgs = append(testArgs, "./"+runtimeSubFileTest) + } } }