1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Pass keyword argument flag when rb_call_super_kw calls method_missing

This makes method_missing take a flag for whether keyword arguments
were passed.

Adds tests both for rb_call_super_kw usage as well as general usage
of super calling method_missing in Ruby methods.
This commit is contained in:
Jeremy Evans 2019-09-16 13:19:06 -07:00
parent 0785469a40
commit 9b35dc3864
Notes: git 2019-09-18 08:23:18 +09:00
6 changed files with 237 additions and 13 deletions

View file

@ -0,0 +1,14 @@
# AUTOGENERATED DEPENDENCIES START
rb_call_super_kw.o: $(RUBY_EXTCONF_H)
rb_call_super_kw.o: $(arch_hdrdir)/ruby/config.h
rb_call_super_kw.o: $(hdrdir)/ruby.h
rb_call_super_kw.o: $(hdrdir)/ruby/assert.h
rb_call_super_kw.o: $(hdrdir)/ruby/backward.h
rb_call_super_kw.o: $(hdrdir)/ruby/defines.h
rb_call_super_kw.o: $(hdrdir)/ruby/intern.h
rb_call_super_kw.o: $(hdrdir)/ruby/missing.h
rb_call_super_kw.o: $(hdrdir)/ruby/ruby.h
rb_call_super_kw.o: $(hdrdir)/ruby/st.h
rb_call_super_kw.o: $(hdrdir)/ruby/subst.h
rb_call_super_kw.o: rb_call_super_kw.c
# AUTOGENERATED DEPENDENCIES END

View file

@ -0,0 +1 @@
create_makefile("-test-/rb_call_super_kw")

View file

@ -0,0 +1,14 @@
#include <ruby.h>
static VALUE
rb_call_super_kw_m(int argc, VALUE *argv, VALUE self)
{
return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
}
void
Init_rb_call_super_kw(void) {
VALUE module = rb_define_module("Bug");
module = rb_define_module_under(module, "RbCallSuperKw");
rb_define_method(module, "m", rb_call_super_kw_m, -1);
}

View file

@ -1,5 +1,6 @@
# frozen_string_literal: false
require 'test/unit'
require '-test-/rb_call_super_kw'
class TestKeywordArguments < Test::Unit::TestCase
def f1(str: "foo", num: 424242)
@ -1518,6 +1519,199 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal([1, h3], c.m(a: 1, **h2))
end
def test_super_method_missing_kwsplat
kw = {}
h = {:a=>1}
h2 = {'a'=>1}
h3 = {'a'=>1, :a=>1}
c = Class.new do
def m(*args, **kw)
super
end
end.new
def c.method_missing(_, *args)
args
end
assert_equal([], c.m(**{}))
assert_equal([], c.m(**kw))
assert_equal([h], c.m(**h))
assert_equal([h], c.m(a: 1))
assert_equal([h2], c.m(**h2))
assert_equal([h3], c.m(**h3))
assert_equal([h3], c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_); end
assert_nil(c.m(**{}))
assert_nil(c.m(**kw))
assert_raise(ArgumentError) { c.m(**h) }
assert_raise(ArgumentError) { c.m(a: 1) }
assert_raise(ArgumentError) { c.m(**h2) }
assert_raise(ArgumentError) { c.m(**h3) }
assert_raise(ArgumentError) { c.m(a: 1, **h2) }
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, args)
args
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal(kw, c.m(**{}))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal(kw, c.m(**kw))
end
assert_equal(h, c.m(**h))
assert_equal(h, c.m(a: 1))
assert_equal(h2, c.m(**h2))
assert_equal(h3, c.m(**h3))
assert_equal(h3, c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, **args)
args
end
assert_equal(kw, c.m(**{}))
assert_equal(kw, c.m(**kw))
assert_equal(h, c.m(**h))
assert_equal(h, c.m(a: 1))
assert_equal(h2, c.m(**h2))
assert_equal(h3, c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, arg, **args)
[arg, args]
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([kw, kw], c.m(**{}))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([kw, kw], c.m(**kw))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h, kw], c.m(**h))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h, kw], c.m(a: 1))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h2, kw], c.m(**h2))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h3, kw], c.m(**h3))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h3, kw], c.m(a: 1, **h2))
end
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, arg=1, **args)
[arg=1, args]
end
assert_equal([1, kw], c.m(**{}))
assert_equal([1, kw], c.m(**kw))
assert_equal([1, h], c.m(**h))
assert_equal([1, h], c.m(a: 1))
assert_equal([1, h2], c.m(**h2))
assert_equal([1, h3], c.m(**h3))
assert_equal([1, h3], c.m(a: 1, **h2))
end
def test_rb_call_super_kw_method_missing_kwsplat
kw = {}
h = {:a=>1}
h2 = {'a'=>1}
h3 = {'a'=>1, :a=>1}
c = Object.new
c.extend Bug::RbCallSuperKw
def c.method_missing(_, *args)
args
end
assert_equal([], c.m(**{}))
assert_equal([], c.m(**kw))
assert_equal([h], c.m(**h))
assert_equal([h], c.m(a: 1))
assert_equal([h2], c.m(**h2))
assert_equal([h3], c.m(**h3))
assert_equal([h3], c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_); end
assert_nil(c.m(**{}))
assert_nil(c.m(**kw))
assert_raise(ArgumentError) { c.m(**h) }
assert_raise(ArgumentError) { c.m(a: 1) }
assert_raise(ArgumentError) { c.m(**h2) }
assert_raise(ArgumentError) { c.m(**h3) }
assert_raise(ArgumentError) { c.m(a: 1, **h2) }
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, args)
args
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal(kw, c.m(**{}))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal(kw, c.m(**kw))
end
assert_equal(h, c.m(**h))
assert_equal(h, c.m(a: 1))
assert_equal(h2, c.m(**h2))
assert_equal(h3, c.m(**h3))
assert_equal(h3, c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, **args)
args
end
assert_equal(kw, c.m(**{}))
assert_equal(kw, c.m(**kw))
assert_equal(h, c.m(**h))
assert_equal(h, c.m(a: 1))
assert_equal(h2, c.m(**h2))
assert_equal(h3, c.m(a: 1, **h2))
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, arg, **args)
[arg, args]
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([kw, kw], c.m(**{}))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([kw, kw], c.m(**kw))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h, kw], c.m(**h))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h, kw], c.m(a: 1))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h2, kw], c.m(**h2))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h3, kw], c.m(**h3))
end
assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
assert_equal([h3, kw], c.m(a: 1, **h2))
end
c.singleton_class.remove_method(:method_missing)
def c.method_missing(_, arg=1, **args)
[arg=1, args]
end
assert_equal([1, kw], c.m(**{}))
assert_equal([1, kw], c.m(**kw))
assert_equal([1, h], c.m(**h))
assert_equal([1, h], c.m(a: 1))
assert_equal([1, h2], c.m(**h2))
assert_equal([1, h3], c.m(**h3))
assert_equal([1, h3], c.m(a: 1, **h2))
end
def test_define_method_kwsplat
kw = {}
h = {:a=>1}

