Class: Opal::Nodes::MassAssignNode

Inherits:
Base
  • Object
show all
Defined in:
lib/opal/nodes/masgn.rb

Constant Summary collapse

SIMPLE_ASSIGNMENT =
%i[lvasgn ivasgn lvar gvasgn cdecl casgn].freeze

Instance Attribute Summary

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

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_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #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

#compileObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/opal/nodes/masgn.rb', line 13

def compile
  with_temp do |array|
    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
    else
      helper :to_ary
      with_temp do |retval|
        push "#{retval} = ", expr(rhs)
        push ", #{array} = $to_ary(#{retval})"
        compile_masgn(lhs.children, array)
        push ", #{retval}"
      end
    end
  end
end

#compile_assignment(child, array, idx, len = nil) ⇒ Object



76
77
78
79
80
81
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
# File 'lib/opal/nodes/masgn.rb', line 76

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
    helper :to_ary
    # nested destructuring
    tmp = scope.new_temp
    push ", (#{tmp} = $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



33
34
35
36
37
38
39
40
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
# File 'lib/opal/nodes/masgn.rb', line 33

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]
        helper :slice
        part = part.dup << s(:js_tmp, "$slice(#{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]
        helper :slice
        part = part.dup << s(:js_tmp, "$slice(#{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