Class: SyntaxTree::YARV::SplatArray

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

Overview

### Summary

splatarray coerces the array object at the top of the stack into Array by calling to_a. It pushes a duplicate of the array if there is a flag, and the original array if there isn’t one.

### Usage

~~~ruby x = *(5) ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flag) ⇒ SplatArray

Returns a new instance of SplatArray.



5083
5084
5085
# File 'lib/syntax_tree/yarv/instructions.rb', line 5083

def initialize(flag)
  @flag = flag
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5081
5082
5083
# File 'lib/syntax_tree/yarv/instructions.rb', line 5081

def flag
  @flag
end

Instance Method Details

#call(vm) ⇒ Object



5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
# File 'lib/syntax_tree/yarv/instructions.rb', line 5111

def call(vm)
  value = vm.pop

  vm.push(
    if Array === value
      value.instance_of?(Array) ? value.dup : Array[*value]
    elsif value.nil?
      value.to_a
    else
      if value.respond_to?(:to_a, true)
        result = value.to_a

        if result.nil?
          [value]
        elsif !result.is_a?(Array)
          raise TypeError, "expected to_a to return an Array"
        end
      else
        [value]
      end
    end
  )
end

#canonicalObject



5107
5108
5109
# File 'lib/syntax_tree/yarv/instructions.rb', line 5107

def canonical
  self
end

#disasm(fmt) ⇒ Object



5087
5088
5089
# File 'lib/syntax_tree/yarv/instructions.rb', line 5087

def disasm(fmt)
  fmt.instruction("splatarray", [fmt.object(flag)])
end

#lengthObject



5095
5096
5097
# File 'lib/syntax_tree/yarv/instructions.rb', line 5095

def length
  2
end

#popsObject



5099
5100
5101
# File 'lib/syntax_tree/yarv/instructions.rb', line 5099

def pops
  1
end

#pushesObject



5103
5104
5105
# File 'lib/syntax_tree/yarv/instructions.rb', line 5103

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5091
5092
5093
# File 'lib/syntax_tree/yarv/instructions.rb', line 5091

def to_a(_iseq)
  [:splatarray, flag]
end