Skip to content

Commit

Permalink
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/opal/parser.rb
Original file line number Diff line number Diff line change
@@ -711,6 +711,12 @@ def process_iter(sexp, level)
args ||= s(:masgn, s(:array))
args = args.first == :lasgn ? s(:array, args) : args[1]

# opt args are last, if present, and are a [:block]
if args.last.is_a?(Array) and args.last[0] == :block
opt_args = args.pop
opt_args.shift
end

if args.last.is_a?(Array) and args.last[0] == :block_pass
block_arg = args.pop
block_arg = block_arg[1][1].to_sym
@@ -730,7 +736,12 @@ def process_iter(sexp, level)
args[1..-1].each do |arg|
arg = arg[1]
arg = "#{arg}$" if RESERVED.include? arg.to_s
code << fragment("if (#{arg} == null) #{arg} = nil;\n", sexp)

if opt_args and current_opt = opt_args.find { |s| s[1] == arg.to_sym }
code << [fragment("if (#{arg} == null) #{arg} = ", sexp), process(current_opt[2], :expr), fragment(";\n", sexp)]
else
code << fragment("if (#{arg} == null) #{arg} = nil;\n", sexp)
end
end

params = js_block_args(args[1..-1])
8 changes: 8 additions & 0 deletions spec/opal/language/block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

describe "Blocks" do
it "accept default arguments" do
proc { |a, b = 100| [a, b] }.call(:foo, :bar).should == [:foo, :bar]
proc { |a, b = 100| [a, b] }.call(:foo).should == [:foo, 100]
end
end

0 comments on commit 97e6c98

Please sign in to comment.