Skip to content

Commit

Permalink
Handle early exits from ensures. Fixes #237.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Mar 31, 2010
1 parent bc783d6 commit 85c4b76
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/compiler/ast/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,35 @@ def bytecode(g)
g.set_stack_local outer_exc_state
g.pop

old_break = g.break
new_break = g.new_label
g.break = new_break

g.state.push_ensure
@body.bytecode(g)
g.state.pop_ensure

g.break = old_break

g.pop_unwind
g.goto ok

check_break = nil

if new_break.used?
used_break_local = g.new_stack_local
check_break = g.new_label

new_break.set!
g.pop_unwind

g.push :true
g.set_stack_local used_break_local
g.pop

g.goto check_break
end

ex.set!

g.push_exception_state
Expand All @@ -66,10 +88,33 @@ def bytecode(g)

ok.set!

if check_break
g.push :false
g.set_stack_local used_break_local
g.pop

check_break.set!
end

# Now, re-emit the code for the ensure which will run if there was no
# exception generated.
@ensure.bytecode(g)
g.pop

if check_break
post = g.new_label

g.push_stack_local used_break_local
g.gif post

if g.break
g.goto g.break
else
g.raise_break
end

post.set!
end
end

def to_sexp
Expand Down
47 changes: 47 additions & 0 deletions spec/custom/helpers/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,33 @@ def for_ensure

g.save_exception

old_break = g.break
new_break = g.new_label
g.break = new_break

eb.body.call

g.break = old_break

g.pop_unwind
g.goto ensure_good

check_break = nil

if new_break.used?
used_break_local = g.new_stack_local
check_break = g.new_label

new_break.set!
g.pop_unwind

g.push :true
g.set_stack_local used_break_local
g.pop

g.goto check_break
end

ensure_bad.set!
g.push_exception_state

Expand All @@ -680,7 +703,31 @@ def for_ensure
g.reraise

ensure_good.set!

if check_break
g.push :false
g.set_stack_local used_break_local
g.pop

check_break.set!
end

eb.handler.call

if check_break
post = g.new_label

g.push_stack_local used_break_local
g.gif post

if g.break
g.goto g.break
else
g.raise_break
end

post.set!
end
end

def optional_arg(slot)
Expand Down

0 comments on commit 85c4b76

Please sign in to comment.