Skip to content

Commit 8068f4f

Browse files
committed
Try to migrate sjs library
1 parent f1c5d19 commit 8068f4f

File tree

6 files changed

+42
-38
lines changed

6 files changed

+42
-38
lines changed

library-js/src/scala/Console.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ import scala.util.DynamicVariable
131131
object Console extends AnsiColor {
132132
private[this] val outVar = new DynamicVariable[PrintStream](java.lang.System.out)
133133
private[this] val errVar = new DynamicVariable[PrintStream](java.lang.System.err)
134-
private[this] val inVar = new DynamicVariable[BufferedReader](null)
134+
private[this] val inVar = new DynamicVariable[BufferedReader](null.asInstanceOf[BufferedReader])
135135
//new BufferedReader(new InputStreamReader(java.lang.System.in)))
136136

137137
protected def setOutDirect(out: PrintStream): Unit = outVar.value = out

library-js/src/scala/Enumeration.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
107107
private val vmap: mutable.Map[Int, Value] = new mutable.HashMap
108108

109109
/** The cache listing all values of this enumeration. */
110-
@transient private var vset: ValueSet = null
110+
@transient private var vset: ValueSet | Null = null
111111
@transient @volatile private var vsetDefined = false
112112

113113
/** The mapping from the integer used to identify values to their
@@ -121,7 +121,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
121121
vset = (ValueSet.newBuilder ++= vmap.values).result()
122122
vsetDefined = true
123123
}
124-
vset
124+
vset.nn
125125
}
126126

127127
/** The integer to use to identify the next created value. */
@@ -130,7 +130,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
130130
/** The string to use to name the next created value. */
131131
protected var nextName: Iterator[String] = _
132132

133-
private def nextNameOrNull =
133+
private def nextNameOrNull: String | Null =
134134
if (nextName != null && nextName.hasNext) nextName.next() else null
135135

136136
/** The highest integer amongst those used to identify values in this
@@ -192,7 +192,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
192192
* @param name A human-readable name for that value.
193193
* @return Fresh value called `name`.
194194
*/
195-
protected final def Value(name: String): Value = Value(nextId, name)
195+
protected final def Value(name: String | Null): Value = Value(nextId, name)
196196

197197
/** Creates a fresh value, part of this enumeration, called `name`
198198
* and identified by the integer `i`.
@@ -202,7 +202,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
202202
* @param name A human-readable name for that value.
203203
* @return Fresh value with the provided identifier `i` and name `name`.
204204
*/
205-
protected final def Value(i: Int, name: String): Value = new Val(i, name)
205+
protected final def Value(i: Int, name: String | Null): Value = new Val(i, name)
206206

207207
/** The type of the enumerated values. */
208208
@SerialVersionUID(7091335633555234129L)
@@ -231,9 +231,9 @@ abstract class Enumeration (initial: Int) extends Serializable {
231231
* identification behaviour.
232232
*/
233233
@SerialVersionUID(0 - 3501153230598116017L)
234-
protected class Val(i: Int, name: String) extends Value with Serializable {
234+
protected class Val(i: Int, name: String | Null) extends Value with Serializable {
235235
def this(i: Int) = this(i, nextNameOrNull)
236-
def this(name: String) = this(nextId, name)
236+
def this(name: String | Null) = this(nextId, name)
237237
def this() = this(nextId)
238238

239239
assert(!vmap.isDefinedAt(i), "Duplicate id: " + i)

library-js/src/scala/collection/mutable/ArrayBuilder.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sealed abstract class ArrayBuilder[T]
3232
extends ReusableBuilder[T, Array[T]]
3333
with Serializable {
3434
protected[this] var capacity: Int = 0
35-
protected[this] def elems: Array[T]
35+
protected[this] def elems: Array[T] | Null // may not be allocated at size = capacity = 0
3636
protected var size: Int = 0
3737

3838
def length: Int = size
@@ -60,7 +60,7 @@ sealed abstract class ArrayBuilder[T]
6060
/** Add a slice of an array */
6161
def addAll(xs: Array[_ <: T], offset: Int, length: Int): this.type = {
6262
ensureSize(this.size + length)
63-
Array.copy(xs, offset, elems, this.size, length)
63+
Array.copy(xs, offset, elems.nn, this.size, length)
6464
size += length
6565
this
6666
}
@@ -70,8 +70,8 @@ sealed abstract class ArrayBuilder[T]
7070
if(k > 0) {
7171
ensureSize(this.size + k)
7272
xs match {
73-
case xs: Iterable[T] => xs.copyToArray(elems, this.size)
74-
case _ => xs.iterator.copyToArray(elems, this.size)
73+
case xs: Iterable[T] => xs.copyToArray(elems.nn, this.size)
74+
case _ => xs.iterator.copyToArray(elems.nn, this.size)
7575
}
7676
size += k
7777
} else if(k < 0) super.addAll(xs)
@@ -227,7 +227,7 @@ object ArrayBuilder {
227227
* @tparam T type of elements for the array builder, subtype of `AnyRef` with a `ClassTag` context bound.
228228
*/
229229
@SerialVersionUID(3L)
230-
final class ofRef[T <: AnyRef](implicit ct: ClassTag[T]) extends ArrayBuilder[T] {
230+
final class ofRef[T <: AnyRef | Null](implicit ct: ClassTag[T]) extends ArrayBuilder[T] {
231231

232232
protected var elems: Array[T] = _
233233

@@ -253,7 +253,7 @@ object ArrayBuilder {
253253
if (capacity != 0 && capacity == size) {
254254
capacity = 0
255255
val res = elems
256-
elems = null
256+
elems = null.asInstanceOf[Array[T]]
257257
res
258258
}
259259
else mkArray(size)
@@ -300,7 +300,7 @@ object ArrayBuilder {
300300
if (capacity != 0 && capacity == size) {
301301
capacity = 0
302302
val res = elems
303-
elems = null
303+
elems = null.asInstanceOf[Array[Byte]]
304304
res
305305
}
306306
else mkArray(size)
@@ -342,7 +342,7 @@ object ArrayBuilder {
342342
if (capacity != 0 && capacity == size) {
343343
capacity = 0
344344
val res = elems
345-
elems = null
345+
elems = null.asInstanceOf[Array[Short]]
346346
res
347347
}
348348
else mkArray(size)
@@ -384,7 +384,7 @@ object ArrayBuilder {
384384
if (capacity != 0 && capacity == size) {
385385
capacity = 0
386386
val res = elems
387-
elems = null
387+
elems = null.asInstanceOf[Array[Char]]
388388
res
389389
}
390390
else mkArray(size)
@@ -426,7 +426,7 @@ object ArrayBuilder {
426426
if (capacity != 0 && capacity == size) {
427427
capacity = 0
428428
val res = elems
429-
elems = null
429+
elems = null.asInstanceOf[Array[Int]]
430430
res
431431
}
432432
else mkArray(size)
@@ -468,7 +468,7 @@ object ArrayBuilder {
468468
if (capacity != 0 && capacity == size) {
469469
capacity = 0
470470
val res = elems
471-
elems = null
471+
elems = null.asInstanceOf[Array[Long]]
472472
res
473473
}
474474
else mkArray(size)
@@ -510,7 +510,7 @@ object ArrayBuilder {
510510
if (capacity != 0 && capacity == size) {
511511
capacity = 0
512512
val res = elems
513-
elems = null
513+
elems = null.asInstanceOf[Array[Float]]
514514
res
515515
}
516516
else mkArray(size)
@@ -552,7 +552,7 @@ object ArrayBuilder {
552552
if (capacity != 0 && capacity == size) {
553553
capacity = 0
554554
val res = elems
555-
elems = null
555+
elems = null.asInstanceOf[Array[Double]]
556556
res
557557
}
558558
else mkArray(size)
@@ -594,7 +594,7 @@ object ArrayBuilder {
594594
if (capacity != 0 && capacity == size) {
595595
capacity = 0
596596
val res = elems
597-
elems = null
597+
elems = null.asInstanceOf[Array[Boolean]]
598598
res
599599
}
600600
else mkArray(size)

library-js/src/scala/runtime/ScalaRunTime.scala

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,25 @@ object ScalaRunTime {
254254
case s => s + "\n"
255255
}
256256

257+
// For backwards compatibility with code compiled without -Yexplicit-nulls
258+
private inline def mapNull[A, B](a: A, inline f: B): B =
259+
if((a: A | Null) == null) null.asInstanceOf[B] else f
260+
257261
// Convert arrays to immutable.ArraySeq for use with Java varargs:
258262
def genericWrapArray[T](xs: Array[T]): ArraySeq[T] =
259-
if (xs eq null) null
260-
else ArraySeq.unsafeWrapArray(xs)
261-
def wrapRefArray[T <: AnyRef](xs: Array[T]): ArraySeq[T] = {
262-
if (xs eq null) null
263+
mapNull(xs, ArraySeq.unsafeWrapArray(xs))
264+
def wrapRefArray[T <: AnyRef | Null](xs: Array[T]): ArraySeq[T] = {
265+
if (xs eq null) null.asInstanceOf[ArraySeq[T]]
263266
else if (xs.length == 0) ArraySeq.empty[AnyRef].asInstanceOf[ArraySeq[T]]
264267
else new ArraySeq.ofRef[T](xs)
265268
}
266-
def wrapIntArray(xs: Array[Int]): ArraySeq[Int] = if (xs ne null) new ArraySeq.ofInt(xs) else null
267-
def wrapDoubleArray(xs: Array[Double]): ArraySeq[Double] = if (xs ne null) new ArraySeq.ofDouble(xs) else null
268-
def wrapLongArray(xs: Array[Long]): ArraySeq[Long] = if (xs ne null) new ArraySeq.ofLong(xs) else null
269-
def wrapFloatArray(xs: Array[Float]): ArraySeq[Float] = if (xs ne null) new ArraySeq.ofFloat(xs) else null
270-
def wrapCharArray(xs: Array[Char]): ArraySeq[Char] = if (xs ne null) new ArraySeq.ofChar(xs) else null
271-
def wrapByteArray(xs: Array[Byte]): ArraySeq[Byte] = if (xs ne null) new ArraySeq.ofByte(xs) else null
272-
def wrapShortArray(xs: Array[Short]): ArraySeq[Short] = if (xs ne null) new ArraySeq.ofShort(xs) else null
273-
def wrapBooleanArray(xs: Array[Boolean]): ArraySeq[Boolean] = if (xs ne null) new ArraySeq.ofBoolean(xs) else null
274-
def wrapUnitArray(xs: Array[Unit]): ArraySeq[Unit] = if (xs ne null) new ArraySeq.ofUnit(xs) else null
269+
def wrapIntArray(xs: Array[Int]): ArraySeq[Int] = mapNull(xs, new ArraySeq.ofInt(xs))
270+
def wrapDoubleArray(xs: Array[Double]): ArraySeq[Double] = mapNull(xs, new ArraySeq.ofDouble(xs))
271+
def wrapLongArray(xs: Array[Long]): ArraySeq[Long] = mapNull(xs, new ArraySeq.ofLong(xs))
272+
def wrapFloatArray(xs: Array[Float]): ArraySeq[Float] = mapNull(xs, new ArraySeq.ofFloat(xs))
273+
def wrapCharArray(xs: Array[Char]): ArraySeq[Char] = mapNull(xs, new ArraySeq.ofChar(xs))
274+
def wrapByteArray(xs: Array[Byte]): ArraySeq[Byte] = mapNull(xs, new ArraySeq.ofByte(xs))
275+
def wrapShortArray(xs: Array[Short]): ArraySeq[Short] = mapNull(xs, new ArraySeq.ofShort(xs))
276+
def wrapBooleanArray(xs: Array[Boolean]): ArraySeq[Boolean] = mapNull(xs, new ArraySeq.ofBoolean(xs))
277+
def wrapUnitArray(xs: Array[Unit]): ArraySeq[Unit] = mapNull(xs, new ArraySeq.ofUnit(xs))
275278
}

library/src/scala/Enumeration.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
107107
private val vmap: mutable.Map[Int, Value] = new mutable.HashMap
108108

109109
/** The cache listing all values of this enumeration. */
110-
@transient @annotation.stableNull private var vset: ValueSet | Null = null
110+
@transient private var vset: ValueSet | Null = null
111111
@transient @volatile private var vsetDefined = false
112112

113113
/** The mapping from the integer used to identify values to their
@@ -190,7 +190,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
190190
protected final def Value(i: Int, name: String | Null): Value = new Val(i, name)
191191

192192
private def populateNameMap(): Unit = {
193-
@tailrec def getFields(clazz: Class[_], acc: Array[JField]): Array[JField] = {
193+
@tailrec def getFields(clazz: Class[_] | Null, acc: Array[JField]): Array[JField] = {
194194
if (clazz == null)
195195
acc
196196
else
@@ -250,7 +250,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
250250
@SerialVersionUID(0 - 3501153230598116017L)
251251
protected class Val(i: Int, name: String | Null) extends Value with Serializable {
252252
def this(i: Int) = this(i, nextNameOrNull)
253-
def this(name: String) = this(nextId, name)
253+
def this(name: String | Null) = this(nextId, name)
254254
def this() = this(nextId)
255255

256256
assert(!vmap.isDefinedAt(i), "Duplicate id: " + i)

project/Build.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,7 @@ object Build {
20282028
// NOTE: The only difference here is that we drop `-Werror` and semanticDB for now
20292029
Compile / scalacOptions := Seq("-deprecation", "-feature", "-unchecked", "-encoding", "UTF8", "-language:implicitConversions", "-nowarn"),
20302030
Compile / scalacOptions += "-Yno-stdlib-patches",
2031+
Compile / scalacOptions += "-Yexplicit-nulls",
20312032
Compile / scalacOptions += "-scalajs",
20322033
// Packaging configuration of the stdlib
20332034
Compile / packageBin / publishArtifact := true,

0 commit comments

Comments
 (0)