Class: SyntaxTree::YARV::CheckType

Inherits:
Instruction show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

‘checktype` checks if the value on top of the stack is of a certain type. The type is the only argument. It pops the value off the stack and pushes a boolean onto the stack indicating whether or not the value is of the given type.

### Usage

~~~ruby foo in [bar] ~~~

Constant Summary collapse

TYPE_OBJECT =
0x01
TYPE_CLASS =
0x02
TYPE_MODULE =
0x03
TYPE_FLOAT =
0x04
TYPE_STRING =
0x05
TYPE_REGEXP =
0x06
TYPE_ARRAY =
0x07
TYPE_HASH =
0x08
TYPE_STRUCT =
0x09
TYPE_BIGNUM =
0x0a
TYPE_FILE =
0x0b
TYPE_DATA =
0x0c
TYPE_MATCH =
0x0d
TYPE_COMPLEX =
0x0e
TYPE_RATIONAL =
0x0f
TYPE_NIL =
0x11
TYPE_TRUE =
0x12
TYPE_FALSE =
0x13
TYPE_SYMBOL =
0x14
TYPE_FIXNUM =
0x15
TYPE_UNDEF =
0x16

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(type) ⇒ CheckType

Returns a new instance of CheckType.



526
527
528
# File 'lib/syntax_tree/yarv/instructions.rb', line 526

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



524
525
526
# File 'lib/syntax_tree/yarv/instructions.rb', line 524

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



588
589
590
# File 'lib/syntax_tree/yarv/instructions.rb', line 588

def ==(other)
  other.is_a?(CheckType) && other.type == type
end

#call(vm) ⇒ Object



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/syntax_tree/yarv/instructions.rb', line 608

def call(vm)
  object = vm.pop
  result =
    case type
    when TYPE_OBJECT
      raise NotImplementedError, "checktype TYPE_OBJECT"
    when TYPE_CLASS
      object.is_a?(Class)
    when TYPE_MODULE
      object.is_a?(Module)
    when TYPE_FLOAT
      object.is_a?(Float)
    when TYPE_STRING
      object.is_a?(String)
    when TYPE_REGEXP
      object.is_a?(Regexp)
    when TYPE_ARRAY
      object.is_a?(Array)
    when TYPE_HASH
      object.is_a?(Hash)
    when TYPE_STRUCT
      object.is_a?(Struct)
    when TYPE_BIGNUM
      raise NotImplementedError, "checktype TYPE_BIGNUM"
    when TYPE_FILE
      object.is_a?(File)
    when TYPE_DATA
      raise NotImplementedError, "checktype TYPE_DATA"
    when TYPE_MATCH
      raise NotImplementedError, "checktype TYPE_MATCH"
    when TYPE_COMPLEX
      object.is_a?(Complex)
    when TYPE_RATIONAL
      object.is_a?(Rational)
    when TYPE_NIL
      object.nil?
    when TYPE_TRUE
      object == true
    when TYPE_FALSE
      object == false
    when TYPE_SYMBOL
      object.is_a?(Symbol)
    when TYPE_FIXNUM
      object.is_a?(Integer)
    when TYPE_UNDEF
      raise NotImplementedError, "checktype TYPE_UNDEF"
    end

  vm.push(result)
end

#deconstruct_keys(_keys) ⇒ Object



584
585
586
# File 'lib/syntax_tree/yarv/instructions.rb', line 584

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/syntax_tree/yarv/instructions.rb', line 530

def disasm(fmt)
  name =
    case type
    when TYPE_OBJECT
      "T_OBJECT"
    when TYPE_CLASS
      "T_CLASS"
    when TYPE_MODULE
      "T_MODULE"
    when TYPE_FLOAT
      "T_FLOAT"
    when TYPE_STRING
      "T_STRING"
    when TYPE_REGEXP
      "T_REGEXP"
    when TYPE_ARRAY
      "T_ARRAY"
    when TYPE_HASH
      "T_HASH"
    when TYPE_STRUCT
      "T_STRUCT"
    when TYPE_BIGNUM
      "T_BIGNUM"
    when TYPE_FILE
      "T_FILE"
    when TYPE_DATA
      "T_DATA"
    when TYPE_MATCH
      "T_MATCH"
    when TYPE_COMPLEX
      "T_COMPLEX"
    when TYPE_RATIONAL
      "T_RATIONAL"
    when TYPE_NIL
      "T_NIL"
    when TYPE_TRUE
      "T_TRUE"
    when TYPE_FALSE
      "T_FALSE"
    when TYPE_SYMBOL
      "T_SYMBOL"
    when TYPE_FIXNUM
      "T_FIXNUM"
    when TYPE_UNDEF
      "T_UNDEF"
    end

  fmt.instruction("checktype", [name])
end

#lengthObject



592
593
594
# File 'lib/syntax_tree/yarv/instructions.rb', line 592

def length
  2
end

#popsObject



596
597
598
# File 'lib/syntax_tree/yarv/instructions.rb', line 596

def pops
  1
end

#pushesObject



600
601
602
603
604
605
606
# File 'lib/syntax_tree/yarv/instructions.rb', line 600

def pushes
  # TODO: This is incorrect. The instruction only pushes a single value
  # onto the stack. However, if this is set to 1, we no longer match the
  # output of RubyVM::InstructionSequence. So leaving this here until we
  # can investigate further.
  2
end

#to_a(_iseq) ⇒ Object



580
581
582
# File 'lib/syntax_tree/yarv/instructions.rb', line 580

def to_a(_iseq)
  [:checktype, type]
end