Class: SyntaxTree::YARV::CheckType

Inherits:
Object
  • Object
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

Constructor Details

#initialize(type) ⇒ CheckType

Returns a new instance of CheckType.



564
565
566
# File 'lib/syntax_tree/yarv/instructions.rb', line 564

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



562
563
564
# File 'lib/syntax_tree/yarv/instructions.rb', line 562

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



626
627
628
# File 'lib/syntax_tree/yarv/instructions.rb', line 626

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

#call(vm) ⇒ Object



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/syntax_tree/yarv/instructions.rb', line 650

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

#canonicalObject



646
647
648
# File 'lib/syntax_tree/yarv/instructions.rb', line 646

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



622
623
624
# File 'lib/syntax_tree/yarv/instructions.rb', line 622

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/syntax_tree/yarv/instructions.rb', line 568

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



630
631
632
# File 'lib/syntax_tree/yarv/instructions.rb', line 630

def length
  2
end

#popsObject



634
635
636
# File 'lib/syntax_tree/yarv/instructions.rb', line 634

def pops
  1
end

#pushesObject



638
639
640
641
642
643
644
# File 'lib/syntax_tree/yarv/instructions.rb', line 638

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



618
619
620
# File 'lib/syntax_tree/yarv/instructions.rb', line 618

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