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 overlay #73

Merged
merged 1 commit into from
Apr 19, 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
4 changes: 4 additions & 0 deletions cmd/xgo/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions cmd/xgo/patch_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ func patchCompilerNoder(goroot string, goVersion *goinfo.GoVersion) error {
}
content = addContentAfter(content, "/*<begin file_autogen>*/", "/*<end file_autogen>*/", anchors,
noderFiles)

// expose the trimFilename func for recording
if goVersion.Major == 1 && goVersion.Minor <= 17 {
content = addContentAtIndex(content,
"/*<begin expose_abs_filename>*/", "/*<end expose_abs_filename>*/",
[]string{
`func absFilename(name string) string {`,
},
0,
true,
"func init(){ xgo_syntax.AbsFilename = absFilename;}\n",
)
} else {
content = addContentAtIndex(content,
"/*<begin expose_trim_filename>*/", "/*<end expose_trim_filename>*/",
[]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
})
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
19 changes: 15 additions & 4 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
33 changes: 33 additions & 0 deletions runtime/test/trap_with_overlay/replace_test.go.txt
Original file line number Diff line number Diff line change
@@ -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() {

}
51 changes: 51 additions & 0 deletions runtime/test/trap_with_overlay/run/run_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
29 changes: 29 additions & 0 deletions runtime/test/trap_with_overlay/trap_with_overlay_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
6 changes: 6 additions & 0 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var runtimeSubTests = []string{
"patch",
"patch_const",
}
var runtimeSubFileTests = []string{
"trap_with_overlay/run",
}

func main() {
args := os.Args[1:]
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
Loading