10000 Use linkTimeIf for WebAssembly · scala-wasm/scala-wasm@eeb00ff · GitHub
[go: up one dir, main page]

Skip to content

Commit eeb00ff

Browse files
committed
Use linkTimeIf for WebAssembly
To avoid analyzer reports the `JSInteropInPureWasm` error
1 parent 534ca6a commit eeb00ff

File tree

14 files changed

+305
-192
lines changed

14 files changed

+305
-192
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,16 @@ object Character {
128128
if (!isValidCodePoint(codePoint))
129129
throw new IllegalArgumentException()
130130

131-
if (LinkingInfo.targetPureWasm) {
131+
LinkingInfo.linkTimeIf (LinkingInfo.targetPureWasm) {
132132
if (isBmpCodePoint(codePoint)) {
133133
Character.toString(codePoint.toChar)
134134
} else {
135135
val dst = new Array[Char](2)
136136
toSurrogate(codePoint, dst, 0)
137137
new String(dst)
138138
}
139-
} else if (LinkingInfo.esVersion >= ESVersion.ES2015) {
139+
} {
140+
if (LinkingInfo.esVersion >= ESVersion.ES2015) {
140141
js.Dynamic.global.String.fromCodePoint(codePoint).asInstanceOf[String]
141142
} else {
142143
if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) {
@@ -148,7 +149,7 @@ object Character {
148149
.fromCharCode(highSurrogate(codePoint).toInt, lowSurrogate(codePoint).toInt)
149150
.asInstanceOf[String]
150151
}
151-
}
152+
}}
152153
}
153154

