Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 256e684

Browse files
committed
Scala.js: Handle @JSName annots with constant-folded arguments.
Previously, if an `@JSName` annotation had an argument that was not a literal, but a reference to a constant expression (such as a `final val`), it would not be constant-folded in the generated Scala.js IR. This produced worse code than necessary. For Wasm, it was particularly bad, as the names must then be evaluated on the Wasm side instead of being pushed to the custom JS helpers.
1 parent ca400bd commit 256e684

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

‎compiler/src/dotty/tools/dotc/transform/sjs/JSSymUtils.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ object JSSymUtils {
164164
sym.getAnnotation(jsdefn.JSNameAnnot).fold[JSName] {
165165
JSName.Literal(defaultJSName)
166166
} { annotation =>
167-
annotation.arguments.head match {
168-
case Literal(Constant(name: String)) => JSName.Literal(name)
169-
case tree => JSName.Computed(tree.symbol)
167+
annotation.argumentConstantString(0) match {
168+
case Some(name) => JSName.Literal(name)
169+
case None => JSName.Computed(annotation.arguments.head.symbol)
170170
}
171171
}
172172
}

‎compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
154154
}
155155

156156
checkJSNameAnnots(sym)
157-
constFoldJSExportTopLevelAndStaticAnnotations(sym)
157+
constantFoldJSAnnotations(sym)
158158

159159
markExposedIfRequired(tree.symbol)
160160

@@ -1089,17 +1089,18 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
10891089
}
10901090
}
10911091

1092-
/** Constant-folds arguments to `@JSExportTopLevel` and `@JSExportStatic`.
1092+
/** Constant-folds arguments to `@JSName`, `@JSExportTopLevel` and `@JSExportStatic`.
10931093
*
10941094
* Unlike scalac, dotc does not constant-fold expressions in annotations.
10951095
* Our back-end needs to have access to the arguments to those two
10961096
* annotations as literal strings, so we specifically constant-fold them
10971097
* here.
10981098
*/
1099-
private def constFoldJSExportTopLevelAndStaticAnnotations(sym: Symbol)(using Context): Unit = {
1099+
private def constantFoldJSAnnotations(sym: Symbol)(using Context): Unit = {
11001100
val annots = sym.annotations
11011101
val newAnnots = annots.mapConserve { annot =>
1102-
if (annot.symbol == jsdefn.JSExportTopLevelAnnot || annot.symbol == jsdefn.JSExportStaticAnnot) {
1102+
if (annot.symbol == jsdefn.JSExportTopLevelAnnot || annot.symbol == jsdefn.JSExportStaticAnnot ||
1103+
annot.symbol == jsdefn.JSNameAnnot) {
11031104
annot.tree match {
11041105
case app @ Apply(fun, args) =>
11051106
val newArgs = args.mapConserve { arg =>
@@ -1109,7 +1110,7 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
11091110
case _ =>
11101111
arg.tpe.widenTermRefExpr.normalized match {
11111112
case ConstantType(c) => Literal(c).withSpan(arg.span)
1112-
case _ => arg// PrepJSExports will emit an error for those cases
1113+
case _ => arg
11131114
}
11141115
}
11151116
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /