@@ -2553,6 +2553,8 @@ class JSCodeGen()(using genCtx: Context) {
2553
2553
genCoercion(tree, receiver, code)
2554
2554
else if (code == JSPrimitives .THROW )
2555
2555
genThrow(tree, args)
2556
+ else if (code == JSPrimitives .NEW_ARRAY )
2557
+ genNewArray(tree, args)
2556
2558
else if (JSPrimitives .isJSPrimitive(code))
2557
2559
genJSPrimitive(tree, args, code, isStat)
2558
2560
else
@@ -3022,6 +3024,24 @@ class JSCodeGen()(using genCtx: Context) {
3022
3024
}
3023
3025
}
3024
3026
3027
+ /** Gen a call to the special `newArray` method. */
3028
+ private def genNewArray (tree : Apply , args : List [Tree ]): js.Tree = {
3029
+ implicit val pos : SourcePosition = tree.sourcePos
3030
+
3031
+ val List (elemClazz, Literal (arrayClassConstant), dimsArray : JavaSeqLiteral ) = args : @ unchecked
3032
+
3033
+ dimsArray.elems match {
3034
+ case singleDim :: Nil =>
3035
+ // Use a js.NewArray
3036
+ val arrayTypeRef = toTypeRef(arrayClassConstant.typeValue).asInstanceOf [jstpe.ArrayTypeRef ]
3037
+ js.NewArray (arrayTypeRef, List (genExpr(singleDim)))
3038
+ case _ =>
3039
+ // Delegate to jlr.Array.newInstance
3040
+ js.ApplyStatic (js.ApplyFlags .empty, JLRArrayClassName , js.MethodIdent (JLRArrayNewInstanceMethodName ),
3041
+ List (genExpr(elemClazz), genJavaSeqLiteral(dimsArray)))(jstpe.AnyType )
3042
+ }
3043
+ }
3044
+
3025
3045
/** Gen a "normal" apply (to a true method).
3026
3046
*
3027
3047
* But even these are further refined into:
@@ -4835,6 +4855,7 @@ class JSCodeGen()(using genCtx: Context) {
4835
4855
object JSCodeGen {
4836
4856
4837
4857
private val NullPointerExceptionClass = ClassName (" java.lang.NullPointerException" )
4858
+ private val JLRArrayClassName = ClassName (" java.lang.reflect.Array" )
4838
4859
private val JSObjectClassName = ClassName (" scala.scalajs.js.Object" )
4839
4860
private val JavaScriptExceptionClassName = ClassName (" scala.scalajs.js.JavaScriptException" )
4840
4861
@@ -4844,6 +4865,9 @@ object JSCodeGen {
4844
4865
4845
4866
private val selectedValueMethodName = MethodName (" selectedValue" , Nil , ObjectClassRef )
4846
4867
4868
+ private val JLRArrayNewInstanceMethodName =
4869
+ MethodName (" newInstance" , List (jstpe.ClassRef (jsNames.ClassClass ), jstpe.ArrayTypeRef (jstpe.IntRef , 1 )), ObjectClassRef )
4870
+
4847
4871
private val ObjectArgConstructorName = MethodName .constructor(List (ObjectClassRef ))
4848
4872
4849
4873
private val thisOriginalName = OriginalName (" this" )
0 commit comments