Skip to content

Commit e018cb7

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 fb6667b commit e018cb7

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

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

+36-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,42 @@ 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+
/** Work around https://github.com/lampepfl/dotty/issues/5936 by bridging
219+
* most (all?) of the gap in encoding so that Dotty.js artifacts are
220+
* compatible with the restrictions on valid IR identifier names.
221+
*/
222+
private def fullyMangledString(name: Name): String = {
223+
val base = name.mangledString
224+
val len = base.length
225+
226+
// slow path
227+
def encodeFurther(): String = {
228+
val result = new java.lang.StringBuilder()
229+
var i = 0
230+
while (i != len) {
231+
val c = base.charAt(i)
232+
if (c == '_')
233+
result.append("$und")
234+
else if (Character.isJavaIdentifierPart(c))
235+
result.append(c)
236+
else
237+
result.append("$u%04x".format(c.toInt))
238+
i += 1
239+
}
240+
result.toString()
241+
}
242+
243+
var i = 0
244+
while (i != len) {
245+
val c = base.charAt(i)
246+
if (c == '_' || !Character.isJavaIdentifierPart(c))
247+
return encodeFurther()
248+
i += 1
249+
}
250+
base
216251
}
217252

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

0 commit comments

Comments
 (0)