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.



5436
5437
5438
# File 'lib/syntax_tree/yarv/instructions.rb', line 5436

def initialize(flag)
  @flag = flag
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5434
5435
5436
# File 'lib/syntax_tree/yarv/instructions.rb', line 5434

def flag
  @flag
end

Instance Method Details

#==(other) ⇒ Object



5452
5453
5454
# File 'lib/syntax_tree/yarv/instructions.rb', line 5452

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

#call(vm) ⇒ Object



5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
# File 'lib/syntax_tree/yarv/instructions.rb', line 5468

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



5448
5449
5450
# File 'lib/syntax_tree/yarv/instructions.rb', line 5448

def deconstruct_keys(_keys)
  { flag: flag }
end

#disasm(fmt) ⇒ Object



5440
5441
5442
# File 'lib/syntax_tree/yarv/instructions.rb', line 5440

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

#lengthObject



5456
5457
5458
# File 'lib/syntax_tree/yarv/instructions.rb', line 5456

def length
  2
end

#popsObject



5460
5461
5462
# File 'lib/syntax_tree/yarv/instructions.rb', line 5460

def pops
  1
end

#pushesObject



5464
5465
5466
# File 'lib/syntax_tree/yarv/instructions.rb', line 5464

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5444
5445
5446
# File 'lib/syntax_tree/yarv/instructions.rb', line 5444

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