Skip to content

Commit

Permalink
Fix typos in code, test and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Apr 2, 2024
1 parent 8a9ed02 commit e65cc0c
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 70 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Arguments:

Scope:
- If `Mock` is called from `init`, then all goroutines will be mocked.
- Otherwise, `Mock` is called after `init`, then the mock interceptor will only be effective for current gorotuine, other goroutines are not affected.
- Otherwise, `Mock` is called after `init`, then the mock interceptor will only be effective for current goroutine, other goroutines are not affected.

Interceptor Signature: `func(ctx context.Context, fn *core.FuncInfo, args core.Object, results core.Object) error`
- If the interceptor returns `nil`, then the target function is mocked,
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestMethodMock(t *testing.T){
## Trace
It is painful when debugging with a deep call stack.

Trace addresses this issue by collecting the hiearchical stack trace and stores it into file for later use.
Trace addresses this issue by collecting the hierarchical stack trace and stores it into file for later use.

Needless to say, with Trace, debug becomes less usual:

Expand Down Expand Up @@ -385,7 +385,7 @@ XGO_TRACE_OUTPUT=stdout xgo run ./
# ]
# }
#
# NOTE: other fields are ommited for displaying key information.
# NOTE: other fields are omitted for displaying key information.
```

You can view the trace with:`xgo tool trace TestExample.json`
Expand All @@ -394,7 +394,7 @@ Output:
![trace html](cmd/trace/testdata/stack_trace.jpg "Trace")

By default, Trace will write traces to a temp directory under current working directory. This behavior can be overridden by setting `XGO_TRACE_OUTPUT` to different values:
- `XGO_TRACE_OUTPUT=stdout`: traces will be written to stdout, for debugging purepose,
- `XGO_TRACE_OUTPUT=stdout`: traces will be written to stdout, for debugging purpose,
- `XGO_TRACE_OUTPUT=<dir>`: traces will be written to `<dir>`,
- `XGO_TRACE_OUTPUT=off`: turn off trace.

Expand All @@ -410,7 +410,7 @@ Yes, no interface, just for mocking. If the only reason to abstract an interface

Extracting interface just for mocking is never an option to me. To the domain of the problem, it's merely a workaround. It enforces the code to be written in one style, that's why we don't like it.

Monkey patching simply does the right thing for the problem. But existing library are bad at compatiblility.
Monkey patching simply does the right thing for the problem. But existing library are bad at compatibility.

So I created `xgo`, so I hope `xgo` will also take over other solutions to the mocking problem.

Expand All @@ -421,13 +421,13 @@ In short, it uses a low level assembly hack to replace function at runtime. Whic

Then it was archived and no longer maintained by the author himself. However, two projects later take over the asm idea and add support for newer go versions and architectures like Apple M1.

Still, the two does not solve the underlying compatiblility issues introduced by asm, including cross-platform support, the need to write to a read-only section of the execution code and lacking of general mock.
Still, the two does not solve the underlying compatibility issues introduced by asm, including cross-platform support, the need to write to a read-only section of the execution code and lacking of general mock.

So developers still get annoying breaked every now and then.
So developers still get annoying failures every now and then.

Xgo managed to solve these problems by avoiding low level hacking of the language itself. Instead, it relies on the IR representation employed by the go compiler.
Xgo managed to solve these problems by avoiding low level hacking of the language itself. Instead, it relies on the IR representation employed by the go compiler.

It does a so-called `IR Rewritting` on the fly when the compiler compiles the source code. The IR(Intermediate Representation) is closer to the source code rather than the machine code. Thus it is much more stable than the monkey solution.
It does a so-called `IR Rewriting` on the fly when the compiler compiles the source code. The IR(Intermediate Representation) is closer to the source code rather than the machine code. Thus it is much more stable than the monkey solution.

In conclusion, `xgo` and monkey are compared as the following:
||xgo|monkey|
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec_tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func handleCompile(cmd string, opts *options, args []string) error {
return nil
}
debugWithDlv := opts.debugWithDlv
// pkg path: the argment after the -p
// pkg path: the argument after the -p
pkgPath := findArgAfterFlag(args, "-p")
if pkgPath == "" {
return fmt.Errorf("compile missing -p package")
Expand Down
6 changes: 3 additions & 3 deletions cmd/xgo/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func addCodeAfterImports(code string, beginMark string, endMark string, contents
if idx < 0 {
panic(fmt.Errorf("import not found"))
}
return insertConentNoDudplicate(code, beginMark, endMark, idx, strings.Join(contents, "\n")+"\n")
return insertContentNoDuplicate(code, beginMark, endMark, idx, strings.Join(contents, "\n")+"\n")
}

func addContentBefore(content string, beginMark string, endMark string, seq []string, addContent string) string {
Expand All @@ -45,7 +45,7 @@ func addContentAt(content string, beginMark string, endMark string, seq []string
if idx < 0 {
panic(fmt.Errorf("sequence not found: %v", seq))
}
return insertConentNoDudplicate(content, beginMark, endMark, idx, addContent)
return insertContentNoDuplicate(content, beginMark, endMark, idx, addContent)
}

func replaceContentAfter(content string, beginMark string, endMark string, seq []string, target string, replaceContent string) string {
Expand All @@ -72,7 +72,7 @@ func replaceContentAfter(content string, beginMark string, endMark string, seq [
}

// signature example: /*<begin ident>*/ {content} /*<end ident>*/
func insertConentNoDudplicate(content string, beginMark string, endMark string, idx int, insertContent string) string {
func insertContentNoDuplicate(content string, beginMark string, endMark string, idx int, insertContent string) string {
if insertContent == "" {
return content
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/patch_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func addRuntimeFunctions(goroot string, goVersion *goinfo.GoVersion, xgoSrc stri

// add debug file
// rational: when debugging, dlv will jump to __xgo_autogen_register_func_helper.go
// previousely this file does not exist, making the debugging blind
// previously this file does not exist, making the debugging blind
runtimeAutoGenFile := filepath.Join(goroot, "src", "runtime", "__xgo_autogen_register_func_helper.go")
srcAutoGen := getInternalPatch(goroot, "syntax", "helper_code.go")
err = filecopy.CopyFile(srcAutoGen, runtimeAutoGenFile)
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/pathsum/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestProcessSpecial(t *testing.T) {
for _, testCase := range testCases {
res := processSpecial(testCase.Arg)
if res != testCase.Res {
t.Fatalf("exepct processSpecial(%q) = %q, actual: %q", testCase.Arg, testCase.Res, res)
t.Fatalf("expect processSpecial(%q) = %q, actual: %q", testCase.Arg, testCase.Res, res)
}
}
}
2 changes: 1 addition & 1 deletion patch/adapter_go1.17_18_19_20_21.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ func NewNameAt(pos src.XPos, sym *types.Sym, typ *types.Type) *ir.Name {
return n
}

const closureMayBeEleminatedDueToIfConst = true
const closureMayBeEliminatedDueToIfConst = true
2 changes: 1 addition & 1 deletion patch/adapter_go1.22.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const goMajor = 1
const goMinor = 22

const genericTrapNeedsWorkaround = true
const closureMayBeEleminatedDueToIfConst = false
const closureMayBeEliminatedDueToIfConst = false

func forEachFunc(callback func(fn *ir.Func) bool) {
for _, fn := range typecheck.Target.Funcs {
Expand Down
2 changes: 1 addition & 1 deletion patch/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewBoolLit(pos src.XPos, b bool) ir.Node {
return NewBasicLit(pos, types.Types[types.TBOOL], constant.MakeBool(b))
}

// how to delcare a new function?
// how to declare a new function?
// init names are usually init.0, init.1, ...
//
// NOTE: when there is already an init function, declare new init function
Expand Down
2 changes: 1 addition & 1 deletion patch/syntax/func_stub_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func init() {

// helperCode: helps us(xgo) write IR more easily
// NOTE: normal code should never define names starts with __xgo
// TODO: if we encounter any of these name conflics, we skip this file or use a different name
// TODO: if we encounter any of these name conflicts, we skip this file or use a different name
const helperCode = helperCodeGen
5 changes: 3 additions & 2 deletions patch/syntax/helper_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func __xgo_link_generate_init_regs_body() {
// no matther whether you have replaced it or not
// panic("failed to link __xgo_link_generate_init_regs_body")
}

func __xgo_link_trap_for_generated(pkgPath string, pc uintptr, identityName string, generic bool, recv interface{}, args []interface{}, results []interface{}) (func(), bool) {
// linked by compiler
return nil, false
Expand All @@ -51,8 +52,8 @@ func __xgo_link_generated_register_func(fn interface{}) {
panic("failed to link __xgo_link_generated_register_func")
}

func __xgo_local_register_func(pkgPath string, identiyName string, fn interface{}, closure bool, recvName string, argNames []string, resNames []string, file string, line int) {
__xgo_link_generated_register_func(__xgo_local_func_stub{PkgPath: pkgPath, IdentityName: identiyName, Fn: fn, Closure: closure, RecvName: recvName, ArgNames: argNames, ResNames: resNames, File: file, Line: line})
func __xgo_local_register_func(pkgPath string, identityName string, fn interface{}, closure bool, recvName string, argNames []string, resNames []string, file string, line int) {
__xgo_link_generated_register_func(__xgo_local_func_stub{PkgPath: pkgPath, IdentityName: identityName, Fn: fn, Closure: closure, RecvName: recvName, ArgNames: argNames, ResNames: resNames, File: file, Line: line})
}

// not used
Expand Down
5 changes: 3 additions & 2 deletions patch/syntax/helper_code_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions patch/trap.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ func initClosureRegs() {
resNames := getFieldNames(fn, fnType.Results())
// TODO: why fn.Name() always returns nil?
var fnRef ir.Node
if !closureMayBeEleminatedDueToIfConst {
if !closureMayBeEliminatedDueToIfConst {
fnRef = convToEFace(pos, fn.Nname, fn.Type(), false)
} else {
// closure may be elminated
// closure may be eliminated
fnRef = newNilInterface(pos)
}
if false {
Expand Down
2 changes: 1 addition & 1 deletion runtime/mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Mock exposes 3 primary APIs to users:

- `MockMethodByName(instance, name, interceptor)` - for **unexported** method

Under 99% circumstances, developer should use `Mock` as long as possible because it does not invovle hard coded name or package path.
Under 99% circumstances, developer should use `Mock` as long as possible because it does not involve hard coded name or package path.

The later two, `MockByName` and `MockMethodByName` are used where the target method cannot be accessed due to unexported, so they must be referenced by hard coded strings.

Expand Down
2 changes: 1 addition & 1 deletion runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// debug test is a convienent package
// debug test is a convenient package
// you can paste your minimal code your
// to focus only the problemtic part of
// failing code
Expand Down
6 changes: 3 additions & 3 deletions runtime/test/mock_func/mock_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ func MockFuncTest(t *testing.T) {
}

// should fail on skip
var havePaniced bool
var havePanicked bool
func() {
defer func() {
if e := recover(); e != nil {
havePaniced = true
havePanicked = true
}
}()
mock.Mock(hello_skipped, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error {
return nil
})
}()
if !havePaniced {
if !havePanicked {
t.Fatalf("expect mock on hello_skipped should panic, actual not panic")
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/test/trap_args/closure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestClosureShouldRetrieveCtxInfoAtTrapTime(t *testing.T) {
t.Fatalf("expect trapCtx to be non nil, atcual nil")
}
if trapCtx != ctx {
t.Fatalf("expect trapCtx to be the same with ctx, actully different")
t.Fatalf("expect trapCtx to be the same with ctx, actually different")
}
return nil
})
Expand All @@ -47,7 +47,7 @@ func TestClosureUnnamedArgShouldRetrieveCtxInfo(t *testing.T) {
t.Fatalf("expect trapCtx to be non nil, atcual nil")
}
if trapCtx != ctx {
t.Fatalf("expect trapCtx to be the same with ctx, actully different")
t.Fatalf("expect trapCtx to be the same with ctx, actually different")
}
return nil
})
Expand Down
40 changes: 20 additions & 20 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const __XGO_SKIP_TRAP = true

// hold goroutine stacks, keyed by goroutine ptr
var stackMap sync.Map // uintptr(goroutine) -> *Root
var testInfoMaping sync.Map // uintptr(goroutine) -> *testInfo
var testInfoMapping sync.Map // uintptr(goroutine) -> *testInfo

type testInfo struct {
name string
Expand All @@ -34,13 +34,13 @@ func init() {
return
}
key := uintptr(__xgo_link_getcurg())
testInfoMaping.LoadOrStore(key, &testInfo{
testInfoMapping.LoadOrStore(key, &testInfo{
name: name,
})
})
__xgo_link_on_goexit(func() {
key := uintptr(__xgo_link_getcurg())
testInfoMaping.Delete(key)
testInfoMapping.Delete(key)
collectingMap.Delete(key)
})
}
Expand Down Expand Up @@ -125,11 +125,11 @@ func setupInterceptor() func() {
Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) {
key := uintptr(__xgo_link_getcurg())
localOptStack, ok := collectingMap.Load(key)
var locaOpts *collectOpts
var localOpts *collectOpts
if ok {
l := localOptStack.(*optStack)
if len(l.list) > 0 {
locaOpts = l.list[len(l.list)-1]
localOpts = l.list[len(l.list)-1]
}
}
stack := &Stack{
Expand All @@ -140,14 +140,14 @@ func setupInterceptor() func() {
var globalRoot interface{}
var localRoot *Root
var initial bool
if locaOpts == nil {
if localOpts == nil {
var globalLoaded bool
globalRoot, globalLoaded = stackMap.Load(key)
if !globalLoaded {
initial = true
}
} else {
localRoot = locaOpts.root
localRoot = localOpts.root
if localRoot == nil {
initial = true
}
Expand All @@ -162,16 +162,16 @@ func setupInterceptor() func() {
},
}
stack.Begin = int64(time.Since(root.Begin))
if locaOpts == nil {
if localOpts == nil {
stackMap.Store(key, root)
} else {
locaOpts.root = root
localOpts.root = root
}
// NOTE: for initial stack, the data is nil
return nil, nil
}
var root *Root
if locaOpts != nil {
if localOpts != nil {
root = localRoot
} else {
root = globalRoot.(*Root)
Expand All @@ -187,19 +187,19 @@ func setupInterceptor() func() {
key := uintptr(__xgo_link_getcurg())

localOptStack, ok := collectingMap.Load(key)
var locaOpts *collectOpts
var localOpts *collectOpts
if ok {
l := localOptStack.(*optStack)
if len(l.list) > 0 {
locaOpts = l.list[len(l.list)-1]
localOpts = l.list[len(l.list)-1]
}
}
var root *Root
if locaOpts != nil {
if locaOpts.root == nil {
if localOpts != nil {
if localOpts.root == nil {
panic(fmt.Errorf("unbalanced stack"))
}
root = locaOpts.root
root = localOpts.root
} else {
v, ok := stackMap.Load(key)
if !ok {
Expand Down Expand Up @@ -228,12 +228,12 @@ func setupInterceptor() func() {
root.Top.End = int64(time.Since(root.Begin))
if data == nil {
// stack finished
if locaOpts != nil {
if locaOpts.onComplete != nil {
locaOpts.onComplete(root)
if localOpts != nil {
if localOpts.onComplete != nil {
localOpts.onComplete(root)
return nil
}
err := emitTrace(locaOpts.name, root)
err := emitTrace(localOpts.name, root)
if err != nil {
return err
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func fmtStack(root *Root) (data []byte, err error) {
func emitTrace(name string, root *Root) error {
if name == "" {
key := uintptr(__xgo_link_getcurg())
tinfo, ok := testInfoMaping.Load(key)
tinfo, ok := testInfoMapping.Load(key)
if ok {
name = tinfo.(*testInfo).name
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/trap/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const methodSuffix = "-fm"
// is bound method
// It can be used to get the unwrapped innermost function of a method
// wrapper.
// if f is a bound method, then guranteed that recvPtr cannot be nil
// if f is a bound method, then guaranteed that recvPtr cannot be nil
func Inspect(f interface{}) (recvPtr interface{}, funcInfo *core.FuncInfo) {
recvPtr, funcInfo, _, _ = InspectPC(f)
return
Expand Down
Loading

0 comments on commit e65cc0c

Please sign in to comment.