mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Fix WeakRef finalize
* array.c (rb_ary_delete_same_obj): new function for WeakRef. This deletes same objects as item argument in the array. * internal.h (rb_ary_delete_same_obj): add a declaration. * gc.c (wmap_final_func): remove WeakRef object reference from the array. rb_ary_delete() is not usable because it uses rb_equal() to compare object references. * gc.c (wmap_finalize): remove recycled object references from weak map hash properly. How to get object reference from object id was wrong. st_delete() doesn't work properly if key and value arguments are same. The key of obj2wmap is referenced object and the value of obj2wmap is WeakRef array. * gc.c (wmap_aset): obj2wmap should contain WeakRef array in the definition. * test/test_weakref.rb (TestWeakRef#test_not_reference_different_object, TestWeakRef#test_weakref_finalize): add tests for above. [ruby-core:49044] [Bug #7304] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37834 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e6c2ffa981
commit
1cdeab5cdd
5 changed files with 104 additions and 10 deletions
34
array.c
34
array.c
|
@ -2776,6 +2776,40 @@ rb_ary_delete(VALUE ary, VALUE item)
|
|||
return v;
|
||||
}
|
||||
|
||||
VALUE
|
||||
rb_ary_delete_same_obj(VALUE ary, VALUE item)
|
||||
{
|
||||
VALUE v = item;
|
||||
long i1, i2;
|
||||
|
||||
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
|
||||
VALUE e = RARRAY_PTR(ary)[i1];
|
||||
|
||||
if (e == item) {
|
||||
v = e;
|
||||
continue;
|
||||
}
|
||||
if (i1 != i2) {
|
||||
rb_ary_store(ary, i2, e);
|
||||
}
|
||||
i2++;
|
||||
}
|
||||
if (RARRAY_LEN(ary) == i2) {
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
rb_ary_modify(ary);
|
||||
if (RARRAY_LEN(ary) > i2) {
|
||||
ARY_SET_LEN(ary, i2);
|
||||
if (i2 * 2 < ARY_CAPA(ary) &&
|
||||
ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
|
||||
ary_resize_capa(ary, i2*2);
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
VALUE
|
||||
rb_ary_delete_at(VALUE ary, long pos)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue