8000 Use explicit 0-arg functions instead of by-name params in javalib. · scala-js/scala-js@5e9956b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e9956b

Browse files
committed
Use explicit 0-arg functions instead of by-name params in javalib.
1 parent 0e9dfa6 commit 5e9956b

19 files changed

+78
-81
lines changed

javalib/src/main/scala/java/io/PrintStream.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ class PrintStream private (_out: OutputStream, autoFlush: Boolean,
7979
private var errorFlag: Boolean = false
8 62FB 080

8181
override def flush(): Unit =
82-
ensureOpenAndTrapIOExceptions(out.flush())
82+
ensureOpenAndTrapIOExceptions(() => out.flush())
8383

84-
override def close(): Unit = trapIOExceptions {
84+
override def close(): Unit = trapIOExceptions { () =>
8585
if (!closing) {
8686
closing = true
8787
encoder.close()
@@ -133,15 +133,15 @@ class PrintStream private (_out: OutputStream, autoFlush: Boolean,
133133
*/
134134

135135
override def write(b: Int): Unit = {
136-
ensureOpenAndTrapIOExceptions {
136+
ensureOpenAndTrapIOExceptions { () =>
137137
out.write(b)
138138
if (autoFlush && b == '\n')
139139
flush()
140140
}
141141
}
142142

143143
override def write(buf: Array[Byte], off: Int, len: Int): Unit = {
144-
ensureOpenAndTrapIOExceptions {
144+
ensureOpenAndTrapIOExceptions { () =>
145145
out.write(buf, off, len)
146146
if (autoFlush)
147147
flush()
@@ -157,17 +157,17 @@ class PrintStream private (_out: OutputStream, autoFlush: Boolean,
157157
def print(s: String): Unit = printString(if (s == null) "null" else s)
158158
def print(obj: AnyRef): Unit = printString(String.valueOf(obj))
159159

160-
private def printString(s: String): Unit = ensureOpenAndTrapIOExceptions {
160+
private def printString(s: String): Unit = ensureOpenAndTrapIOExceptions { () =>
161161
encoder.write(s)
162162
encoder.flushBuffer()
163163
}
164164

165-
def print(s: Array[Char]): Unit = ensureOpenAndTrapIOExceptions {
165+
def print(s: Array[Char]): Unit = ensureOpenAndTrapIOExceptions { () =>
166166
encoder.write(s)
167167
encoder.flushBuffer()
168168
}
169169

170-
def println(): Unit = ensureOpenAndTrapIOExceptions {
170+
def println(): Unit = ensureOpenAndTrapIOExceptions { () =>
171171
encoder.write('\n') // In Scala.js the line separator is always LF
172172
encoder.flushBuffer()
173173
if (autoFlush)
@@ -214,15 +214,15 @@ class PrintStream private (_out: OutputStream, autoFlush: Boolean,
214214
this
215215
}
216216

217-
@inline private[this] def trapIOExceptions(body: => Unit): Unit = {
217+
@inline private[this] def trapIOExceptions(body: () => Unit): Unit = {
218218
try {
219-
body
219+
body()
220220
} catch {
221221
case _: IOException => setError()
222222
}
223223
}
224224

225-
@inline private[this] def ensureOpenAndTrapIOExceptions(body: => Unit): Unit = {
225+
@inline private[this] def ensureOpenAndTrapIOExceptions(body: () => Unit): Unit = {
226226
if (closed) setError()
227227
else trapIOExceptions(body)
228228
}

javalib/src/main/scala/java/io/PrintWriter.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class PrintWriter(protected[io] var out: Writer,
4141
private var errorFlag: Boolean = false
4242

4343
def flush(): Unit =
44-
ensureOpenAndTrapIOExceptions(out.flush())
44+
ensureOpenAndTrapIOExceptions(() => out.flush())
4545

46-
def close(): Unit = trapIOExceptions {
46+
def close(): Unit = trapIOExceptions { () =>
4747
if (!closed) {
4848
flush()
4949
closed = true
@@ -76,19 +76,19 @@ class PrintWriter(protected[io] var out: Writer,
7676
protected[io] def clearError(): Unit = errorFlag = false
7777

7878
override def write(c: Int): Unit =
79-
ensureOpenAndTrapIOExceptions(out.write(c))
79+
ensureOpenAndTrapIOExceptions(() => out.write(c))
8080

8181
override def write(buf: Array[Char], off: Int, len: Int): Unit =
82-
ensureOpenAndTrapIOExceptions(out.write(buf, off, len))
82+
ensureOpenAndTrapIOExceptions(() => out.write(buf, off, len))
8383

8484
override def write(buf: Array[Char]): Unit =
85-
ensureOpenAndTrapIOExceptions(out.write(buf))
85+
ensureOpenAndTrapIOExceptions(() => out.write(buf))
8686

8787
override def write(s: String, off: Int, len: Int): Unit =
88-
ensureOpenAndTrapIOExceptions(out.write(s, off, len))
88+
ensureOpenAndTrapIOExceptions(() => out.write(s, off, len))
8989

9090
override def write(s: String): Unit =
91-
ensureOpenAndTrapIOExceptions(out.write(s))
91+
ensureOpenAndTrapIOExceptions(() => out.write(s))
9292

9393
def print(b: Boolean): Unit = write(String.valueOf(b))
9494
def print(c: Char): Unit = write(c)
@@ -147,15 +147,15 @@ class PrintWriter(protected[io] var out: Writer,
147147
this
148148
}
149149

150-
@inline private[this] def trapIOExceptions(body: => Unit): Unit = {
150+
@inline private[this] def trapIOExceptions(body: () => Unit): Unit = {
151151
try {
152-
body
152+
body()
153153
} catch {
154154
case _: IOException => setError()
155155
}
156156
}
157157

158-
@inline private[this] def ensureOpenAndTrapIOExceptions(body: => Unit): Unit = {
158+
@inline private[this] def ensureOpenAndTrapIOExceptions(body: () => Unit): Unit = {
159159
if (closed) setError()
160160
else trapIOExceptions(body)
161161
}

javalib/src/main/scala/java/lang/ClassValue.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract class ClassValue[T] protected () {
5050

5151
def get(`type`: Class[_]): T = {
5252
if (useJSMap) {
53-
mapGetOrElseUpdate(jsMap, `type`)(computeValue(`type`))
53+
mapGetOrElseUpdate(jsMap, `type`)(() => computeValue(`type`))
5454
} else {
5555
/* We first perform `get`, and if the result is null, we use
5656
* `containsKey` to disambiguate a present null from an absent key.

javalib/src/main/scala/java/lang/Float.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ object Float {
122122
} else if (undefOrIsDefined(groups(4))) {
123123
// Decimal notation
124124
val fullNumberStr = undefOrForceGet(groups(4))
125-
val integralPartStr = undefOrGetOrElse(groups(5))("")
126-
val fractionalPartStr = undefOrGetOrElse(groups(6))("") + undefOrGetOrElse(groups(7))("")
127-
val exponentStr = undefOrGetOrElse(groups(8))("0")
125+
val integralPartStr = undefOrGetOrElse(groups(5))(() => "")
126+
val fractionalPartStr = undefOrGetOrElse(groups(6))(() => "") + undefOrGetOrElse(groups(7))(() => "")
127+
val exponentStr = undefOrGetOrElse(groups(8))(() => "0")
128128
parseFloatDecimal(fullNumberStr, integralPartStr, fractionalPartStr, exponentStr)
129129
} else {
130130
// Hexadecimal notation
131-
val integralPartStr = undefOrGetOrElse(groups(10))("")
132-
val fractionalPartStr = undefOrGetOrElse(groups(11))("") + undefOrGetOrElse(groups(12))("")
131+
val integralPartStr = undefOrGetOrElse(groups(10))(() => "")
132+
val fractionalPartStr = undefOrGetOrElse(groups(11))(() => "") + undefOrGetOrElse(groups(12))(() => "")
133133
val binaryExpStr = undefOrForceGet(groups(13))
134134
parseFloatHexadecimal(integralPartStr, fractionalPartStr, binaryExpStr)
135135
}

javalib/src/main/scala/java/lang/StackTrace.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private[lang] object StackTrace {
126126
trace.push(new StackTraceElement(classAndMethodName(0),
127127
classAndMethodName(1), undefOrForceGet(mtch(2)),
128128
parseInt(undefOrForceGet(mtch(3))),
129-
undefOrFold(mtch(4))(-1)(parseInt(_))))
129+
undefOrFold(mtch(4))(() => -1)(parseInt(_))))
130130
} else {
131131
// just in case
132132 // (explicitly use the constructor with column number so that STE has an inlineable init)
@@ -413,7 +413,7 @@ private[lang] object StackTrace {
413413
while (i < len) {
414414
val mtch = lineRE.exec(lines(i))
415415
if (mtch ne null) {
416-
val fnName = undefOrGetOrElse(mtch(3))("{anonymous}")
416+
val fnName = undefOrGetOrElse(mtch(3))(() => "{anonymous}")
417417
result.push(
418418
fnName + "()@" + undefOrForceGet(mtch(2)) + ":" +
419419
undefOrForceGet(mtch(1))
@@ -438,7 +438,7 @@ private[lang] object StackTrace {
438438
while (i < len) {
439439
val mtch = lineRE.exec(lines(i))
440440
if (mtch ne null) {
441-
val fnName = undefOrFold(mtch(1))("global code")(_ + "()")
441+
val fnName = undefOrFold(mtch(1))(() => "global code")(_ + "()")
442442
result.push(fnName + "@" + undefOrForceGet(mtch(2)) + ":" + undefOrForceGet(mtch(3)))
443443
}
444444
i += 1
@@ -458,7 +458,7 @@ private[lang] object StackTrace {
458458
val mtch = lineRE.exec(lines(i))
459459
if (mtch ne null) {
460460
val location = undefOrForceGet(mtch(4)) + ":" + undefOrForceGet(mtch(1)) + ":" + undefOrForceGet(mtch(2))
461-
val fnName0 = undefOrGetOrElse(mtch(2))("global code")
461+
val fnName0 = undefOrGetOrElse(mtch(2))(() => "global code")
462462
val fnName = fnName0
463463
.jsReplace("""<anonymous function: (\S+)>""".re, "$1")
464464
.jsReplace("""<anonymous function>""".re, "{anonymous}")

javalib/src/main/scala/java/lang/System.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ object System {
233233
}
234234

235235
def getProperty(key: String): String =
236-
if (dict ne null) dictGetOrElse(dict, key)(null)
236+
if (dict ne null) dictGetOrElse(dict, key)(() => null)
237237
else properties.getProperty(key)
238238

239239
def getProperty(key: String, default: String): String =
240-
if (dict ne null) dictGetOrElse(dict, key)(default)
240+
if (dict ne null) dictGetOrElse(dict, key)(() => default)
241241
else properties.getProperty(key, default)
242242

243243
def clearProperty(key: String): String =

javalib/src/main/scala/java/lang/Utils.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ private[java] object Utils {
3434
x.asInstanceOf[A]
3535

3636
@inline
37-
def undefOrGetOrElse[A](x: js.UndefOr[A])(default: => A): A =
37+
def undefOrGetOrElse[A](x: js.UndefOr[A])(default: () => A): A =
3838
if (undefOrIsDefined(x)) undefOrForceGet(x)
39-
else default
39+
else default()
4040

4141
@inline
4242
def undefOrGetOrNull[A >: Null](x: js.UndefOr[A]): A =
@@ -50,9 +50,9 @@ private[java] object Utils {
5050
}
5151

5252
@inline
53-
def undefOrFold[A, B](x: js.UndefOr[A])(default: => B)(f: A => B): B =
53+
def undefOrFold[A, B](x: js.UndefOr[A])(default: () => B)(f: A => B): B =
5454
if (undefOrIsDefined(x)) f(undefOrForceGet(x))
55-
else default
55+
else default()
5656

5757
private object Cache {
5858
val safeHasOwnProperty =
@@ -84,11 +84,11 @@ private[java] object Utils {
8484

8585
@inline
8686
def dictGetOrElse[A](dict: js.Dictionary[_ <: A], key: String)(
87-
default: => A): A = {
87+
default: () => A): A = {
8888
if (dictContains(dict, key))
8989
dictRawApply(dict, key)
9090
else
91-
default
91+
default()
9292
}
9393

9494
def dictGetOrElseAndRemove[A](dict: js.Dictionary[_ <: A], key: String,
@@ -139,16 +139,16 @@ private[java] object Utils {
139139
map.asInstanceOf[MapRaw[K, V]].set(key, value)
140140

141141
@inline
142-
def mapGetOrElse[K, V](map: js.Map[K, V], key: K)(default: => V): V = {
142+
def mapGetOrElse[K, V](map: js.Map[K, V], key: K)(default: () => V): V = {
143143
val value = map.asInstanceOf[MapRaw[K, V]].getOrUndefined(key)
144144
if (!isUndefined(value) || mapHas(map, key)) value.asInstanceOf[V]
145-
else default
145+
else default()
146146
}
147147

148148
@inline
149-
def mapGetOrElseUpdate[K, V](map: js.Map[K, V], key: K)(default: => V): V = {
150-
mapGetOrElse(map, key) {
151-
val value = default
149+
def mapGetOrElseUpdate[K, V](map: js.Map[K, V], key: K)(default: () => V): V = {
150+
mapGetOrElse(map, key) { () =>
151+
val value = default()
152152
mapSet(map, key, value)
153153
value
154154
}

javalib/src/main/scala/java/math/BigInteger.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ object BigInteger {
124124
reference
125125
}
126126

127-
@inline
128-
private def checkCriticalArgument(expression: Boolean, errorMessage: => String): Unit = {
129-
if (!expression)
130-
throw new IllegalArgumentException(errorMessage)
131-
}
132-
133127
private[math] def checkRangeBasedOnIntArrayLength(byteLength: Int): Unit = {
134128
if (byteLength < 0 || byteLength >= ((Int.MaxValue + 1) >>> 5))
135129
throw new ArithmeticException("BigInteger would overflow supported range")
@@ -221,7 +215,10 @@ class BigInteger extends Number with Comparable[BigInteger] {
221215

222216
def this(numBits: Int, rnd: Random) = {
223217
this()
224-
checkCriticalArgument(numBits >= 0, "numBits must be non-negative")
218+
219+
if (numBits < 0)
220+
throw new IllegalArgumentException("numBits must be non-negative")
221+
225222
if (numBits == 0) {
226223
sign = 0
227224
numberLength = 1

javalib/src/main/scala/java/nio/charset/Charset.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object Charset {
8080
UTF_8
8181

8282
def forName(charsetName: String): Charset = {
83-
dictGetOrElse(CharsetMap, charsetName.toLowerCase()) {
83+
dictGetOrElse(CharsetMap, charsetName.toLowerCase()) { () =>
8484
throw new UnsupportedCharsetException(charsetName)
8585
}
8686
}

javalib/src/main/scala/java/nio/charset/CoderResult.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ object CoderResult {
7878
}
7979

8080
private def malformedForLengthImpl(length: Int): CoderResult = {
81-
undefOrFold(uniqueMalformed(length)) {
81+
undefOrFold(uniqueMalformed(length)) { () =>
8282
val result = new CoderResult(Malformed, length)
8383
uniqueMalformed(length) = result
8484
result
@@ -96,7 +96,7 @@ object CoderResult {
9696
}
9797

9898
private def unmappableForLengthImpl(length: Int): CoderResult = {
99-
undefOrFold(uniqueUnmappable(length)) {
99+
undefOrFold(uniqueUnmappable(length)) { () =>
100100
val result = new CoderResult(Unmappable, length)
101101
uniqueUnmappable(length) = result
102102
result

0 commit comments

Comments
 (0)
0