[Feature #19362] Call #initialize_dup hook at Proc#dup

This commit is contained in:
Nobuyoshi Nakada 2023-01-22 11:40:37 +09:00
parent 7d159a8787
commit 1507118f0b
2 changed files with 20 additions and 1 deletions

12
proc.c
View File

@ -152,6 +152,16 @@ proc_clone(VALUE self)
{
VALUE procval = rb_proc_dup(self);
CLONESETUP(procval, self);
rb_check_funcall(procval, idInitialize_clone, 1, &self);
return procval;
}
/* :nodoc: */
static VALUE
proc_dup(VALUE self)
{
VALUE procval = rb_proc_dup(self);
rb_check_funcall(procval, idInitialize_dup, 1, &self);
return procval;
}
@ -4258,7 +4268,7 @@ Init_Proc(void)
rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
rb_define_method(rb_cProc, "arity", proc_arity, 0);
rb_define_method(rb_cProc, "clone", proc_clone, 0);
rb_define_method(rb_cProc, "dup", rb_proc_dup, 0);
rb_define_method(rb_cProc, "dup", proc_dup, 0);
rb_define_method(rb_cProc, "hash", proc_hash, 0);
rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
rb_define_alias(rb_cProc, "inspect", "to_s");

View File

@ -388,6 +388,15 @@ class TestProc < Test::Unit::TestCase
def test_dup_subclass
c1 = Class.new(Proc)
assert_equal c1, c1.new{}.dup.class, '[Bug #17545]'
c1 = Class.new(Proc) {def initialize_dup(*) throw :initialize_dup; end}
assert_throw(:initialize_dup) {c1.new{}.dup}
end
def test_clone_subclass
c1 = Class.new(Proc)
assert_equal c1, c1.new{}.clone.class, '[Bug #17545]'
c1 = Class.new(Proc) {def initialize_clone(*) throw :initialize_clone; end}
assert_throw(:initialize_clone) {c1.new{}.clone}
end
def test_binding