154155
// Low-level code point and code unit manipulations -------------------------
@@ -706,10 +707,10 @@ object Character {
706707
case _ =>
707708
// In WASI implementation, we cannot use String#toUpperCase
708709
// since it uses Character#toUpperCase.
709-
if (LinkingInfo.targetPureWasm) {
710+
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
710711
import CaseUtil._
711712< 57AE code class="diff-text syntax-highlighted-line">
toCase(codePoint, a, z, lowerBeta, lowerRanges, lowerDeltas, lowerSteps)
712-
} else {
713+
} {
713714
val upperChars = toString(codePoint).toUpperCase()
714715
upperChars.length match {
715716
case 1 =>
@@ -735,12 +736,12 @@ object Character {
735736
case 0x0130 =>
736737
0x0069 // İ => i
737738
case _ =>
738-
if (LinkingInfo.targetPureWasm) {
739+
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
739740
// in pure Wasm implementation, we cannot use String#toLowerCase
740741
// since it uses Character$toLowerCase
741742
import CaseUtil._
742743
toCase(codePoint, A, Z, upperMu, upperRanges, upperDeltas, upperSteps)
743-
} else {
744+
} {
744745
val lowerChars = toString(codePoint).toLowerCase()
745746
lowerChars.length match {
746747
case 1 =>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.lang.constant.{Constable, ConstantDesc}
1616

1717
import scala.scalajs.js
1818
import scala.scalajs.LinkingInfo
19+
import scala.scalajs.LinkingInfo.linkTimeIf
1920

2021
import Utils._
2122

@@ -380,10 +381,11 @@ object Double {
380381
* The two implementations compute the same results.
381382
*/
382383
@inline def hashCode(value: scala.Double): Int = {
383-
if (LinkingInfo.isWebAssembly)
384+
linkTimeIf(LinkingInfo.isWebAssembly) {
384385
hashCodeForWasm(value)
385-
else
386+
} {
386387
hashCodeForJS(value)
388+
}
387389
}
388390

389391
@inline
F438

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.util.function._
1717

1818
import scala.scalajs.js
1919
import scala.scalajs.LinkingInfo
20-
import scala.scalajs.LinkingInfo.ESVersion
20+
import scala.scalajs.LinkingInfo.{ESVersion, linkTimeIf}
2121

2222
/* This is a hijacked class. Its instances are primitive numbers.
2323
* Constructors are not emitted.
@@ -103,7 +103,7 @@ object Integer {
103103
if (i >= s.length)
104104
fail()
105105

106-
if (LinkingInfo.targetPureWasm) {
106+
linkTimeIf(LinkingInfo.targetPureWasm) {
107107
val maxAbsValue: scala.Long = {
108108
if (!signed) 0xffffffffL
109109
else if (negative) 0x80000000L
@@ -123,7 +123,7 @@ object Integer {
123123
-result.toInt
124124
else
125125
result.toInt
126-
} else {
126+
} {
127127
val maxAbsValue: scala.Double = {
128128
if (!signed) 0xffffffffL.toDouble
129129
else if (negative) 0x80000000L.toDouble
@@ -298,29 +298,42 @@ object Integer {
298298

299299
// Intrinsic, fallback on actual code for non-literal in JS
300300
@inline def numberOfLeadingZeros(i: scala.Int): scala.Int = {
301-
if (LinkingInfo.esVersion >= ESVersion.ES2015) js.Math.clz32(i)
302-
else clz32Dynamic(i)
301+
linkTimeIf(LinkingInfo.targetPureWasm) {
302+
clz32Dynamic(i)
303+
} {
304+
if (LinkingInfo.esVersion >= ESVersion.ES2015) js.Math.clz32(i)
305+
else clz32Dynamic(i)
306+
}
307+
303308
}
304309

305310
private def clz32Dynamic(i: scala.Int) = {
306-
if (js.typeOf(js.Dynamic.global.Math.clz32) == "function") {
307-
js.Math.clz32(i)
308-
} else {
309-
// See Hacker's Delight, Section 5-3
310-
var x = i
311-
if (x == 0) {
312-
32
311+
linkTimeIf(LinkingInfo.targetPureWasm) {
312+
clz32Dynamic0(i)
313+
} {
314+
if (js.typeOf(js.Dynamic.global.Math.clz32) == "function") {
315+
js.Math.clz32(i)
313316
} else {
314-
var r = 1
315-
if ((x & 0xffff0000) == 0) { x <<= 16; r += 16 }
316-
if ((x & 0xff000000) == 0) { x <<= 8; r += 8 }
317-
if ((x & 0xf0000000) == 0) { x <<= 4; r += 4 }
318-
if ((x & 0xc0000000) == 0) { x <<= 2; r += 2 }
319-
r + (x >> 31)
317+
clz32Dynamic0(i)
320318
}
321319
}
322320
}
323321

322+
@inline private def clz32Dynamic0(i: scala.Int) = {
323+
// See Hacker's Delight, Section 5-3
324+
var x = i
325+
if (x == 0) {
326+
32
327+
} else {
328+
var r = 1
329+
if ((x & 0xffff0000) == 0) { x <<= 16; r += 16 }
330+
if ((x & 0xff000000) == 0) { x <<= 8; r += 8 }
331+
if ((x & 0xf0000000) == 0) { x <<= 4; r += 4 }
332+
if ((x & 0xc0000000) == 0) { x <<= 2; r += 2 }
333+
r + (x >> 31)
334+
}
335+
}
336+
324337
// Wasm intrinsic
325338
@inline def numberOfTrailingZeros(i: scala.Int): scala.Int =
326339
if (i == 0) 32

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import scala.scalajs.js
1717
import js.Dynamic.{ global => g }
1818

1919
import scala.scalajs.LinkingInfo
20-
import scala.scalajs.LinkingInfo.ESVersion
20+
import scala.scalajs.LinkingInfo.{ESVersion, linkTimeIf}
2121

2222
object Math {
2323
final val E = 2.718281828459045
@@ -30,8 +30,19 @@ object Math {
3030
@inline def abs(a: scala.Long): scala.Long = if (a < 0) -a else a
3131

3232
// Wasm intrinsics
33-
@inline def abs(a: scala.Float): scala.Float = js.Math.abs(a).toFloat
34-
@inline def abs(a: scala.Double): scala.Double = js.Math.abs(a)
33+
@inline def abs(a: scala.Float): scala.Float =
34+
linkTimeIf(LinkingInfo.targetPureWasm) {
35+
Float.intBitsToFloat(Float.floatToIntBits(a) & ~Int.MinValue)
36+
} {
37+
js.Math.abs(a).toFloat
38+
}
39+
40+
@inline def abs(a: scala.Double): scala.Double =
41+
linkTimeIf(LinkingInfo.targetPureWasm) {
42+
Double.longBitsToDouble(Double.doubleToLongBits(a) & ~scala.Long.MinValue)
43+
} {
44+
js.Math.abs(a)
45+
}
3546

3647
@inline def max(a: scala.Int, b: scala.Int): scala.Int = if (a > b) a else b
3748
@inline def max(a: scala.Long, b: scala.Long): scala.Long = if (a > b) a else b
@@ -154,8 +165,11 @@ object Math {
154165
@inline def atan2(y: scala.Double, x: scala.Double): scala.Double = js.Math.atan2(y, x)
155166

156167
@inline def random(): scala.Double =
157-
if (LinkingInfo.targetPureWasm) WasmSystem.random()
158-
else js.Math.random()
168+
linkTimeIf(LinkingInfo.targetPureWasm) {
169+
WasmSystem.random()
170+
} {
171+
js.Math.random()
172+
}
159173

160174
@inline def toDegrees(a: scala.Double): scala.Double = a * 180.0 / PI
161175
@inline def toRadians(a: scala.Double): scala.Double = a / 180.0 * PI

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ object System {
6868

6969
@inline
7070
def currentTimeMillis(): scala.Long =
71-
if (LinkingInfo.targetPureWasm) WasmSystem.currentTimeMillis()
72-
else js.Date.now().toLong
71+
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
72+
WasmSystem.currentTimeMillis()
73+
} {
74+
js.Date.now().toLong
75+
}
7376

7477
private object NanoTime {
7578
val getHighPrecisionTime: js.Function0[scala.Double] = {
@@ -91,8 +94,11 @@ object System {
9194

9295
@inline
9396
def nanoTime(): scala.Long =
94-
if (LinkingInfo.targetPureWasm) WasmSystem.nanoTime()
95-
else (NanoTime.getHighPrecisionTime() * 1000000).toLong
97+
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
98+
WasmSystem.nanoTime()
99+
} {
100+
(NanoTime.getHighPrecisionTime() * 1000000).toLong
101+
}
96102

97103
// arraycopy ----------------------------------------------------------------
98104

@@ -385,9 +391,9 @@ private final class JSConsoleBasedPrintStream(isErr: scala.Boolean)
385391
override def close(): Unit = ()
386392

387393
private def doWriteLine(line: String): Unit = {
388-
if (LinkingInfo.targetPureWasm) {
394+
LinkingInfo.linkTimeIf (LinkingInfo.targetPureWasm) {
389395
WasmSystem.print(line)
390-
} else {
396+
} {
391397
import js.DynamicImplicits.truthValue
392398

393399
if (js.typeOf(global.console) != "undefined") {

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Throwable protected (s: String, private var e: Throwable,
3535
*/
3636
private[this] var suppressed: Array[Throwable] = _
3737

38-
if (writableStackTrace && !LinkingInfo.targetPureWasm)
38+
if (writableStackTrace)
3939
fillInStackTrace()
4040

4141
def initCause(cause: Throwable): Throwable = {
@@ -48,30 +48,41 @@ class Throwable protected (s: String, private var e: Throwable,
4848
def getLocalizedMessage(): String = getMessage()
4949

5050
def fillInStackTrace(): Throwable = {
51-
if (!LinkingInfo.targetPureWasm) jsErrorForStackTrace = StackTrace.captureJSError(this)
52-
this
51+
LinkingInfo.linkTimeIf(!LinkingInfo.targetPureWasm) {
52+
jsErrorForStackTrace = StackTrace.captureJSError(this)
53+
this
54+
} {
55+
this
56+
}
5357
}
5458

5559
def getStackTrace(): Array[StackTraceElement] = {
5660
if (stackTrace eq null) {
57-
if (!LinkingInfo.targetPureWasm && writableStackTrace)
58-
stackTrace = StackTrace.extract(jsErrorForStackTrace)
59-
else
61+
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
6062
stackTrace = new Array[StackTraceElement](0)
63+
} {
64+
if (writableStackTrace)
65+
stackTrace = StackTrace.extract(jsErrorForStackTrace)
66+
else
67+
stackTrace = new Array[StackTraceElement](0)
68+
}
6169
}
6270
stackTrace
6371
}
6472

6573
def setStackTrace(stackTrace: Array[StackTraceElement]): Unit = {
66-
if (writableStackTrace && !LinkingInfo.targetPureWasm) {
67-
var i = 0
68-
while (i < stackTrace.length) {
69-
if (stackTrace(i) eq null)
70-
throw new NullPointerException()
71-
i += 1
72-
}
74+
LinkingInfo.linkTimeIf(!LinkingInfo.targetPureWasm) {
75+
if (writableStackTrace) {
76+
var i = 0
77+
while (i < stackTrace.length) {
78+
if (stackTrace(i) eq null)
79+
throw new NullPointerException()
80+
i += 1
81+
}
7382

74-
this.stackTrace = stackTrace.clone()
83+
this.stackTrace = stackTrace.clone()
84+
}
85+
} {
7586
}
7687
}
7788

0 commit comments

Comments
 (0)
0