From 162dbba35167b3fb7e21feaa10d26156c98bd9fc Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 24 Sep 2024 23:39:40 +0800 Subject: [PATCH] Update EnvironmentHelper API --- Sources/OpenSwiftUI/App/AppGraph.swift | 2 +- .../OpenSwiftUI/Core/Graph/GraphHost.swift | 2 +- Sources/OpenSwiftUI/Test/_BenchmarkHost.swift | 2 +- .../View/Debug/TODO/ViewDebug.swift | 2 +- .../Util/EnvironmentHelper.swift | 19 +++++++++++++++---- 5 files changed, 19 insertions(+), 8 deletions(-) rename Sources/{OpenSwiftUI/Core => OpenSwiftUICore}/Util/EnvironmentHelper.swift (50%) diff --git a/Sources/OpenSwiftUI/App/AppGraph.swift b/Sources/OpenSwiftUI/App/AppGraph.swift index 8911118c..2b8fbfaa 100644 --- a/Sources/OpenSwiftUI/App/AppGraph.swift +++ b/Sources/OpenSwiftUI/App/AppGraph.swift @@ -25,7 +25,7 @@ final class AppGraph: GraphHost { } private lazy var launchProfileOptions = LaunchProfileOptions( - rawValue: EnvironmentHelper.value(for: "OPENSWIFTUI_PROFILE_LAUNCH") + rawValue: EnvironmentHelper.int32(for: "OPENSWIFTUI_PROFILE_LAUNCH") ) var didCollectLaunchProfile: Bool = false diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift index c6aa958c..bae0a5f9 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift @@ -9,7 +9,7 @@ internal import COpenSwiftUI internal import OpenGraphShims -private let waitingForPreviewThunks = EnvironmentHelper.value(for: "XCODE_RUNNING_FOR_PREVIEWS") != 0 +private let waitingForPreviewThunks = EnvironmentHelper.bool(for: "XCODE_RUNNING_FOR_PREVIEWS") private var blockedGraphHosts: [Unmanaged] = [] class GraphHost { diff --git a/Sources/OpenSwiftUI/Test/_BenchmarkHost.swift b/Sources/OpenSwiftUI/Test/_BenchmarkHost.swift index 60553a7f..7bf68b32 100644 --- a/Sources/OpenSwiftUI/Test/_BenchmarkHost.swift +++ b/Sources/OpenSwiftUI/Test/_BenchmarkHost.swift @@ -12,7 +12,7 @@ import Foundation import QuartzCore #endif -private let enableProfiler = EnvironmentHelper.value(for: "OPENSWIFTUI_PROFILE_BENCHMARKS") != 0 +private let enableProfiler = EnvironmentHelper.bool(for: "OPENSWIFTUI_PROFILE_BENCHMARKS") public protocol _BenchmarkHost: AnyObject { func _renderForTest(interval: Double) diff --git a/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift b/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift index 765c8248..dd0baedc 100644 --- a/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift +++ b/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift @@ -69,7 +69,7 @@ extension _ViewDebug { @inline(__always) static func instantiateIfNeeded() { if !isInitialized { - let debugValue = UInt32(bitPattern: EnvironmentHelper.value(for: "OPENSWIFTUI_VIEW_DEBUG")) + let debugValue = UInt32(bitPattern: EnvironmentHelper.int32(for: "OPENSWIFTUI_VIEW_DEBUG")) properties = Properties(rawValue: debugValue) isInitialized = true } diff --git a/Sources/OpenSwiftUI/Core/Util/EnvironmentHelper.swift b/Sources/OpenSwiftUICore/Util/EnvironmentHelper.swift similarity index 50% rename from Sources/OpenSwiftUI/Core/Util/EnvironmentHelper.swift rename to Sources/OpenSwiftUICore/Util/EnvironmentHelper.swift index b873b083..e4e6427a 100644 --- a/Sources/OpenSwiftUI/Core/Util/EnvironmentHelper.swift +++ b/Sources/OpenSwiftUICore/Util/EnvironmentHelper.swift @@ -1,3 +1,10 @@ +// +// EnvironmentHelper.swift +// OpenSwiftUICore +// +// Audited for RELEASE_2024 +// Status: Complete + #if canImport(Darwin) import Darwin #elseif canImport(Glibc) @@ -8,15 +15,19 @@ import WASILibc #error("Unsupported Platform") #endif -enum EnvironmentHelper { +package enum EnvironmentHelper { @_transparent - @inline(__always) - static func value(for key: String) -> Int32 { + package static func int32(for key: String) -> Int32 { key.withCString { string in guard let env = getenv(string) else { return 0 } return atoi(env) - } + } + } + + @_transparent + package static func bool(for key: String) -> Bool { + int32(for: key) != 0 } }