Class: Opal::Rewriters::BinaryOperatorAssignment::SendHandler

Inherits:
Opal::Rewriters::BinaryOperatorAssignment show all
Defined in:
opal/lib/opal/rewriters/binary_operator_assignment.rb

Overview

Takes recvr.meth += rhs Produces recvr.meth = recvr.meth + rhs (lhs is a recvr.meth, op is :+)

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'opal/lib/opal/rewriters/binary_operator_assignment.rb', line 52

def self.call(lhs, op, rhs)
  recvr, reader_method, *args = *lhs

  # If recvr is a complex expression it must be cached.
  # MRI calls recvr in `recvr.meth ||= rhs` only once.
  if recvr && recvr.type == :send
    recvr_tmp = new_temp
    cache_recvr = s(:lvasgn, recvr_tmp, recvr)                         # $tmp = recvr
    recvr = s(:js_tmp, recvr_tmp)
  end

  writer_method = :#{reader_method}="

  call_reader = lhs.updated(:send, [recvr, reader_method, *args])      # $tmp.meth
  call_op = s(:send, call_reader, op, rhs) # $tmp.meth + rhs
  call_writer = lhs.updated(:send, [recvr, writer_method, *args, call_op]) # $tmp.meth = $tmp.meth + rhs

  if cache_recvr
    s(:begin, cache_recvr, call_writer)
  else
    call_writer
  end
end