Skip to content

Commit d18c07a

Browse files
authored
Ensure "exit" without status code doesn't set exit code (#193)
Also update stringer version, as I was getting this error with the current version for some reason: golang/go#52710
1 parent 05005d6 commit d18c07a

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

internal/compiler/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,10 @@ func (c *compiler) stmt(stmt ast.Stmt) {
462462
case *ast.ExitStmt:
463463
if s.Status != nil {
464464
c.expr(s.Status)
465+
c.add(ExitStatus)
465466
} else {
466-
c.expr(&ast.NumExpr{0})
467+
c.add(Exit)
467468
}
468-
c.add(Exit)
469469

470470
case *ast.DeleteStmt:
471471
scope, index := c.arrayInfo(s.Array)

internal/compiler/opcode_string.go

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/compiler/opcodes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package compiler
22

3-
//go:generate go run golang.org/x/tools/cmd/stringer@v0.1.8 -type=Opcode,AugOp,BuiltinOp
3+
//go:generate go run golang.org/x/tools/cmd/stringer@v0.10.0 -type=Opcode,AugOp,BuiltinOp
44

55
// Opcode represents a single virtual machine instruction (or argument). The
66
// comments beside each opcode show any arguments that instruction consumes.
@@ -109,6 +109,7 @@ const (
109109
Next
110110
Nextfile
111111
Exit
112+
ExitStatus
112113
ForIn // varScope varIndex arrayScope arrayIndex offset
113114
BreakForIn
114115

interp/interp_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,9 @@ func TestExit(t *testing.T) {
16071607
{`{ print "x"; exit 1+2; print "y" } END { print "z" }`, "x\nz\n", 3},
16081608
{`END { print "x"; exit; print "y" }`, "x\n", 0},
16091609
{`END { print "x"; exit 1+2; print "y" }`, "x\n", 3},
1610+
{`BEGIN { print "x"; exit 42 } END { print "z"; exit }`, "x\nz\n", 42},
1611+
{`BEGIN { print "x"; exit } END { print "z"; exit 43 }`, "x\nz\n", 43},
1612+
{`BEGIN { print "x"; exit 44 } { print "a"; exit }`, "x\n", 44},
16101613
}
16111614
for _, test := range tests {
16121615
t.Run(test.src, func(t *testing.T) {

interp/vm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,13 @@ func (p *interp) execute(code []compiler.Opcode) error {
591591
return errNextfile
592592

593593
case compiler.Exit:
594-
p.exitStatus = int(p.pop().num())
595594
// Return special errExit value "caught" by top-level executor
596595
return errExit
597596

597+
case compiler.ExitStatus:
598+
p.exitStatus = int(p.pop().num())
599+
return errExit
600+
598601
case compiler.ForIn:
599602
varScope := code[ip]
600603
varIndex := code[ip+1]

0 commit comments

Comments
 (0)