Skip to content

Commit

Permalink
Assign masgn parts for block destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 18, 2013
1 parent 23fe4d2 commit 958bb3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/opal/parser.rb
Expand Up @@ -747,25 +747,29 @@ def process_iter(sexp, level)
identity = @scope.identify!
@scope.add_temp "#{current_self} = #{identity}._s || this"

args[1..-1].each do |arg|
params = js_block_args(args[1..-1])

args[1..-1].each_with_index do |arg, idx|
if arg[0] == :lasgn
arg = arg[1]
arg = "#{arg}$" if RESERVED.include? arg.to_s

if opt_args and current_opt = opt_args.find { |s| s[1] == arg.to_sym }
code << [f("if (#{arg} == null) #{arg} = ", sexp), process(current_opt[2]), f(";\n", sexp)]
code << [f("if (#{arg} == null) #{arg} = ", sexp), process(current_opt[2]), f(";\n#{@indent}", sexp)]
else
code << f("if (#{arg} == null) #{arg} = nil;\n", sexp)
code << f("if (#{arg} == null) #{arg} = nil;\n#{@indent}", sexp)
end
elsif arg[0] == :array
# destructure
arg[1..-1].each_with_index do |arg, midx|
arg = arg[1]
arg = "#{arg}$" if RESERVED.include? arg.to_s
code << f("#{arg} = #{params[idx]}[#{midx}];\n#{@indent}")
end
else
raise "Bad block_arg type: #{arg[0]}"
end
end

params = js_block_args(args[1..-1])

if splat
@scope.add_arg splat
params << splat
Expand Down Expand Up @@ -815,7 +819,7 @@ def js_block_args(sexp)
@scope.add_arg ref
result << ref
elsif arg[0] == :array
result << @scope.new_temp
result << @scope.next_temp
else
raise "Bad js_block_arg: #{arg[0]}"
end
Expand Down
7 changes: 6 additions & 1 deletion lib/opal/target_scope.rb
Expand Up @@ -197,9 +197,14 @@ def has_temp?(tmp)
def new_temp
return @queue.pop unless @queue.empty?

tmp = next_temp
@temps << tmp
tmp
end

def next_temp
tmp = "$#{@unique}"
@unique = @unique.succ
@temps << tmp
tmp
end

Expand Down

0 comments on commit 958bb3f

Please sign in to comment.