class SyntaxTree::YARV::CheckType

### 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] ~~~

Constants

TYPE_ARRAY
TYPE_BIGNUM
TYPE_CLASS
TYPE_COMPLEX
TYPE_DATA
TYPE_FALSE
TYPE_FILE
TYPE_FIXNUM
TYPE_FLOAT
TYPE_HASH
TYPE_MATCH
TYPE_MODULE
TYPE_NIL
TYPE_OBJECT
TYPE_RATIONAL
TYPE_REGEXP
TYPE_STRING
TYPE_STRUCT
TYPE_SYMBOL
TYPE_TRUE
TYPE_UNDEF

Attributes

type[R]

Public Class Methods

new(type) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 526
def initialize(type)
  @type = type
end

Public Instance Methods

==(other) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 588
def ==(other)
  other.is_a?(CheckType) && other.type == type
end
call(vm) click to toggle source
# 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) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 584
def deconstruct_keys(_keys)
  { type: type }
end
disasm(fmt) click to toggle source
# 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
length() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 592
def length
  2
end
pops() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 596
def pops
  1
end
pushes() click to toggle source
# 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) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 580
def to_a(_iseq)
  [:checktype, type]
end