Classes made from Struct should have default new
singleton method.
[Bug #16465] [Bug #16801] [Fix GH-2795] [Fix GH-2944] [Fix GH-3045] [Fix GH-3093] Note: Backporting shouldn't modify object.h and instead can use struct_new_kw which is basically a duplicate implementation of rb_class_new_instance_pass_kw Co-authored-by: Yusuke Endoh <mame@ruby-lang.org> Co-authored-by: John Hawthorn <john@hawthorn.email> Co-authored-by: Adam Hess <HParker@github.com> Co-authored-by: Jose Cortinas <jacortinas@gmail.com> Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
This commit is contained in:
parent
da345adc1c
commit
adf709a785
Notes:
git
2020-05-08 17:19:12 +09:00
@ -30,6 +30,7 @@ RUBY3_SYMBOL_EXPORT_BEGIN()
|
|||||||
((obj) != (orig) && (rb_obj_init_copy((obj), (orig)), 1))
|
((obj) != (orig) && (rb_obj_init_copy((obj), (orig)), 1))
|
||||||
#define OBJ_INIT_COPY(obj, orig) RB_OBJ_INIT_COPY(obj, orig)
|
#define OBJ_INIT_COPY(obj, orig) RB_OBJ_INIT_COPY(obj, orig)
|
||||||
|
|
||||||
|
VALUE rb_class_new_instance_pass_kw(int, const VALUE *, VALUE);
|
||||||
VALUE rb_class_new_instance(int, const VALUE*, VALUE);
|
VALUE rb_class_new_instance(int, const VALUE*, VALUE);
|
||||||
VALUE rb_class_new_instance_kw(int, const VALUE*, VALUE, int);
|
VALUE rb_class_new_instance_kw(int, const VALUE*, VALUE, int);
|
||||||
|
|
||||||
|
6
object.c
6
object.c
@ -2203,8 +2203,8 @@ rb_class_allocate_instance(VALUE klass)
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
VALUE
|
||||||
rb_class_s_new(int argc, const VALUE *argv, VALUE klass)
|
rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
VALUE obj;
|
VALUE obj;
|
||||||
|
|
||||||
@ -4726,7 +4726,7 @@ InitVM_Object(void)
|
|||||||
rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
|
rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
|
||||||
|
|
||||||
rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
|
rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
|
||||||
rb_define_method(rb_cClass, "new", rb_class_s_new, -1);
|
rb_define_method(rb_cClass, "new", rb_class_new_instance_pass_kw, -1);
|
||||||
rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
|
rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
|
||||||
rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
|
rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
|
||||||
rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
|
rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
|
||||||
|
21
struct.c
21
struct.c
@ -317,24 +317,15 @@ rb_struct_s_inspect(VALUE klass)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
struct_new_kw(int argc, const VALUE *argv, VALUE klass)
|
setup_struct(VALUE nstr, VALUE members)
|
||||||
{
|
|
||||||
return rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE
|
|
||||||
setup_struct(VALUE nstr, VALUE members, int keyword_init)
|
|
||||||
{
|
{
|
||||||
long i, len;
|
long i, len;
|
||||||
VALUE (*new_func)(int, const VALUE *, VALUE) = rb_class_new_instance;
|
|
||||||
|
|
||||||
if (keyword_init) new_func = struct_new_kw;
|
|
||||||
|
|
||||||
members = struct_set_members(nstr, members);
|
members = struct_set_members(nstr, members);
|
||||||
|
|
||||||
rb_define_alloc_func(nstr, struct_alloc);
|
rb_define_alloc_func(nstr, struct_alloc);
|
||||||
rb_define_singleton_method(nstr, "new", new_func, -1);
|
rb_define_singleton_method(nstr, "new", rb_class_new_instance_pass_kw, -1);
|
||||||
rb_define_singleton_method(nstr, "[]", new_func, -1);
|
rb_define_singleton_method(nstr, "[]", rb_class_new_instance_pass_kw, -1);
|
||||||
rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
|
rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
|
||||||
rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0);
|
rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0);
|
||||||
len = RARRAY_LEN(members);
|
len = RARRAY_LEN(members);
|
||||||
@ -449,7 +440,7 @@ rb_struct_define(const char *name, ...)
|
|||||||
|
|
||||||
if (!name) st = anonymous_struct(rb_cStruct);
|
if (!name) st = anonymous_struct(rb_cStruct);
|
||||||
else st = new_struct(rb_str_new2(name), rb_cStruct);
|
else st = new_struct(rb_str_new2(name), rb_cStruct);
|
||||||
return setup_struct(st, ary, 0);
|
return setup_struct(st, ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
@ -462,7 +453,7 @@ rb_struct_define_under(VALUE outer, const char *name, ...)
|
|||||||
ary = struct_make_members_list(ar);
|
ary = struct_make_members_list(ar);
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
|
|
||||||
return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary, 0);
|
return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -581,7 +572,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
|
|||||||
else {
|
else {
|
||||||
st = new_struct(name, klass);
|
st = new_struct(name, klass);
|
||||||
}
|
}
|
||||||
setup_struct(st, rest, (int)keyword_init);
|
setup_struct(st, rest);
|
||||||
rb_ivar_set(st, id_keyword_init, keyword_init);
|
rb_ivar_set(st, id_keyword_init, keyword_init);
|
||||||
if (rb_block_given_p()) {
|
if (rb_block_given_p()) {
|
||||||
rb_mod_module_eval(0, 0, st);
|
rb_mod_module_eval(0, 0, st);
|
||||||
|
@ -118,10 +118,9 @@ module TestStruct
|
|||||||
assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect
|
assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect
|
||||||
assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect
|
assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect
|
||||||
# eval is needed to prevent the warning duplication filter
|
# eval is needed to prevent the warning duplication filter
|
||||||
k = eval("Class.new(@Struct::KeywordInitFalse) {def initialize(**) end}")
|
k = Class.new(@Struct::KeywordInitTrue) {def initialize(b, options); super(a: options, b: b); end}
|
||||||
assert_raise(ArgumentError) { k.new(a: 1, b: 2) }
|
o = assert_warn('') { k.new(42, {foo: 1, bar: 2}) }
|
||||||
k = Class.new(@Struct::KeywordInitTrue) {def initialize(**) end}
|
assert_equal(1, o.a[:foo])
|
||||||
assert_warn('') {k.new(a: 1, b: 2)}
|
|
||||||
|
|
||||||
@Struct.instance_eval do
|
@Struct.instance_eval do
|
||||||
remove_const(:KeywordInitTrue)
|
remove_const(:KeywordInitTrue)
|
||||||
@ -150,6 +149,17 @@ module TestStruct
|
|||||||
assert_equal 3, klass.new(1,2).total
|
assert_equal 3, klass.new(1,2).total
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_initialize_with_kw
|
||||||
|
klass = @Struct.new(:foo, :options) do
|
||||||
|
def initialize(foo, **options)
|
||||||
|
super(foo, options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal({}, klass.new(42, **Hash.new).options)
|
||||||
|
x = assert_warn('') { klass.new(1, bar: 2) }
|
||||||
|
assert_equal 2, x.options[:bar]
|
||||||
|
end
|
||||||
|
|
||||||
def test_each
|
def test_each
|
||||||
klass = @Struct.new(:a, :b)
|
klass = @Struct.new(:a, :b)
|
||||||
o = klass.new(1, 2)
|
o = klass.new(1, 2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user