mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
marshal.c: allow marshalling keyword_init struct
struct.c: define rb_struct_s_keyword_init to shared with marshal.c internal.h: add the declaration to be used by marshal.c test/ruby/test_marshal.rb: add test for Bug#14314 [Feature #14314] [ruby-core:84629] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61616 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d3cea2edeb
commit
26df9588d6
4 changed files with 40 additions and 12 deletions
|
@ -1768,6 +1768,7 @@ VALUE rb_to_symbol_type(VALUE obj);
|
||||||
/* struct.c */
|
/* struct.c */
|
||||||
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
||||||
VALUE rb_struct_lookup(VALUE s, VALUE idx);
|
VALUE rb_struct_lookup(VALUE s, VALUE idx);
|
||||||
|
VALUE rb_struct_s_keyword_init(VALUE klass);
|
||||||
|
|
||||||
/* time.c */
|
/* time.c */
|
||||||
struct timeval rb_time_timeval(VALUE);
|
struct timeval rb_time_timeval(VALUE);
|
||||||
|
|
14
marshal.c
14
marshal.c
|
@ -1811,6 +1811,14 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
|
||||||
arg->readable += (len - 1) * 2;
|
arg->readable += (len - 1) * 2;
|
||||||
v = r_entry0(v, idx, arg);
|
v = r_entry0(v, idx, arg);
|
||||||
values = rb_ary_new2(len);
|
values = rb_ary_new2(len);
|
||||||
|
{
|
||||||
|
VALUE keywords;
|
||||||
|
int keyword_init = RTEST(rb_struct_s_keyword_init(klass));
|
||||||
|
if (keyword_init) {
|
||||||
|
keywords = rb_hash_new();
|
||||||
|
rb_ary_push(values, keywords);
|
||||||
|
}
|
||||||
|
|
||||||
for (i=0; i<len; i++) {
|
for (i=0; i<len; i++) {
|
||||||
VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
|
VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
|
||||||
slot = r_symbol(arg);
|
slot = r_symbol(arg);
|
||||||
|
@ -1820,9 +1828,15 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
|
||||||
rb_class_name(klass),
|
rb_class_name(klass),
|
||||||
slot, n);
|
slot, n);
|
||||||
}
|
}
|
||||||
|
if (keyword_init) {
|
||||||
|
rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
|
||||||
|
}
|
||||||
|
else {
|
||||||
rb_ary_push(values, r_object(arg));
|
rb_ary_push(values, r_object(arg));
|
||||||
|
}
|
||||||
arg->readable -= 2;
|
arg->readable -= 2;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rb_struct_initialize(v, values);
|
rb_struct_initialize(v, values);
|
||||||
v = r_leave(v, arg);
|
v = r_leave(v, arg);
|
||||||
arg->readable += 2;
|
arg->readable += 2;
|
||||||
|
|
10
struct.c
10
struct.c
|
@ -47,6 +47,12 @@ struct_ivar_get(VALUE c, ID id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_struct_s_keyword_init(VALUE klass)
|
||||||
|
{
|
||||||
|
return struct_ivar_get(klass, id_keyword_init);
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_struct_s_members(VALUE klass)
|
rb_struct_s_members(VALUE klass)
|
||||||
{
|
{
|
||||||
|
@ -298,7 +304,7 @@ static VALUE
|
||||||
rb_struct_s_inspect(VALUE klass)
|
rb_struct_s_inspect(VALUE klass)
|
||||||
{
|
{
|
||||||
VALUE inspect = rb_class_name(klass);
|
VALUE inspect = rb_class_name(klass);
|
||||||
if (RTEST(struct_ivar_get(klass, id_keyword_init))) {
|
if (RTEST(rb_struct_s_keyword_init(klass))) {
|
||||||
rb_str_cat_cstr(inspect, "(keyword_init: true)");
|
rb_str_cat_cstr(inspect, "(keyword_init: true)");
|
||||||
}
|
}
|
||||||
return inspect;
|
return inspect;
|
||||||
|
@ -616,7 +622,7 @@ rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self)
|
||||||
|
|
||||||
rb_struct_modify(self);
|
rb_struct_modify(self);
|
||||||
n = num_members(klass);
|
n = num_members(klass);
|
||||||
if (argc > 0 && RTEST(struct_ivar_get(klass, id_keyword_init))) {
|
if (argc > 0 && RTEST(rb_struct_s_keyword_init(klass))) {
|
||||||
struct struct_hash_set_arg arg;
|
struct struct_hash_set_arg arg;
|
||||||
if (argc > 2 || !RB_TYPE_P(argv[0], T_HASH)) {
|
if (argc > 2 || !RB_TYPE_P(argv[0], T_HASH)) {
|
||||||
rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0)", argc);
|
rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0)", argc);
|
||||||
|
|
|
@ -772,4 +772,11 @@ class TestMarshal < Test::Unit::TestCase
|
||||||
Marshal.dump(Bug12974.new)
|
Marshal.dump(Bug12974.new)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Bug14314 = Struct.new(:foo, keyword_init: true)
|
||||||
|
|
||||||
|
def test_marshal_keyword_init_struct
|
||||||
|
obj = Bug14314.new(foo: 42)
|
||||||
|
assert_equal obj, Marshal.load(Marshal.dump(obj))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue