Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Improve tree-shaking: #45

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 7 additions & 0 deletions lib/os_detect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ String get operatingSystemVersion => OperatingSystem.current.version;
/// for example Android (see [isAndroid]),
/// or if the code is running inside a browser (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isLinux => OperatingSystem.current.isLinux;

/// Whether the current operating system is a version of
Expand All @@ -45,6 +46,7 @@ bool get isLinux => OperatingSystem.current.isLinux;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on MacOS (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isMacOS => OperatingSystem.current.isMacOS;

/// Whether the current operating system is a version of
Expand All @@ -55,6 +57,7 @@ bool get isMacOS => OperatingSystem.current.isMacOS;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Windows (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isWindows => OperatingSystem.current.isWindows;

/// Whether the current operating system is a version of
Expand All @@ -65,6 +68,7 @@ bool get isWindows => OperatingSystem.current.isWindows;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Android (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isAndroid => OperatingSystem.current.isAndroid;

/// Whether the current operating system is a version of
Expand All @@ -75,6 +79,7 @@ bool get isAndroid => OperatingSystem.current.isAndroid;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on iOS (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isIOS => OperatingSystem.current.isIOS;

/// Whether the current operating system is a version of
Expand All @@ -85,6 +90,7 @@ bool get isIOS => OperatingSystem.current.isIOS;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Fuchsia (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isFuchsia => OperatingSystem.current.isFuchsia;

/// Whether running in a web browser.
Expand All @@ -101,4 +107,5 @@ bool get isFuchsia => OperatingSystem.current.isFuchsia;
/// but browsers are able to lie in the app-version/user-agent
/// string.
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isBrowser => OperatingSystem.current.isBrowser;
36 changes: 27 additions & 9 deletions lib/src/os_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,23 @@ final class OperatingSystem {
/// from known platform specific libraries,
/// but can be overridden using functionality from the
/// `osid_override.dart` library.
@pragma('vm:try-inline')
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
static OperatingSystem get current =>
Zone.current[#_os] as OperatingSystem? ?? platformOS;
_getOperatingSystemOverride?.call() ?? platformOS;

/// A string representing the operating system or platform.
String get id => _osId.id;

// Operating system ID object.
final RecognizedOS _osId;

final String Function() _computeVersion;

/// A string representing the version of the operating system or platform.
///
/// May be empty if no version is known or available.
final String version;
String get version => _computeVersion();

/// Creates a new operating system object for testing.
///
Expand All @@ -91,7 +94,8 @@ final class OperatingSystem {
// That can avoid retaining *all* the subclasses of `OS`.
@visibleForTesting
@pragma('vm:prefer-inline')
OperatingSystem(String id, String version)
@pragma('dart2js:prefer-inline')
OperatingSystem(String id, String computeVersion)
: this._(
id == linuxId
? const LinuxOS()
Expand All @@ -108,10 +112,10 @@ final class OperatingSystem {
: id == browserId
? const BrowserOS()
: UnknownOS(id),
version);
() => computeVersion);

/// Used by platforms which know the ID object.
const OperatingSystem._(this._osId, this.version);
const OperatingSystem._(this._osId, this._computeVersion);

/// Whether the operating system is a version of
/// [Linux](https://en.wikipedia.org/wiki/Linux).
Expand Down Expand Up @@ -170,11 +174,25 @@ final class OperatingSystem {
/// This override affects the `operatingSystem` and `version`
/// exported by `package:osid/osid.dart`.
R overrideOperatingSystem<R>(
OperatingSystem operatingSystem, R Function() body) =>
runZoned(body, zoneValues: {#_os: operatingSystem});
OperatingSystem operatingSystem, R Function() body) {
// Initialize [_getOperatingSystemOverride]: it is initially set to `null`
// to enable compilers to fully constant fold [OperatingSystem.current].
_getOperatingSystemOverride = _getOperatingSystemOverrideFromZone;
return runZoned(body, zoneValues: {#_os: operatingSystem});
}

// Exposes the `OperatingSystem._` constructor to the conditionally imported
// libraries. Not exported by `../override.dart'.
final class OperatingSystemInternal extends OperatingSystem {
const OperatingSystemInternal(super.id, super.version) : super._();
const OperatingSystemInternal(super.id, super.computeVersion) : super._();
}

/// Return platform override set by [overrideOperatingSystem].
///
/// If [overrideOperatingSystem] is never called this callback will be `null`
/// which allows compilers to fully constant fold away
/// [OperatingSystem.current].
OperatingSystem? Function()? _getOperatingSystemOverride;

OperatingSystem? _getOperatingSystemOverrideFromZone() =>
Zone.current[#_os] as OperatingSystem?;
6 changes: 3 additions & 3 deletions lib/src/osid_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:html';
import 'os_kind.dart' show BrowserOS;
import 'os_override.dart';

String get _osVersion => window.navigator.appVersion;
String _osVersion() => window.navigator.appVersion;

final OperatingSystem platformOS =
OperatingSystemInternal(const BrowserOS(), _osVersion);
const OperatingSystem platformOS =
OperatingSystemInternal(BrowserOS(), _osVersion);
6 changes: 4 additions & 2 deletions lib/src/osid_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final RecognizedOS? _osType = Platform.operatingSystem == RecognizedOS.linuxId
? const BrowserOS()
: null;

String _osVersion() => Platform.operatingSystemVersion;

@pragma('vm:platform-const')
final OperatingSystem platformOS = OperatingSystemInternal(
_osType ?? UnknownOS(Platform.operatingSystem),
Platform.operatingSystemVersion);
_osType ?? UnknownOS(Platform.operatingSystem), _osVersion);
2 changes: 1 addition & 1 deletion lib/src/osid_unknown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'os_override.dart';
@pragma('vm:platform-const')
const String _os =
String.fromEnvironment('dart.os.name', defaultValue: 'unknown');
const String _osVersion = String.fromEnvironment('dart.os.version');
String _osVersion() => const String.fromEnvironment('dart.os.version');

const OperatingSystem platformOS = OperatingSystemInternal(
_os == RecognizedOS.linuxId
Expand Down