Skip to content

Commit fd20330

Browse files
committedDec 7, 2014
Support keyword rest arguments
1 parent 732d323 commit fd20330

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

Diff for: ‎lib/opal/nodes/def.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ def compile_keyword_args
144144
line " throw new Error('expecting keyword arg: #{arg_name}')"
145145
line "}"
146146
when :kwrestarg
147-
nil
147+
arg_name = kwarg[1]
148+
var_name = variable(arg_name.to_s)
149+
150+
kwarg_names = @kwargs.select do |kw|
151+
[:kwoptarg, :kwarg].include? kw.first
152+
end.map { |kw| "#{kw[1].to_s.inspect}: true" }
153+
154+
used_args = "{#{kwarg_names.join ','}}"
155+
line "#{var_name} = Opal.kwrestargs($kwargs, #{used_args});"
148156
else
149157
raise "unknown kwarg type #{kwarg.first}"
150158
end

Diff for: ‎opal/corelib/runtime.js

+28
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,34 @@
890890
return [value];
891891
};
892892

893+
/**
894+
Used to get a list of rest keyword arguments. Method takes the given
895+
keyword args, i.e. the hash literal passed to the method containing all
896+
keyword arguemnts passed to method, as well as the used args which are
897+
the names of required and optional arguments defined. This method then
898+
just returns all key/value pairs which have not been used, in a new
899+
hash literal.
900+
901+
@param given_args [Hash] all kwargs given to method
902+
@param used_args [Object<String: true>] all keys used as named kwargs
903+
@return [Hash]
904+
*/
905+
Opal.kwrestargs = function(given_args, used_args) {
906+
var keys = [],
907+
map = {},
908+
key = null,
909+
given_map = given_args.smap;
910+
911+
for (key in given_map) {
912+
if (!used_args[key]) {
913+
keys.push(key);
914+
map[key] = given_map[key];
915+
}
916+
}
917+
918+
return Opal.hash2(keys, map);
919+
};
920+
893921
/*
894922
* Call a ruby method on a ruby object with some arguments:
895923
*

0 commit comments

Comments
 (0)
Please sign in to comment.