Skip to content

Backport "Scala.js: Emit js.NewArray IR nodes when possible." to 3.3 LTS #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,8 @@ class JSCodeGen()(using genCtx: Context) {
genCoercion(tree, receiver, code)
else if (code == JSPrimitives.THROW)
genThrow(tree, args)
else if (code == JSPrimitives.NEW_ARRAY)
genNewArray(tree, args)
else if (JSPrimitives.isJSPrimitive(code))
genJSPrimitive(tree, args, code, isStat)
else
Expand Down Expand Up @@ -3022,6 +3024,24 @@ class JSCodeGen()(using genCtx: Context) {
}
}

/** Gen a call to the special `newArray` method. */
private def genNewArray(tree: Apply, args: List[Tree]): js.Tree = {
implicit val pos: SourcePosition = tree.sourcePos

val List(elemClazz, Literal(arrayClassConstant), dimsArray: JavaSeqLiteral) = args: @unchecked

dimsArray.elems match {
case singleDim :: Nil =>
// Use a js.NewArray
val arrayTypeRef = toTypeRef(arrayClassConstant.typeValue).asInstanceOf[jstpe.ArrayTypeRef]
js.NewArray(arrayTypeRef, List(genExpr(singleDim)))
case _ =>
// Delegate to jlr.Array.newInstance
js.ApplyStatic(js.ApplyFlags.empty, JLRArrayClassName, js.MethodIdent(JLRArrayNewInstanceMethodName),
List(genExpr(elemClazz), genJavaSeqLiteral(dimsArray)))(jstpe.AnyType)
}
}

/** Gen a "normal" apply (to a true method).
*
* But even these are further refined into:
Expand Down Expand Up @@ -4835,6 +4855,7 @@ class JSCodeGen()(using genCtx: Context) {
object JSCodeGen {

private val NullPointerExceptionClass = ClassName("java.lang.NullPointerException")
private val JLRArrayClassName = ClassName("java.lang.reflect.Array")
private val JSObjectClassName = ClassName("scala.scalajs.js.Object")
private val JavaScriptExceptionClassName = ClassName("scala.scalajs.js.JavaScriptException")

Expand All @@ -4844,6 +4865,9 @@ object JSCodeGen {

private val selectedValueMethodName = MethodName("selectedValue", Nil, ObjectClassRef)

private val JLRArrayNewInstanceMethodName =
MethodName("newInstance", List(jstpe.ClassRef(jsNames.ClassClass), jstpe.ArrayTypeRef(jstpe.IntRef, 1)), ObjectClassRef)

private val ObjectArgConstructorName = MethodName.constructor(List(ObjectClassRef))

private val thisOriginalName = OriginalName("this")
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/backend/sjs/JSPrimitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ object JSPrimitives {
inline val UNWRAP_FROM_THROWABLE = WRAP_AS_THROWABLE + 1 // js.special.unwrapFromThrowable
inline val DEBUGGER = UNWRAP_FROM_THROWABLE + 1 // js.special.debugger

inline val THROW = DEBUGGER + 1
inline val THROW = DEBUGGER + 1 // <special-ops>.throw
inline val NEW_ARRAY = THROW + 1 // scala.runtime.Arrays.newArray

inline val UNION_FROM = THROW + 1 // js.|.from
inline val UNION_FROM = NEW_ARRAY + 1 // js.|.from
inline val UNION_FROM_TYPE_CONSTRUCTOR = UNION_FROM + 1 // js.|.fromTypeConstructor

inline val REFLECT_SELECTABLE_SELECTDYN = UNION_FROM_TYPE_CONSTRUCTOR + 1 // scala.reflect.Selectable.selectDynamic
Expand Down Expand Up @@ -137,6 +138,7 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
addPrimitive(jsdefn.Special_debugger, DEBUGGER)

addPrimitive(defn.throwMethod, THROW)
addPrimitive(defn.newArrayMethod, NEW_ARRAY)

addPrimitive(jsdefn.PseudoUnion_from, UNION_FROM)
addPrimitive(jsdefn.PseudoUnion_fromTypeConstructor, UNION_FROM_TYPE_CONSTRUCTOR)
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5794,11 +5794,13 @@ object Types extends TypeUtils {
protected def derivedLambdaType(tp: LambdaType)(formals: List[tp.PInfo], restpe: Type): Type =
tp.derivedLambdaType(tp.paramNames, formals, restpe)

protected def mapArg(arg: Type, tparam: ParamInfo): Type = arg match
case arg: TypeBounds => this(arg)
case arg => atVariance(variance * tparam.paramVarianceSign)(this(arg))

protected def mapArgs(args: List[Type], tparams: List[ParamInfo]): List[Type] = args match
case arg :: otherArgs if tparams.nonEmpty =>
val arg1 = arg match
case arg: TypeBounds => this(arg)
case arg => atVariance(variance * tparams.head.paramVarianceSign)(this(arg))
val arg1 = mapArg(arg, tparams.head)
val otherArgs1 = mapArgs(otherArgs, tparams.tail)
if ((arg1 eq arg) && (otherArgs1 eq otherArgs)) args
else arg1 :: otherArgs1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ object InteractiveEnrichments extends CommonMtagsEnrichments:
sym,
() => parentSymbols.iterator.map(toSemanticdbSymbol).toList.asJava,
contentType,
)
).nn
documentation.nn.toScala
end symbolDocumentation
end extension
Expand Down
Loading