diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-12-26 23:04:46 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-02-14 12:02:07 +0900 |
commit | 45f0e3a673964069a4c9c57ce8665cbc21ac267f (patch) | |
tree | 9045469a13bc02380afd2a3109d48f3a5d289368 /struct.c | |
parent | dbe5b0dcfffbad5a0ce3af1570dbb6db70266275 (diff) |
[Bug #19259] `Data#with` should call `initialize` method
Diffstat (limited to 'struct.c')
-rw-r--r-- | struct.c | 23 |
1 files changed, 6 insertions, 17 deletions
@@ -1820,10 +1820,12 @@ rb_data_initialize_m(int argc, const VALUE *argv, VALUE self) arg.self = self; arg.unknown_keywords = Qnil; rb_hash_foreach(argv[0], struct_hash_set_i, (VALUE)&arg); + // Freeze early before potentially raising, so that we don't leave an + // unfrozen copy on the heap, which could get exposed via ObjectSpace. + OBJ_FREEZE_RAW(self); if (arg.unknown_keywords != Qnil) { rb_exc_raise(rb_keyword_error_new("unknown", arg.unknown_keywords)); } - OBJ_FREEZE_RAW(self); return Qnil; } @@ -1875,22 +1877,9 @@ rb_data_with(int argc, const VALUE *argv, VALUE self) return self; } - VALUE copy = rb_obj_alloc(rb_obj_class(self)); - rb_struct_init_copy(copy, self); - - struct struct_hash_set_arg arg; - arg.self = copy; - arg.unknown_keywords = Qnil; - rb_hash_foreach(kwargs, struct_hash_set_i, (VALUE)&arg); - // Freeze early before potentially raising, so that we don't leave an - // unfrozen copy on the heap, which could get exposed via ObjectSpace. - RB_OBJ_FREEZE_RAW(copy); - - if (arg.unknown_keywords != Qnil) { - rb_exc_raise(rb_keyword_error_new("unknown", arg.unknown_keywords)); - } - - return copy; + VALUE h = rb_struct_to_h(self); + rb_hash_update_by(h, kwargs, NULL); + return rb_class_new_instance_kw(1, &h, rb_obj_class(self), TRUE); } /* |