Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mrbgems/mruby-objectspace-weakmap/mrbgem.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ MRuby::Gem::Specification.new('mruby-objectspace-weakmap') do |spec|
spec.author = 'mruby developers'
spec.summary = 'ObjectSpace::WeakMap and ObjectSpace::WeakKeyMap'
spec.build.defines << 'MRB_USE_GC_WEAK'
spec.add_test_dependency 'mruby-objectspace', core: 'mruby-objectspace'
end
3 changes: 3 additions & 0 deletions mrbgems/mruby-objectspace-weakmap/src/weakmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ static struct RArray*
wm_vals_new(mrb_state *mrb, mrb_value self, uint32_t capa)
{
mrb_value ary = mrb_ary_new_capa(mrb, capa);
/* the map and wm_clear_dead() index it by entry position without checking
its length, so Ruby must not be able to find it and shrink it */
mrb_ary_ptr(ary)->c = NULL; /* hide from ObjectSpace.each_object */
if (capa > 0) mrb_ary_set(mrb, ary, capa-1, mrb_nil_value());
/* a frozen map still takes entries, as in CRuby */
mrb_obj_iv_set_force(mrb, mrb_obj_ptr(self), MRB_SYM(__values__), ary);
Expand Down
19 changes: 19 additions & 0 deletions mrbgems/mruby-objectspace-weakmap/test/weakmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ def weakkeymap_fill(m, n)
extra.each { |k| assert_equal("e#{k}", m[k]) }
end

assert("ObjectSpace.each_object does not reach WeakKeyMap's values") do
# the map indexes the Array of its values by entry position; one Ruby
# could shrink would have #[], #delete and the GC go past its end
m = ObjectSpace::WeakKeyMap.new
marker = Object.new
keys = (0...20).map { |i| "k#{i}" }
keys.each { |k| m[k] = marker }
found = 0
ObjectSpace.each_object(Array) do |a|
next unless a.size >= keys.size && a[0].equal?(marker)
found += 1
a.clear
end
assert_equal 0, found
keys.each { |k| assert_same marker, m[k] }
assert_same marker, m.delete(keys[19])
GC.start
end

assert("ObjectSpace::WeakMap#each yields each entry when the block grows the map") do
m = ObjectSpace::WeakMap.new
keys = (0...8).map { |i| "k#{i}" }
Expand Down
Loading