[前][次][番号順一覧][スレッド一覧]

ruby-changes:49501

From: k0kubun <ko1@a...>
Date: Fri, 5 Jan 2018 20:44:38 +0900 (JST)
Subject: [ruby-changes:49501] k0kubun:r61616 (trunk): marshal.c: allow marshalling keyword_init struct

k0kubun	2018-01-05 20:44:31 +0900 (Fri, 05 Jan 2018)

  New Revision: 61616

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61616

  Log:
    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]

  Modified files:
    trunk/internal.h
    trunk/marshal.c
    trunk/struct.c
    trunk/test/ruby/test_marshal.rb
Index: struct.c
===================================================================
--- struct.c	(revision 61615)
+++ struct.c	(revision 61616)
@@ -48,6 +48,12 @@ struct_ivar_get(VALUE c, ID id) https://github.com/ruby/ruby/blob/trunk/struct.c#L48
 }
 
 VALUE
+rb_struct_s_keyword_init(VALUE klass)
+{
+    return struct_ivar_get(klass, id_keyword_init);
+}
+
+VALUE
 rb_struct_s_members(VALUE klass)
 {
     VALUE members = struct_ivar_get(klass, id_members);
@@ -298,7 +304,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/struct.c#L304
 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 V https://github.com/ruby/ruby/blob/trunk/struct.c#L622
 
     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);
Index: internal.h
===================================================================
--- internal.h	(revision 61615)
+++ internal.h	(revision 61616)
@@ -1768,6 +1768,7 @@ VALUE rb_to_symbol_type(VALUE obj); https://github.com/ruby/ruby/blob/trunk/internal.h#L1768
 /* 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);
Index: marshal.c
===================================================================
--- marshal.c	(revision 61615)
+++ marshal.c	(revision 61616)
@@ -1811,17 +1811,31 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1811
 	    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);
+	    {
+		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 (!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);
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 61615)
+++ test/ruby/test_marshal.rb	(revision 61616)
@@ -772,4 +772,11 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L772
       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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]