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 */
|
||||
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
||||
VALUE rb_struct_lookup(VALUE s, VALUE idx);
|
||||
VALUE rb_struct_s_keyword_init(VALUE klass);
|
||||
|
||||
/* time.c */
|
||||
struct timeval rb_time_timeval(VALUE);
|
||||
|
|
34
marshal.c
34
marshal.c
|
@ -1811,17 +1811,31 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
|
|||
arg->readable += (len - 1) * 2;
|
||||
v = r_entry0(v, idx, arg);
|
||||
values = rb_ary_new2(len);
|
||||
for (i=0; i<len; i++) {
|
||||
VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
|
||||
slot = r_symbol(arg);
|
||||
|
||||
if (!rb_str_equal(n, slot)) {
|
||||
rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
|
||||
rb_class_name(klass),
|
||||
slot, n);
|
||||
{
|
||||
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++) {
|
||||
VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
|
||||
slot = r_symbol(arg);
|
||||
|
||||
if (!rb_str_equal(n, slot)) {
|
||||
rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
|
||||
rb_class_name(klass),
|
||||
slot, n);
|
||||
}
|
||||
if (keyword_init) {
|
||||
rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
|
||||
}
|
||||
else {
|
||||
rb_ary_push(values, r_object(arg));
|
||||
}
|
||||
arg->readable -= 2;
|
||||
}
|
||||
rb_ary_push(values, r_object(arg));
|
||||
arg->readable -= 2;
|
||||
}
|
||||
rb_struct_initialize(v, values);
|
||||
v = r_leave(v, arg);
|
||||
|
|
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
|
||||
rb_struct_s_members(VALUE klass)
|
||||
{
|
||||
|
@ -298,7 +304,7 @@ static VALUE
|
|||
rb_struct_s_inspect(VALUE 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)");
|
||||
}
|
||||
return inspect;
|
||||
|
@ -616,7 +622,7 @@ rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self)
|
|||
|
||||
rb_struct_modify(self);
|
||||
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;
|
||||
if (argc > 2 || !RB_TYPE_P(argv[0], T_HASH)) {
|
||||
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)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue