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.



3939
3940
3941
# File 'lib/syntax_tree/yarv/instructions.rb', line 3939

def initialize(calldata)
  @calldata = calldata
end

Instance Attribute Details

#calldataObject (readonly)

Returns the value of attribute calldata.



3937
3938
3939
# File 'lib/syntax_tree/yarv/instructions.rb', line 3937

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object



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

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

#canonicalObject



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

def canonical
  Send.new(calldata, nil)
end

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  { calldata: calldata }
end

#disasm(fmt) ⇒ Object



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

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

#lengthObject



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

def length
  2
end

#popsObject



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

def pops
  1
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



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

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