@@ -54,42 +54,44 @@ object JSEncoding {
54
54
55
55
// Fresh local name generator ----------------------------------------------
56
56
57
- private val usedLocalNames = new ScopedVar [mutable.Set [String ]]
58
- private val localSymbolNames = new ScopedVar [mutable.Map [Symbol , String ]]
59
- private val isReserved =
60
- Set (" arguments" , " eval" , ScalaJSEnvironmentName )
61
-
62
- def withNewLocalNameScope [A ](body : => A ): A =
63
- withScopedVars(
64
- usedLocalNames := mutable.Set .empty,
65
- localSymbolNames := mutable.Map .empty
66
- )(body)
67
-
68
- private def freshName (base : String = " x" ): String = {
69
- var suffix = 1
70
- var longName = base
71
- while (usedLocalNames(longName) || isReserved(longName)) {
72
- suffix += 1
73
- longName = base+ " $" + suffix
74
- }
75
- usedLocalNames += longName
76
- mangleJSName(longName)
77
- }
57
+ class LocalNameGenerator {
58
+ import LocalNameGenerator ._
59
+
60
+ private val usedLocalNames = mutable.Set .empty[String ]
61
+ private val localSymbolNames = mutable.Map .empty[Symbol , String ]
62
+
63
+ def localSymbolName (sym : Symbol )(implicit ctx : Context ): String =
64
+ localSymbolNames.getOrElseUpdate(sym, freshName(sym.name.toString))
65
+
66
+ def freshLocalIdent ()(implicit pos : ir.Position ): js.Ident =
67
+ js.Ident (freshName(), None )
78
68
79
- def freshLocalIdent ()(implicit pos : ir.Position ): js.Ident =
80
- js.Ident (freshName(), None )
69
+ def freshLocalIdent (base : String )(implicit pos : ir.Position ): js.Ident =
70
+ js.Ident (freshName(base ), Some (base) )
81
71
82
- def freshLocalIdent (base : String )(implicit pos : ir.Position ): js.Ident =
83
- js.Ident (freshName(base), Some (base))
72
+ private def freshName (base : String = " x" ): String = {
73
+ var suffix = 1
74
+ var longName = base
75
+ while (usedLocalNames(longName) || isReserved(longName)) {
76
+ suffix += 1
77
+ longName = base+ " $" + suffix
78
+ }
79
+ usedLocalNames += longName
80
+ mangleJSName(longName)
81
+ }
82
+ }
84
83
85
- private def localSymbolName (sym : Symbol )(implicit ctx : Context ): String =
86
- localSymbolNames.getOrElseUpdate(sym, freshName(sym.name.toString))
84
+ private object LocalNameGenerator {
85
+ private val isReserved =
86
+ Set (" arguments" , " eval" , ScalaJSEnvironmentName )
87
+ }
87
88
88
89
// Encoding methods ----------------------------------------------------------
89
90
90
- def encodeLabelSym (sym : Symbol )(implicit ctx : Context , pos : ir.Position ): js.Ident = {
91
+ def encodeLabelSym (sym : Symbol )(
92
+ implicit ctx : Context , pos : ir.Position , localNames : LocalNameGenerator ): js.Ident = {
91
93
require(sym.is(Flags .Label ), " encodeLabelSym called with non-label symbol: " + sym)
92
- js.Ident (localSymbolName(sym), Some (sym.unexpandedName.decoded))
94
+ js.Ident (localNames. localSymbolName(sym), Some (sym.unexpandedName.decoded))
93
95
}
94
96
95
97
private def allRefClasses (implicit ctx : Context ): Set [Symbol ] = {
@@ -190,10 +192,10 @@ object JSEncoding {
190
192
}
191
193
192
194
def encodeLocalSym (sym : Symbol )(
193
- implicit ctx : Context , pos : ir.Position ): js.Ident = {
195
+ implicit ctx : Context , pos : ir.Position , localNames : LocalNameGenerator ): js.Ident = {
194
196
require(! sym.owner.isClass && sym.isTerm && ! sym.is(Flags .Method ) && ! sym.is(Flags .Module ),
195
197
" encodeLocalSym called with non-local symbol: " + sym)
196
- js.Ident (localSymbolName(sym), Some (sym.unexpandedName.decoded))
198
+ js.Ident (localNames. localSymbolName(sym), Some (sym.unexpandedName.decoded))
197
199
}
198
200
199
201
def foreignIsImplClass (sym : Symbol )(implicit ctx : Context ): Boolean =
0 commit comments