aboutsummaryrefslogtreecommitdiffstats
path: root/eval.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-05 19:25:34 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-06 19:41:23 -0700
commit37a2c660aa4f4aacfd6a56651b10124e3ac01321 (patch)
treee31ede184f37489e1cda4ae2400e6424a03ffb39 /eval.c
parent3fafc549ba909e986917f2b6b96eb14624c26329 (diff)
downloadruby-37a2c660aa4f4aacfd6a56651b10124e3ac01321.tar.gz
Convert keyword argument to required positional hash argument for Class#new, Method#call, UnboundMethod#bind_call
Also add keyword argument separation warnings for Class#new and Method#call. To allow for keyword argument to required positional hash converstion in cfuncs, add a vm frame flag indicating the cfunc was called with an empty keyword hash (which was removed before calling the cfunc). The cfunc can check this frame flag and add back an empty hash if it is passing its arguments to another Ruby method. Add rb_empty_keyword_given_p function for checking if called with an empty keyword hash, and rb_add_empty_keyword for adding back an empty hash to argv. All of this empty keyword argument support is only for 2.7. It will be removed in 3.0 as Ruby 3 will not convert empty keyword arguments to required positional hash arguments. Comment all of the relevent code to make it obvious this is expected to be removed. Add rb_funcallv_kw as an public C-API function, just like rb_funcallv but with a keyword flag. This is used by rb_obj_call_init (internals of Class#new). This also required expected call_type enum with CALL_FCALL_KW, similar to the recent addition of CALL_PUBLIC_KW. Add rb_vm_call_kw as a internal function, used by call_method_data (internals of Method#call and UnboundMethod#bind_call). Add tests for UnboundMethod#bind_call keyword handling.
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/eval.c b/eval.c
index c588171b0a..fbdde7da9b 100644
--- a/eval.c
+++ b/eval.c
@@ -907,6 +907,22 @@ rb_keyword_given_p(void)
return rb_vm_cframe_keyword_p(GET_EC()->cfp);
}
+/* -- Remove In 3.0 -- */
+int rb_vm_cframe_empty_keyword_p(const rb_control_frame_t *cfp);
+int
+rb_empty_keyword_given_p(void)
+{
+ return rb_vm_cframe_empty_keyword_p(GET_EC()->cfp);
+}
+VALUE *
+rb_add_empty_keyword(int argc, const VALUE *argv)
+{
+ VALUE *ptr = ALLOC_N(VALUE,argc+1);
+ memcpy(ptr, argv, sizeof(VALUE)*(argc));
+ ptr[argc] = rb_hash_new();
+ return ptr;
+}
+
VALUE rb_eThreadError;
/*! Declares that the current method needs a block.
@@ -1664,7 +1680,12 @@ void
rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
{
PASS_PASSED_BLOCK_HANDLER();
- rb_funcallv(obj, idInitialize, argc, argv);
+ if (rb_empty_keyword_given_p()) {
+ rb_funcallv_kw(obj, idInitialize, argc+1, rb_add_empty_keyword(argc, argv), 1);
+ }
+ else {
+ rb_funcallv_kw(obj, idInitialize, argc, argv, rb_keyword_given_p());
+ }
}
/*!