Skip to content

Commit

Permalink
Save return value and restore the stack before running finally. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Feb 20, 2024
1 parent b396bb4 commit e401ed4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ func (c *compiler) compileReturnStatement(v *ast.ReturnStatement) {
for b := c.block; b != nil; b = b.outer {
switch b.typ {
case blockTry:
c.emit(leaveTry{})
c.emit(saveResult, leaveTry{}, loadResult)
case blockLoopEnum:
c.emit(enumPopClose)
}
Expand Down
14 changes: 14 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,20 @@ func TestReturnOutOfTryNested(t *testing.T) {
testScript(SCRIPT, intToValue(1), t)
}

func TestReturnOutOfTryWithFinally(t *testing.T) {
const SCRIPT = `
function test() {
try {
return 'Hello, world!';
} finally {
const dummy = 'unexpected';
}
}
test();
`
testScript(SCRIPT, asciiString("Hello, world!"), t)
}

func TestContinueLoop(t *testing.T) {
const SCRIPT = `
function A() {
Expand Down
2 changes: 1 addition & 1 deletion runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ func TestStacktraceLocationThrowFromCatch(t *testing.T) {
if frame := stack[0]; frame.funcName != "f2" || frame.pc != 2 {
t.Fatalf("Unexpected stack frame 0: %#v", frame)
}
if frame := stack[1]; frame.funcName != "main" || frame.pc != 15 {
if frame := stack[1]; frame.funcName != "main" || frame.pc != 17 {
t.Fatalf("Unexpected stack frame 1: %#v", frame)
}
if frame := stack[2]; frame.funcName != "" || frame.pc != 7 {
Expand Down
9 changes: 9 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,15 @@ func (_saveResult) exec(vm *vm) {
vm.pc++
}

type _loadResult struct{}

var loadResult _loadResult

func (_loadResult) exec(vm *vm) {
vm.push(vm.result)
vm.pc++
}

type _clearResult struct{}

var clearResult _clearResult
Expand Down

0 comments on commit e401ed4

Please sign in to comment.