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 "bin/thread.h" |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 Dart_SetReturnValue(args, Dart_True()); | 100 Dart_SetReturnValue(args, Dart_True()); |
101 } else { | 101 } else { |
102 Dart_Handle err = DartUtils::NewDartOSError(); | 102 Dart_Handle err = DartUtils::NewDartOSError(); |
103 if (Dart_IsError(err)) Dart_PropagateError(err); | 103 if (Dart_IsError(err)) Dart_PropagateError(err); |
104 Dart_SetReturnValue(args, err); | 104 Dart_SetReturnValue(args, err); |
105 } | 105 } |
106 Dart_ExitScope(); | 106 Dart_ExitScope(); |
107 } | 107 } |
108 | 108 |
109 | 109 |
| 110 void FUNCTION_NAME(Directory_List)(Dart_NativeArguments args) { |
| 111 Dart_EnterScope(); |
| 112 Dart_Handle path = Dart_GetNativeArgument(args, 0); |
| 113 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); |
| 114 // Create the list to hold the directory listing here, and pass it to the |
| 115 // SyncDirectoryListing object, which adds elements to it. |
| 116 Dart_Handle results = |
| 117 Dart_New(DartUtils::GetDartClass(DartUtils::kCoreLibURL, "List"), |
| 118 Dart_Null(), |
| 119 0, |
| 120 NULL); |
| 121 SyncDirectoryListing sync_listing(results); |
| 122 Directory::List(DartUtils::GetStringValue(path), |
| 123 DartUtils::GetBooleanValue(recursive), |
| 124 &sync_listing); |
| 125 Dart_SetReturnValue(args, results); |
| 126 Dart_ExitScope(); |
| 127 } |
| 128 |
| 129 |
110 static CObject* DirectoryCreateRequest(const CObjectArray& request) { | 130 static CObject* DirectoryCreateRequest(const CObjectArray& request) { |
111 if (request.Length() == 2 && request[1]->IsString()) { | 131 if (request.Length() == 2 && request[1]->IsString()) { |
112 CObjectString path(request[1]); | 132 CObjectString path(request[1]); |
113 if (Directory::Create(path.CString())) { | 133 if (Directory::Create(path.CString())) { |
114 return CObject::True(); | 134 return CObject::True(); |
115 } else { | 135 } else { |
116 return CObject::NewOSError(); | 136 return CObject::NewOSError(); |
117 } | 137 } |
118 } | 138 } |
119 return CObject::IllegalArgumentError(); | 139 return CObject::IllegalArgumentError(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 return CObject::NewOSError(); | 184 return CObject::NewOSError(); |
165 } | 185 } |
166 } | 186 } |
167 return CObject::IllegalArgumentError(); | 187 return CObject::IllegalArgumentError(); |
168 } | 188 } |
169 | 189 |
170 | 190 |
171 static CObject* DirectoryListRequest(const CObjectArray& request, | 191 static CObject* DirectoryListRequest(const CObjectArray& request, |
172 Dart_Port response_port) { | 192 Dart_Port response_port) { |
173 if (request.Length() == 3 && request[1]->IsString() && request[2]->IsBool()) { | 193 if (request.Length() == 3 && request[1]->IsString() && request[2]->IsBool()) { |
174 DirectoryListing* dir_listing = new DirectoryListing(response_port); | 194 AsyncDirectoryListing* dir_listing = |
| 195 new AsyncDirectoryListing(response_port); |
175 CObjectString path(request[1]); | 196 CObjectString path(request[1]); |
176 CObjectBool recursive(request[2]); | 197 CObjectBool recursive(request[2]); |
177 bool completed = Directory::List( | 198 bool completed = Directory::List( |
178 path.CString(), recursive.Value(), dir_listing); | 199 path.CString(), recursive.Value(), dir_listing); |
179 delete dir_listing; | 200 delete dir_listing; |
180 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); | 201 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); |
181 response->SetAt( | 202 response->SetAt( |
182 0, new CObjectInt32(CObject::NewInt32(DirectoryListing::kListDone))); | 203 0, |
| 204 new CObjectInt32(CObject::NewInt32(AsyncDirectoryListing::kListDone))); |
183 response->SetAt(1, CObject::Bool(completed)); | 205 response->SetAt(1, CObject::Bool(completed)); |
184 return response; | 206 return response; |
185 } | 207 } |
186 // Respond with an illegal argument list error message. | 208 // Respond with an illegal argument list error message. |
187 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); | 209 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); |
188 response->SetAt(0, new CObjectInt32( | 210 response->SetAt(0, new CObjectInt32( |
189 CObject::NewInt32(DirectoryListing::kListError))); | 211 CObject::NewInt32(AsyncDirectoryListing::kListError))); |
190 response->SetAt(1, CObject::Null()); | 212 response->SetAt(1, CObject::Null()); |
191 response->SetAt(2, CObject::IllegalArgumentError()); | 213 response->SetAt(2, CObject::IllegalArgumentError()); |
192 Dart_PostCObject(response_port, response->AsApiCObject()); | 214 Dart_PostCObject(response_port, response->AsApiCObject()); |
193 | 215 |
194 response = new CObjectArray(CObject::NewArray(2)); | 216 response = new CObjectArray(CObject::NewArray(2)); |
195 response->SetAt( | 217 response->SetAt( |
196 0, new CObjectInt32(CObject::NewInt32(DirectoryListing::kListDone))); | 218 0, new CObjectInt32(CObject::NewInt32(AsyncDirectoryListing::kListDone))); |
197 response->SetAt(1, CObject::False()); | 219 response->SetAt(1, CObject::False()); |
198 return response; | 220 return response; |
199 } | 221 } |
200 | 222 |
201 | 223 |
202 static CObject* DirectoryRenameRequest(const CObjectArray& request, | 224 static CObject* DirectoryRenameRequest(const CObjectArray& request, |
203 Dart_Port response_port) { | 225 Dart_Port response_port) { |
204 if (request.Length() == 3 && | 226 if (request.Length() == 3 && |
205 request[1]->IsString() && | 227 request[1]->IsString() && |
206 request[2]->IsString()) { | 228 request[2]->IsString()) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 Dart_Port service_port = Directory::GetServicePort(); | 304 Dart_Port service_port = Directory::GetServicePort(); |
283 if (service_port != ILLEGAL_PORT) { | 305 if (service_port != ILLEGAL_PORT) { |
284 // Return a send port for the service port. | 306 // Return a send port for the service port. |
285 Dart_Handle send_port = Dart_NewSendPort(service_port); | 307 Dart_Handle send_port = Dart_NewSendPort(service_port); |
286 Dart_SetReturnValue(args, send_port); | 308 Dart_SetReturnValue(args, send_port); |
287 } | 309 } |
288 Dart_ExitScope(); | 310 Dart_ExitScope(); |
289 } | 311 } |
290 | 312 |
291 | 313 |
292 CObjectArray* DirectoryListing::NewResponse(Response type, char* arg) { | 314 CObjectArray* AsyncDirectoryListing::NewResponse(Response type, char* arg) { |
293 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); | 315 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); |
294 response->SetAt(0, new CObjectInt32(CObject::NewInt32(type))); | 316 response->SetAt(0, new CObjectInt32(CObject::NewInt32(type))); |
295 response->SetAt(1, new CObjectString(CObject::NewString(arg))); | 317 response->SetAt(1, new CObjectString(CObject::NewString(arg))); |
296 return response; | 318 return response; |
297 } | 319 } |
298 | 320 |
299 | 321 |
300 bool DirectoryListing::HandleDirectory(char* dir_name) { | 322 bool AsyncDirectoryListing::HandleDirectory(char* dir_name) { |
301 CObjectArray* response = NewResponse(kListDirectory, dir_name); | 323 CObjectArray* response = NewResponse(kListDirectory, dir_name); |
302 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 324 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
303 } | 325 } |
304 | 326 |
305 | 327 |
306 bool DirectoryListing::HandleFile(char* file_name) { | 328 bool AsyncDirectoryListing::HandleFile(char* file_name) { |
307 CObjectArray* response = NewResponse(kListFile, file_name); | 329 CObjectArray* response = NewResponse(kListFile, file_name); |
308 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 330 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
309 } | 331 } |
310 | 332 |
311 | 333 |
312 bool DirectoryListing::HandleError(const char* dir_name) { | 334 bool AsyncDirectoryListing::HandleError(const char* dir_name) { |
313 CObject* err = CObject::NewOSError(); | 335 CObject* err = CObject::NewOSError(); |
314 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); | 336 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); |
315 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); | 337 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); |
316 response->SetAt(1, new CObjectString(CObject::NewString(dir_name))); | 338 response->SetAt(1, new CObjectString(CObject::NewString(dir_name))); |
317 response->SetAt(2, err); | 339 response->SetAt(2, err); |
318 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 340 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
319 } | 341 } |
| 342 |
| 343 bool SyncDirectoryListing::HandleDirectory(char* dir_name) { |
| 344 Dart_Handle dir_name_dart = DartUtils::NewString(dir_name); |
| 345 Dart_Handle dir = Dart_New(directory_class_, Dart_Null(), 1, &dir_name_dart); |
| 346 Dart_Invoke(results_, add_string_, 1, &dir); |
| 347 return true; |
| 348 } |
| 349 |
| 350 bool SyncDirectoryListing::HandleFile(char* file_name) { |
| 351 Dart_Handle file_name_dart = DartUtils::NewString(file_name); |
| 352 Dart_Handle file = Dart_New(file_class_, Dart_Null(), 1, &file_name_dart); |
| 353 Dart_Invoke(results_, add_string_, 1, &file); |
| 354 return true; |
| 355 } |
| 356 |
| 357 bool SyncDirectoryListing::HandleError(const char* dir_name) { |
| 358 Dart_Handle dart_os_error = DartUtils::NewDartOSError(); |
| 359 Dart_Handle args[3]; |
| 360 args[0] = DartUtils::NewString("Directory listing failed"); |
| 361 args[1] = DartUtils::NewString(dir_name); |
| 362 args[2] = dart_os_error; |
| 363 Dart_ThrowException(Dart_New( |
| 364 DartUtils::GetDartClass(DartUtils::kIOLibURL, "DirectoryIOException"), |
| 365 Dart_Null(), |
| 366 3, |
| 367 args)); |
| 368 return true; |
| 369 } |
OLD | NEW |