Skip to content
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

Replace all runtime platform checks with linktime conditions #3335

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions javalib/src/main/scala/java/io/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,10 @@ object File {

// found an absolute path. continue from there.
case link if link(0) == separatorChar =>
if (Platform.isWindows() && strncmp(link, c"\\\\?\\", 4.toUInt) == 0)
path
if (isWindows)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be possible to remove the include of Platform.
I was moving quickly, but I did not see any other uses of it in the this new file.

if (strncmp(link, c"\\\\?\\", 4.toUInt) == 0)
path
else resolveLink(link, resolveAbsolute, restart = resolveAbsolute)
else
resolveLink(link, resolveAbsolute, restart = resolveAbsolute)

Expand Down Expand Up @@ -936,11 +938,11 @@ object File {
}
}

val pathSeparatorChar: Char = if (Platform.isWindows()) ';' else ':'
val pathSeparatorChar: Char = if (isWindows) ';' else ':'
val pathSeparator: String = pathSeparatorChar.toString
val separatorChar: Char = if (Platform.isWindows()) '\\' else '/'
val separatorChar: Char = if (isWindows) '\\' else '/'
val separator: String = separatorChar.toString
private val caseSensitive: Boolean = !Platform.isWindows()
private val caseSensitive: Boolean = !isWindows

