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

ruby-changes:62378

From: nagachika <ko1@a...>
Date: Thu, 23 Jul 2020 16:59:25 +0900 (JST)
Subject: [ruby-changes:62378] ae804b1434 (ruby_2_7): merge revision(s) b23fd59cbb3f097bcd559d0c85a86ff7a1eeeb7e: [Backport #16501]

https://git.ruby-lang.org/ruby.git/commit/?id=ae804b1434

From ae804b143455075687c8b4a401fba48fda72a217 Mon Sep 17 00:00:00 2001
From: nagachika <nagachika@r...>
Date: Thu, 23 Jul 2020 16:59:03 +0900
Subject: merge revision(s) b23fd59cbb3f097bcd559d0c85a86ff7a1eeeb7e: [Backport
 #16501]

	marshal.c: Support dump and load of a Hash with the ruby2_keywords
	 flag

	It is useful for a program that dumps and load arguments (like drb).
	In future, they should deal with both positional arguments and keyword
	ones explicitly, but until ruby2_keywords is deprecated, it is good to
	support the flag in marshal.

	The implementation is similar to String's encoding; it is dumped as a
	hidden instance variable.

	[Feature #16501]

diff --git a/marshal.c b/marshal.c
index cac6329..8baae8f 100644
--- a/marshal.c
+++ b/marshal.c
@@ -83,7 +83,7 @@ shortlen(size_t len, BDIGIT *ds) https://github.com/ruby/ruby/blob/trunk/marshal.c#L83
 static ID s_dump, s_load, s_mdump, s_mload;
 static ID s_dump_data, s_load_data, s_alloc, s_call;
 static ID s_getbyte, s_read, s_write, s_binmode;
-static ID s_encoding_short;
+static ID s_encoding_short, s_ruby2_keywords_flag;
 
 #define name_s_dump	"_dump"
 #define name_s_load	"_load"
@@ -98,6 +98,7 @@ static ID s_encoding_short; https://github.com/ruby/ruby/blob/trunk/marshal.c#L98
 #define name_s_write	"write"
 #define name_s_binmode	"binmode"
 #define name_s_encoding_short "E"
+#define name_s_ruby2_keywords_flag "K"
 
 typedef struct {
     VALUE newclass;
@@ -557,7 +558,7 @@ w_uclass(VALUE obj, VALUE super, struct dump_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L558
     }
 }
 
-#define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || !rb_id2str(id))
+#define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || id == s_ruby2_keywords_flag || !rb_id2str(id))
 
 struct w_ivar_arg {
     struct dump_call_arg *dump;
@@ -577,6 +578,10 @@ w_obj_each(st_data_t key, st_data_t val, st_data_t a) https://github.com/ruby/ruby/blob/trunk/marshal.c#L578
             rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped",
                     CLASS_OF(arg->obj));
         }
+        if (id == s_ruby2_keywords_flag) {
+            rb_warn("instance variable `"name_s_ruby2_keywords_flag"' on class %"PRIsVALUE" is not dumped",
+                    CLASS_OF(arg->obj));
+        }
         return ST_CONTINUE;
     }
     if (!ivarg->num_ivar) {
@@ -654,6 +659,7 @@ has_ivars(VALUE obj, VALUE encname, VALUE *ivobj) https://github.com/ruby/ruby/blob/trunk/marshal.c#L659
 {
     st_index_t enc = !NIL_P(encname);
     st_index_t num = 0;
+    st_index_t ruby2_keywords_flag = 0;
 
     if (SPECIAL_CONST_P(obj)) goto generic;
     switch (BUILTIN_TYPE(obj)) {
@@ -661,13 +667,16 @@ has_ivars(VALUE obj, VALUE encname, VALUE *ivobj) https://github.com/ruby/ruby/blob/trunk/marshal.c#L667
       case T_CLASS:
       case T_MODULE:
 	break; /* counted elsewhere */
+      case T_HASH:
+        ruby2_keywords_flag = RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS ? 1 : 0;
+        /* fall through */
       default:
       generic:
 	rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
-	if (num) *ivobj = obj;
+	if (ruby2_keywords_flag || num) *ivobj = obj;
     }
 
-    return num + enc;
+    return num + enc + ruby2_keywords_flag;
 }
 
 static void
@@ -687,7 +696,14 @@ w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L696
 {
     w_long(num, arg->arg);
     num -= w_encoding(encname, arg);
-    if (ivobj != Qundef) {
+    if (RB_TYPE_P(ivobj, T_HASH) && (RHASH(ivobj)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
+        int limit = arg->limit;
+        if (limit >= 0) ++limit;
+        w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
+	w_object(Qtrue, arg->arg, limit);
+        num--;
+    }
+    if (ivobj != Qundef && num) {
         w_ivar_each(ivobj, num, arg);
     }
 }
@@ -1388,6 +1404,19 @@ sym2encidx(VALUE sym, VALUE val) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1404
     return -1;
 }
 
+static int
+ruby2_keywords_flag_check(VALUE sym)
+{
+    const char *p;
+    long l;
+    RSTRING_GETMEM(sym, p, l);
+    if (l <= 0) return 0;
+    if (name_equal(name_s_ruby2_keywords_flag, rb_strlen_lit(name_s_ruby2_keywords_flag), p, 1)) {
+        return 1;
+    }
+    return 0;
+}
+
 static VALUE
 r_symlink(struct load_arg *arg)
 {
@@ -1541,8 +1570,16 @@ r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1570
                 }
 		if (has_encoding) *has_encoding = TRUE;
 	    }
-	    else {
-		rb_ivar_set(obj, rb_intern_str(sym), val);
+	    else if (ruby2_keywords_flag_check(sym)) {
+                if (RB_TYPE_P(obj, T_HASH)) {
+                    RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
+                }
+                else {
+                    rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
+                }
+            }
+            else {
+                rb_ivar_set(obj, rb_intern_str(sym), val);
 	    }
 	} while (--len > 0);
     }
@@ -2288,6 +2325,7 @@ Init_marshal(void) https://github.com/ruby/ruby/blob/trunk/marshal.c#L2325
     set_id(s_write);
     set_id(s_binmode);
     set_id(s_encoding_short);
+    set_id(s_ruby2_keywords_flag);
 
     rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
     rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);
diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb
index f300710..850f467 100644
--- a/test/ruby/test_marshal.rb
+++ b/test/ruby/test_marshal.rb
@@ -753,4 +753,18 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L753
       Marshal.dump(obj)
     end
   end
+
+  ruby2_keywords def ruby2_keywords_hash(*a)
+    a.last
+  end
+
+  def ruby2_keywords_test(key: 1)
+    key
+  end
+
+  def test_marshal_with_ruby2_keywords_hash
+    flagged_hash = ruby2_keywords_hash(key: 42)
+    hash = Marshal.load(Marshal.dump(flagged_hash))
+    assert_equal(42, ruby2_keywords_test(*[hash]))
+  end
 end
diff --git a/version.h b/version.h
index 874e21a..8ba74e9 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L2
 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
 #define RUBY_VERSION_TEENY 1
 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 103
+#define RUBY_PATCHLEVEL 104
 
 #define RUBY_RELEASE_YEAR 2020
 #define RUBY_RELEASE_MONTH 7
-- 
cgit v0.10.2


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

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