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

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, #prepend_to_body, #s, s

Class Method Details

.call(lhs, rhs, root_type) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'opal/lib/opal/rewriters/logical_operator_assignment.rb', line 82

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