Skip to content

Commit

Permalink
fix trace marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Jul 26, 2024
1 parent baec4d5 commit ddb79e3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/xgo/runtime_gen/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (c *collectOpts) WithFilter(f func(stack *Stack) bool) *collectOpts {
c.filter = append(c.filter, f)
return c
}

func (c *collectOpts) WithOptions(opts *CollectOptions) *collectOpts {
c.options = opts
return c
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "fmt"

// auto updated
const VERSION = "1.0.46"
const REVISION = "f6841ef832f77218084fcba9af395cce5705fff9+1"
const NUMBER = 298
const REVISION = "baec4d50e4c26d279fe32cd30c7649d707b8f1ce+1"
const NUMBER = 299

// manually updated
const CORE_VERSION = "1.0.43"
Expand Down
11 changes: 11 additions & 0 deletions patch/ctxt/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func SkipPackageTrap() bool {
return false
}

// always trap for encoding/json.newTypeEncoder
func AlwaysTrap(pkgPath string, isStd bool, identityName string, funcName string) bool {
if !isStd {
return false
}
if pkgPath == "encoding/json" && identityName == "newTypeEncoder" {
return true
}
return false
}

func AllowPkgFuncTrap(pkgPath string, isStd bool, identityName string, funcName string) bool {
if isStd {
return allowStdFunc(pkgPath, identityName, funcName)
Expand Down
17 changes: 12 additions & 5 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,20 @@ func filterFuncDecls(funcDecls []*info.DeclInfo, pkgPath string) []*info.DeclInf
for j := 0; j < n; j++ {
fn := funcDecls[j]

action := xgo_ctxt.GetAction(fn)
if action == "" {
// disable part of stdlibs
if !xgo_ctxt.AllowPkgFuncTrap(pkgPath, base.Flag.Std, fn.IdentityName(), fn.FuncName()) {
action = "exclude"
var action string

idName := fn.IdentityName()
funcName := fn.FuncName()
if !xgo_ctxt.AlwaysTrap(pkgPath, base.Flag.Std, idName, funcName) {
action = xgo_ctxt.GetAction(fn)
if action == "" {
// disable part of stdlibs
if !xgo_ctxt.AllowPkgFuncTrap(pkgPath, base.Flag.Std, idName, funcName) {
action = "exclude"
}
}
}

if action == "" || action == "include" {
funcDecls[i] = fn
i++
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package marshal

import (
"testing"

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

// go run -tags dev ./cmd/xgo test --project-dir runtime/test/trace/marshal/exclue --mock-rule='{"pkg":"encoding/json","name":"newTypeEncoder","action":"exclude"}' -run TestMarshalFuncEvenActionExclude

// flag: --mock-rule='{"pkg":"encoding/json","name":"newTypeEncoder","action":"exclude"}'
func TestMarshalFuncEvenActionExclude(t *testing.T) {
data, err := trace.MarshalAnyJSON(map[string]interface{}{
"test": test,
})
if err != nil {
t.Error(err)
return
}
expect := `{"test":{}}`
if string(data) != expect {
t.Errorf("expect: %s, actual: %s", expect, string(data))
}
}

func test() {

}
26 changes: 26 additions & 0 deletions runtime/test/trace/marshal/flag/func_even_stdlib_is_false_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package marshal

import (
"testing"

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

// flag: --trap-stdlib=false
func TestMarshalFuncEvenStdlibIsFalse(t *testing.T) {
data, err := trace.MarshalAnyJSON(map[string]interface{}{
"test": test,
})
if err != nil {
t.Error(err)
return
}
expect := `{"test":{}}`
if string(data) != expect {
t.Errorf("expect: %s, actual: %s", expect, string(data))
}
}

func test() {

}
1 change: 1 addition & 0 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (c *collectOpts) WithFilter(f func(stack *Stack) bool) *collectOpts {
c.filter = append(c.filter, f)
return c
}

func (c *collectOpts) WithOptions(opts *CollectOptions) *collectOpts {
c.options = opts
return c
Expand Down
11 changes: 11 additions & 0 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ var extraSubTests = []*TestCase{
usePlainGo: true,
dir: "test/xgo_integration",
},
{
// go run ./script/run-test/ --name trace-marshal-not-trap-stdlib
name: "trace-marshal-not-trap-stdlib",
dir: "runtime/test/trace/marshal/flag",
flags: []string{"--trap-stdlib=false"},
},
{
name: "trace-marshal-exclude",
dir: "runtime/test/trace/marshal/exclude",
flags: []string{"--mock-rule", `{"pkg":"encoding/json","name":"newTypeEncoder","action":"exclude"}`},
},
}

func main() {
Expand Down
3 changes: 0 additions & 3 deletions support/assert/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ func diffText(expected []byte, actual []byte) (string, error) {
}
defer os.RemoveAll(tmpDir)

// var jsonExpected []byte
// var jsonActual []byte

err = fileutil.WriteFile(filepath.Join(tmpDir, "expected"), expected)
if err != nil {
return "", err
Expand Down

0 comments on commit ddb79e3

Please sign in to comment.