Class: SyntaxTree::YARV::OptLength

Inherits:
Instruction 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

Methods inherited from Instruction

#branch_targets, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(calldata) ⇒ OptLength

Returns a new instance of OptLength.



3484
3485
3486
# File 'lib/syntax_tree/yarv/instructions.rb', line 3484

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3482
3483
3484
# File 'lib/syntax_tree/yarv/instructions.rb', line 3482

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



3500
3501
3502
# File 'lib/syntax_tree/yarv/instructions.rb', line 3500

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

#call(vm) ⇒ Object



3520
3521
3522
# File 'lib/syntax_tree/yarv/instructions.rb', line 3520

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

#canonicalObject



3516
3517
3518
# File 'lib/syntax_tree/yarv/instructions.rb', line 3516

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



3496
3497
3498
# File 'lib/syntax_tree/yarv/instructions.rb', line 3496

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



3488
3489
3490
# File 'lib/syntax_tree/yarv/instructions.rb', line 3488

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

#lengthObject



3504
3505
3506
# File 'lib/syntax_tree/yarv/instructions.rb', line 3504

def length
  2
end

#popsObject



3508
3509
3510
# File 'lib/syntax_tree/yarv/instructions.rb', line 3508

def pops
  1
end

#pushesObject



3512
3513
3514
# File 'lib/syntax_tree/yarv/instructions.rb', line 3512

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3492
3493
3494
# File 'lib/syntax_tree/yarv/instructions.rb', line 3492

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