Class: SyntaxTree::YARV::OptNilP

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

Overview

### Summary

opt_nil_p is an optimization applied when the method nil? is called. It returns true immediately when the receiver is nil and defers to the nil? method in other cases. It pops the receiver off the stack and pushes on the result.

### Usage

~~~ruby “”.nil? ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(calldata) ⇒ OptNilP

Returns a new instance of OptNilP.



3881
3882
3883
# File 'lib/syntax_tree/yarv/instructions.rb', line 3881

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3879
3880
3881
# File 'lib/syntax_tree/yarv/instructions.rb', line 3879

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



3897
3898
3899
# File 'lib/syntax_tree/yarv/instructions.rb', line 3897

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

#call(vm) ⇒ Object



3917
3918
3919
# File 'lib/syntax_tree/yarv/instructions.rb', line 3917

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

#canonicalObject



3913
3914
3915
# File 'lib/syntax_tree/yarv/instructions.rb', line 3913

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



3893
3894
3895
# File 'lib/syntax_tree/yarv/instructions.rb', line 3893

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



3885
3886
3887
# File 'lib/syntax_tree/yarv/instructions.rb', line 3885

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

#lengthObject



3901
3902
3903
# File 'lib/syntax_tree/yarv/instructions.rb', line 3901

def length
  2
end

#popsObject



3905
3906
3907
# File 'lib/syntax_tree/yarv/instructions.rb', line 3905

def pops
  1
end

#pushesObject



3909
3910
3911
# File 'lib/syntax_tree/yarv/instructions.rb', line 3909

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3889
3890
3891
# File 'lib/syntax_tree/yarv/instructions.rb', line 3889

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