Class: SyntaxTree::YARV::OptLength

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

opt_length is a specialization of opt_send_without_block, when the length method is called. There are fast paths when the receiver is either a string, hash, or array. It pops the receiver off the stack and pushes on the result of the method call.

### Usage

~~~ruby “”.length ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calldata) ⇒ OptLength

Returns a new instance of OptLength.



3643
3644
3645
# File 'lib/syntax_tree/yarv/instructions.rb', line 3643

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3641
3642
3643
# File 'lib/syntax_tree/yarv/instructions.rb', line 3641

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



3659
3660
3661
# File 'lib/syntax_tree/yarv/instructions.rb', line 3659

def ==(other)
  other.is_a?(OptLength) && other.calldata == calldata
end

#call(vm) ⇒ Object



3679
3680
3681
# File 'lib/syntax_tree/yarv/instructions.rb', line 3679

def call(vm)
  canonical.call(vm)
end

#canonicalObject



3675
3676
3677
# File 'lib/syntax_tree/yarv/instructions.rb', line 3675

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



3655
3656
3657
# File 'lib/syntax_tree/yarv/instructions.rb', line 3655

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



3647
3648
3649
# File 'lib/syntax_tree/yarv/instructions.rb', line 3647

def disasm(fmt)
  fmt.instruction("opt_length", [fmt.calldata(calldata)])
end

#lengthObject



3663
3664
3665
# File 'lib/syntax_tree/yarv/instructions.rb', line 3663

def length
  2
end

#popsObject



3667
3668
3669
# File 'lib/syntax_tree/yarv/instructions.rb', line 3667

def pops
  1
end

#pushesObject



3671
3672
3673
# File 'lib/syntax_tree/yarv/instructions.rb', line 3671

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3651
3652
3653
# File 'lib/syntax_tree/yarv/instructions.rb', line 3651

def to_a(_iseq)
  [:opt_length, calldata.to_h]
end