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; |
} |
} |