Class: CodeTools::AST::RescueCondition

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, conditions, body, nxt) ⇒ RescueCondition

Returns a new instance of RescueCondition.



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rubinius/code/ast/exceptions.rb', line 373

def initialize(line, conditions, body, nxt)
  @line = line
  @next = nxt
  @splat = nil
  @assignment = nil

  case conditions
  when ArrayLiteral
    @conditions = conditions
  when ConcatArgs
    @conditions = conditions.array
    @splat = RescueSplat.new line, conditions.rest
  when SplatValue
    @splat = RescueSplat.new line, conditions.value
  when nil
    condition = ConstantAccess.new line, :StandardError, true
    @conditions = ArrayLiteral.new line, [condition]
  end

  case body
  when Block
    @assignment = body.array.shift if assignment? body.array.first
    @body = body.array.size == 1 ? body.array.first : body
  when nil
    @body = NilLiteral.new line
  else
    if assignment? body
      @assignment = body
      @body = NilLiteral.new line
    else
      @body = body
    end
  end
end

Instance Attribute Details

#assignmentObject

Returns the value of attribute assignment.



371
372
373
# File 'lib/rubinius/code/ast/exceptions.rb', line 371

def assignment
  @assignment
end

#bodyObject

Returns the value of attribute body.



371
372
373
# File 'lib/rubinius/code/ast/exceptions.rb', line 371

def body
  @body
end

#conditionsObject

Returns the value of attribute conditions.



371
372
373
# File 'lib/rubinius/code/ast/exceptions.rb', line 371

def conditions
  @conditions
end

#nextObject

Returns the value of attribute next.



371
372
373
# File 'lib/rubinius/code/ast/exceptions.rb', line 371

def next
  @next
end

#splatObject

Returns the value of attribute splat.



371
372
373
# File 'lib/rubinius/code/ast/exceptions.rb', line 371

def splat
  @splat
end

Instance Method Details

#assignment?(node) ⇒ Boolean

Returns:

  • (Boolean)


408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/rubinius/code/ast/exceptions.rb', line 408

def assignment?(node)
  case node
  when VariableAssignment
    value = node.value
  when AttributeAssignment
    value = node.arguments.array.last
  else
    return false
  end

  return true if value.kind_of? CurrentException
end

#bytecode(g, reraise, done, outer_exc_state) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/rubinius/code/ast/exceptions.rb', line 421

def bytecode(g, reraise, done, outer_exc_state)
  pos(g)
  body = g.new_label

  # Exception has left the current exception on the top
  # of the stack, use it rather than using push_current_exception.

  if @conditions
    @conditions.body.each do |c|
      g.dup
      c.bytecode(g)
      g.swap
      g.send :===, 1
      g.goto_if_true body
    end
  end

  @splat.bytecode(g, body) if @splat

  if @next
    if_false = g.new_label
    g.goto if_false
  else
    g.goto reraise
  end

  body.set!

  # Remove the current exception from the top of the stack now
  # that we've hit a handler
  g.pop

  if @assignment
    @assignment.bytecode(g)
    g.pop
  end

  current_break = g.break
  g.break = g.new_label

  current_next = g.next
  g.next = g.new_label

  g.state.push_rescue(outer_exc_state)
  @body.bytecode(g)
  g.state.pop_rescue

  g.clear_exception
  g.goto done

  if g.break.used?
    g.break.set!
    g.clear_exception

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

    if current_break
      g.goto current_break
    else
      g.raise_break
    end
  end

  g.break = current_break

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

    g.clear_exception

    # 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 @next
    if_false.set!
    @next.bytecode(g, reraise, done, outer_exc_state)
  end
end

#to_sexpObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/rubinius/code/ast/exceptions.rb', line 512

def to_sexp
  sexp = [:resbody]

  if @conditions
    array = @conditions.to_sexp
    sexp << array
  else
    array = sexp
  end

  array << @splat.to_sexp if @splat
  array << @assignment.to_sexp if @assignment

  case @body
  when Block
    sexp << (@body ? @body.array.map { |x| x.to_sexp } : nil)
  when nil
    sexp << nil
  else
    sexp << @body.to_sexp
  end

  sexp << @next.to_sexp if @next

  sexp
end