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.



3946
3947
3948
# File 'lib/syntax_tree/yarv/instructions.rb', line 3946

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3944
3945
3946
# File 'lib/syntax_tree/yarv/instructions.rb', line 3944

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



3962
3963
3964
# File 'lib/syntax_tree/yarv/instructions.rb', line 3962

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

#call(vm) ⇒ Object



3982
3983
3984
# File 'lib/syntax_tree/yarv/instructions.rb', line 3982

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

#canonicalObject



3978
3979
3980
# File 'lib/syntax_tree/yarv/instructions.rb', line 3978

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



3958
3959
3960
# File 'lib/syntax_tree/yarv/instructions.rb', line 3958

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



3950
3951
3952
# File 'lib/syntax_tree/yarv/instructions.rb', line 3950

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

#lengthObject



3966
3967
3968
# File 'lib/syntax_tree/yarv/instructions.rb', line 3966

def length
  2
end

#popsObject



3970
3971
3972
# File 'lib/syntax_tree/yarv/instructions.rb', line 3970

def pops
  1
end

#pushesObject



3974
3975
3976
# File 'lib/syntax_tree/yarv/instructions.rb', line 3974

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3954
3955
3956
# File 'lib/syntax_tree/yarv/instructions.rb', line 3954

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