Skip to content

Commit

Permalink
allow trap and cancel while init, see #55 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 14, 2024
1 parent a9cbbd9 commit 75b44bd
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 123 deletions.
24 changes: 24 additions & 0 deletions cmd/xgo/patch/runtime_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ if os.Getenv("XGO_COMPILER_ENABLE")=="true" {
}
p.file = file
noders = append(noders, p)
// move to head
n := len(noders)
for i:=n-1;i>0;i--{
noders[i]=noders[i-1]
}
noders[0]=p
return file
})
}
Expand All @@ -114,6 +122,14 @@ if os.Getenv("XGO_COMPILER_ENABLE")=="true" {
}
p.file = file
noders = append(noders, p)
// move to head
n := len(noders)
for i:=n-1;i>0;i--{
noders[i]=noders[i-1]
}
noders[0]=p
return file
})
}
Expand All @@ -136,6 +152,14 @@ if os.Getenv("XGO_COMPILER_ENABLE")=="true" {
}
p.file = file
noders = append(noders, p)
// move to head
n := len(noders)
for i:=n-1;i>0;i--{
noders[i]=noders[i-1]
}
noders[0]=p
return file
})
}
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.24"
const REVISION = "b0dd1873bc3e5e5464f55c7b01a077712dc4c818+1"
const NUMBER = 180
const REVISION = "a9cbbd937997b1473a5f70f0131927adcdbf3b79+1"
const NUMBER = 181

func getRevision() string {
revSuffix := ""
Expand Down
11 changes: 2 additions & 9 deletions patch/syntax/helper_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ type __xgo_local_func_stub struct {
Line int
}

// ensure early init
var _ = func() bool {
__xgo_trap_skip()
func init() {
__xgo_link_generate_init_regs_body()
return true
}()

// func init() {
// __xgo_link_generate_init_regs_body()
// }
}

// TODO: ensure safety for this
func __xgo_link_generate_init_regs_body() {
Expand Down
11 changes: 2 additions & 9 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 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.24"
const REVISION = "b0dd1873bc3e5e5464f55c7b01a077712dc4c818+1"
const NUMBER = 180
const REVISION = "a9cbbd937997b1473a5f70f0131927adcdbf3b79+1"
const NUMBER = 181

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
3 changes: 2 additions & 1 deletion runtime/functab/functab.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ func registerFuncInfo(fnInfo interface{}) {
}
// fmt.Fprintf(os.Stderr, "empty name\n",pkgPath)
}
pkgPath := rv.FieldByName("PkgPath").String()
// fmt.Printf("register: %s %s\n", pkgPath, identityName)
var fnKind core.Kind
fnKindV := rv.FieldByName("Kind")
if fnKindV.IsValid() {
fnKind = core.Kind(fnKindV.Int())
}
varField := rv.FieldByName("Var")
pkgPath := rv.FieldByName("PkgPath").String()
recvTypeName := rv.FieldByName("RecvTypeName").String()
recvPtr := rv.FieldByName("RecvPtr").Bool()
name := rv.FieldByName("Name").String()
Expand Down
55 changes: 55 additions & 0 deletions runtime/test/trap/trap_init/trap_init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package trap_init

import (
"bytes"
"context"
"fmt"
"testing"

"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/trap"
)

var trapBuf bytes.Buffer
var initA string

func init() {
cancel := trap.AddInterceptor(&trap.Interceptor{
Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (data interface{}, err error) {
trapBuf.WriteString(fmt.Sprintf("call %s\n", f.IdentityName))
if f.IdentityName == "B" {
result.GetFieldIndex(0).Set("mock B")
return nil, trap.ErrAbort
}
return
},
})
defer cancel()
initA = A()
}

func TestTrapInsideInit(t *testing.T) {
str := trapBuf.String()
expectTrapBuf := "call A\ncall B\n"
if str != expectTrapBuf {
t.Fatalf("expect trap buf: %q, actual: %q", expectTrapBuf, str)
}
expectInitA := "A:mock B"
if initA != expectInitA {
t.Fatalf("expect initA: %q, actual: %q", expectInitA, initA)
}

// check if the interceptor is cancelled
a := A()
expectA := "A:B"
if a != expectA {
t.Fatalf("expect a: %q, actual: %q", expectA, a)
}
}

func A() string {
return "A:" + B()
}
func B() string {
return "B"
}
Loading

0 comments on commit 75b44bd

Please sign in to comment.