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.



5424
5425
5426
# File 'lib/syntax_tree/yarv/instructions.rb', line 5424

def initialize(flag)
  @flag = flag
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5422
5423
5424
# File 'lib/syntax_tree/yarv/instructions.rb', line 5422

def flag
  @flag
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object



5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
# File 'lib/syntax_tree/yarv/instructions.rb', line 5456

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



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

def deconstruct_keys(_keys)
  { flag: flag }
end

#disasm(fmt) ⇒ Object



5428
5429
5430
# File 'lib/syntax_tree/yarv/instructions.rb', line 5428

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

#lengthObject



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

def length
  2
end

#popsObject



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

def pops
  1
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5432
5433
5434
# File 'lib/syntax_tree/yarv/instructions.rb', line 5432

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