Skip to content

Commit b14eb61

Browse files
author
Nicolas Laurent
committed
allow immediate and frozen key/values in WeakMap
1 parent dfc60c6 commit b14eb61

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Compatibility:
6969
* Implemented `Kernel#raise` `cause` parameter.
7070
* Improved compatibility of `Signal.trap` and `Kernel#trap` (#2287, @chrisseaton).
7171
* Implemented `GC.stat(:total_allocated_objects)` as `0` (#2292, @chrisseaton).
72+
* `ObjectSpace::WeakMap` now supports immediate and frozen values as both keys and values (#2267).
7273

7374
Performance:
7475

spec/tags/core/objectspace/weakmap/element_set_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/truffle/objectspace/weakmap_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,20 @@
7171
map.each_value { |v| a << v }
7272
a.should == ["X"]
7373
end
74+
75+
it "supports constant objects" do
76+
map = ObjectSpace::WeakMap.new
77+
map[1] = 2
78+
Primitive.gc_force
79+
map[1].should == 2
80+
end
81+
82+
it "supports frozen objects" do
83+
map = ObjectSpace::WeakMap.new
84+
x = "x".freeze
85+
y = "y".freeze
86+
map[x] = y
87+
Primitive.gc_force
88+
map[x].should == y
89+
end
7490
end

src/main/ruby/truffleruby/core/weakmap.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,15 @@
1010

1111
module ObjectSpace
1212
# WeakMap uses identity comparison semantics. The implementation assumes that the Java representation of objects
13-
# do compare (equals() and hashCode()) using object identity. This is the case for instances of RubyDynamicObject.
13+
# do compare (equals() and hashCode()) using object identity. This is the case for instances of RubyDynamicObject,
14+
# boxed primitives, and RubySymbol.
1415
#
1516
# However, results are unspecified if used with instances of TruffleObject that override equals() and
1617
# hashCode().
17-
#
18-
# Note that, following MRI, we disallow immediate (primitive) values and frozen objects as keys or value of
19-
# WeakMap. We could easily transcend this limitation as we do not modify objects like MRI does (it sets a finalizer).
20-
# However, keeping the limitation enables the above assumption of identity comparison.
2118
class WeakMap
2219
include Enumerable
2320

24-
private def check_key_or_value(kv, type)
25-
if Primitive.immediate_value?(kv)
26-
raise ArgumentError, "WeakMap #{type} can't be an instance of #{kv.class}"
27-
elsif Truffle::KernelOperations.value_frozen?(kv)
28-
raise FrozenError, "WeakMap #{type} can't be a frozen object"
29-
end
30-
end
31-
3221
def []=(key, value)
33-
check_key_or_value(key, 'key')
34-
check_key_or_value(value, 'value')
3522
Primitive.weakmap_aset(self, key, value)
3623
end
3724

test/mri/excludes/TestWeakMap.rb

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)