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.



5482
5483
5484
# File 'lib/syntax_tree/yarv/instructions.rb', line 5482

def initialize(flag)
  @flag = flag
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5480
5481
5482
# File 'lib/syntax_tree/yarv/instructions.rb', line 5480

def flag
  @flag
end

Instance Method Details

#==(other) ⇒ Object



5498
5499
5500
# File 'lib/syntax_tree/yarv/instructions.rb', line 5498

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

#call(vm) ⇒ Object



5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
# File 'lib/syntax_tree/yarv/instructions.rb', line 5514

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



5494
5495
5496
# File 'lib/syntax_tree/yarv/instructions.rb', line 5494

def deconstruct_keys(_keys)
  { flag: flag }
end

#disasm(fmt) ⇒ Object



5486
5487
5488
# File 'lib/syntax_tree/yarv/instructions.rb', line 5486

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

#lengthObject



5502
5503
5504
# File 'lib/syntax_tree/yarv/instructions.rb', line 5502

def length
  2
end

#popsObject



5506
5507
5508
# File 'lib/syntax_tree/yarv/instructions.rb', line 5506

def pops
  1
end

#pushesObject



5510
5511
5512
# File 'lib/syntax_tree/yarv/instructions.rb', line 5510

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5490
5491
5492
# File 'lib/syntax_tree/yarv/instructions.rb', line 5490

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