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 "bin/directory.h" | 5 #include "bin/directory.h" |
6 | 6 |
7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/thread.h" |
8 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
9 #include "platform/assert.h" | 10 #include "platform/assert.h" |
10 | 11 |
| 12 dart::Mutex Directory::mutex_; |
| 13 int Directory::service_ports_size_ = 0; |
| 14 Dart_Port* Directory::service_ports_ = NULL; |
| 15 int Directory::service_ports_index_ = 0; |
| 16 |
11 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { | 17 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { |
12 static const int kError = -1; | 18 static const int kError = -1; |
13 static const int kExists = 1; | 19 static const int kExists = 1; |
14 static const int kDoesNotExist = 0; | 20 static const int kDoesNotExist = 0; |
15 Dart_EnterScope(); | 21 Dart_EnterScope(); |
16 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 22 Dart_Handle path = Dart_GetNativeArgument(args, 0); |
17 if (Dart_IsString(path)) { | 23 if (Dart_IsString(path)) { |
18 Directory::ExistsResult result = | 24 Directory::ExistsResult result = |
19 Directory::Exists(DartUtils::GetStringValue(path)); | 25 Directory::Exists(DartUtils::GetStringValue(path)); |
20 int return_value = kError; | 26 int return_value = kError; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 default: | 229 default: |
224 UNREACHABLE(); | 230 UNREACHABLE(); |
225 } | 231 } |
226 } | 232 } |
227 } | 233 } |
228 | 234 |
229 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | 235 Dart_PostCObject(reply_port_id, response->AsApiCObject()); |
230 } | 236 } |
231 | 237 |
232 | 238 |
| 239 Dart_Port Directory::GetServicePort() { |
| 240 MutexLocker lock(&mutex_); |
| 241 if (service_ports_size_ == 0) { |
| 242 ASSERT(service_ports_ == NULL); |
| 243 service_ports_size_ = 16; |
| 244 service_ports_ = new Dart_Port[service_ports_size_]; |
| 245 service_ports_index_ = 0; |
| 246 for (int i = 0; i < service_ports_size_; i++) { |
| 247 service_ports_[i] = kIllegalPort; |
| 248 } |
| 249 } |
| 250 |
| 251 Dart_Port result = service_ports_[service_ports_index_]; |
| 252 if (result == kIllegalPort) { |
| 253 result = Dart_NewNativePort("DirectoryService", |
| 254 DirectoryService, |
| 255 true); |
| 256 ASSERT(result != kIllegalPort); |
| 257 service_ports_[service_ports_index_] = result; |
| 258 } |
| 259 service_ports_index_ = (service_ports_index_ + 1) % service_ports_size_; |
| 260 return result; |
| 261 } |
| 262 |
| 263 |
233 void FUNCTION_NAME(Directory_NewServicePort)(Dart_NativeArguments args) { | 264 void FUNCTION_NAME(Directory_NewServicePort)(Dart_NativeArguments args) { |
234 Dart_EnterScope(); | 265 Dart_EnterScope(); |
235 Dart_SetReturnValue(args, Dart_Null()); | 266 Dart_SetReturnValue(args, Dart_Null()); |
236 Dart_Port service_port = kIllegalPort; | 267 Dart_Port service_port = Directory::GetServicePort(); |
237 service_port = Dart_NewNativePort("DirectoryService", | |
238 DirectoryService, | |
239 true); | |
240 if (service_port != kIllegalPort) { | 268 if (service_port != kIllegalPort) { |
241 // Return a send port for the service port. | 269 // Return a send port for the service port. |
242 Dart_Handle send_port = Dart_NewSendPort(service_port); | 270 Dart_Handle send_port = Dart_NewSendPort(service_port); |
243 Dart_SetReturnValue(args, send_port); | 271 Dart_SetReturnValue(args, send_port); |
244 } | 272 } |
245 Dart_ExitScope(); | 273 Dart_ExitScope(); |
246 } | 274 } |
247 | 275 |
248 | 276 |
249 CObjectArray* DirectoryListing::NewResponse(Response type, char* arg) { | 277 CObjectArray* DirectoryListing::NewResponse(Response type, char* arg) { |
(...skipping 19 matching lines...) Expand all Loading... |
269 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 297 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
270 } | 298 } |
271 | 299 |
272 | 300 |
273 bool DirectoryListing::HandleError(char* message) { | 301 bool DirectoryListing::HandleError(char* message) { |
274 // TODO(sgjesse): Pass flags to indicate whether error | 302 // TODO(sgjesse): Pass flags to indicate whether error |
275 // responses are needed. | 303 // responses are needed. |
276 CObjectArray* response = NewResponse(kListError, message); | 304 CObjectArray* response = NewResponse(kListError, message); |
277 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 305 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
278 } | 306 } |
OLD | NEW |