View file

@ -13,7 +13,7 @@ NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_i
NORETURN(static void argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys));
VALUE rb_keyword_error_new(const char *error, VALUE keys); /* class.c */
static VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv,
enum method_missing_reason call_status);
enum method_missing_reason call_status, int kw_splat);
struct args_info {
/* basic args info */
@ -1072,7 +1072,7 @@ refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
vm_passed_block_handler_set(ec, blockarg);
}
if (!me) {
return method_missing(obj, mid, argc, argv, MISSING_NOENTRY);
return method_missing(obj, mid, argc, argv, MISSING_NOENTRY, VM_NO_KEYWORDS);
}
return rb_vm_call0(ec, obj, mid, argc, argv, me, VM_NO_KEYWORDS);
}

View file

@ -15,7 +15,7 @@ struct local_var_list {
VALUE tbl;
};
static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status);
static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat);
static inline VALUE vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, const rb_cref_t *cref, int is_lambda);
static inline VALUE vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv);
static inline VALUE vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler);
@ -193,7 +193,7 @@ vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const
if (!super_class || !(cc->me = rb_callable_method_entry(super_class, ci->mid))) {
enum method_missing_reason ex = (type == VM_METHOD_TYPE_ZSUPER) ? MISSING_SUPER : 0;
ret = method_missing(calling->recv, ci->mid, calling->argc, argv, ex);
ret = method_missing(calling->recv, ci->mid, calling->argc, argv, ex, calling->kw_splat);
goto success;
}
RUBY_VM_CHECK_INTS(ec);
@ -206,7 +206,7 @@ vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const
{
vm_passed_block_handler_set(ec, calling->block_handler);
return method_missing(calling->recv, ci->mid, calling->argc,
argv, MISSING_NOENTRY);
argv, MISSING_NOENTRY, calling->kw_splat);
}
case VM_METHOD_TYPE_OPTIMIZED:
switch (cc->me->def->body.optimize_type) {
@ -284,6 +284,7 @@ vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_sp
{
VALUE recv = ec->cfp->self;
VALUE klass;
VALUE v, ret;
ID id;
rb_control_frame_t *cfp = ec->cfp;
const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
@ -297,15 +298,15 @@ vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_sp
id = me->def->original_id;
me = rb_callable_method_entry(klass, id);
v = add_empty_keyword(&argc, &argv, &kw_splat);
if (!me) {
return method_missing(recv, id, argc, argv, MISSING_SUPER);
ret = method_missing(recv, id, argc, argv, MISSING_SUPER, kw_splat);
}
else {
VALUE v = add_empty_keyword(&argc, &argv, &kw_splat);
VALUE ret = rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
rb_free_tmp_buffer(&v);
return ret;
ret = rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
}
rb_free_tmp_buffer(&v);
return ret;
}
VALUE
@ -400,7 +401,7 @@ rb_call0(rb_execution_context_t *ec,
call_status = rb_method_call_status(ec, me, scope, self);
if (call_status != MISSING_NONE) {
return method_missing(recv, mid, argc, argv, call_status);
return method_missing(recv, mid, argc, argv, call_status, kw_splat);
}
stack_check(ec);
return rb_vm_call0(ec, recv, mid, argc, argv, me, kw_splat);
@ -830,7 +831,7 @@ vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
}
static inline VALUE
method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status)
method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat)
{
VALUE *nargv, result, work, klass;
rb_execution_context_t *ec = GET_EC();
@ -862,7 +863,7 @@ method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missin
me = rb_callable_method_entry(klass, idMethodMissing);
if (!me || METHOD_ENTRY_BASIC(me)) goto missing;
vm_passed_block_handler_set(ec, block_handler);
result = rb_vm_call0(ec, obj, idMethodMissing, argc, argv, me, VM_NO_KEYWORDS);
result = rb_vm_call0(ec, obj, idMethodMissing, argc, argv, me, kw_splat);
if (work) ALLOCV_END(work);
return result;
}