aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2020-07-23 16:59:03 +0900
committernagachika <nagachika@ruby-lang.org>2020-07-23 16:59:03 +0900
commitae804b143455075687c8b4a401fba48fda72a217 (patch)
treea1a5009a9cb04686d308832030c71c00440a5aec
parentf1563edca0af1a5ad5cc748eb855ff9265e69e7f (diff)
downloadruby-ae804b143455075687c8b4a401fba48fda72a217.tar.gz
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]
-rw-r--r--marshal.c52
-rw-r--r--test/ruby/test_marshal.rb14
-rw-r--r--version.h2
3 files changed, 60 insertions, 8 deletions
diff --git a/marshal.c b/marshal.c
index cac6329599..8baae8f146 100644
--- a/marshal.c
+++ b/marshal.c
@@ -83,7 +83,7 @@ shortlen(size_t len, BDIGIT *ds)
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;
#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)
}
}
-#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)
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)
{
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)
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)
{
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)
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)
}
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)
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 f300710d2c..850f467c10 100644
--- a/test/ruby/test_marshal.rb
+++ b/test/ruby/test_marshal.rb
@@ -753,4 +753,18 @@ class TestMarshal < Test::Unit::TestCase
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 874e21aede..8ba74e948b 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
# 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