Sha256: 8df7bb5e528bad44174840f81891532d8e026e21a433af52f2d59ffc4353e15f

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 KB

Contents

require 'opal/nodes/base'

module Opal
  module Nodes
    class MassAssignNode < Base
      handle :masgn

      children :lhs, :rhs

      def compile
        tmp = scope.new_temp
        len = 0 # how many rhs items are we sure we have

        if rhs.type == :array
          len = rhs.size - 1
          push "#{tmp} = ", expr(rhs)
        elsif rhs.type == :to_ary
          push "#{tmp} = $opal.to_ary(", expr(rhs[1]), ")"
        elsif rhs.type == :splat
          push "(#{tmp} = ", expr(rhs[1]), ")['$to_a'] ? (#{tmp} = #{tmp}['$to_a']())"
          push " : (#{tmp})._isArray ? #{tmp} : (#{tmp} = [#{tmp}])"
        else
          raise "unsupported mlhs type"
        end

        lhs.children.each_with_index do |child, idx|
          push ', '

          if child.type == :splat
            if part = child[1]
              part = part.dup
              part << s(:js_tmp, "$slice.call(#{tmp}, #{idx})")
              push expr(part)
            end
          else
            if idx >= len
              assign = s(:js_tmp, "(#{tmp}[#{idx}] == null ? nil : #{tmp}[#{idx}])")
            else
              assign = s(:js_tmp, "#{tmp}[#{idx}]")
            end

            part = child.dup
            if child.type == :lasgn or child.type == :iasgn or child.type == :lvar or child.type == :gasgn
              part << assign
            elsif child.type == :call
              part[2] = "#{part[2]}=".to_sym
              part.last << assign
            elsif child.type == :attrasgn
              part.last << assign
            else
              raise "Bad lhs for masgn"
            end

            push expr(part)
          end
        end

        scope.queue_temp tmp
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
opal-0.6.3 lib/opal/nodes/masgn.rb
opal-0.6.2 lib/opal/nodes/masgn.rb
opal-0.6.1 lib/opal/nodes/masgn.rb
opal-0.6.0 lib/opal/nodes/masgn.rb
opal-0.5.5 lib/opal/nodes/masgn.rb
opal-0.5.4 lib/opal/nodes/masgn.rb
opal-0.5.2 lib/opal/nodes/masgn.rb
opal-0.5.0 lib/opal/nodes/masgn.rb