Skip to content

Commit a4e9f5f

Browse files
committed
Try to migrate sjs library
1 parent f2ef977 commit a4e9f5f

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
@@ -129,7 +129,7 @@ import scala.util.DynamicVariable
129129
object Console extends AnsiColor {
130130
private[this] val outVar = new DynamicVariable[PrintStream](java.lang.System.out)
131131
private[this] val errVar = new DynamicVariable[PrintStream](java.lang.System.err)
132-
private[this] val inVar = new DynamicVariable[BufferedReader](null)
132+
private[this] val inVar = new DynamicVariable[BufferedReader](null.asInstanceOf[BufferedReader])
133133
//new BufferedReader(new InputStreamReader(java.lang.System.in)))
134134

135135
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
@@ -105,7 +105,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
105105
private val vmap: mutable.Map[Int, Value] = new mutable.HashMap
106106

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

111111
/** The mapping from the integer used to identify values to their
@@ -119,7 +119,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
119119
vset = (ValueSet.newBuilder ++= vmap.values).result()
120120
vsetDefined = true
121121
}
122-
vset
122+
vset.nn
123123
}
124124

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

131-
private def nextNameOrNull =
131+
private def nextNameOrNull: String | Null =
132132
if (nextName != null && nextName.hasNext) nextName.next() else null
133133

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

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

205205
/** The type of the enumerated values. */
206206
@SerialVersionUID(7091335633555234129L)
@@ -229,9 +229,9 @@ abstract class Enumeration (initial: Int) extends Serializable {
229229
* identification behaviour.
230230
*/
231231
@SerialVersionUID(0 - 3501153230598116017L)
232-
protected class Val(i: Int, name: String) extends Value with Serializable {
232+
protected class Val(i: Int, name: String | Null) extends Value with Serializable {
233233
def this(i: Int) = this(i, nextNameOrNull)
234-
def this(name: String) = this(nextId, name)
234+
def this(name: String | Null) = this(nextId, name)
235235
def this() = this(nextId)
236236

237237
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
@@ -30,7 +30,7 @@ sealed abstract class ArrayBuilder[T]
3030
extends ReusableBuilder[T, Array[T]]
3131
with Serializable {
3232
protected[this] var capacity: Int = 0
33-
protected[this] def elems: Array[T]
33+
protected[this] def elems: Array[T] | Null // may not be allocated at size = capacity = 0
3434
protected var size: Int = 0
3535

3636
def length: Int = size
@@ -58,7 +58,7 @@ sealed abstract class ArrayBuilder[T]
5858
/** Add a slice of an array */
5959
def addAll(xs: Array[_ <: T], offset: Int, length: Int): this.type = {
6060
ensureSize(this.size + length)
61-
Array.copy(xs, offset, elems, this.size, length)
61+
Array.copy(xs, offset, elems.nn, this.size, length)
6262
size += length
6363
this
6464
}
@@ -68,8 +68,8 @@ sealed abstract class ArrayBuilder[T]
6868
if(k > 0) {
6969
ensureSize(this.size + k)
7070
xs match {
71-
case xs: Iterable[T] => xs.copyToArray(elems, this.size)
72-
case _ => xs.iterator.copyToArray(elems, this.size)
71+
case xs: Iterable[T] => xs.copyToArray(elems.nn, this.size)
72+
case _ => xs.iterator.copyToArray(elems.nn, this.size)
7373
}
7474
size += k
7575
} else if(k < 0) super.addAll(xs)
@@ -225,7 +225,7 @@ object ArrayBuilder {
225225
* @tparam T type of elements for the array builder, subtype of `AnyRef` with a `ClassTag` context bound.
226226
*/
227227
@SerialVersionUID(3L)
228-
final class ofRef[T <: AnyRef](implicit ct: ClassTag[T]) extends ArrayBuilder[T] {
228+
final class ofRef[T <: AnyRef | Null](implicit ct: ClassTag[T]) extends ArrayBuilder[T] {
229229

230230
protected var elems: Array[T] = _
231231

@@ -251,7 +251,7 @@ object ArrayBuilder {
251251
if (capacity != 0 && capacity == size) {
252252
capacity = 0
253253
val res = elems
254-
elems = null
254+
elems = null.asInstanceOf[Array[T]]
255255
res
256256
}
257257
else mkArray(size)
@@ -298,7 +298,7 @@ object ArrayBuilder {
298298
if (capacity != 0 && capacity == size) {
299299
capacity = 0
300300
val res = elems
301-
elems = null
301+
elems = null.asInstanceOf[Array[Byte]]
302302
res
303303
}
304304
else mkArray(size)
@@ -340,7 +340,7 @@ object ArrayBuilder {
340340
if (capacity != 0 && capacity == size) {
341341
capacity = 0
342342
val res = elems
343-
elems = null
343+
elems = null.asInstanceOf[Array[Short]]
344344
res
345345
}
346346
else mkArray(size)
@@ -382,7 +382,7 @@ object ArrayBuilder {
382382
if (capacity != 0 && capacity == size) {
383383
capacity = 0
384384
val res = elems
385-
elems = null
385+
elems = null.asInstanceOf[Array[Char]]
386386
res
387387
}
388388
else mkArray(size)
@@ -424,7 +424,7 @@ object ArrayBuilder {
424424
if (capacity != 0 && capacity == size) {
425425
capacity = 0
426426
val res = elems
427-
elems = null
427+
elems = null.asInstanceOf[Array[Int]]
428428
res
429429
}
430430
else mkArray(size)
@@ -466,7 +466,7 @@ object ArrayBuilder {
466466
if (capacity != 0 && capacity == size) {
467467
capacity = 0
468468
val res = elems
469-
elems = null
469+
elems = null.asInstanceOf[Array[Long]]
470470
res
471471
}
472472
else mkArray(size)
@@ -508,7 +508,7 @@ object ArrayBuilder {
508508
if (capacity != 0 && capacity == size) {
509509
capacity = 0
510510
val res = elems
511-
elems = null
511+
elems = null.asInstanceOf[Array[Float]]
512512
res
513513
}
514514
else mkArray(size)
@@ -550,7 +550,7 @@ object ArrayBuilder {
550550
if (capacity != 0 && capacity == size) {
551551
capacity = 0
552552
val res = elems
553-
elems = null
553+
elems = null.asInstanceOf[Array[Double]]
554554
res
555555
}
556556
else mkArray(size)
@@ -592,7 +592,7 @@ object ArrayBuilder {
592592
if (capacity != 0 && capacity == size) {
593593
capacity = 0
594594
val res = elems
595-
elems = null
595+
elems = null.asInstanceOf[Array[Boolean]]
596596
res
597597
}
598598
else mkArray(size)

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

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

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

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
@@ -1846,6 +1846,7 @@ object Build {
18461846
// NOTE: The only difference here is that we drop `-Werror` and semanticDB for now
18471847
Compile / scalacOptions := Seq("-deprecation", "-feature", "-unchecked", "-encoding", "UTF8", "-language:implicitConversions", "-nowarn"),
18481848
Compile / scalacOptions += "-Yno-stdlib-patches",
1849+
Compile / scalacOptions += "-Yexplicit-nulls",
18491850
Compile / scalacOptions += "-scalajs",
18501851
// Packaging configuration of the stdlib
18511852
Compile / packageBin / publishArtifact := true,

0 commit comments

Comments
 (0)