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

compileExpr: tryGopExec (see TestSpxGopExec) #1741

Merged
merged 9 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 1 addition & 7 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,10 @@ func TestGetTypeName(t *testing.T) {

func TestHandleRecover(t *testing.T) {
var ctx pkgCtx
ctx.handleRecover("hello")
ctx.handleRecover("hello", nil)
if !(len(ctx.errs) == 1 && ctx.errs[0].Error() == "hello") {
t.Fatal("TestHandleRecover failed:", ctx.errs)
}
defer func() {
if e := recover(); e == nil {
t.Fatal("TestHandleRecover failed: no error?")
}
}()
ctx.handleRecover(100)
}

func TestCheckCommandWithoutArgs(t *testing.T) {
Expand Down
27 changes: 18 additions & 9 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (p *pkgCtx) loadSymbol(name string) bool {
if enableRecover {
defer func() {
if e := recover(); e != nil {
p.handleRecover(e)
p.handleRecover(e, nil)
}
}()
}
Expand All @@ -443,16 +443,22 @@ func (p *pkgCtx) loadSymbol(name string) bool {
return false
}

func (p *pkgCtx) handleRecover(e interface{}) {
func (p *pkgCtx) handleRecover(e interface{}, src ast.Node) {
err := p.recoverErr(e, src)
p.handleErr(err)
}

func (p *pkgCtx) recoverErr(e interface{}, src ast.Node) error {
err, ok := e.(error)
if !ok {
if msg, ok := e.(string); ok {
err = errors.New(msg)
if src != nil {
text := p.LoadExpr(src)
err = p.newCodeErrorf(src.Pos(), "compile `%v`: %v", text, e)
} else {
panic(e)
err = fmt.Errorf("%v", e)
}
}
p.handleErr(err)
return err
}

const (
Expand Down Expand Up @@ -502,7 +508,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
if enableRecover {
defer func() {
if e := recover(); e != nil {
ctx.handleRecover(e)
ctx.handleRecover(e, nil)
err = ctx.errs.ToError()
}
}()
Expand Down Expand Up @@ -1315,7 +1321,7 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
if enableRecover {
defer func() {
if e := recover(); e != nil {
ctx.handleRecover(e)
ctx.handleRecover(e, spec)
}
}()
}
Expand Down Expand Up @@ -1431,7 +1437,10 @@ func loadVars(ctx *blockCtx, v *ast.ValueSpec, doc *ast.CommentGroup, global boo
switch e := val.(type) {
case *ast.LambdaExpr, *ast.LambdaExpr2:
if len(v.Values) == 1 {
sig := checkLambdaFuncType(ctx, e, typ, clLambaAssign, v.Names[0])
sig, err := checkLambdaFuncType(ctx, e, typ, clLambaAssign, v.Names[0])
if err != nil {
panic(err)
}
compileLambda(ctx, e, sig)
} else {
panic(ctx.newCodeErrorf(e.Pos(), "lambda unsupport multiple assignment"))
Expand Down
2 changes: 2 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (this *index) onInit() {
func TestSpxGopExec(t *testing.T) {
gopSpxTest(t, `
vim "a.txt"
ls 10
capout => { ls }
capout => { ls "-l" }
`, ``, `package main
Expand All @@ -272,6 +273,7 @@ type index struct {

func (this *index) MainEntry() {
this.Gop_Exec("vim", "a.txt")
this.Ls(10)
this.Capout(func() {
this.Gop_Exec("ls")
})
Expand Down
2 changes: 1 addition & 1 deletion cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ a := uint128(b)
}

func TestErrCompileFunc(t *testing.T) {
codeErrorTest(t, `bar.gop:2:1: compile func printf("%+v\n", int32) error: unreachable`, `
codeErrorTest(t, "bar.gop:2:1: compile `printf(\"%+v\\n\", int32)`: unreachable", `
printf("%+v\n", int32)
`)
}
Expand Down
Loading