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

Side by Side Diff: runtime/bin/file.cc

Issue 3001963002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Fuchsia fix Created 3 years, 3 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 unified diff | Download patch
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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/file.h" 5 #include "bin/file.h"
6 6
7 #include "bin/builtin.h" 7 #include "bin/builtin.h"
8 #include "bin/dartutils.h" 8 #include "bin/dartutils.h"
9 #include "bin/embedded_dart_io.h" 9 #include "bin/embedded_dart_io.h"
10 #include "bin/io_buffer.h" 10 #include "bin/io_buffer.h"
11 #include "bin/namespace.h"
11 #include "bin/utils.h" 12 #include "bin/utils.h"
12 #include "include/dart_api.h" 13 #include "include/dart_api.h"
13 #include "include/dart_tools_api.h" 14 #include "include/dart_tools_api.h"
14 #include "platform/globals.h" 15 #include "platform/globals.h"
15 16
16 namespace dart { 17 namespace dart {
17 namespace bin { 18 namespace bin {
18 19
19 static const int kFileNativeFieldIndex = 0; 20 static const int kFileNativeFieldIndex = 0;
20 21
21 // The file pointer has been passed into Dart as an intptr_t and it is safe 22 // The file pointer has been passed into Dart as an intptr_t and it is safe
22 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and 23 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and
23 // from there to a File pointer. 24 // from there to a File pointer.
24 static File* GetFile(Dart_NativeArguments args) { 25 static File* GetFile(Dart_NativeArguments args) {
25 File* file; 26 File* file;
26 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); 27 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
27 ASSERT(Dart_IsInstance(dart_this)); 28 ASSERT(Dart_IsInstance(dart_this));
28 ThrowIfError(Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIndex, 29 Dart_Handle result = Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIn dex,
29 reinterpret_cast<intptr_t*>(&file))); 30 reinterpret_cast<intptr_t*>(&file));
31 ASSERT(!Dart_IsError(result));
30 return file; 32 return file;
31 } 33 }
32 34
33 static void SetFile(Dart_Handle dart_this, intptr_t file_pointer) { 35 static void SetFile(Dart_Handle dart_this, intptr_t file_pointer) {
34 Dart_Handle result = Dart_SetNativeInstanceField( 36 Dart_Handle result = Dart_SetNativeInstanceField(
35 dart_this, kFileNativeFieldIndex, file_pointer); 37 dart_this, kFileNativeFieldIndex, file_pointer);
36 if (Dart_IsError(result)) { 38 if (Dart_IsError(result)) {
37 Log::PrintErr("SetNativeInstanceField in SetFile() failed\n"); 39 Log::PrintErr("SetNativeInstanceField in SetFile() failed\n");
38 Dart_PropagateError(result); 40 Dart_PropagateError(result);
39 } 41 }
(...skipping 23 matching lines...) Expand all
63 intptr_t file_pointer = 65 intptr_t file_pointer =
64 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); 66 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
65 File* file = reinterpret_cast<File*>(file_pointer); 67 File* file = reinterpret_cast<File*>(file_pointer);
66 Dart_WeakPersistentHandle handle = Dart_NewWeakPersistentHandle( 68 Dart_WeakPersistentHandle handle = Dart_NewWeakPersistentHandle(
67 dart_this, reinterpret_cast<void*>(file), sizeof(*file), ReleaseFile); 69 dart_this, reinterpret_cast<void*>(file), sizeof(*file), ReleaseFile);
68 file->SetWeakHandle(handle); 70 file->SetWeakHandle(handle);
69 SetFile(dart_this, file_pointer); 71 SetFile(dart_this, file_pointer);
70 } 72 }
71 73
72 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { 74 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) {
75 Namespace* namespc = Namespace::GetNamespace(args, 0);
73 const char* filename = 76 const char* filename =
74 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 77 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
75 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 78 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
76 File::DartFileOpenMode dart_file_mode = 79 File::DartFileOpenMode dart_file_mode =
77 static_cast<File::DartFileOpenMode>(mode); 80 static_cast<File::DartFileOpenMode>(mode);
78 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); 81 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
79 // Check that the file exists before opening it only for 82 // Check that the file exists before opening it only for
80 // reading. This is to prevent the opening of directories as 83 // reading. This is to prevent the opening of directories as
81 // files. Directories can be opened for reading using the posix 84 // files. Directories can be opened for reading using the posix
82 // 'open' call. 85 // 'open' call.
83 File* file = File::Open(filename, file_mode); 86 File* file = File::Open(namespc, filename, file_mode);
84 if (file != NULL) { 87 if (file != NULL) {
85 Dart_SetReturnValue(args, 88 Dart_SetReturnValue(args,
86 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); 89 Dart_NewInteger(reinterpret_cast<intptr_t>(file)));
87 } else { 90 } else {
88 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 91 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
89 } 92 }
90 } 93 }
91 94
92 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { 95 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) {
96 Namespace* namespc = Namespace::GetNamespace(args, 0);
93 const char* filename = 97 const char* filename =
94 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 98 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
95 bool exists = File::Exists(filename); 99 bool exists = File::Exists(namespc, filename);
96 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); 100 Dart_SetReturnValue(args, Dart_NewBoolean(exists));
97 } 101 }
98 102
99 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { 103 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) {
100 File* file = GetFile(args); 104 File* file = GetFile(args);
101 ASSERT(file != NULL); 105 ASSERT(file != NULL);
102 file->Close(); 106 file->Close();
103 file->DeleteWeakHandle(Dart_CurrentIsolate()); 107 file->DeleteWeakHandle(Dart_CurrentIsolate());
104 file->Release(); 108 file->Release();
105 109
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 ASSERT(file != NULL); 309 ASSERT(file != NULL);
306 int64_t return_value = file->Length(); 310 int64_t return_value = file->Length();
307 if (return_value >= 0) { 311 if (return_value >= 0) {
308 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 312 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
309 } else { 313 } else {
310 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 314 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
311 } 315 }
312 } 316 }
313 317
314 void FUNCTION_NAME(File_LengthFromPath)(Dart_NativeArguments args) { 318 void FUNCTION_NAME(File_LengthFromPath)(Dart_NativeArguments args) {
315 const char* path = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 319 Namespace* namespc = Namespace::GetNamespace(args, 0);
316 int64_t return_value = File::LengthFromPath(path); 320 const char* path = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
321 int64_t return_value = File::LengthFromPath(namespc, path);
317 if (return_value >= 0) { 322 if (return_value >= 0) {
318 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 323 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
319 } else { 324 } else {
320 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 325 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
321 } 326 }
322 } 327 }
323 328
324 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) { 329 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) {
325 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 330 Namespace* namespc = Namespace::GetNamespace(args, 0);
326 int64_t return_value = File::LastModified(name); 331 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
332 int64_t return_value = File::LastModified(namespc, name);
327 if (return_value >= 0) { 333 if (return_value >= 0) {
328 Dart_SetReturnValue(args, 334 Dart_SetReturnValue(args,
329 Dart_NewInteger(return_value * kMillisecondsPerSecond)); 335 Dart_NewInteger(return_value * kMillisecondsPerSecond));
330 } else { 336 } else {
331 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 337 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
332 } 338 }
333 } 339 }
334 340
335 void FUNCTION_NAME(File_SetLastModified)(Dart_NativeArguments args) { 341 void FUNCTION_NAME(File_SetLastModified)(Dart_NativeArguments args) {
336 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 342 Namespace* namespc = Namespace::GetNamespace(args, 0);
343 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
337 int64_t millis; 344 int64_t millis;
338 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { 345 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &millis)) {
339 Dart_ThrowException(DartUtils::NewDartArgumentError( 346 Dart_ThrowException(DartUtils::NewDartArgumentError(
340 "The second argument must be a 64-bit int.")); 347 "The second argument must be a 64-bit int."));
341 } 348 }
342 if (!File::SetLastModified(name, millis)) { 349 if (!File::SetLastModified(namespc, name, millis)) {
343 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 350 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
344 } 351 }
345 } 352 }
346 353
347 void FUNCTION_NAME(File_LastAccessed)(Dart_NativeArguments args) { 354 void FUNCTION_NAME(File_LastAccessed)(Dart_NativeArguments args) {
348 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 355 Namespace* namespc = Namespace::GetNamespace(args, 0);
349 int64_t return_value = File::LastAccessed(name); 356 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
357 int64_t return_value = File::LastAccessed(namespc, name);
350 if (return_value >= 0) { 358 if (return_value >= 0) {
351 Dart_SetReturnValue(args, 359 Dart_SetReturnValue(args,
352 Dart_NewInteger(return_value * kMillisecondsPerSecond)); 360 Dart_NewInteger(return_value * kMillisecondsPerSecond));
353 } else { 361 } else {
354 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 362 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
355 } 363 }
356 } 364 }
357 365
358 void FUNCTION_NAME(File_SetLastAccessed)(Dart_NativeArguments args) { 366 void FUNCTION_NAME(File_SetLastAccessed)(Dart_NativeArguments args) {
359 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 367 Namespace* namespc = Namespace::GetNamespace(args, 0);
368 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
360 int64_t millis; 369 int64_t millis;
361 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { 370 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &millis)) {
362 Dart_ThrowException(DartUtils::NewDartArgumentError( 371 Dart_ThrowException(DartUtils::NewDartArgumentError(
363 "The second argument must be a 64-bit int.")); 372 "The second argument must be a 64-bit int."));
364 } 373 }
365 if (!File::SetLastAccessed(name, millis)) { 374 if (!File::SetLastAccessed(namespc, name, millis)) {
366 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 375 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
367 } 376 }
368 } 377 }
369 378
370 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { 379 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) {
371 File* file = GetFile(args); 380 File* file = GetFile(args);
372 ASSERT(file != NULL); 381 ASSERT(file != NULL);
373 if (file->Flush()) { 382 if (file->Flush()) {
374 Dart_SetReturnValue(args, Dart_True()); 383 Dart_SetReturnValue(args, Dart_True());
375 } else { 384 } else {
(...skipping 18 matching lines...) Expand all
394 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 403 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
395 } 404 }
396 return; 405 return;
397 } 406 }
398 } 407 }
399 OSError os_error(-1, "Invalid argument", OSError::kUnknown); 408 OSError os_error(-1, "Invalid argument", OSError::kUnknown);
400 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); 409 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
401 } 410 }
402 411
403 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { 412 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) {
404 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 413 Namespace* namespc = Namespace::GetNamespace(args, 0);
405 bool result = File::Create(str); 414 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
415 bool result = File::Create(namespc, str);
406 if (result) { 416 if (result) {
407 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 417 Dart_SetReturnValue(args, Dart_NewBoolean(result));
408 } else { 418 } else {
409 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 419 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
410 } 420 }
411 } 421 }
412 422
413 void FUNCTION_NAME(File_CreateLink)(Dart_NativeArguments args) { 423 void FUNCTION_NAME(File_CreateLink)(Dart_NativeArguments args) {
414 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && 424 Namespace* namespc = Namespace::GetNamespace(args, 0);
415 Dart_IsString(Dart_GetNativeArgument(args, 1))) { 425 if (Dart_IsString(Dart_GetNativeArgument(args, 1)) &&
426 Dart_IsString(Dart_GetNativeArgument(args, 2))) {
416 const char* name = 427 const char* name =
417 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 428 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
418 const char* target = 429 const char* target =
419 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 430 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
420 if (!File::CreateLink(name, target)) { 431 if (!File::CreateLink(namespc, name, target)) {
421 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 432 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
422 } 433 }
423 } else { 434 } else {
424 Dart_Handle err = 435 Dart_Handle err =
425 DartUtils::NewDartArgumentError("Non-string argument to Link.create"); 436 DartUtils::NewDartArgumentError("Non-string argument to Link.create");
426 Dart_SetReturnValue(args, err); 437 Dart_SetReturnValue(args, err);
427 } 438 }
428 } 439 }
429 440
430 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) { 441 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) {
431 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { 442 Namespace* namespc = Namespace::GetNamespace(args, 0);
443 if (Dart_IsString(Dart_GetNativeArgument(args, 1))) {
432 const char* name = 444 const char* name =
433 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 445 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
434 const char* target = File::LinkTarget(name); 446 const char* target = File::LinkTarget(namespc, name);
435 if (target == NULL) { 447 if (target == NULL) {
436 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 448 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
437 } else { 449 } else {
438 Dart_SetReturnValue(args, DartUtils::NewString(target)); 450 Dart_SetReturnValue(args, DartUtils::NewString(target));
439 } 451 }
440 } else { 452 } else {
441 Dart_Handle err = 453 Dart_Handle err =
442 DartUtils::NewDartArgumentError("Non-string argument to Link.target"); 454 DartUtils::NewDartArgumentError("Non-string argument to Link.target");
443 Dart_SetReturnValue(args, err); 455 Dart_SetReturnValue(args, err);
444 } 456 }
445 } 457 }
446 458
447 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { 459 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) {
448 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 460 Namespace* namespc = Namespace::GetNamespace(args, 0);
449 bool result = File::Delete(str); 461 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
462 bool result = File::Delete(namespc, str);
450 if (result) { 463 if (result) {
451 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 464 Dart_SetReturnValue(args, Dart_NewBoolean(result));
452 } else { 465 } else {
453 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 466 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
454 } 467 }
455 } 468 }
456 469
457 void FUNCTION_NAME(File_DeleteLink)(Dart_NativeArguments args) { 470 void FUNCTION_NAME(File_DeleteLink)(Dart_NativeArguments args) {
458 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 471 Namespace* namespc = Namespace::GetNamespace(args, 0);
459 bool result = File::DeleteLink(str); 472 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
473 bool result = File::DeleteLink(namespc, str);
460 if (result) { 474 if (result) {
461 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 475 Dart_SetReturnValue(args, Dart_NewBoolean(result));
462 } else { 476 } else {
463 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 477 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
464 } 478 }
465 } 479 }
466 480
467 void FUNCTION_NAME(File_Rename)(Dart_NativeArguments args) { 481 void FUNCTION_NAME(File_Rename)(Dart_NativeArguments args) {
482 Namespace* namespc = Namespace::GetNamespace(args, 0);
468 const char* old_path = 483 const char* old_path =
469 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 484 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
470 const char* new_path = 485 const char* new_path =
471 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 486 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
472 bool result = File::Rename(old_path, new_path); 487 bool result = File::Rename(namespc, old_path, new_path);
473 if (result) { 488 if (result) {
474 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 489 Dart_SetReturnValue(args, Dart_NewBoolean(result));
475 } else { 490 } else {
476 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 491 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
477 } 492 }
478 } 493 }
479 494
480 void FUNCTION_NAME(File_RenameLink)(Dart_NativeArguments args) { 495 void FUNCTION_NAME(File_RenameLink)(Dart_NativeArguments args) {
496 Namespace* namespc = Namespace::GetNamespace(args, 0);
481 const char* old_path = 497 const char* old_path =
482 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 498 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
483 const char* new_path = 499 const char* new_path =
484 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 500 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
485 bool result = File::RenameLink(old_path, new_path); 501 bool result = File::RenameLink(namespc, old_path, new_path);
486 if (result) { 502 if (result) {
487 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 503 Dart_SetReturnValue(args, Dart_NewBoolean(result));
488 } else { 504 } else {
489 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 505 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
490 } 506 }
491 } 507 }
492 508
493 void FUNCTION_NAME(File_Copy)(Dart_NativeArguments args) { 509 void FUNCTION_NAME(File_Copy)(Dart_NativeArguments args) {
510 Namespace* namespc = Namespace::GetNamespace(args, 0);
494 const char* old_path = 511 const char* old_path =
495 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 512 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
496 const char* new_path = 513 const char* new_path =
497 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 514 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
498 bool result = File::Copy(old_path, new_path); 515 bool result = File::Copy(namespc, old_path, new_path);
499 if (result) { 516 if (result) {
500 Dart_SetReturnValue(args, Dart_NewBoolean(result)); 517 Dart_SetReturnValue(args, Dart_NewBoolean(result));
501 } else { 518 } else {
502 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 519 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
503 } 520 }
504 } 521 }
505 522
506 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) { 523 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) {
507 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 524 Namespace* namespc = Namespace::GetNamespace(args, 0);
508 const char* path = File::GetCanonicalPath(str); 525 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
526 const char* path = File::GetCanonicalPath(namespc, str);
509 if (path != NULL) { 527 if (path != NULL) {
510 Dart_SetReturnValue(args, DartUtils::NewString(path)); 528 Dart_SetReturnValue(args, DartUtils::NewString(path));
511 } else { 529 } else {
512 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 530 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
513 } 531 }
514 } 532 }
515 533
516 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { 534 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) {
517 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 535 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
518 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || 536 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) ||
519 (fd == STDERR_FILENO)); 537 (fd == STDERR_FILENO));
520 File* file = File::OpenStdio(static_cast<int>(fd)); 538 File* file = File::OpenStdio(static_cast<int>(fd));
521 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); 539 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file)));
522 } 540 }
523 541
524 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { 542 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) {
525 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 543 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
526 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || 544 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) ||
527 (fd == STDERR_FILENO)); 545 (fd == STDERR_FILENO));
528 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd)); 546 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd));
529 Dart_SetReturnValue(args, Dart_NewInteger(type)); 547 Dart_SetReturnValue(args, Dart_NewInteger(type));
530 } 548 }
531 549
532 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) { 550 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) {
533 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && 551 Namespace* namespc = Namespace::GetNamespace(args, 0);
534 Dart_IsBoolean(Dart_GetNativeArgument(args, 1))) { 552 if (Dart_IsString(Dart_GetNativeArgument(args, 1)) &&
553 Dart_IsBoolean(Dart_GetNativeArgument(args, 2))) {
535 const char* str = 554 const char* str =
536 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 555 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
537 bool follow_links = 556 bool follow_links =
538 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); 557 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2));
539 File::Type type = File::GetType(str, follow_links); 558 File::Type type = File::GetType(namespc, str, follow_links);
540 Dart_SetReturnValue(args, Dart_NewInteger(static_cast<int>(type))); 559 Dart_SetReturnValue(args, Dart_NewInteger(static_cast<int>(type)));
541 } else { 560 } else {
542 Dart_Handle err = DartUtils::NewDartArgumentError( 561 Dart_Handle err = DartUtils::NewDartArgumentError(
543 "Non-string argument to FileSystemEntity.type"); 562 "Non-string argument to FileSystemEntity.type");
544 Dart_SetReturnValue(args, err); 563 Dart_SetReturnValue(args, err);
545 } 564 }
546 } 565 }
547 566
548 void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) { 567 void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) {
549 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { 568 Namespace* namespc = Namespace::GetNamespace(args, 0);
569 if (Dart_IsString(Dart_GetNativeArgument(args, 1))) {
550 const char* path = 570 const char* path =
551 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 571 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
552 572
553 int64_t stat_data[File::kStatSize]; 573 int64_t stat_data[File::kStatSize];
554 File::Stat(path, stat_data); 574 File::Stat(namespc, path, stat_data);
555 if (stat_data[File::kType] == File::kDoesNotExist) { 575 if (stat_data[File::kType] == File::kDoesNotExist) {
556 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 576 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
557 } else { 577 } else {
558 Dart_Handle returned_data = 578 Dart_Handle returned_data =
559 Dart_NewTypedData(Dart_TypedData_kInt64, File::kStatSize); 579 Dart_NewTypedData(Dart_TypedData_kInt64, File::kStatSize);
560 if (Dart_IsError(returned_data)) { 580 if (Dart_IsError(returned_data)) {
561 Dart_PropagateError(returned_data); 581 Dart_PropagateError(returned_data);
562 } 582 }
563 Dart_TypedData_Type data_type_unused; 583 Dart_TypedData_Type data_type_unused;
564 void* data_location; 584 void* data_location;
(...skipping 12 matching lines...) Expand all
577 Dart_SetReturnValue(args, returned_data); 597 Dart_SetReturnValue(args, returned_data);
578 } 598 }
579 } else { 599 } else {
580 Dart_Handle err = DartUtils::NewDartArgumentError( 600 Dart_Handle err = DartUtils::NewDartArgumentError(
581 "Non-string argument to FileSystemEntity.stat"); 601 "Non-string argument to FileSystemEntity.stat");
582 Dart_SetReturnValue(args, err); 602 Dart_SetReturnValue(args, err);
583 } 603 }
584 } 604 }
585 605
586 void FUNCTION_NAME(File_AreIdentical)(Dart_NativeArguments args) { 606 void FUNCTION_NAME(File_AreIdentical)(Dart_NativeArguments args) {
587 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && 607 Namespace* namespc = Namespace::GetNamespace(args, 0);
588 Dart_IsString(Dart_GetNativeArgument(args, 1))) { 608 if (Dart_IsString(Dart_GetNativeArgument(args, 1)) &&
609 Dart_IsString(Dart_GetNativeArgument(args, 2))) {
589 const char* path_1 = 610 const char* path_1 =
590 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 611 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
591 const char* path_2 = 612 const char* path_2 =
592 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 613 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
593 File::Identical result = File::AreIdentical(path_1, path_2); 614 File::Identical result = File::AreIdentical(namespc, path_1, path_2);
594 if (result == File::kError) { 615 if (result == File::kError) {
595 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 616 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
596 } else { 617 } else {
597 Dart_SetReturnValue(args, Dart_NewBoolean(result == File::kIdentical)); 618 Dart_SetReturnValue(args, Dart_NewBoolean(result == File::kIdentical));
598 } 619 }
599 } else { 620 } else {
600 Dart_Handle err = DartUtils::NewDartArgumentError( 621 Dart_Handle err = DartUtils::NewDartArgumentError(
601 "Non-string argument to FileSystemEntity.identical"); 622 "Non-string argument to FileSystemEntity.identical");
602 Dart_SetReturnValue(args, err); 623 Dart_SetReturnValue(args, err);
603 } 624 }
604 } 625 }
605 626
606 static int64_t CObjectInt32OrInt64ToInt64(CObject* cobject) { 627 static int64_t CObjectInt32OrInt64ToInt64(CObject* cobject) {
607 ASSERT(cobject->IsInt32OrInt64()); 628 ASSERT(cobject->IsInt32OrInt64());
608 int64_t result; 629 int64_t result;
609 if (cobject->IsInt32()) { 630 if (cobject->IsInt32()) {
610 CObjectInt32 value(cobject); 631 CObjectInt32 value(cobject);
611 result = value.Value(); 632 result = value.Value();
612 } else { 633 } else {
613 CObjectInt64 value(cobject); 634 CObjectInt64 value(cobject);
614 result = value.Value(); 635 result = value.Value();
615 } 636 }
616 return result; 637 return result;
617 } 638 }
618 639
619 File* CObjectToFilePointer(CObject* cobject) { 640 static File* CObjectToFilePointer(CObject* cobject) {
620 CObjectIntptr value(cobject); 641 CObjectIntptr value(cobject);
621 return reinterpret_cast<File*>(value.Value()); 642 return reinterpret_cast<File*>(value.Value());
622 } 643 }
623 644
645 static Namespace* CObjectToNamespacePointer(CObject* cobject) {
646 CObjectIntptr value(cobject);
647 return reinterpret_cast<Namespace*>(value.Value());
648 }
649
624 CObject* File::ExistsRequest(const CObjectArray& request) { 650 CObject* File::ExistsRequest(const CObjectArray& request) {
625 if ((request.Length() == 1) && request[0]->IsString()) { 651 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
626 CObjectString filename(request[0]); 652 return CObject::IllegalArgumentError();
627 bool result = File::Exists(filename.CString()); 653 }
628 return CObject::Bool(result); 654 Namespace* namespc = CObjectToNamespacePointer(request[0]);
629 } 655 RefCntReleaseScope<Namespace> rs(namespc);
630 return CObject::IllegalArgumentError(); 656 if ((request.Length() != 2) || !request[1]->IsString()) {
657 return CObject::IllegalArgumentError();
658 }
659 CObjectString filename(request[1]);
660 return CObject::Bool(File::Exists(namespc, filename.CString()));
631 } 661 }
632 662
633 CObject* File::CreateRequest(const CObjectArray& request) { 663 CObject* File::CreateRequest(const CObjectArray& request) {
634 if ((request.Length() == 1) && request[0]->IsString()) { 664 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
635 CObjectString filename(request[0]); 665 return CObject::IllegalArgumentError();
636 bool result = File::Create(filename.CString()); 666 }
637 if (result) { 667 Namespace* namespc = CObjectToNamespacePointer(request[0]);
638 return CObject::True(); 668 RefCntReleaseScope<Namespace> rs(namespc);
639 } else { 669 if ((request.Length() != 2) || !request[1]->IsString()) {
640 return CObject::NewOSError(); 670 return CObject::IllegalArgumentError();
641 } 671 }
642 } 672 CObjectString filename(request[1]);
643 return CObject::IllegalArgumentError(); 673 return File::Create(namespc, filename.CString()) ? CObject::True()
674 : CObject::NewOSError();
644 } 675 }
645 676
646 CObject* File::OpenRequest(const CObjectArray& request) { 677 CObject* File::OpenRequest(const CObjectArray& request) {
647 File* file = NULL; 678 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
648 if ((request.Length() == 2) && request[0]->IsString() && 679 return CObject::IllegalArgumentError();
649 request[1]->IsInt32()) { 680 }
650 CObjectString filename(request[0]); 681 Namespace* namespc = CObjectToNamespacePointer(request[0]);
651 CObjectInt32 mode(request[1]); 682 RefCntReleaseScope<Namespace> rs(namespc);
652 File::DartFileOpenMode dart_file_mode = 683 if ((request.Length() != 3) ||
653 static_cast<File::DartFileOpenMode>(mode.Value()); 684 !request[1]->IsString() ||
654 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); 685 !request[2]->IsInt32()) {
655 file = File::Open(filename.CString(), file_mode); 686 return CObject::IllegalArgumentError();
656 if (file != NULL) { 687 }
657 return new CObjectIntptr( 688 CObjectString filename(request[1]);
658 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); 689 CObjectInt32 mode(request[2]);
659 } else { 690 File::DartFileOpenMode dart_file_mode =
660 return CObject::NewOSError(); 691 static_cast<File::DartFileOpenMode>(mode.Value());
661 } 692 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
662 } 693 File* file = File::Open(namespc, filename.CString(), file_mode);
663 return CObject::IllegalArgumentError(); 694 if (file == NULL) {
695 return CObject::NewOSError();
696 }
697 return new CObjectIntptr(
698 CObject::NewIntptr(reinterpret_cast<intptr_t>(file)));
664 } 699 }
665 700
666 CObject* File::DeleteRequest(const CObjectArray& request) { 701 CObject* File::DeleteRequest(const CObjectArray& request) {
667 if ((request.Length() == 1) && request[0]->IsString()) { 702 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
668 CObjectString filename(request[0]); 703 return CObject::False();
669 bool result = File::Delete(filename.CString()); 704 }
670 if (result) { 705 Namespace* namespc = CObjectToNamespacePointer(request[0]);
671 return CObject::True(); 706 RefCntReleaseScope<Namespace> rs(namespc);
672 } else { 707 if ((request.Length() != 2) || !request[1]->IsString()) {
673 return CObject::NewOSError(); 708 return CObject::False();
674 } 709 }
675 } 710 CObjectString filename(request[1]);
676 return CObject::False(); 711 return File::Delete(namespc, filename.CString()) ? CObject::True()
712 : CObject::NewOSError();
677 } 713 }
678 714
679 CObject* File::RenameRequest(const CObjectArray& request) { 715 CObject* File::RenameRequest(const CObjectArray& request) {
680 if ((request.Length() == 2) && request[0]->IsString() && 716 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
681 request[1]->IsString()) { 717 return CObject::IllegalArgumentError();
682 CObjectString old_path(request[0]); 718 }
683 CObjectString new_path(request[1]); 719 Namespace* namespc = CObjectToNamespacePointer(request[0]);
684 bool completed = File::Rename(old_path.CString(), new_path.CString()); 720 RefCntReleaseScope<Namespace> rs(namespc);
685 if (completed) { 721 if ((request.Length() != 3) ||
686 return CObject::True(); 722 !request[1]->IsString() ||
687 } 723 !request[2]->IsString()) {
688 return CObject::NewOSError(); 724 return CObject::IllegalArgumentError();
689 } 725 }
690 return CObject::IllegalArgumentError(); 726 CObjectString old_path(request[1]);
727 CObjectString new_path(request[2]);
728 return File::Rename(namespc, old_path.CString(), new_path.CString())
729 ? CObject::True()
730 : CObject::NewOSError();
691 } 731 }
692 732
693 CObject* File::CopyRequest(const CObjectArray& request) { 733 CObject* File::CopyRequest(const CObjectArray& request) {
694 if ((request.Length() == 2) && request[0]->IsString() && 734 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
695 request[1]->IsString()) { 735 return CObject::IllegalArgumentError();
696 CObjectString old_path(request[0]); 736 }
697 CObjectString new_path(request[1]); 737 Namespace* namespc = CObjectToNamespacePointer(request[0]);
698 bool completed = File::Copy(old_path.CString(), new_path.CString()); 738 RefCntReleaseScope<Namespace> rs(namespc);
699 if (completed) { 739 if ((request.Length() != 3) ||
700 return CObject::True(); 740 !request[1]->IsString() ||
701 } 741 !request[2]->IsString()) {
702 return CObject::NewOSError(); 742 return CObject::IllegalArgumentError();
703 } 743 }
704 return CObject::IllegalArgumentError(); 744 CObjectString old_path(request[1]);
745 CObjectString new_path(request[2]);
746 return File::Copy(namespc, old_path.CString(), new_path.CString())
747 ? CObject::True()
748 : CObject::NewOSError();
705 } 749 }
706 750
707 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { 751 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) {
708 if ((request.Length() == 1) && request[0]->IsString()) { 752 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
709 CObjectString filename(request[0]); 753 return CObject::IllegalArgumentError();
710 const char* result = File::GetCanonicalPath(filename.CString()); 754 }
711 if (result != NULL) { 755 Namespace* namespc = CObjectToNamespacePointer(request[0]);
712 CObject* path = new CObjectString(CObject::NewString(result)); 756 RefCntReleaseScope<Namespace> rs(namespc);
713 return path; 757 if ((request.Length() != 2) || !request[1]->IsString()) {
714 } else { 758 return CObject::IllegalArgumentError();
715 return CObject::NewOSError(); 759 }
716 } 760 CObjectString filename(request[1]);
717 } 761 const char* result = File::GetCanonicalPath(namespc, filename.CString());
718 return CObject::IllegalArgumentError(); 762 if (result == NULL) {
763 return CObject::NewOSError();
764 }
765 return new CObjectString(CObject::NewString(result));
719 } 766 }
720 767
721 CObject* File::CloseRequest(const CObjectArray& request) { 768 CObject* File::CloseRequest(const CObjectArray& request) {
722 intptr_t return_value = -1; 769 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
723 if ((request.Length() == 1) && request[0]->IsIntptr()) { 770 return new CObjectIntptr(CObject::NewIntptr(-1));
724 File* file = CObjectToFilePointer(request[0]); 771 }
725 RefCntReleaseScope<File> rs(file); 772 File* file = CObjectToFilePointer(request[0]);
726 return_value = 0; 773 RefCntReleaseScope<File> rs(file);
727 // We have retained a reference to the file here. Therefore the file's 774 // We have retained a reference to the file here. Therefore the file's
728 // destructor can't be running. Since no further requests are dispatched by 775 // destructor can't be running. Since no further requests are dispatched by
729 // the Dart code after an async close call, this Close() can't be racing 776 // the Dart code after an async close call, this Close() can't be racing
730 // with any other call on the file. We don't do an extra Release(), and we 777 // with any other call on the file. We don't do an extra Release(), and we
731 // don't delete the weak persistent handle. The file is closed here, but the 778 // don't delete the weak persistent handle. The file is closed here, but the
732 // memory will be cleaned up when the finalizer runs. 779 // memory will be cleaned up when the finalizer runs.
733 ASSERT(!file->IsClosed()); 780 ASSERT(!file->IsClosed());
734 file->Close(); 781 file->Close();
782 return new CObjectIntptr(CObject::NewIntptr(0));
783 }
784
785 CObject* File::PositionRequest(const CObjectArray& request) {
786 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
787 return CObject::IllegalArgumentError();
788 }
789 File* file = CObjectToFilePointer(request[0]);
790 RefCntReleaseScope<File> rs(file);
791 if (file->IsClosed()) {
792 return CObject::FileClosedError();
793 }
794 const intptr_t return_value = file->Position();
795 if (return_value < 0) {
796 return CObject::NewOSError();
735 } 797 }
736 return new CObjectIntptr(CObject::NewIntptr(return_value)); 798 return new CObjectIntptr(CObject::NewIntptr(return_value));
737 } 799 }
738 800
739 CObject* File::PositionRequest(const CObjectArray& request) {
740 if ((request.Length() == 1) && request[0]->IsIntptr()) {
741 File* file = CObjectToFilePointer(request[0]);
742 RefCntReleaseScope<File> rs(file);
743 if (!file->IsClosed()) {
744 intptr_t return_value = file->Position();
745 if (return_value >= 0) {
746 return new CObjectIntptr(CObject::NewIntptr(return_value));
747 } else {
748 return CObject::NewOSError();
749 }
750 } else {
751 return CObject::FileClosedError();
752 }
753 }
754 return CObject::IllegalArgumentError();
755 }
756
757 CObject* File::SetPositionRequest(const CObjectArray& request) { 801 CObject* File::SetPositionRequest(const CObjectArray& request) {
758 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 802 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
759 File* file = CObjectToFilePointer(request[0]); 803 return CObject::IllegalArgumentError();
760 RefCntReleaseScope<File> rs(file); 804 }
761 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 805 File* file = CObjectToFilePointer(request[0]);
762 if (!file->IsClosed()) { 806 RefCntReleaseScope<File> rs(file);
763 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); 807 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
764 if (file->SetPosition(position)) { 808 return CObject::IllegalArgumentError();
765 return CObject::True(); 809 }
766 } else { 810 if (file->IsClosed()) {
767 return CObject::NewOSError(); 811 return CObject::FileClosedError();
768 } 812 }
769 } else { 813 const int64_t position = CObjectInt32OrInt64ToInt64(request[1]);
770 return CObject::FileClosedError(); 814 return file->SetPosition(position) ? CObject::True() : CObject::NewOSError();
771 }
772 } else {
773 return CObject::IllegalArgumentError();
774 }
775 }
776 return CObject::IllegalArgumentError();
777 } 815 }
778 816
779 CObject* File::TruncateRequest(const CObjectArray& request) { 817 CObject* File::TruncateRequest(const CObjectArray& request) {
780 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 818 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
781 File* file = CObjectToFilePointer(request[0]); 819 return CObject::IllegalArgumentError();
782 RefCntReleaseScope<File> rs(file); 820 }
783 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 821 File* file = CObjectToFilePointer(request[0]);
784 if (!file->IsClosed()) { 822 RefCntReleaseScope<File> rs(file);
785 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 823 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
786 if (file->Truncate(length)) { 824 return CObject::IllegalArgumentError();
787 return CObject::True(); 825 }
788 } else { 826 if (file->IsClosed()) {
789 return CObject::NewOSError(); 827 return CObject::FileClosedError();
790 } 828 }
791 } else { 829 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
792 return CObject::FileClosedError(); 830 if (file->Truncate(length)) {
793 } 831 return CObject::True();
794 } else { 832 }
795 return CObject::IllegalArgumentError(); 833 return CObject::NewOSError();
796 }
797 }
798 return CObject::IllegalArgumentError();
799 } 834 }
800 835
801 CObject* File::LengthRequest(const CObjectArray& request) { 836 CObject* File::LengthRequest(const CObjectArray& request) {
802 if ((request.Length() == 1) && request[0]->IsIntptr()) { 837 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
803 File* file = CObjectToFilePointer(request[0]); 838 return CObject::IllegalArgumentError();
804 RefCntReleaseScope<File> rs(file); 839 }
805 if (!file->IsClosed()) { 840 File* file = CObjectToFilePointer(request[0]);
806 int64_t return_value = file->Length(); 841 RefCntReleaseScope<File> rs(file);
807 if (return_value >= 0) { 842 if (file->IsClosed()) {
808 return new CObjectInt64(CObject::NewInt64(return_value)); 843 return CObject::FileClosedError();
809 } else { 844 }
810 return CObject::NewOSError(); 845 const int64_t return_value = file->Length();
811 } 846 if (return_value < 0) {
812 } else { 847 return CObject::NewOSError();
813 return CObject::FileClosedError(); 848 }
814 } 849 return new CObjectInt64(CObject::NewInt64(return_value));
815 }
816 return CObject::IllegalArgumentError();
817 } 850 }
818 851
819 CObject* File::LengthFromPathRequest(const CObjectArray& request) { 852 CObject* File::LengthFromPathRequest(const CObjectArray& request) {
820 if ((request.Length() == 1) && request[0]->IsString()) { 853 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
821 CObjectString filepath(request[0]); 854 return CObject::IllegalArgumentError();
822 int64_t return_value = File::LengthFromPath(filepath.CString()); 855 }
823 if (return_value >= 0) { 856 Namespace* namespc = CObjectToNamespacePointer(request[0]);
824 return new CObjectInt64(CObject::NewInt64(return_value)); 857 RefCntReleaseScope<Namespace> rs(namespc);
825 } else { 858 if ((request.Length() != 2) || !request[1]->IsString()) {
826 return CObject::NewOSError(); 859 return CObject::IllegalArgumentError();
827 } 860 }
828 } 861 CObjectString filepath(request[1]);
829 return CObject::IllegalArgumentError(); 862 const int64_t return_value = File::LengthFromPath(
863 namespc, filepath.CString());
864 if (return_value < 0) {
865 return CObject::NewOSError();
866 }
867 return new CObjectInt64(CObject::NewInt64(return_value));
830 } 868 }
831 869
832 CObject* File::LastAccessedRequest(const CObjectArray& request) { 870 CObject* File::LastAccessedRequest(const CObjectArray& request) {
833 if ((request.Length() == 1) && request[0]->IsString()) { 871 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
834 CObjectString filepath(request[0]); 872 return CObject::IllegalArgumentError();
835 int64_t return_value = File::LastAccessed(filepath.CString()); 873 }
836 if (return_value >= 0) { 874 Namespace* namespc = CObjectToNamespacePointer(request[0]);
837 return new CObjectIntptr( 875 RefCntReleaseScope<Namespace> rs(namespc);
838 CObject::NewInt64(return_value * kMillisecondsPerSecond)); 876 if ((request.Length() != 2) || !request[1]->IsString()) {
839 } else { 877 return CObject::IllegalArgumentError();
840 return CObject::NewOSError(); 878 }
841 } 879 CObjectString filepath(request[1]);
842 } 880 const int64_t return_value = File::LastAccessed(namespc, filepath.CString());
843 return CObject::IllegalArgumentError(); 881 if (return_value < 0) {
882 return CObject::NewOSError();
883 }
884 return new CObjectIntptr(
885 CObject::NewInt64(return_value * kMillisecondsPerSecond));
844 } 886 }
845 887
846 CObject* File::SetLastAccessedRequest(const CObjectArray& request) { 888 CObject* File::SetLastAccessedRequest(const CObjectArray& request) {
847 if ((request.Length() == 2) && request[0]->IsString() && 889 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
848 request[1]->IsInt32OrInt64()) { 890 return CObject::IllegalArgumentError();
849 CObjectString filepath(request[0]); 891 }
850 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); 892 Namespace* namespc = CObjectToNamespacePointer(request[0]);
851 if (File::SetLastAccessed(filepath.CString(), millis)) { 893 RefCntReleaseScope<Namespace> rs(namespc);
852 return CObject::Null(); 894 if ((request.Length() != 3) ||
853 } else { 895 !request[1]->IsString() ||
854 return CObject::NewOSError(); 896 !request[2]->IsInt32OrInt64()) {
855 } 897 return CObject::IllegalArgumentError();
856 } 898 }
857 return CObject::IllegalArgumentError(); 899 CObjectString filepath(request[1]);
900 const int64_t millis = CObjectInt32OrInt64ToInt64(request[2]);
901 return File::SetLastAccessed(namespc, filepath.CString(), millis)
902 ? CObject::Null()
903 : CObject::NewOSError();
858 } 904 }
859 905
860 CObject* File::LastModifiedRequest(const CObjectArray& request) { 906 CObject* File::LastModifiedRequest(const CObjectArray& request) {
861 if ((request.Length() == 1) && request[0]->IsString()) { 907 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
862 CObjectString filepath(request[0]); 908 return CObject::IllegalArgumentError();
863 int64_t return_value = File::LastModified(filepath.CString()); 909 }
864 if (return_value >= 0) { 910 Namespace* namespc = CObjectToNamespacePointer(request[0]);
865 return new CObjectIntptr( 911 RefCntReleaseScope<Namespace> rs(namespc);
866 CObject::NewInt64(return_value * kMillisecondsPerSecond)); 912 if ((request.Length() != 2) || !request[1]->IsString()) {
867 } else { 913 return CObject::IllegalArgumentError();
868 return CObject::NewOSError(); 914 }
869 } 915 CObjectString filepath(request[1]);
870 } 916 const int64_t return_value = File::LastModified(namespc, filepath.CString());
871 return CObject::IllegalArgumentError(); 917 if (return_value < 0) {
918 return CObject::NewOSError();
919 }
920 return new CObjectIntptr(
921 CObject::NewInt64(return_value * kMillisecondsPerSecond));
872 } 922 }
873 923
874 CObject* File::SetLastModifiedRequest(const CObjectArray& request) { 924 CObject* File::SetLastModifiedRequest(const CObjectArray& request) {
875 if ((request.Length() == 2) && request[0]->IsString() && 925 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
876 request[1]->IsInt32OrInt64()) { 926 return CObject::IllegalArgumentError();
877 CObjectString filepath(request[0]); 927 }
878 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); 928 Namespace* namespc = CObjectToNamespacePointer(request[0]);
879 if (File::SetLastModified(filepath.CString(), millis)) { 929 RefCntReleaseScope<Namespace> rs(namespc);
880 return CObject::Null(); 930 if ((request.Length() != 3) ||
881 } else { 931 !request[1]->IsString() ||
882 return CObject::NewOSError(); 932 !request[2]->IsInt32OrInt64()) {
883 } 933 return CObject::IllegalArgumentError();
884 } 934 }
885 return CObject::IllegalArgumentError(); 935 CObjectString filepath(request[1]);
936 const int64_t millis = CObjectInt32OrInt64ToInt64(request[2]);
937 return File::SetLastModified(namespc, filepath.CString(), millis)
938 ? CObject::Null()
939 : CObject::NewOSError();
886 } 940 }
887 941
888 CObject* File::FlushRequest(const CObjectArray& request) { 942 CObject* File::FlushRequest(const CObjectArray& request) {
889 if ((request.Length() == 1) && request[0]->IsIntptr()) { 943 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
890 File* file = CObjectToFilePointer(request[0]); 944 return CObject::IllegalArgumentError();
891 RefCntReleaseScope<File> rs(file); 945 }
892 if (!file->IsClosed()) { 946 File* file = CObjectToFilePointer(request[0]);
893 if (file->Flush()) { 947 RefCntReleaseScope<File> rs(file);
894 return CObject::True(); 948 if (file->IsClosed()) {
895 } else { 949 return CObject::FileClosedError();
896 return CObject::NewOSError(); 950 }
897 } 951 return file->Flush() ? CObject::True() : CObject::NewOSError();
898 } else {
899 return CObject::FileClosedError();
900 }
901 }
902 return CObject::IllegalArgumentError();
903 } 952 }
904 953
905 CObject* File::ReadByteRequest(const CObjectArray& request) { 954 CObject* File::ReadByteRequest(const CObjectArray& request) {
906 if ((request.Length() == 1) && request[0]->IsIntptr()) { 955 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
907 File* file = CObjectToFilePointer(request[0]); 956 return CObject::IllegalArgumentError();
908 RefCntReleaseScope<File> rs(file); 957 }
909 if (!file->IsClosed()) { 958 File* file = CObjectToFilePointer(request[0]);
910 uint8_t buffer; 959 RefCntReleaseScope<File> rs(file);
911 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); 960 if (file->IsClosed()) {
912 if (bytes_read > 0) { 961 return CObject::FileClosedError();
913 return new CObjectIntptr(CObject::NewIntptr(buffer)); 962 }
914 } else if (bytes_read == 0) { 963 uint8_t buffer;
915 return new CObjectIntptr(CObject::NewIntptr(-1)); 964 const int64_t bytes_read = file->Read(
916 } else { 965 reinterpret_cast<void*>(&buffer), 1);
917 return CObject::NewOSError(); 966 if (bytes_read < 0) {
918 } 967 return CObject::NewOSError();
919 } else { 968 }
920 return CObject::FileClosedError(); 969 if (bytes_read == 0) {
921 } 970 return new CObjectIntptr(CObject::NewIntptr(-1));
922 } 971 }
923 return CObject::IllegalArgumentError(); 972 return new CObjectIntptr(CObject::NewIntptr(buffer));
924 } 973 }
925 974
926 CObject* File::WriteByteRequest(const CObjectArray& request) { 975 CObject* File::WriteByteRequest(const CObjectArray& request) {
927 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 976 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
928 File* file = CObjectToFilePointer(request[0]); 977 return CObject::IllegalArgumentError();
929 RefCntReleaseScope<File> rs(file); 978 }
930 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 979 File* file = CObjectToFilePointer(request[0]);
931 if (!file->IsClosed()) { 980 RefCntReleaseScope<File> rs(file);
932 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); 981 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
933 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); 982 return CObject::IllegalArgumentError();
934 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); 983 }
935 if (success) { 984 if (file->IsClosed()) {
936 return new CObjectInt64(CObject::NewInt64(1)); 985 return CObject::FileClosedError();
937 } else { 986 }
938 return CObject::NewOSError(); 987 const int64_t byte = CObjectInt32OrInt64ToInt64(request[1]);
939 } 988 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
940 } else { 989 return file->WriteFully(reinterpret_cast<void*>(&buffer), 1)
941 return CObject::FileClosedError(); 990 ? new CObjectInt64(CObject::NewInt64(1))
942 } 991 : CObject::NewOSError();
943 } else {
944 return CObject::IllegalArgumentError();
945 }
946 }
947 return CObject::IllegalArgumentError();
948 } 992 }
949 993
950 CObject* File::ReadRequest(const CObjectArray& request) { 994 CObject* File::ReadRequest(const CObjectArray& request) {
951 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 995 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
952 File* file = CObjectToFilePointer(request[0]); 996 return CObject::IllegalArgumentError();
953 RefCntReleaseScope<File> rs(file); 997 }
954 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 998 File* file = CObjectToFilePointer(request[0]);
955 if (!file->IsClosed()) { 999 RefCntReleaseScope<File> rs(file);
956 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 1000 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
957 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 1001 return CObject::IllegalArgumentError();
958 ASSERT(io_buffer != NULL); 1002 }
959 uint8_t* data = io_buffer->value.as_external_typed_data.data; 1003 if (file->IsClosed()) {
960 int64_t bytes_read = file->Read(data, length); 1004 return CObject::FileClosedError();
961 if (bytes_read >= 0) { 1005 }
962 CObjectExternalUint8Array* external_array = 1006 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
963 new CObjectExternalUint8Array(io_buffer); 1007 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
964 external_array->SetLength(bytes_read); 1008 ASSERT(io_buffer != NULL);
965 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); 1009 uint8_t* data = io_buffer->value.as_external_typed_data.data;
966 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 1010 const int64_t bytes_read = file->Read(data, length);
967 result->SetAt(1, external_array); 1011 if (bytes_read < 0) {
968 return result; 1012 CObject::FreeIOBufferData(io_buffer);
969 } else { 1013 return CObject::NewOSError();
970 CObject::FreeIOBufferData(io_buffer); 1014 }
971 return CObject::NewOSError(); 1015 CObjectExternalUint8Array* external_array =
972 } 1016 new CObjectExternalUint8Array(io_buffer);
973 } else { 1017 external_array->SetLength(bytes_read);
974 return CObject::FileClosedError(); 1018 CObjectArray* result = new CObjectArray(CObject::NewArray(2));
975 } 1019 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
976 } else { 1020 result->SetAt(1, external_array);
977 return CObject::IllegalArgumentError(); 1021 return result;
978 }
979 }
980 return CObject::IllegalArgumentError();
981 } 1022 }
982 1023
983 CObject* File::ReadIntoRequest(const CObjectArray& request) { 1024 CObject* File::ReadIntoRequest(const CObjectArray& request) {
984 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1025 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
985 File* file = CObjectToFilePointer(request[0]); 1026 return CObject::IllegalArgumentError();
986 RefCntReleaseScope<File> rs(file); 1027 }
987 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 1028 File* file = CObjectToFilePointer(request[0]);
988 if (!file->IsClosed()) { 1029 RefCntReleaseScope<File> rs(file);
989 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 1030 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
990 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 1031 return CObject::IllegalArgumentError();
991 ASSERT(io_buffer != NULL); 1032 }
992 uint8_t* data = io_buffer->value.as_external_typed_data.data; 1033 if (file->IsClosed()) {
993 int64_t bytes_read = file->Read(data, length); 1034 return CObject::FileClosedError();
994 if (bytes_read >= 0) { 1035 }
995 CObjectExternalUint8Array* external_array = 1036 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
996 new CObjectExternalUint8Array(io_buffer); 1037 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
997 external_array->SetLength(bytes_read); 1038 ASSERT(io_buffer != NULL);
998 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 1039 uint8_t* data = io_buffer->value.as_external_typed_data.data;
999 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 1040 const int64_t bytes_read = file->Read(data, length);
1000 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); 1041 if (bytes_read < 0) {
1001 result->SetAt(2, external_array); 1042 CObject::FreeIOBufferData(io_buffer);
1002 return result; 1043 return CObject::NewOSError();
1003 } else { 1044 }
1004 CObject::FreeIOBufferData(io_buffer); 1045 CObjectExternalUint8Array* external_array =
1005 return CObject::NewOSError(); 1046 new CObjectExternalUint8Array(io_buffer);
1006 } 1047 external_array->SetLength(bytes_read);
1007 } else { 1048 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
1008 return CObject::FileClosedError(); 1049 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
1009 } 1050 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read)));
1010 } else { 1051 result->SetAt(2, external_array);
1011 return CObject::IllegalArgumentError(); 1052 return result;
1012 }
1013 }
1014 return CObject::IllegalArgumentError();
1015 } 1053 }
1016 1054
1017 static int SizeInBytes(Dart_TypedData_Type type) { 1055 static int SizeInBytes(Dart_TypedData_Type type) {
1018 switch (type) { 1056 switch (type) {
1019 case Dart_TypedData_kInt8: 1057 case Dart_TypedData_kInt8:
1020 case Dart_TypedData_kUint8: 1058 case Dart_TypedData_kUint8:
1021 case Dart_TypedData_kUint8Clamped: 1059 case Dart_TypedData_kUint8Clamped:
1022 return 1; 1060 return 1;
1023 case Dart_TypedData_kInt16: 1061 case Dart_TypedData_kInt16:
1024 case Dart_TypedData_kUint16: 1062 case Dart_TypedData_kUint16:
1025 return 2; 1063 return 2;
1026 case Dart_TypedData_kInt32: 1064 case Dart_TypedData_kInt32:
1027 case Dart_TypedData_kUint32: 1065 case Dart_TypedData_kUint32:
1028 case Dart_TypedData_kFloat32: 1066 case Dart_TypedData_kFloat32:
1029 return 4; 1067 return 4;
1030 case Dart_TypedData_kInt64: 1068 case Dart_TypedData_kInt64:
1031 case Dart_TypedData_kUint64: 1069 case Dart_TypedData_kUint64:
1032 case Dart_TypedData_kFloat64: 1070 case Dart_TypedData_kFloat64:
1033 return 8; 1071 return 8;
1034 default: 1072 default:
1035 break; 1073 break;
1036 } 1074 }
1037 UNREACHABLE(); 1075 UNREACHABLE();
1038 return -1; 1076 return -1;
1039 } 1077 }
1040 1078
1041 CObject* File::WriteFromRequest(const CObjectArray& request) { 1079 CObject* File::WriteFromRequest(const CObjectArray& request) {
1042 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1080 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1043 File* file = CObjectToFilePointer(request[0]); 1081 return CObject::IllegalArgumentError();
1044 RefCntReleaseScope<File> rs(file); 1082 }
1045 if ((request.Length() == 4) && 1083 File* file = CObjectToFilePointer(request[0]);
1046 (request[1]->IsTypedData() || request[1]->IsArray()) && 1084 RefCntReleaseScope<File> rs(file);
1047 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { 1085 if ((request.Length() != 4) ||
1048 if (!file->IsClosed()) { 1086 (!request[1]->IsTypedData() && !request[1]->IsArray()) ||
1049 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); 1087 !request[2]->IsInt32OrInt64() || !request[3]->IsInt32OrInt64()) {
1050 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); 1088 return CObject::IllegalArgumentError();
1051 int64_t length = end - start; 1089 }
1052 uint8_t* buffer_start; 1090 if (file->IsClosed()) {
1053 if (request[1]->IsTypedData()) { 1091 return CObject::FileClosedError();
1054 CObjectTypedData typed_data(request[1]); 1092 }
1055 start = start * SizeInBytes(typed_data.Type()); 1093 int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
1056 length = length * SizeInBytes(typed_data.Type()); 1094 int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
1057 buffer_start = typed_data.Buffer() + start; 1095 int64_t length = end - start;
1058 } else { 1096 uint8_t* buffer_start;
1059 CObjectArray array(request[1]); 1097 if (request[1]->IsTypedData()) {
1060 buffer_start = Dart_ScopeAllocate(length); 1098 CObjectTypedData typed_data(request[1]);
1061 for (int i = 0; i < length; i++) { 1099 start = start * SizeInBytes(typed_data.Type());
1062 if (array[i + start]->IsInt32OrInt64()) { 1100 length = length * SizeInBytes(typed_data.Type());
1063 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); 1101 buffer_start = typed_data.Buffer() + start;
1064 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); 1102 } else {
1065 } else { 1103 CObjectArray array(request[1]);
1066 // Unsupported type. 1104 buffer_start = Dart_ScopeAllocate(length);
1067 return CObject::IllegalArgumentError(); 1105 for (int i = 0; i < length; i++) {
1068 } 1106 if (array[i + start]->IsInt32OrInt64()) {
1069 } 1107 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]);
1070 start = 0; 1108 buffer_start[i] = static_cast<uint8_t>(value & 0xFF);
1071 }
1072 bool success =
1073 file->WriteFully(reinterpret_cast<void*>(buffer_start), length);
1074 if (success) {
1075 return new CObjectInt64(CObject::NewInt64(length));
1076 } else {
1077 return CObject::NewOSError();
1078 }
1079 } else { 1109 } else {
1080 return CObject::FileClosedError(); 1110 // Unsupported type.
1111 return CObject::IllegalArgumentError();
1081 } 1112 }
1082 } else {
1083 return CObject::IllegalArgumentError();
1084 } 1113 }
1114 start = 0;
1085 } 1115 }
1086 return CObject::IllegalArgumentError(); 1116 return file->WriteFully(reinterpret_cast<void*>(buffer_start), length)
1117 ? new CObjectInt64(CObject::NewInt64(length))
1118 : CObject::NewOSError();
1087 } 1119 }
1088 1120
1089 CObject* File::CreateLinkRequest(const CObjectArray& request) { 1121 CObject* File::CreateLinkRequest(const CObjectArray& request) {
1090 if ((request.Length() != 2) || !request[0]->IsString() || 1122 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1091 !request[1]->IsString()) {
1092 return CObject::IllegalArgumentError(); 1123 return CObject::IllegalArgumentError();
1093 } 1124 }
1094 CObjectString link_name(request[0]); 1125 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1095 CObjectString target_name(request[1]); 1126 RefCntReleaseScope<Namespace> rs(namespc);
1096 if (File::CreateLink(link_name.CString(), target_name.CString())) { 1127 if (!request[1]->IsString() || !request[2]->IsString()) {
1097 return CObject::True(); 1128 return CObject::IllegalArgumentError();
1098 } else {
1099 return CObject::NewOSError();
1100 } 1129 }
1130 CObjectString link_name(request[1]);
1131 CObjectString target_name(request[2]);
1132 return File::CreateLink(namespc, link_name.CString(), target_name.CString())
1133 ? CObject::True()
1134 : CObject::NewOSError();
1101 } 1135 }
1102 1136
1103 CObject* File::DeleteLinkRequest(const CObjectArray& request) { 1137 CObject* File::DeleteLinkRequest(const CObjectArray& request) {
1104 if ((request.Length() == 1) && request[0]->IsString()) { 1138 if ((request.Length() != 2) || !request[0]->IsIntptr()) {
1105 CObjectString link_path(request[0]); 1139 return CObject::IllegalArgumentError();
1106 bool result = File::DeleteLink(link_path.CString());
1107 if (result) {
1108 return CObject::True();
1109 } else {
1110 return CObject::NewOSError();
1111 }
1112 } 1140 }
1113 return CObject::IllegalArgumentError(); 1141 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1142 RefCntReleaseScope<Namespace> rs(namespc);
1143 if (!request[1]->IsString()) {
1144 return CObject::IllegalArgumentError();
1145 }
1146 CObjectString link_path(request[1]);
1147 return File::DeleteLink(namespc, link_path.CString()) ? CObject::True()
1148 : CObject::NewOSError();
1114 } 1149 }
1115 1150
1116 CObject* File::RenameLinkRequest(const CObjectArray& request) { 1151 CObject* File::RenameLinkRequest(const CObjectArray& request) {
1117 if ((request.Length() == 2) && request[0]->IsString() && 1152 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1118 request[1]->IsString()) { 1153 return CObject::IllegalArgumentError();
1119 CObjectString old_path(request[0]);
1120 CObjectString new_path(request[1]);
1121 bool completed = File::RenameLink(old_path.CString(), new_path.CString());
1122 if (completed) {
1123 return CObject::True();
1124 }
1125 return CObject::NewOSError();
1126 } 1154 }
1127 return CObject::IllegalArgumentError(); 1155 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1156 RefCntReleaseScope<Namespace> rs(namespc);
1157 if (!request[1]->IsString() || !request[2]->IsString()) {
1158 return CObject::IllegalArgumentError();
1159 }
1160 CObjectString old_path(request[1]);
1161 CObjectString new_path(request[2]);
1162 return File::RenameLink(namespc, old_path.CString(), new_path.CString())
1163 ? CObject::True()
1164 : CObject::NewOSError();
1128 } 1165 }
1129 1166
1130 CObject* File::LinkTargetRequest(const CObjectArray& request) { 1167 CObject* File::LinkTargetRequest(const CObjectArray& request) {
1131 if ((request.Length() == 1) && request[0]->IsString()) { 1168 if ((request.Length() != 2) || !request[0]->IsIntptr()) {
1132 CObjectString link_path(request[0]); 1169 return CObject::IllegalArgumentError();
1133 const char* target = File::LinkTarget(link_path.CString());
1134 if (target != NULL) {
1135 CObject* result = new CObjectString(CObject::NewString(target));
1136 return result;
1137 } else {
1138 return CObject::NewOSError();
1139 }
1140 } 1170 }
1141 return CObject::IllegalArgumentError(); 1171 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1172 RefCntReleaseScope<Namespace> rs(namespc);
1173 if (!request[1]->IsString()) {
1174 return CObject::IllegalArgumentError();
1175 }
1176 CObjectString link_path(request[1]);
1177 const char* target = File::LinkTarget(namespc, link_path.CString());
1178 if (target == NULL) {
1179 return CObject::NewOSError();
1180 }
1181 return new CObjectString(CObject::NewString(target));
1142 } 1182 }
1143 1183
1144 CObject* File::TypeRequest(const CObjectArray& request) { 1184 CObject* File::TypeRequest(const CObjectArray& request) {
1145 if ((request.Length() == 2) && request[0]->IsString() && 1185 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1146 request[1]->IsBool()) { 1186 return CObject::IllegalArgumentError();
1147 CObjectString path(request[0]);
1148 CObjectBool follow_links(request[1]);
1149 File::Type type = File::GetType(path.CString(), follow_links.Value());
1150 return new CObjectInt32(CObject::NewInt32(type));
1151 } 1187 }
1152 return CObject::IllegalArgumentError(); 1188 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1189 RefCntReleaseScope<Namespace> rs(namespc);
1190 if (!request[1]->IsString() || !request[2]->IsBool()) {
1191 return CObject::IllegalArgumentError();
1192 }
1193 CObjectString path(request[1]);
1194 CObjectBool follow_links(request[2]);
1195 File::Type type = File::GetType(
1196 namespc, path.CString(), follow_links.Value());
1197 return new CObjectInt32(CObject::NewInt32(type));
1153 } 1198 }
1154 1199
1155 CObject* File::IdenticalRequest(const CObjectArray& request) { 1200 CObject* File::IdenticalRequest(const CObjectArray& request) {
1156 if ((request.Length() == 2) && request[0]->IsString() && 1201 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1157 request[1]->IsString()) { 1202 return CObject::IllegalArgumentError();
1158 CObjectString path1(request[0]);
1159 CObjectString path2(request[1]);
1160 File::Identical result =
1161 File::AreIdentical(path1.CString(), path2.CString());
1162 if (result == File::kError) {
1163 return CObject::NewOSError();
1164 } else if (result == File::kIdentical) {
1165 return CObject::True();
1166 } else {
1167 return CObject::False();
1168 }
1169 } 1203 }
1170 return CObject::IllegalArgumentError(); 1204 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1205 RefCntReleaseScope<Namespace> rs(namespc);
1206 if (!request[1]->IsString() || !request[2]->IsString()) {
1207 return CObject::IllegalArgumentError();
1208 }
1209 CObjectString path1(request[1]);
1210 CObjectString path2(request[2]);
1211 File::Identical result = File::AreIdentical(
1212 namespc, path1.CString(), path2.CString());
1213 if (result == File::kError) {
1214 return CObject::NewOSError();
1215 }
1216 return (result == File::kIdentical) ? CObject::True() : CObject::False();
1171 } 1217 }
1172 1218
1173 CObject* File::StatRequest(const CObjectArray& request) { 1219 CObject* File::StatRequest(const CObjectArray& request) {
1174 if ((request.Length() == 1) && request[0]->IsString()) { 1220 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1175 int64_t data[File::kStatSize]; 1221 return CObject::IllegalArgumentError();
1176 CObjectString path(request[0]);
1177 File::Stat(path.CString(), data);
1178 if (data[File::kType] == File::kDoesNotExist) {
1179 return CObject::NewOSError();
1180 }
1181 CObjectArray* result = new CObjectArray(CObject::NewArray(File::kStatSize));
1182 for (int i = 0; i < File::kStatSize; ++i) {
1183 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i])));
1184 }
1185 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2));
1186 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess)));
1187 wrapper->SetAt(1, result);
1188 return wrapper;
1189 } 1222 }
1190 return CObject::IllegalArgumentError(); 1223 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1224 RefCntReleaseScope<Namespace> rs(namespc);
1225 if ((request.Length() != 2) || !request[1]->IsString()) {
1226 return CObject::IllegalArgumentError();
1227 }
1228 int64_t data[File::kStatSize];
1229 CObjectString path(request[1]);
1230 File::Stat(namespc, path.CString(), data);
1231 if (data[File::kType] == File::kDoesNotExist) {
1232 return CObject::NewOSError();
1233 }
1234 CObjectArray* result = new CObjectArray(CObject::NewArray(File::kStatSize));
1235 for (int i = 0; i < File::kStatSize; ++i) {
1236 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i])));
1237 }
1238 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2));
1239 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess)));
1240 wrapper->SetAt(1, result);
1241 return wrapper;
1191 } 1242 }
1192 1243
1193 CObject* File::LockRequest(const CObjectArray& request) { 1244 CObject* File::LockRequest(const CObjectArray& request) {
1194 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1245 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1195 File* file = CObjectToFilePointer(request[0]); 1246 return CObject::IllegalArgumentError();
1196 RefCntReleaseScope<File> rs(file);
1197 if ((request.Length() == 4) && request[1]->IsInt32OrInt64() &&
1198 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) {
1199 if (!file->IsClosed()) {
1200 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]);
1201 int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
1202 int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
1203 if (file->Lock(static_cast<File::LockType>(lock), start, end)) {
1204 return CObject::True();
1205 } else {
1206 return CObject::NewOSError();
1207 }
1208 } else {
1209 return CObject::FileClosedError();
1210 }
1211 } else {
1212 return CObject::IllegalArgumentError();
1213 }
1214 } 1247 }
1215 return CObject::IllegalArgumentError(); 1248 File* file = CObjectToFilePointer(request[0]);
1249 RefCntReleaseScope<File> rs(file);
1250 if ((request.Length() != 4) || !request[1]->IsInt32OrInt64() ||
1251 !request[2]->IsInt32OrInt64() || !request[3]->IsInt32OrInt64()) {
1252 return CObject::IllegalArgumentError();
1253 }
1254 if (file->IsClosed()) {
1255 return CObject::FileClosedError();
1256 }
1257 const int64_t lock = CObjectInt32OrInt64ToInt64(request[1]);
1258 const int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
1259 const int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
1260 return file->Lock(static_cast<File::LockType>(lock), start, end)
1261 ? CObject::True()
1262 : CObject::NewOSError();
1216 } 1263 }
1217 1264
1218 } // namespace bin 1265 } // namespace bin
1219 } // namespace dart 1266 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698