Class: Opal::Rewriters::LogicalOperatorAssignment::ConditionalSendHandler

Inherits:
Opal::Rewriters::LogicalOperatorAssignment show all
Defined in:
opal/lib/opal/rewriters/logical_operator_assignment.rb

Overview

Takes recvr&.meth ||= rhs Produces recvr.nil? ? nil : recvr.meth ||= rhs NOTE: Later output of this handler gets post-processed by this rewriter again using SendHandler to recvr.nil? ? nil : (recvr.meth || recvr.meth = rhs)

Constant Summary

Constants inherited from Opal::Rewriters::LogicalOperatorAssignment

ASSIGNMENT_STRING_NODE, ClassVariableHandler, ConstantHandler, GET_SET, GlobalVariableHandler, HANDLERS, InstanceVariableHandler, LocalVariableHandler

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Class Method Summary collapse

Methods inherited from Opal::Rewriters::LogicalOperatorAssignment

new_temp, #on_and_asgn, #on_defined?, #on_or_asgn, reset_tmp_counter!

Methods inherited from Base

#append_to_body, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Class Method Details

.call(lhs, rhs, root_type) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'opal/lib/opal/rewriters/logical_operator_assignment.rb', line 94

def self.call(lhs, rhs, root_type)
  root_type = :"#{root_type}_asgn"

  recvr, meth, *args = *lhs

  recvr_tmp = new_temp
  cache_recvr = s(:lvasgn, recvr_tmp, recvr) # $tmp = recvr
  recvr = s(:js_tmp, recvr_tmp)

  recvr_is_nil = s(:send, recvr, :nil?)                 # recvr.nil?
  plain_send = lhs.updated(:send, [recvr, meth, *args]) # recvr.meth
  plain_or_asgn = s(root_type, plain_send, rhs)         # recvr.meth ||= rhs

  s(:begin,
    cache_recvr,
    s(:if, recvr_is_nil,                          # if recvr.nil?
      s(:nil),                                    #   nil
                                                  # else
      plain_or_asgn                               #   recvr.meth ||= rhs
    ),
  )                                               # end
end