Class: SyntaxTree::YARV::SplatArray

Inherits:
Instruction 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

Methods inherited from Instruction

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

Constructor Details

#initialize(flag) ⇒ SplatArray

Returns a new instance of SplatArray.



5583
5584
5585
# File 'lib/syntax_tree/yarv/instructions.rb', line 5583

def initialize(flag)
  @flag = flag
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5581
5582
5583
# File 'lib/syntax_tree/yarv/instructions.rb', line 5581

def flag
  @flag
end

Instance Method Details

#==(other) ⇒ Object



5599
5600
5601
# File 'lib/syntax_tree/yarv/instructions.rb', line 5599

def ==(other)
  other.is_a?(SplatArray) && other.flag == flag
end

#call(vm) ⇒ Object



5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
# File 'lib/syntax_tree/yarv/instructions.rb', line 5615

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

#deconstruct_keys(_keys) ⇒ Object



5595
5596
5597
# File 'lib/syntax_tree/yarv/instructions.rb', line 5595

def deconstruct_keys(_keys)
  { flag: flag }
end

#disasm(fmt) ⇒ Object



5587
5588
5589
# File 'lib/syntax_tree/yarv/instructions.rb', line 5587

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

#lengthObject



5603
5604
5605
# File 'lib/syntax_tree/yarv/instructions.rb', line 5603

def length
  2
end

#popsObject



5607
5608
5609
# File 'lib/syntax_tree/yarv/instructions.rb', line 5607

def pops
  1
end

#pushesObject



5611
5612
5613
# File 'lib/syntax_tree/yarv/instructions.rb', line 5611

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5591
5592
5593
# File 'lib/syntax_tree/yarv/instructions.rb', line 5591

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