1212
1313package scala
1414
15+ import scala .language .`2.13`
16+
1517import scala .scalajs .js
1618
1719/** This class provides a simple way to get unique objects for equal strings.
@@ -37,15 +39,14 @@ final class Symbol private (val name: String) extends Serializable {
3739 override def equals (other : Any ) = this eq other.asInstanceOf [AnyRef ]
3840}
3941
40- // Modified to use Scala.js specific cache
41- object Symbol extends JSUniquenessCache [Symbol ] {
42+ object Symbol extends UniquenessCache [String , Symbol ] {
4243 override def apply (name : String ): Symbol = super .apply(name)
4344 protected def valueFromKey (name : String ): Symbol = new Symbol (name)
4445 protected def keyFromValue (sym : Symbol ): Option [String ] = Some (sym.name)
4546}
4647
47- private [scala] abstract class JSUniquenessCache [ V ]
48- {
48+ // Modified to use Scala.js specific cache
49+ private [scala] abstract class UniquenessCache [ K , V >: Null ] {
4950 private val cache = js.Dictionary .empty[V ]
5051
5152 protected def valueFromKey (k : String ): V
@@ -55,59 +56,4 @@ private[scala] abstract class JSUniquenessCache[V]
5556 cache.getOrElseUpdate(name, valueFromKey(name))
5657
5758 def unapply (other : V ): Option [String ] = keyFromValue(other)
58- }
59-
60- /** This is private so it won't appear in the library API, but
61- * abstracted to offer some hope of reusability. */
62- /* DELETED for Scala.js
63- private[scala] abstract class UniquenessCache[K >: js.String, V >: Null]
64- {
65-
66- import java.lang.ref.WeakReference
67- import java.util.WeakHashMap
68- import java.util.concurrent.locks.ReentrantReadWriteLock
69-
70- private[this] val rwl = new ReentrantReadWriteLock()
71- private[this] val rlock = rwl.readLock
72- private[this] val wlock = rwl.writeLock
73- private[this] val map = new WeakHashMap[K, WeakReference[V]]
74-
75- protected def valueFromKey(k: K): V
76- protected def keyFromValue(v: V): Option[K]
77-
78- def apply(name: K): V = {
79- def cached(): V = {
80- rlock.lock
81- try {
82- val reference = map get name
83- if (reference == null) null
84- else reference.get // will be null if we were gc-ed
85- }
86- finally rlock.unlock
87- }
88- def updateCache(): V = {
89- wlock.lock
90- try {
91- val res = cached()
92- if (res != null) res
93- else {
94- // If we don't remove the old String key from the map, we can
95- // wind up with one String as the key and a different String as
96- // the name field in the Symbol, which can lead to surprising GC
97- // behavior and duplicate Symbols. See scala/bug#6706.
98- map remove name
99- val sym = valueFromKey(name)
100- map.put(name, new WeakReference(sym))
101- sym
102- }
103- }
104- finally wlock.unlock
105- }
106-
107- val res = cached()
108- if (res == null) updateCache()
109- else res
110- }
111- def unapply(other: V): Option[K] = keyFromValue(other)
112- }
113- */
59+ }
0 commit comments