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.



3373
3374
3375
# File 'lib/syntax_tree/yarv/instructions.rb', line 3373

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3371
3372
3373
# File 'lib/syntax_tree/yarv/instructions.rb', line 3371

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



3389
3390
3391
# File 'lib/syntax_tree/yarv/instructions.rb', line 3389

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

#call(vm) ⇒ Object



3409
3410
3411
# File 'lib/syntax_tree/yarv/instructions.rb', line 3409

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

#canonicalObject



3405
3406
3407
# File 'lib/syntax_tree/yarv/instructions.rb', line 3405

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



3385
3386
3387
# File 'lib/syntax_tree/yarv/instructions.rb', line 3385

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



3377
3378
3379
# File 'lib/syntax_tree/yarv/instructions.rb', line 3377

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

#lengthObject



3393
3394
3395
# File 'lib/syntax_tree/yarv/instructions.rb', line 3393

def length
  2
end

#popsObject



3397
3398
3399
# File 'lib/syntax_tree/yarv/instructions.rb', line 3397

def pops
  1
end

#pushesObject



3401
3402
3403
# File 'lib/syntax_tree/yarv/instructions.rb', line 3401

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3381
3382
3383
# File 'lib/syntax_tree/yarv/instructions.rb', line 3381

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