Class: CodeTools::AST::Ensure

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/ast/exceptions.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, body, ensr) ⇒ Ensure

Returns a new instance of Ensure.



32
33
34
35
36
# File 'lib/rubinius/code/ast/exceptions.rb', line 32

def initialize(line, body, ensr)
  @line = line
  @body = body || NilLiteral.new(line)
  @ensure = ensr
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



30
31
32
# File 'lib/rubinius/code/ast/exceptions.rb', line 30

def body
  @body
end

#ensureObject

Returns the value of attribute ensure.



30
31
32
# File 'lib/rubinius/code/ast/exceptions.rb', line 30

def ensure
  @ensure
end

Instance Method Details

#bytecode(g) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rubinius/code/ast/exceptions.rb', line 38

def bytecode(g)
  pos(g)

  ok = g.new_label
  ex = g.new_label
  g.setup_unwind ex, EnsureType

  # TODO: ?
  g.new_label.set!

  g.push_exception_state
  outer_exc_state = g.new_stack_local
  g.set_stack_local outer_exc_state
  g.pop

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

  old_next = g.next
  new_next = g.new_label
  g.next = new_next

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

  g.break = old_break
  g.next = old_next

  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

  check_next = nil

  if new_next.used?
    used_next_local = g.new_stack_local
    check_next = g.new_label

    new_next.set!
    g.pop_unwind

    g.push_true
    g.set_stack_local used_next_local
    g.pop

    g.goto check_next
  end

  ex.set!

  g.push_exception_state

  g.state.push_inside_ensure
  g.state.push_rescue(outer_exc_state)
  @ensure.bytecode(g)
  g.state.pop_rescue
  g.pop

  g.restore_exception_state

  # Re-raise the exception
  g.reraise

  # There is no mapping to the original source here, so
  # reflect that in the lines table to try and make it
  # more accurate.
  g.set_line 0

  ok.set!

  if check_break
    g.push_false
    g.set_stack_local used_break_local
    g.pop

    check_break.set!
  end

  if check_next
    g.push_false
    g.set_stack_local used_next_local
    g.pop

    check_next.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.goto_if_false post

    if g.break
      g.goto g.break
    else
      g.raise_break
    end
    post.set!
  end

  if check_next
    post = g.new_label

    g.push_stack_local used_next_local
    g.goto_if_false post

    g.next ? g.goto(g.next) : g.ret
    post.set!
  end
  g.state.pop_inside_ensure
end

#to_sexpObject



172
173
174
# File 'lib/rubinius/code/ast/exceptions.rb', line 172

def to_sexp
  [:ensure, @body.to_sexp, @ensure.to_sexp]
end