Class: Opal::Rewriters::BinaryOperatorAssignment::ConditionalSendHandler

Inherits:
Opal::Rewriters::BinaryOperatorAssignment show all
Defined in:
opal/lib/opal/rewriters/binary_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::BinaryOperatorAssignment

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

Class Method Summary collapse

Methods inherited from Opal::Rewriters::BinaryOperatorAssignment

new_temp, #on_defined?, #on_op_asgn, reset_tmp_counter!

Methods inherited from Opal::Rewriters::Base

#append_to_body, #prepend_to_body, #s, s

Class Method Details

.call(lhs, op, rhs) ⇒ Object



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

def self.call(lhs, op, rhs)
  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_op_asgn = s(:op_asgn, plain_send, op, rhs)      # recvr.meth += rhs

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