Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Unified Diff: sdk/lib/io/platform_impl.dart

Issue 123263003: Make the Platform.environment return value an unmodifiable map (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/platform.dart ('k') | tests/standalone/io/process_environment_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/platform_impl.dart
diff --git a/sdk/lib/io/platform_impl.dart b/sdk/lib/io/platform_impl.dart
index 40fe2f0cf79a7b557d2dfa1c3385262426fd1c19..e85ce5c42c21f66f3169e13e01ff617fd2754dee 100644
--- a/sdk/lib/io/platform_impl.dart
+++ b/sdk/lib/io/platform_impl.dart
@@ -15,6 +15,10 @@ class _Platform {
external static String _packageRoot();
external static String _version();
+ // Cache the OS environemnt. This can be an OSError instance if
+ // retrieving the environment failed.
+ static var _environmentCache;
+
static int get numberOfProcessors => _numberOfProcessors();
static String get pathSeparator => _pathSeparator();
static String get operatingSystem => _operatingSystem();
@@ -45,28 +49,37 @@ class _Platform {
static List<String> get executableArguments => _executableArguments();
static Map<String, String> get environment {
- var env = _environment();
- if (env is OSError) {
- throw env;
- } else {
- var isWindows = operatingSystem == 'windows';
- var result = isWindows ? new _CaseInsensitiveStringMap() : new Map();
- for (var str in env) {
- // When running on Windows through cmd.exe there are strange
- // environment variables that are used to record the current
- // working directory for each drive and the exit code for the
- // last command. As an example: '=A:=A:\subdir' records the
- // current working directory on the 'A' drive. In order to
- // handle these correctly we search for a second occurrence of
- // of '=' in the string if the first occurrence is at index 0.
- var equalsIndex = str.indexOf('=');
- if (equalsIndex == 0) {
- equalsIndex = str.indexOf('=', 1);
+ if (_environmentCache == null) {
+ var env = _environment();
+ if (env is !OSError) {
+ var isWindows = operatingSystem == 'windows';
+ var result = isWindows ? new _CaseInsensitiveStringMap() : new Map();
+ for (var str in env) {
+ // When running on Windows through cmd.exe there are strange
+ // environment variables that are used to record the current
+ // working directory for each drive and the exit code for the
+ // last command. As an example: '=A:=A:\subdir' records the
+ // current working directory on the 'A' drive. In order to
+ // handle these correctly we search for a second occurrence of
+ // of '=' in the string if the first occurrence is at index 0.
+ var equalsIndex = str.indexOf('=');
+ if (equalsIndex == 0) {
+ equalsIndex = str.indexOf('=', 1);
+ }
+ assert(equalsIndex != -1);
+ result[str.substring(0, equalsIndex)] =
+ str.substring(equalsIndex + 1);
}
- assert(equalsIndex != -1);
- result[str.substring(0, equalsIndex)] = str.substring(equalsIndex + 1);
+ _environmentCache = new _UnmodifiableMap(result);
+ } else {
+ _environmentCache = env;
}
- return result;
+ }
+
+ if (_environmentCache is OSError) {
+ throw _environmentCache;
+ } else {
+ return _environmentCache;
}
}
« no previous file with comments | « sdk/lib/io/platform.dart ('k') | tests/standalone/io/process_environment_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698