Class: Opal::Nodes::MassAssignNode
- Defined in:
- opal/lib/opal/nodes/masgn.rb
Constant Summary
- SIMPLE_ASSIGNMENT =
%i[lvasgn ivasgn lvar gvasgn cdecl casgn].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #compile ⇒ Object
- #compile_assignment(child, array, idx, len = nil) ⇒ Object
-
#compile_masgn(lhs_items, array, len = nil) ⇒ Object
'len' is how many rhs items are we sure we have.
Methods inherited from Base
#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap
Methods included from Helpers
#conditional_send, #current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?
Constructor Details
This class inherits a constructor from Opal::Nodes::Base
Instance Method Details
#compile ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'opal/lib/opal/nodes/masgn.rb', line 13 def compile array = scope.new_temp if rhs.type == :array push "#{array} = ", expr(rhs) rhs_len = rhs.children.any? { |c| c.type == :splat } ? nil : rhs.children.size compile_masgn(lhs.children, array, rhs_len) push ", #{array}" # a mass assignment evaluates to the RHS elsif rhs.type == :begin retval = scope.new_temp push "#{retval} = ", expr(rhs) push ", #{array} = Opal.to_ary(#{retval})" compile_masgn(lhs.children, array) push ", #{retval}" scope.queue_temp(retval) else retval = scope.new_temp push "#{retval} = ", expr(rhs) push ", #{array} = Opal.to_ary(#{retval})" compile_masgn(lhs.children, array) push ", #{retval}" scope.queue_temp(retval) end scope.queue_temp(array) end |
#compile_assignment(child, array, idx, len = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'opal/lib/opal/nodes/masgn.rb', line 82 def compile_assignment(child, array, idx, len = nil) assign = if !len || idx >= len s(:js_tmp, "(#{array}[#{idx}] == null ? nil : #{array}[#{idx}])") else s(:js_tmp, "#{array}[#{idx}]") end part = child.updated if SIMPLE_ASSIGNMENT.include?(child.type) part = part.updated(nil, part.children + [assign]) elsif child.type == :send part = part.updated(nil, part.children + [assign]) elsif child.type == :attrasgn part.last << assign elsif child.type == :mlhs # nested destructuring tmp = scope.new_temp push ", (#{tmp} = Opal.to_ary(#{assign.children[0]})" compile_masgn(child.children, tmp) push ')' scope.queue_temp(tmp) return else raise "Bad child node in masgn LHS: #{child}. LHS: #{lhs}" end push ', ' push expr(part) end |
#compile_masgn(lhs_items, array, len = nil) ⇒ Object
'len' is how many rhs items are we sure we have
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'opal/lib/opal/nodes/masgn.rb', line 41 def compile_masgn(lhs_items, array, len = nil) pre_splat = lhs_items.take_while { |child| child.type != :splat } post_splat = lhs_items.drop(pre_splat.size) pre_splat.each_with_index do |child, idx| compile_assignment(child, array, idx, len) end unless post_splat.empty? splat = post_splat.shift if post_splat.empty? # trailing splat if part = splat.children[0] part = part.dup << s(:js_tmp, "$slice.call(#{array}, #{pre_splat.size})") push ', ' push expr(part) end else tmp = scope.new_temp # end index for items consumed by splat push ", #{tmp} = #{array}.length - #{post_splat.size}" push ", #{tmp} = (#{tmp} < #{pre_splat.size}) ? #{pre_splat.size} : #{tmp}" if part = splat.children[0] part = part.dup << s(:js_tmp, "$slice.call(#{array}, #{pre_splat.size}, #{tmp})") push ', ' push expr(part) end post_splat.each_with_index do |child, idx| if idx == 0 compile_assignment(child, array, tmp) else compile_assignment(child, array, "#{tmp} + #{idx}") end end scope.queue_temp(tmp) end end end |