1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Share freeze option handling

This commit is contained in:
Nobuyoshi Nakada 2021-06-28 10:51:42 +09:00
parent e724857f42
commit 47a9b58b2a
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
3 changed files with 11 additions and 24 deletions

20
hash.c
View file

@ -6501,25 +6501,11 @@ static VALUE
env_clone(int argc, VALUE *argv, VALUE obj)
{
if (argc) {
static ID keyword_ids[1];
VALUE opt, kwfreeze;
if (!keyword_ids[0]) {
CONST_ID(keyword_ids[0], "freeze");
}
rb_scan_args(argc, argv, "0:", &opt);
if (!NIL_P(opt)) {
rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
switch (kwfreeze) {
case Qtrue:
if (rb_scan_args(argc, argv, "0:", &opt) < argc) {
kwfreeze = rb_get_freeze_opt(1, &opt);
if (RTEST(kwfreeze)) {
rb_raise(rb_eTypeError, "cannot freeze ENV");
break;
default:
rb_raise(rb_eArgError, "invalid value for freeze keyword");
break;
case Qnil:
case Qfalse:
break;
}
}
}

View file

@ -42,6 +42,7 @@ void rb_obj_copy_ivar(VALUE dest, VALUE obj);
VALUE rb_false(VALUE obj);
VALUE rb_convert_type_with_id(VALUE v, int t, const char* nam, ID mid);
VALUE rb_obj_size(VALUE self, VALUE args, VALUE obj);
VALUE rb_get_freeze_opt(int argc, VALUE *argv);
MJIT_SYMBOL_EXPORT_END
static inline void

View file

@ -369,7 +369,6 @@ init_copy(VALUE dest, VALUE obj)
}
}
static VALUE freeze_opt(int argc, VALUE *argv);
static VALUE immutable_obj_clone(VALUE obj, VALUE kwfreeze);
static VALUE mutable_obj_clone(VALUE obj, VALUE kwfreeze);
PUREFUNC(static inline int special_object_p(VALUE obj)); /*!< \private */
@ -418,12 +417,12 @@ rb_obj_clone2(rb_execution_context_t *ec, VALUE obj, VALUE freeze)
VALUE
rb_immutable_obj_clone(int argc, VALUE *argv, VALUE obj)
{
VALUE kwfreeze = freeze_opt(argc, argv);
VALUE kwfreeze = rb_get_freeze_opt(argc, argv);
return immutable_obj_clone(obj, kwfreeze);
}
static VALUE
freeze_opt(int argc, VALUE *argv)
VALUE
rb_get_freeze_opt(int argc, VALUE *argv)
{
static ID keyword_ids[1];
VALUE opt;
@ -657,9 +656,10 @@ static VALUE
rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
{
VALUE orig, opts;
rb_scan_args(argc, argv, "1:", &orig, &opts);
/* Ignore a freeze keyword */
if (argc == 2) (void)freeze_opt(1, &opts);
if (rb_scan_args(argc, argv, "1:", &orig, &opts) < argc) {
/* Ignore a freeze keyword */
rb_get_freeze_opt(1, &opts);
}
rb_funcall(obj, id_init_copy, 1, orig);
return obj;
}