8000 Report JS tree in targetPureWasm=true in Analyzer by tanishiking · Pull Request #24 · scala-wasm/scala-wasm · GitHub
[go: up one dir, main page]

Skip to content

Report JS tree in targetPureWasm=true in Analyzer #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions javalib/src/main/scala/java/lang/Character.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ object Character {
if (!isValidCodePoint(codePoint))
throw new IllegalArgumentException()

if (LinkingInfo.targetPureWasm) {
LinkingInfo.linkTimeIf (LinkingInfo.targetPureWasm) {
if (isBmpCodePoint(codePoint)) {
Character.toString(codePoint.toChar)
} else {
val dst = new Array[Char](2)
toSurrogate(codePoint, dst, 0)
new String(dst)
}
} else if (LinkingInfo.esVersion >= ESVersion.ES2015) {
} {
if (LinkingInfo.esVersion >= ESVersion.ES2015) {
js.Dynamic.global.String.fromCodePoint(codePoint).asInstanceOf[String]
} else {
if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) {
Expand All @@ -148,7 +149,7 @@ object Character {
.fromCharCode(highSurrogate(codePoint).toInt, lowSurrogate(codePoint).toInt)
.asInstanceOf[String]
}
}
}}
}

// Low-level code point and code unit manipulations -------------------------
Expand Down Expand Up @@ -706,10 +707,10 @@ object Character {
case _ =>
// In WASI implementation, we cannot use String#toUpperCase
// since it uses Character#toUpperCase.
if (LinkingInfo.targetPureWasm) {
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
import CaseUtil._
toCase(codePoint, a, z, lowerBeta, lowerRanges, lowerDeltas, lowerSteps)
} else {
} {
val upperChars = toString(codePoint).toUpperCase()
upperChars.length match {
case 1 =>
Expand All @@ -735,12 +736,12 @@ object Character {
case 0x0130 =>
0x0069 // İ => i
case _ =>
if (LinkingInfo.targetPureWasm) {
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
// in pure Wasm implementation, we cannot use String#toLowerCase
// since it uses Character$toLowerCase
import CaseUtil._
toCase(codePoint, A, Z, upperMu, upperRanges, upperDeltas, upperSteps)
} else {
} {
val lowerChars = toString(codePoint).toLowerCase()
lowerChars.length match {
case 1 =>
Expand Down
6 changes: 4 additions & 2 deletions javalib/src/main/scala/java/lang/Double.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.lang.constant.{Constable, ConstantDesc}

import scala.scalajs.js
import scala.scalajs.LinkingInfo
import scala.scalajs.LinkingInfo.linkTimeIf

import Utils._

Expand Down Expand Up @@ -380,10 +381,11 @@ object Double {
* The two implementations compute the same results.
*/
@inline def hashCode(value: scala.Double): Int = {
if (LinkingInfo.isWebAssembly)
linkTimeIf(LinkingInfo.isWebAssembly) {
hashCodeForWasm(value)
else
} {
hashCodeForJS(value)
}
}

@inline
Expand Down
49 changes: 31 additions & 18 deletions javalib/src/main/scala/java/lang/Integer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.function._

import scala.scalajs.js
import scala.scalajs.LinkingInfo
import scala.scalajs.LinkingInfo.ESVersion
import scala.scalajs.LinkingInfo.{ESVersion, linkTimeIf}

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

if (LinkingInfo.targetPureWasm) {
linkTimeIf(LinkingInfo.targetPureWasm) {
val maxAbsValue: scala.Long = {
if (!signed) 0xffffffffL
else if (negative) 0x80000000L
Expand All @@ -123,7 +123,7 @@ object Integer {
-result.toInt
else
result.toInt
} else {
} {
val maxAbsValue: scala.Double = {
if (!signed) 0xffffffffL.toDouble
else if (negative) 0x80000000L.toDouble
Expand Down Expand Up @@ -298,29 +298,42 @@ object Integer {

// Intrinsic, fallback on actual code for non-literal in JS
@inline def numberOfLeadingZeros(i: scala.Int): scala.Int = {
if (LinkingInfo.esVersion >= ESVersion.ES2015) js.Math.clz32(i)
else clz32Dynamic(i)
linkTimeIf(LinkingInfo.targetPureWasm) {
clz32Dynamic(i)
} {
if (LinkingInfo.esVersion >= ESVersion.ES2015) js.Math.clz32(i)
else clz32Dynamic(i)
}

}

private def clz32Dynamic(i: scala.Int) = {
if (js.typeOf(js.Dynamic.global.Math.clz32) == "function") {
js.Math.clz32(i)
} else {
// See Hacker's Delight, Section 5-3
var x = i
if (x == 0) {
32
linkTimeIf(LinkingInfo.targetPureWasm) {
clz32Dynamic0(i)
} {
if (js.typeOf(js.Dynamic.global.Math.clz32) == "function") {
js.Math.clz32(i)
} else {
var r = 1
if ((x & 0xffff0000) == 0) { x <<= 16; r += 16 }
if ((x & 0xff000000) == 0) { x <<= 8; r += 8 }
if ((x & 0xf0000000) == 0) { x <<= 4; r += 4 }
if ((x & 0xc0000000) == 0) { x <<= 2; r += 2 }
r + (x >> 31)
clz32Dynamic0(i)
}
}
}

@inline private def clz32Dynamic0(i: scala.Int) = {
// See Hacker's Delight, Section 5-3
var x = i
if (x == 0) {
32
} else {
var r = 1
if ((x & 0xffff0000) == 0) { x <<= 16; r += 16 }
if ((x & 0xff000000) == 0) { x <<= 8; r += 8 }
if ((x & 0xf0000000) == 0) { x <<= 4; r += 4 }
if ((x & 0xc0000000) == 0) { x <<= 2; r += 2 }
r + (x >> 31)
}
}

// Wasm intrinsic
@inline def numberOfTrailingZeros(i: scala.Int): scala.Int =
if (i == 0) 32
Expand Down
24 changes: 19 additions & 5 deletions javalib/src/main/scala/java/lang/Math.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.scalajs.js
import js.Dynamic.{ global => g }

import scala.scalajs.LinkingInfo
import scala.scalajs.LinkingInfo.ESVersion
import scala.scalajs.LinkingInfo.{ESVersion, linkTimeIf}

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

// Wasm intrinsics
@inline def abs(a: scala.Float): scala.Float = js.Math.abs(a).toFloat
@inline def abs(a: scala.Double): scala.Double = js.Math.abs(a)
@inline def abs(a: scala.Float): scala.Float =
linkTimeIf(LinkingInfo.targetPureWasm) {
Float.intBitsToFloat(Float.floatToIntBits(a) & ~Int.MinValue)
} {
js.Math.abs(a).toFloat
}

@inline def abs(a: scala.Double): scala.Double =
linkTimeIf(LinkingInfo.targetPureWasm) {
Double.longBitsToDouble(Double.doubleToLongBits(a) & ~scala.Long.MinValue)
} {
js.Math.abs(a)
}

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

@inline def random(): scala.Double =
if (LinkingInfo.targetPureWasm) WasmSystem.random()
else js.Math.random()
linkTimeIf(LinkingInfo.targetPureWasm) {
WasmSystem.random()
} {
js.Math.random()
}

@inline def toDegrees(a: scala.Double): scala.Double = a * 180.0 / PI
@inline def toRadians(a: scala.Double): scala.Double = a / 180.0 * PI
Expand Down
18 changes: 12 additions & 6 deletions javalib/src/main/scala/java/lang/System.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ object System {

@inline
def currentTimeMillis(): scala.Long =
if (LinkingInfo.targetPureWasm) WasmSystem.currentTimeMillis()
else js.Date.now().toLong
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
WasmSystem.currentTimeMillis()
} {
js.Date.now().toLong
}

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

@inline
def nanoTime(): scala.Long =
if (LinkingInfo.targetPureWasm) WasmSystem.nanoTime()
else (NanoTime.getHighPrecisionTime() * 1000000).toLong
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
WasmSystem.nanoTime()
} {
(NanoTime.getHighPrecisionTime() * 1000000).toLong
}

// arraycopy ----------------------------------------------------------------

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

private def doWriteLine(line: String): Unit = {
if (LinkingInfo.targetPureWasm) {
LinkingInfo.linkTimeIf (LinkingInfo.targetPureWasm) {
WasmSystem.print(line)
} else {
} {
import js.DynamicImplicits.truthValue

if (js.typeOf(global.console) != "undefined") {
Expand Down
39 changes: 25 additions & 14 deletions javalib/src/main/scala/java/lang/Throwables.scala
< EBB7 th scope="col">Diff line change
Original file line number Diff line number
Expand Up @@ -35,7 +35,7 @@ class Throwable protected (s: String, private var e: Throwable,
*/
private[this] var suppressed: Array[Throwable] = _

if (writableStackTrace && !LinkingInfo.targetPureWasm)
if (writableStackTrace)
fillInStackTrace()

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

def fillInStackTrace(): Throwable = {
if (!LinkingInfo.targetPureWasm) jsErrorForStackTrace = StackTrace.captureJSError(this)
this
LinkingInfo.linkTimeIf(!LinkingInfo.targetPureWasm) {
jsErrorForStackTrace = StackTrace.captureJSError(this)
this
} {
this
}
}

def getStackTrace(): Array[StackTraceElement] = {
if (stackTrace eq null) {
if (!LinkingInfo.targetPureWasm && writableStackTrace)
stackTrace = StackTrace.extract(jsErrorForStackTrace)
else
LinkingInfo.linkTimeIf(LinkingInfo.targetPureWasm) {
stackTrace = new Array[StackTraceElement](0)
} {
if (writableStackTrace)
stackTrace = StackTrace.extract(jsErrorForStackTrace)
else
stackTrace = new Array[StackTraceElement](0)
}
}
stackTrace
}

def setStackTrace(stackTrace: Array[StackTraceElement]): Unit = {
if (writableStackTrace && !LinkingInfo.targetPureWasm) {
var i = 0
while (i < stackTrace.length) {
if (stackTrace(i) eq null)
throw new NullPointerException()
i += 1
}
LinkingInfo.linkTimeIf(!LinkingInfo.targetPureWasm) {
if (writableStackTrace) {
var i = 0
while (i < stackTrace.length) {
if (stackTrace(i) eq null)
throw new NullPointerException()
i += 1
}

this.stackTrace = stackTrace.clone()
this.stackTrace = stackTrace.clone()
}
} {
}
}

Expand Down
Loading
0