Skip to content

Commit 63ced1e

Browse files
committed
Scala.js: Fully mangle names in the backend.
In dotty, `mangledString` does not mangle operators that are not at the end of an identifier, nor characters that are not JavaIdentifierPart's. This is OK for the JVM, but not the Scala.js IR. This commit forces full encoding of all characters.
1 parent e1385a3 commit 63ced1e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

compiler/src/dotty/tools/backend/sjs/JSEncoding.scala

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,38 @@ object JSEncoding {
212212

213213
private def encodeMemberNameInternal(sym: Symbol)(
214214
implicit ctx: Context): String = {
215-
sym.name.toString.replace("_", "$und").replace("~", "$tilde")
215+
fullyMangledString(sym.name)
216+
}
217+
218+
private def fullyMangledString(name: Name): String = {
219+
val base = name.mangledString
220+
val len = base.length
221+
222+
// slow path
223+
def encodeFurther(): String = {
224+
val result = new java.lang.StringBuilder()
225+
var i = 0
226+
while (i != len) {
227+
val c = base.charAt(i)
228+
if (c == '_')
229+
result.append("$und")
230+
else if (Character.isJavaIdentifierPart(c))
231+
result.append(c)
232+
else
233+
result.append("$u%04x".format(c.toInt))
234+
i += 1
235+
}
236+
result.toString()
237+
}
238+
239+
var i = 0
240+
while (i != len) {
241+
val c = base.charAt(i)
242+
if (c == '_' || !Character.isJavaIdentifierPart(c))
243+
return encodeFurther()
244+
i += 1
245+
}
246+
base
216247
}
217248

218249
def toIRType(tp: Type)(implicit ctx: Context): jstpe.Type = {

0 commit comments

Comments
 (0)