OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 #include "bin/platform.h" | 9 #include "bin/platform.h" |
10 #include "bin/log.h" | 10 #include "bin/log.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 const char* Platform::LibraryExtension() { | 41 const char* Platform::LibraryExtension() { |
42 return "dll"; | 42 return "dll"; |
43 } | 43 } |
44 | 44 |
45 | 45 |
46 bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) { | 46 bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) { |
47 #if defined(PLATFORM_DISABLE_SOCKET) | 47 #if defined(PLATFORM_DISABLE_SOCKET) |
48 return false; | 48 return false; |
49 #else | 49 #else |
50 if (!Socket::Initialize()) return false; | 50 if (!Socket::Initialize()) { |
| 51 return false; |
| 52 } |
51 return gethostname(buffer, buffer_length) == 0; | 53 return gethostname(buffer, buffer_length) == 0; |
52 #endif | 54 #endif |
53 } | 55 } |
54 | 56 |
55 | 57 |
56 char** Platform::Environment(intptr_t* count) { | 58 char** Platform::Environment(intptr_t* count) { |
57 wchar_t* strings = GetEnvironmentStringsW(); | 59 wchar_t* strings = GetEnvironmentStringsW(); |
58 if (strings == NULL) return NULL; | 60 if (strings == NULL) { |
| 61 return NULL; |
| 62 } |
59 wchar_t* tmp = strings; | 63 wchar_t* tmp = strings; |
60 intptr_t i = 0; | 64 intptr_t i = 0; |
61 while (*tmp != '\0') { | 65 while (*tmp != '\0') { |
62 // Skip environment strings starting with "=". | 66 // Skip environment strings starting with "=". |
63 // These are synthetic variables corresponding to dynamic environment | 67 // These are synthetic variables corresponding to dynamic environment |
64 // variables like %=C:% and %=ExitCode%, and the Dart environment does | 68 // variables like %=C:% and %=ExitCode%, and the Dart environment does |
65 // not include these. | 69 // not include these. |
66 if (*tmp != '=') i++; | 70 if (*tmp != '=') { |
| 71 i++; |
| 72 } |
67 tmp += (wcslen(tmp) + 1); | 73 tmp += (wcslen(tmp) + 1); |
68 } | 74 } |
69 *count = i; | 75 *count = i; |
70 char** result = new char*[i]; | 76 char** result; |
| 77 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); |
71 tmp = strings; | 78 tmp = strings; |
72 for (intptr_t current = 0; current < i;) { | 79 for (intptr_t current = 0; current < i;) { |
73 // Skip the strings that were not counted above. | 80 // Skip the strings that were not counted above. |
74 if (*tmp != '=') result[current++] = StringUtilsWin::WideToUtf8(tmp); | 81 if (*tmp != '=') { |
| 82 result[current++] = StringUtilsWin::WideToUtf8(tmp); |
| 83 } |
75 tmp += (wcslen(tmp) + 1); | 84 tmp += (wcslen(tmp) + 1); |
76 } | 85 } |
77 FreeEnvironmentStringsW(strings); | 86 FreeEnvironmentStringsW(strings); |
78 return result; | 87 return result; |
79 } | 88 } |
80 | 89 |
81 | 90 |
82 void Platform::FreeEnvironment(char** env, intptr_t count) { | 91 const char* Platform::ResolveExecutablePath() { |
83 for (intptr_t i = 0; i < count; i++) { | |
84 free(env[i]); | |
85 } | |
86 delete[] env; | |
87 } | |
88 | |
89 | |
90 char* Platform::ResolveExecutablePath() { | |
91 // GetModuleFileNameW cannot directly provide information on the | 92 // GetModuleFileNameW cannot directly provide information on the |
92 // required buffer size, so start out with a buffer large enough to | 93 // required buffer size, so start out with a buffer large enough to |
93 // hold any Windows path. | 94 // hold any Windows path. |
94 const int kTmpBufferSize = 32768; | 95 const int kTmpBufferSize = 32768; |
95 wchar_t* tmp_buffer = reinterpret_cast<wchar_t*>(malloc(kTmpBufferSize)); | 96 wchar_t* tmp_buffer = |
| 97 reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(kTmpBufferSize)); |
96 // Ensure no last error before calling GetModuleFileNameW. | 98 // Ensure no last error before calling GetModuleFileNameW. |
97 SetLastError(ERROR_SUCCESS); | 99 SetLastError(ERROR_SUCCESS); |
98 // Get the required length of the buffer. | 100 // Get the required length of the buffer. |
99 int path_length = GetModuleFileNameW(NULL, tmp_buffer, kTmpBufferSize); | 101 int path_length = GetModuleFileNameW(NULL, tmp_buffer, kTmpBufferSize); |
100 if (GetLastError() != ERROR_SUCCESS) { | 102 if (GetLastError() != ERROR_SUCCESS) { |
101 free(tmp_buffer); | |
102 return NULL; | 103 return NULL; |
103 } | 104 } |
104 char* path = StringUtilsWin::WideToUtf8(tmp_buffer); | 105 char* path = StringUtilsWin::WideToUtf8(tmp_buffer); |
105 free(tmp_buffer); | |
106 // Return the canonical path as the returned path might contain symlinks. | 106 // Return the canonical path as the returned path might contain symlinks. |
107 char* canon_path = File::GetCanonicalPath(path); | 107 const char* canon_path = File::GetCanonicalPath(path); |
108 free(path); | |
109 return canon_path; | 108 return canon_path; |
110 } | 109 } |
111 | 110 |
| 111 |
112 void Platform::Exit(int exit_code) { | 112 void Platform::Exit(int exit_code) { |
113 // TODO(zra): Remove once VM shuts down cleanly. | 113 // TODO(zra): Remove once VM shuts down cleanly. |
114 ::dart::private_flag_windows_run_tls_destructors = false; | 114 ::dart::private_flag_windows_run_tls_destructors = false; |
115 // On Windows we use ExitProcess so that threads can't clobber the exit_code. | 115 // On Windows we use ExitProcess so that threads can't clobber the exit_code. |
116 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 | 116 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 |
117 ::ExitProcess(exit_code); | 117 ::ExitProcess(exit_code); |
118 } | 118 } |
119 | 119 |
120 } // namespace bin | 120 } // namespace bin |
121 } // namespace dart | 121 } // namespace dart |
122 | 122 |
123 #endif // defined(TARGET_OS_WINDOWS) | 123 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |