Skip to content

Commit 856dd5f

Browse files
committedDec 7, 2014
Support optional args and keyword args together
1 parent a932256 commit 856dd5f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed
 

‎lib/opal/nodes/def.rb

+15-6
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,23 @@ def compile_keyword_args
144144

145145
if rest_arg
146146
with_temp do |tmp|
147-
rest_arg_name = variable(rest_arg[1].to_sym)
148-
line "#{tmp} = #{rest_arg_name}[#{rest_arg_name}.length - 1];"
149-
line "if (#{tmp} == null || !#{tmp}.$$is_hash) {"
147+
rest_arg_name = variable(rest_arg[1].to_sym)
148+
line "#{tmp} = #{rest_arg_name}[#{rest_arg_name}.length - 1];"
149+
line "if (#{tmp} == null || !#{tmp}.$$is_hash) {"
150+
line " $kwargs = $hash2([], {});"
151+
line "} else {"
152+
line " $kwargs = #{rest_arg_name}.pop();"
153+
line "}"
154+
end
155+
elsif last_opt_arg = opt_args.last
156+
opt_arg_name = variable(last_opt_arg[1])
157+
line "if (#{opt_arg_name} == null) {"
150158
line " $kwargs = $hash2([], {});"
151-
line "} else {"
152-
line " $kwargs = #{rest_arg_name}.pop();"
153159
line "}"
154-
end
160+
line "else if (#{opt_arg_name}.$$is_hash) {"
161+
line " $kwargs = #{opt_arg_name};"
162+
line " #{opt_arg_name} = ", expr(last_opt_arg[2]), ";"
163+
line "}"
155164
else
156165
line "if ($kwargs == null) {"
157166
line " $kwargs = $hash2([], {});"

0 commit comments

Comments
 (0)
Please sign in to comment.