class SyntaxTree::YARV::ExpandArray

### Summary

‘expandarray` looks at the top of the stack, and if the value is an array it replaces it on the stack with `number` elements of the array, or `nil` if the elements are missing.

### Usage

~~~ruby x, = [true, false, nil] ~~~

Attributes

flags[R]
number[R]

Public Class Methods

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

Public Instance Methods

==(other) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1412
def ==(other)
  other.is_a?(ExpandArray) && other.number == number &&
    other.flags == flags
end
call(vm) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1429
def call(vm)
  object = vm.pop
  object =
    if Array === object
      object.dup
    elsif object.respond_to?(:to_ary, true)
      object.to_ary
    else
      [object]
    end

  splat_flag = flags & 0x01 > 0
  postarg_flag = flags & 0x02 > 0

  if number == 0 && splat_flag == 0
    # no space left on stack
  elsif postarg_flag
    values = []

    if number > object.size
      (number - object.size).times { values.push(nil) }
    end
    [number, object.size].min.times { values.push(object.pop) }
    values.push(object.to_a) if splat_flag

    values.each { |item| vm.push(item) }
  else
    values = []

    [number, object.size].min.times { values.push(object.shift) }
    if number > values.size
      (number - values.size).times { values.push(nil) }
    end
    values.push(object.to_a) if splat_flag

    values.reverse_each { |item| vm.push(item) }
  end
end
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1408
def deconstruct_keys(_keys)
  { number: number, flags: flags }
end
disasm(fmt) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1400
def disasm(fmt)
  fmt.instruction("expandarray", [fmt.object(number), fmt.object(flags)])
end
length() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1417
def length
  3
end
pops() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1421
def pops
  1
end
pushes() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1425
def pushes
  number
end
to_a(_iseq) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1404
def to_a(_iseq)
  [:expandarray, number, flags]
end