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

Constants inherited from Opal::Rewriters::Base

Opal::Rewriters::Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Opal::Rewriters::Base

#current_node

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, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Class Method Details

.call(lhs, operation, rhs) ⇒ Object



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

def self.call(lhs, operation, 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, operation, 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