def listRoots(): Array[File] = {
val list = new java.util.ArrayList[File]()
Expand Down
2 changes: 1 addition & 1 deletion javalib/src/main/scala/java/lang/System.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object System {
new PrintStream(new FileOutputStream(FileDescriptor.err))

def lineSeparator(): String = {
if (Platform.isWindows()) "\r\n"
if (isWindows) "\r\n"
else "\n"
}

Expand Down
2 changes: 1 addition & 1 deletion javalib/src/main/scala/java/lang/impl/PosixThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private[java] class PosixThread(val thread: Thread, stackSize: Long)
val status = pthread_cond_wait(condition(conditionIdx), lock)
assert(
status == 0 ||
(scalanative.runtime.Platform.isMac() && status == ETIMEDOUT),
(isMac && status == ETIMEDOUT),
"park, wait"
)
} else {
Expand Down
36 changes: 18 additions & 18 deletions javalib/src/main/scala/java/net/NetworkInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import scala.scalanative.posix.string._
import scala.scalanative.posix.unistd

import scala.scalanative.meta.LinktimeInfo
import scala.scalanative.runtime.Platform

import macOsIf._
import macOsIfDl._
Expand Down Expand Up @@ -59,37 +58,37 @@ class NetworkInterface private (ifName: String) {
def getDisplayName(): String = getName()

def getHardwareAddress(): Array[Byte] = {
if (Platform.isWindows()) new Array[Byte](0) // No Windows support
if (LinktimeInfo.isWindows) new Array[Byte](0) // No Windows support
else {
NetworkInterface.unixImplGetHardwareAddress(ifName)
}
}

def getIndex(): Int = {
if (Platform.isWindows()) 0 // No Windows support
if (LinktimeInfo.isWindows) 0 // No Windows support
else {
NetworkInterface.unixImplGetIndex(ifName)
}
}

def getInetAddresses(): ju.Enumeration[InetAddress] = {
if (Platform.isWindows()) { // No Windows support
if (LinktimeInfo.isWindows) { // No Windows support
ju.Collections.enumeration[InetAddress](new ju.ArrayList[InetAddress])
} else {
NetworkInterface.unixImplGetInetAddresses(ifName)
}
}

def getInterfaceAddresses(): ju.List[InterfaceAddress] = {
if (Platform.isWindows()) { // No Windows support
if (LinktimeInfo.isWindows) { // No Windows support
ju.Collections.emptyList[InterfaceAddress]()
} else {
NetworkInterface.unixImplGetInterfaceAddresses(ifName)
}
}

def getMTU(): Int = {
if (Platform.isWindows()) 0 // No Windows support
if (LinktimeInfo.isWindows) 0 // No Windows support
else {
NetworkInterface.unixImplGetIfMTU(ifName)
}
Expand All @@ -98,7 +97,7 @@ class NetworkInterface private (ifName: String) {
def getName(): String = ifName

def getParent(): NetworkInterface = {
if (Platform.isWindows()) null // No Windows support
if (LinktimeInfo.isWindows) null // No Windows support
else if (!this.isVirtual()) null
else {
val parentName = ifName.split(":")(0)
Expand All @@ -110,7 +109,7 @@ class NetworkInterface private (ifName: String) {
val ifList = new ju.ArrayList[NetworkInterface]()

// No Windows support, so empty Enumeration will be returned.
if (!Platform.isWindows()) {
if (!LinktimeInfo.isWindows) {
val allIfs = NetworkInterface.getNetworkInterfaces()
val matchMe = s"${ifName}:"
while (allIfs.hasMoreElements()) {
Expand All @@ -124,30 +123,31 @@ class NetworkInterface private (ifName: String) {
}

def inetAddresses(): Stream[InetAddress] = {
if (Platform.isWindows()) Stream.empty[InetAddress]() // No Windows support
if (LinktimeInfo.isWindows)
Stream.empty[InetAddress]() // No Windows support
else {
NetworkInterface.unixImplInetAddresses(ifName)
}
}

def isLoopback(): Boolean = {
if (Platform.isWindows()) false // No Windows support
if (LinktimeInfo.isWindows) false // No Windows support
else {
val ifFlags = NetworkInterface.unixImplGetIfFlags(ifName)
(ifFlags & unixIf.IFF_LOOPBACK) == unixIf.IFF_LOOPBACK
}
}

def isPointToPoint(): Boolean = {
if (Platform.isWindows()) false // No Windows support
if (LinktimeInfo.isWindows) false // No Windows support
else {
val ifFlags = NetworkInterface.unixImplGetIfFlags(ifName)
(ifFlags & unixIf.IFF_POINTOPOINT) == unixIf.IFF_POINTOPOINT
}
}

def isUp(): Boolean = {
if (Platform.isWindows()) false // No Windows support
if (LinktimeInfo.isWindows) false // No Windows support
else {
val ifFlags = NetworkInterface.unixImplGetIfFlags(ifName)
(ifFlags & unixIf.IFF_UP) == unixIf.IFF_UP
Expand All @@ -166,7 +166,7 @@ class NetworkInterface private (ifName: String) {
}

def supportsMulticast(): Boolean = {
if (Platform.isWindows()) false // No Windows support
if (LinktimeInfo.isWindows) false // No Windows support
else {
val ifFlags = NetworkInterface.unixImplGetIfFlags(ifName)
(ifFlags & unixIf.IFF_MULTICAST) == unixIf.IFF_MULTICAST
Expand All @@ -185,7 +185,7 @@ object NetworkInterface {
if (index < 0)
throw new IllegalArgumentException("Interface index can't be negative")

if (Platform.isWindows()) {
if (LinktimeInfo.isWindows) {
null
} else {
unixGetByIndex(index)
Expand All @@ -194,7 +194,7 @@ object NetworkInterface {

def getByInetAddress(addr: InetAddress): NetworkInterface = {
Objects.requireNonNull(addr)
if (Platform.isWindows()) {
if (LinktimeInfo.isWindows) {
null
} else {
unixGetByInetAddress(addr)
Expand All @@ -203,15 +203,15 @@ object NetworkInterface {

def getByName(name: String): NetworkInterface = {
Objects.requireNonNull(name)
if (Platform.isWindows()) {
if (LinktimeInfo.isWindows) {
null
} else {
unixGetByName(name)
}
}

def getNetworkInterfaces(): ju.Enumeration[NetworkInterface] = {
if (Platform.isWindows()) {
if (LinktimeInfo.isWindows) {
null
} else {
unixGetNetworkInterfaces()
Expand All @@ -222,7 +222,7 @@ object NetworkInterface {
* less clumsy than Enumerations.
*/
def networkInterfaces(): Stream[NetworkInterface] = {
if (Platform.isWindows()) {
if (LinktimeInfo.isWindows) {
null
} else {
unixNetworkInterfaces()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala.scalanative.runtime

import scalanative.runtime.Platform.isWindows
import scala.scalanative.meta.LinktimeInfo.isWindows
import scalanative.unsigned._
import scala.scalanative.unsafe._
import scala.scalanative.runtime.libc._
Expand Down Expand Up @@ -31,12 +31,20 @@ object SymbolFormatter {
// On Windows symbol names are different then on Unix platforms.
// Due to differences in implementation between WinDbg and libUnwind used
// on each platform, symbols on Windows do not contain '_' prefix.
if (!isWindows() && read() != '_') {
false
} else if (read() != 'S') {
false
if (!isWindows) {
if (read() != '_') {
false
} else if (read() != 'S') {
false
} else {
readGlobal()
}
} else {
readGlobal()
if (read() != 'S') {
false
} else {
readGlobal()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ object CVarArgList {
def vrOffset_=(value: Int): Unit = ptr._5 = value
}

val isWindowsOrMac = Platform.isWindows() || Platform.isMac()
@resolvedAtLinktime
def isWindowsOrMac = isWindows || isMac
@resolvedAtLinktime
def isArm64 = target.arch == "aarch64"

private final val countGPRegisters =
if (PlatformExt.isArm64 && !isWindowsOrMac) 8
if (isArm64 && !isWindowsOrMac) 8
else 6
private final val countFPRegisters = 8
private final val fpRegisterWords =
if (PlatformExt.isArm64 && !isWindowsOrMac)
if (isArm64 && !isWindowsOrMac)
16 / fromRawUSize(Intrinsics.sizeOf[Size]).toInt
else 2
private final val registerSaveWords =
Expand All @@ -63,7 +67,7 @@ object CVarArgList {
varargs: Seq[CVarArg]
)(implicit z: Zone): CVarArgList = {
if (isWindows) toCVarArgList_X86_64_Windows(varargs)
else if (PlatformExt.isArm64 && Platform.isMac())
else if (isArm64 && isMac)
toCVarArgList_Arm64_MacOS(varargs)
else if (is32BitPlatform) toCVarArgList_X86_Unix(varargs)
else toCVarArgList_Unix(varargs)
Expand Down Expand Up @@ -152,8 +156,8 @@ object CVarArgList {
toRawPtr(storageStart),
wordsUsed.toUSize * fromRawUSize(Intrinsics.sizeOf[Long])
)
val rawPtr = if (PlatformExt.isArm64) {
if (Platform.isMac()) toRawPtr(storageStart)
val rawPtr = if (isArm64) {
if (isMac) toRawPtr(storageStart)
else {
val vrTop = resultStorage + fpRegisterWords * countFPRegisters
val grTop = vrTop + countGPRegisters
Expand Down
9 changes: 5 additions & 4 deletions posixlib/src/main/scala/scala/scalanative/posix/netdb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scalanative.unsafe._

import scalanative.posix.sys.socket

import scalanative.runtime.Platform
import scala.scalanative.meta.LinktimeInfo

/** netdb.h for Scala
* @see
Expand Down Expand Up @@ -148,9 +148,10 @@ object netdb {
object netdbOps {
import netdb._

final val useBsdAddrinfo = (Platform.isMac() ||
Platform.isFreeBSD() ||
Platform.isWindows())
@resolvedAtLinktime
def useBsdAddrinfo = (LinktimeInfo.isMac ||
LinktimeInfo.isFreeBSD ||
LinktimeInfo.isWindows)

implicit class addrinfoOps(private val ptr: Ptr[addrinfo]) extends AnyVal {
def ai_flags: CInt = ptr._1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalanative.runtime.Platform

import scalanative.unsafe._
import scalanative.unsigned._
import scalanative.meta.LinktimeInfo.isWindows
import scalanative.meta.LinktimeInfo._

/** socket.h for Scala
* @see
Expand Down Expand Up @@ -394,8 +394,9 @@ object socketOps {
import posix.inttypes.uint8_t

// Also used by posixlib netinet/in.scala
val useSinXLen = !Platform.isLinux() &&
(Platform.isMac() || Platform.isFreeBSD())
@resolvedAtLinktime
def useSinXLen = !isLinux &&
(isMac || isFreeBSD)

implicit class sockaddrOps(val ptr: Ptr[sockaddr]) extends AnyVal {
def sa_len: uint8_t = if (!useSinXLen) {
Expand Down
7 changes: 4 additions & 3 deletions posixlib/src/main/scala/scala/scalanative/posix/sys/un.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package sys
import scalanative.unsafe._
import scalanative.unsigned._

import scalanative.runtime.Platform
import scalanative.meta.LinktimeInfo._

/** POSIX sys/un.h for Scala
*/
Expand Down Expand Up @@ -33,8 +33,9 @@ object unOps {
import un._
import posix.inttypes.uint8_t

val useSinXLen = !Platform.isLinux() &&
(Platform.isMac() || Platform.isFreeBSD())
@resolvedAtLinktime
def useSinXLen = !isLinux &&
(isMac || isFreeBSD)

implicit class sockaddr_unOps(val ptr: Ptr[sockaddr_un]) extends AnyVal {
def sun_len: uint8_t = if (!useSinXLen) {
Expand Down