Class: CodeTools::AST::Rescue

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, 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, rescue_body, else_body) ⇒ Rescue

Returns a new instance of Rescue.



182
183
184
185
186
187
# File 'lib/rubinius/code/ast/exceptions.rb', line 182

def initialize(line, body, rescue_body, else_body)
  @line = line
  @body = body
  @rescue = rescue_body
  @else = else_body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



180
181
182
# File 'lib/rubinius/code/ast/exceptions.rb', line 180

def body
  @body
end

#elseObject

Returns the value of attribute else.



180
181
182
# File 'lib/rubinius/code/ast/exceptions.rb', line 180

def else
  @else
end

#rescueObject

Returns the value of attribute rescue.



180
181
182
# File 'lib/rubinius/code/ast/exceptions.rb', line 180

def rescue
  @rescue
end

Instance Method Details

#bytecode(g) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/rubinius/code/ast/exceptions.rb', line 189

def bytecode(g)
  pos(g)

  g.push_modifiers
  if @body.nil?
    if @else.nil?
      # Stupid. No body and no else.
      g.push_tagged_nil 0
    else
      # Only an else, run it.
      @else.bytecode(g)
    end
  else
    outer_retry = g.retry

    this_retry = g.new_label
    reraise = g.new_label
    els     = g.new_label
    done    = g.new_label

    # Save the current exception into a stack local
    g.push_exception_state
    outer_exc_state = g.new_stack_local
    g.set_stack_local outer_exc_state
    g.pop

    this_retry.set!
    ex = g.new_label
    g.setup_unwind ex, RescueType

    # TODO: ?
    g.new_label.set!

    if current_break = g.break
      # Make a break available to use, which we'll use to
      # lazily generate a cleanup area
      g.break = g.new_label
    end

    # Setup a lazy cleanup area for next'ing out of the handler
    current_next = g.next
    g.next = g.new_label

    # Use a lazy label to patch up prematuraly leaving a begin
    # body via retry.
    if outer_retry
      g.retry = g.new_label
    end

    # Also handle redo unwinding through the rescue
    if current_redo = g.redo
      g.redo = g.new_label
    end

    @body.bytecode(g)
    g.pop_unwind
    g.goto els

    if current_break
      if g.break.used?
        g.break.set!
        g.pop_unwind

        # Reset the outer exception
        g.push_stack_local outer_exc_state
        g.restore_exception_state

        g.goto current_break
      end

      g.break = current_break
    end

    if g.next.used?
      g.next.set!
      g.pop_unwind

      # Reset the outer exception
      g.push_stack_local outer_exc_state
      g.restore_exception_state

      if current_next
        g.goto current_next
      else
        g.ret
      end
    end

    g.next = current_next

    if current_redo
      if g.redo.used?
        g.redo.set!
        g.pop_unwind

        # Reset the outer exception
        g.push_stack_local outer_exc_state
        g.restore_exception_state

        g.goto current_redo
      end

      g.redo = current_redo
    end

    if outer_retry
      if g.retry.used?
        g.retry.set!
        g.pop_unwind

        # Reset the outer exception
        g.push_stack_local outer_exc_state
        g.restore_exception_state

        g.goto outer_retry
      end

      g.retry = outer_retry
    end

    g.set_line 0

    # We jump here if an exception has occured in the body
    ex.set!

    # Expose the retry label here only, not before this.
    g.retry = this_retry

    # Save exception state to use in reraise
    g.push_exception_state

    raised_exc_state = g.new_stack_local
    g.set_stack_local raised_exc_state
    g.pop

    # Save the current exception, so that calling #=== can't trample
    # it.
    g.push_current_exception

    @rescue.bytecode(g, reraise, done, outer_exc_state)
    reraise.set!

    # Restore the exception state we saved and the reraise. The act
    # of checking if an exception matches can run any code, which
    # can easily trample on the current exception.
    #
    # Remove the direct exception so we can get to the state
    g.pop

    # Restore the state and reraise
    g.push_stack_local raised_exc_state
    g.restore_exception_state
    g.reraise

    els.set!
    if @else
      g.pop
      @else.bytecode(g)
    end

    g.set_line 0
    done.set!

    g.push_stack_local outer_exc_state
    g.restore_exception_state
  end
  g.pop_modifiers
end

#defined(g) ⇒ Object



358
359
360
361
# File 'lib/rubinius/code/ast/exceptions.rb', line 358

def defined(g)
  @body.defined(g) if @body
  g.push_literal "nil"
end

#to_sexpObject



363
364
365
366
367
# File 'lib/rubinius/code/ast/exceptions.rb', line 363

def to_sexp
  sexp = [:rescue, @body.to_sexp, @rescue.to_sexp]
  sexp << @else.to_sexp if @else
  sexp
end