From 26df9588d6b55cc72526d7a45ebdd631600a6226 Mon Sep 17 00:00:00 2001 From: k0kubun Date: Fri, 5 Jan 2018 11:44:31 +0000 Subject: [PATCH] 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 --- internal.h | 1 + marshal.c | 34 ++++++++++++++++++++++++---------- struct.c | 10 ++++++++-- test/ruby/test_marshal.rb | 7 +++++++ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/internal.h b/internal.h index 25b382e554..119726b384 100644 --- a/internal.h +++ b/internal.h @@ -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); diff --git a/marshal.c b/marshal.c index dbf8dd461b..1593ca2930 100644 --- a/marshal.c +++ b/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; ireadable -= 2; } - rb_ary_push(values, r_object(arg)); - arg->readable -= 2; } rb_struct_initialize(v, values); v = r_leave(v, arg); diff --git a/struct.c b/struct.c index 654e8670fa..cf28fec6fd 100644 --- a/struct.c +++ b/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); diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb index 80ec0b1684..e269428dda 100644 --- a/test/ruby/test_marshal.rb +++ b/test/ruby/test_marshal.rb @@ -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