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

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

Issue 3007703002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Remove namespace_unsupported.cc 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(
29 reinterpret_cast<intptr_t*>(&file))); 30 dart_this, kFileNativeFieldIndex, 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) || !request[1]->IsString() ||
653 static_cast<File::DartFileOpenMode>(mode.Value()); 684 !request[2]->IsInt32()) {
654 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); 685 return CObject::IllegalArgumentError();
655 file = File::Open(filename.CString(), file_mode); 686 }
656 if (file != NULL) { 687 CObjectString filename(request[1]);
657 return new CObjectIntptr( 688 CObjectInt32 mode(request[2]);
658 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); 689 File::DartFileOpenMode dart_file_mode =
659 } else { 690 static_cast<File::DartFileOpenMode>(mode.Value());
660 return CObject::NewOSError(); 691 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
661 } 692 File* file = File::Open(namespc, filename.CString(), file_mode);
662 } 693 if (file == NULL) {
663 return CObject::IllegalArgumentError(); 694 return CObject::NewOSError();
695 }
696 return new CObjectIntptr(
697 CObject::NewIntptr(reinterpret_cast<intptr_t>(file)));
664 } 698 }
665 699
666 CObject* File::DeleteRequest(const CObjectArray& request) { 700 CObject* File::DeleteRequest(const CObjectArray& request) {
667 if ((request.Length() == 1) && request[0]->IsString()) { 701 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
668 CObjectString filename(request[0]); 702 return CObject::False();
669 bool result = File::Delete(filename.CString()); 703 }
670 if (result) { 704 Namespace* namespc = CObjectToNamespacePointer(request[0]);
671 return CObject::True(); 705 RefCntReleaseScope<Namespace> rs(namespc);
672 } else { 706 if ((request.Length() != 2) || !request[1]->IsString()) {
673 return CObject::NewOSError(); 707 return CObject::False();
674 } 708 }
675 } 709 CObjectString filename(request[1]);
676 return CObject::False(); 710 return File::Delete(namespc, filename.CString()) ? CObject::True()
711 : CObject::NewOSError();
677 } 712 }
678 713
679 CObject* File::RenameRequest(const CObjectArray& request) { 714 CObject* File::RenameRequest(const CObjectArray& request) {
680 if ((request.Length() == 2) && request[0]->IsString() && 715 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
681 request[1]->IsString()) { 716 return CObject::IllegalArgumentError();
682 CObjectString old_path(request[0]); 717 }
683 CObjectString new_path(request[1]); 718 Namespace* namespc = CObjectToNamespacePointer(request[0]);
684 bool completed = File::Rename(old_path.CString(), new_path.CString()); 719 RefCntReleaseScope<Namespace> rs(namespc);
685 if (completed) { 720 if ((request.Length() != 3) || !request[1]->IsString() ||
686 return CObject::True(); 721 !request[2]->IsString()) {
687 } 722 return CObject::IllegalArgumentError();
688 return CObject::NewOSError(); 723 }
689 } 724 CObjectString old_path(request[1]);
690 return CObject::IllegalArgumentError(); 725 CObjectString new_path(request[2]);
726 return File::Rename(namespc, old_path.CString(), new_path.CString())
727 ? CObject::True()
728 : CObject::NewOSError();
691 } 729 }
692 730
693 CObject* File::CopyRequest(const CObjectArray& request) { 731 CObject* File::CopyRequest(const CObjectArray& request) {
694 if ((request.Length() == 2) && request[0]->IsString() && 732 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
695 request[1]->IsString()) { 733 return CObject::IllegalArgumentError();
696 CObjectString old_path(request[0]); 734 }
697 CObjectString new_path(request[1]); 735 Namespace* namespc = CObjectToNamespacePointer(request[0]);
698 bool completed = File::Copy(old_path.CString(), new_path.CString()); 736 RefCntReleaseScope<Namespace> rs(namespc);
699 if (completed) { 737 if ((request.Length() != 3) || !request[1]->IsString() ||
700 return CObject::True(); 738 !request[2]->IsString()) {
701 } 739 return CObject::IllegalArgumentError();
702 return CObject::NewOSError(); 740 }
703 } 741 CObjectString old_path(request[1]);
704 return CObject::IllegalArgumentError(); 742 CObjectString new_path(request[2]);
743 return File::Copy(namespc, old_path.CString(), new_path.CString())
744 ? CObject::True()
745 : CObject::NewOSError();
705 } 746 }
706 747
707 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { 748 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) {
708 if ((request.Length() == 1) && request[0]->IsString()) { 749 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
709 CObjectString filename(request[0]); 750 return CObject::IllegalArgumentError();
710 const char* result = File::GetCanonicalPath(filename.CString()); 751 }
711 if (result != NULL) { 752 Namespace* namespc = CObjectToNamespacePointer(request[0]);
712 CObject* path = new CObjectString(CObject::NewString(result)); 753 RefCntReleaseScope<Namespace> rs(namespc);
713 return path; 754 if ((request.Length() != 2) || !request[1]->IsString()) {
714 } else { 755 return CObject::IllegalArgumentError();
715 return CObject::NewOSError(); 756 }
716 } 757 CObjectString filename(request[1]);
717 } 758 const char* result = File::GetCanonicalPath(namespc, filename.CString());
718 return CObject::IllegalArgumentError(); 759 if (result == NULL) {
760 return CObject::NewOSError();
761 }
762 return new CObjectString(CObject::NewString(result));
719 } 763 }
720 764
721 CObject* File::CloseRequest(const CObjectArray& request) { 765 CObject* File::CloseRequest(const CObjectArray& request) {
722 intptr_t return_value = -1; 766 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
723 if ((request.Length() == 1) && request[0]->IsIntptr()) { 767 return new CObjectIntptr(CObject::NewIntptr(-1));
724 File* file = CObjectToFilePointer(request[0]); 768 }
725 RefCntReleaseScope<File> rs(file); 769 File* file = CObjectToFilePointer(request[0]);
726 return_value = 0; 770 RefCntReleaseScope<File> rs(file);
727 // We have retained a reference to the file here. Therefore the file's 771 // 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 772 // 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 773 // 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 774 // 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 775 // don't delete the weak persistent handle. The file is closed here, but the
732 // memory will be cleaned up when the finalizer runs. 776 // memory will be cleaned up when the finalizer runs.
733 ASSERT(!file->IsClosed()); 777 ASSERT(!file->IsClosed());
734 file->Close(); 778 file->Close();
779 return new CObjectIntptr(CObject::NewIntptr(0));
780 }
781
782 CObject* File::PositionRequest(const CObjectArray& request) {
783 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
784 return CObject::IllegalArgumentError();
785 }
786 File* file = CObjectToFilePointer(request[0]);
787 RefCntReleaseScope<File> rs(file);
788 if (file->IsClosed()) {
789 return CObject::FileClosedError();
790 }
791 const intptr_t return_value = file->Position();
792 if (return_value < 0) {
793 return CObject::NewOSError();
735 } 794 }
736 return new CObjectIntptr(CObject::NewIntptr(return_value)); 795 return new CObjectIntptr(CObject::NewIntptr(return_value));
737 } 796 }
738 797
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) { 798 CObject* File::SetPositionRequest(const CObjectArray& request) {
758 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 799 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
759 File* file = CObjectToFilePointer(request[0]); 800 return CObject::IllegalArgumentError();
760 RefCntReleaseScope<File> rs(file); 801 }
761 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 802 File* file = CObjectToFilePointer(request[0]);
762 if (!file->IsClosed()) { 803 RefCntReleaseScope<File> rs(file);
763 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); 804 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
764 if (file->SetPosition(position)) { 805 return CObject::IllegalArgumentError();
765 return CObject::True(); 806 }
766 } else { 807 if (file->IsClosed()) {
767 return CObject::NewOSError(); 808 return CObject::FileClosedError();
768 } 809 }
769 } else { 810 const int64_t position = CObjectInt32OrInt64ToInt64(request[1]);
770 return CObject::FileClosedError(); 811 return file->SetPosition(position) ? CObject::True() : CObject::NewOSError();
771 }
772 } else {
773 return CObject::IllegalArgumentError();
774 }
775 }
776 return CObject::IllegalArgumentError();
777 } 812 }
778 813
779 CObject* File::TruncateRequest(const CObjectArray& request) { 814 CObject* File::TruncateRequest(const CObjectArray& request) {
780 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 815 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
781 File* file = CObjectToFilePointer(request[0]); 816 return CObject::IllegalArgumentError();
782 RefCntReleaseScope<File> rs(file); 817 }
783 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 818 File* file = CObjectToFilePointer(request[0]);
784 if (!file->IsClosed()) { 819 RefCntReleaseScope<File> rs(file);
785 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 820 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
786 if (file->Truncate(length)) { 821 return CObject::IllegalArgumentError();
787 return CObject::True(); 822 }
788 } else { 823 if (file->IsClosed()) {
789 return CObject::NewOSError(); 824 return CObject::FileClosedError();
790 } 825 }
791 } else { 826 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
792 return CObject::FileClosedError(); 827 if (file->Truncate(length)) {
793 } 828 return CObject::True();
794 } else { 829 }
795 return CObject::IllegalArgumentError(); 830 return CObject::NewOSError();
796 }
797 }
798 return CObject::IllegalArgumentError();
799 } 831 }
800 832
801 CObject* File::LengthRequest(const CObjectArray& request) { 833 CObject* File::LengthRequest(const CObjectArray& request) {
802 if ((request.Length() == 1) && request[0]->IsIntptr()) { 834 if ((request.Length() != 1) || !request[0]->IsIntptr()) {
803 File* file = CObjectToFilePointer(request[0]); 835 return CObject::IllegalArgumentError();
804 RefCntReleaseScope<File> rs(file); 836 }
805 if (!file->IsClosed()) { 837 File* file = CObjectToFilePointer(request[0]);
806 int64_t return_value = file->Length(); 838 RefCntReleaseScope<File> rs(file);
807 if (return_value >= 0) { 839 if (file->IsClosed()) {
808 return new CObjectInt64(CObject::NewInt64(return_value)); 840 return CObject::FileClosedError();
809 } else { 841 }
810 return CObject::NewOSError(); 842 const int64_t return_value = file->Length();
811 } 843 if (return_value < 0) {
812 } else { 844 return CObject::NewOSError();
813 return CObject::FileClosedError(); 845 }
814 } 846 return new CObjectInt64(CObject::NewInt64(return_value));
815 }
816 return CObject::IllegalArgumentError();
817 } 847 }
818 848
819 CObject* File::LengthFromPathRequest(const CObjectArray& request) { 849 CObject* File::LengthFromPathRequest(const CObjectArray& request) {
820 if ((request.Length() == 1) && request[0]->IsString()) { 850 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
821 CObjectString filepath(request[0]); 851 return CObject::IllegalArgumentError();
822 int64_t return_value = File::LengthFromPath(filepath.CString()); 852 }
823 if (return_value >= 0) { 853 Namespace* namespc = CObjectToNamespacePointer(request[0]);
824 return new CObjectInt64(CObject::NewInt64(return_value)); 854 RefCntReleaseScope<Namespace> rs(namespc);
825 } else { 855 if ((request.Length() != 2) || !request[1]->IsString()) {
826 return CObject::NewOSError(); 856 return CObject::IllegalArgumentError();
827 } 857 }
828 } 858 CObjectString filepath(request[1]);
829 return CObject::IllegalArgumentError(); 859 const int64_t return_value =
860 File::LengthFromPath(namespc, filepath.CString());
861 if (return_value < 0) {
862 return CObject::NewOSError();
863 }
864 return new CObjectInt64(CObject::NewInt64(return_value));
830 } 865 }
831 866
832 CObject* File::LastAccessedRequest(const CObjectArray& request) { 867 CObject* File::LastAccessedRequest(const CObjectArray& request) {
833 if ((request.Length() == 1) && request[0]->IsString()) { 868 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
834 CObjectString filepath(request[0]); 869 return CObject::IllegalArgumentError();
835 int64_t return_value = File::LastAccessed(filepath.CString()); 870 }
836 if (return_value >= 0) { 871 Namespace* namespc = CObjectToNamespacePointer(request[0]);
837 return new CObjectIntptr( 872 RefCntReleaseScope<Namespace> rs(namespc);
838 CObject::NewInt64(return_value * kMillisecondsPerSecond)); 873 if ((request.Length() != 2) || !request[1]->IsString()) {
839 } else { 874 return CObject::IllegalArgumentError();
840 return CObject::NewOSError(); 875 }
841 } 876 CObjectString filepath(request[1]);
842 } 877 const int64_t return_value = File::LastAccessed(namespc, filepath.CString());
843 return CObject::IllegalArgumentError(); 878 if (return_value < 0) {
879 return CObject::NewOSError();
880 }
881 return new CObjectIntptr(
882 CObject::NewInt64(return_value * kMillisecondsPerSecond));
844 } 883 }
845 884
846 CObject* File::SetLastAccessedRequest(const CObjectArray& request) { 885 CObject* File::SetLastAccessedRequest(const CObjectArray& request) {
847 if ((request.Length() == 2) && request[0]->IsString() && 886 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
848 request[1]->IsInt32OrInt64()) { 887 return CObject::IllegalArgumentError();
849 CObjectString filepath(request[0]); 888 }
850 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); 889 Namespace* namespc = CObjectToNamespacePointer(request[0]);
851 if (File::SetLastAccessed(filepath.CString(), millis)) { 890 RefCntReleaseScope<Namespace> rs(namespc);
852 return CObject::Null(); 891 if ((request.Length() != 3) || !request[1]->IsString() ||
853 } else { 892 !request[2]->IsInt32OrInt64()) {
854 return CObject::NewOSError(); 893 return CObject::IllegalArgumentError();
855 } 894 }
856 } 895 CObjectString filepath(request[1]);
857 return CObject::IllegalArgumentError(); 896 const int64_t millis = CObjectInt32OrInt64ToInt64(request[2]);
897 return File::SetLastAccessed(namespc, filepath.CString(), millis)
898 ? CObject::Null()
899 : CObject::NewOSError();
858 } 900 }
859 901
860 CObject* File::LastModifiedRequest(const CObjectArray& request) { 902 CObject* File::LastModifiedRequest(const CObjectArray& request) {
861 if ((request.Length() == 1) && request[0]->IsString()) { 903 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
862 CObjectString filepath(request[0]); 904 return CObject::IllegalArgumentError();
863 int64_t return_value = File::LastModified(filepath.CString()); 905 }
864 if (return_value >= 0) { 906 Namespace* namespc = CObjectToNamespacePointer(request[0]);
865 return new CObjectIntptr( 907 RefCntReleaseScope<Namespace> rs(namespc);
866 CObject::NewInt64(return_value * kMillisecondsPerSecond)); 908 if ((request.Length() != 2) || !request[1]->IsString()) {
867 } else { 909 return CObject::IllegalArgumentError();
868 return CObject::NewOSError(); 910 }
869 } 911 CObjectString filepath(request[1]);
870 } 912 const int64_t return_value = File::LastModified(namespc, filepath.CString());
871 return CObject::IllegalArgumentError(); 913 if (return_value < 0) {
914 return CObject::NewOSError();
915 }
916 return new CObjectIntptr(
917 CObject::NewInt64(return_value * kMillisecondsPerSecond));
872 } 918 }
873 919
874 CObject* File::SetLastModifiedRequest(const CObjectArray& request) { 920 CObject* File::SetLastModifiedRequest(const CObjectArray& request) {
875 if ((request.Length() == 2) && request[0]->IsString() && 921 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
876 request[1]->IsInt32OrInt64()) { 922 return CObject::IllegalArgumentError();
877 CObjectString filepath(request[0]); 923 }
878 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); 924 Namespace* namespc = CObjectToNamespacePointer(request[0]);
879 if (File::SetLastModified(filepath.CString(), millis)) { 925 RefCntReleaseScope<Namespace> rs(namespc);
880 return CObject::Null(); 926 if ((request.Length() != 3) || !request[1]->IsString() ||
881 } else { 927 !request[2]->IsInt32OrInt64()) {
882 return CObject::NewOSError(); 928 return CObject::IllegalArgumentError();
883 } 929 }
884 } 930 CObjectString filepath(request[1]);
885 return CObject::IllegalArgumentError(); 931 const int64_t millis = CObjectInt32OrInt64ToInt64(request[2]);
932 return File::SetLastModified(namespc, filepath.CString(), millis)
933 ? CObject::Null()
934 : CObject::NewOSError();
886 } 935 }
887 936
888 CObject* File::FlushRequest(const CObjectArray& request) { 937 CObject* File::FlushRequest(const CObjectArray& request) {
889 if ((request.Length() == 1) && request[0]->IsIntptr()) { 938 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
890 File* file = CObjectToFilePointer(request[0]); 939 return CObject::IllegalArgumentError();
891 RefCntReleaseScope<File> rs(file); 940 }
892 if (!file->IsClosed()) { 941 File* file = CObjectToFilePointer(request[0]);
893 if (file->Flush()) { 942 RefCntReleaseScope<File> rs(file);
894 return CObject::True(); 943 if (file->IsClosed()) {
895 } else { 944 return CObject::FileClosedError();
896 return CObject::NewOSError(); 945 }
897 } 946 return file->Flush() ? CObject::True() : CObject::NewOSError();
898 } else {
899 return CObject::FileClosedError();
900 }
901 }
902 return CObject::IllegalArgumentError();
903 } 947 }
904 948
905 CObject* File::ReadByteRequest(const CObjectArray& request) { 949 CObject* File::ReadByteRequest(const CObjectArray& request) {
906 if ((request.Length() == 1) && request[0]->IsIntptr()) { 950 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
907 File* file = CObjectToFilePointer(request[0]); 951 return CObject::IllegalArgumentError();
908 RefCntReleaseScope<File> rs(file); 952 }
909 if (!file->IsClosed()) { 953 File* file = CObjectToFilePointer(request[0]);
910 uint8_t buffer; 954 RefCntReleaseScope<File> rs(file);
911 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); 955 if (file->IsClosed()) {
912 if (bytes_read > 0) { 956 return CObject::FileClosedError();
913 return new CObjectIntptr(CObject::NewIntptr(buffer)); 957 }
914 } else if (bytes_read == 0) { 958 uint8_t buffer;
915 return new CObjectIntptr(CObject::NewIntptr(-1)); 959 const int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1);
916 } else { 960 if (bytes_read < 0) {
917 return CObject::NewOSError(); 961 return CObject::NewOSError();
918 } 962 }
919 } else { 963 if (bytes_read == 0) {
920 return CObject::FileClosedError(); 964 return new CObjectIntptr(CObject::NewIntptr(-1));
921 } 965 }
922 } 966 return new CObjectIntptr(CObject::NewIntptr(buffer));
923 return CObject::IllegalArgumentError();
924 } 967 }
925 968
926 CObject* File::WriteByteRequest(const CObjectArray& request) { 969 CObject* File::WriteByteRequest(const CObjectArray& request) {
927 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 970 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
928 File* file = CObjectToFilePointer(request[0]); 971 return CObject::IllegalArgumentError();
929 RefCntReleaseScope<File> rs(file); 972 }
930 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 973 File* file = CObjectToFilePointer(request[0]);
931 if (!file->IsClosed()) { 974 RefCntReleaseScope<File> rs(file);
932 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); 975 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
933 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); 976 return CObject::IllegalArgumentError();
934 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); 977 }
935 if (success) { 978 if (file->IsClosed()) {
936 return new CObjectInt64(CObject::NewInt64(1)); 979 return CObject::FileClosedError();
937 } else { 980 }
938 return CObject::NewOSError(); 981 const int64_t byte = CObjectInt32OrInt64ToInt64(request[1]);
939 } 982 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
940 } else { 983 return file->WriteFully(reinterpret_cast<void*>(&buffer), 1)
941 return CObject::FileClosedError(); 984 ? new CObjectInt64(CObject::NewInt64(1))
942 } 985 : CObject::NewOSError();
943 } else {
944 return CObject::IllegalArgumentError();
945 }
946 }
947 return CObject::IllegalArgumentError();
948 } 986 }
949 987
950 CObject* File::ReadRequest(const CObjectArray& request) { 988 CObject* File::ReadRequest(const CObjectArray& request) {
951 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 989 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
952 File* file = CObjectToFilePointer(request[0]); 990 return CObject::IllegalArgumentError();
953 RefCntReleaseScope<File> rs(file); 991 }
954 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 992 File* file = CObjectToFilePointer(request[0]);
955 if (!file->IsClosed()) { 993 RefCntReleaseScope<File> rs(file);
956 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 994 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
957 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 995 return CObject::IllegalArgumentError();
958 ASSERT(io_buffer != NULL); 996 }
959 uint8_t* data = io_buffer->value.as_external_typed_data.data; 997 if (file->IsClosed()) {
960 int64_t bytes_read = file->Read(data, length); 998 return CObject::FileClosedError();
961 if (bytes_read >= 0) { 999 }
962 CObjectExternalUint8Array* external_array = 1000 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
963 new CObjectExternalUint8Array(io_buffer); 1001 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
964 external_array->SetLength(bytes_read); 1002 ASSERT(io_buffer != NULL);
965 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); 1003 uint8_t* data = io_buffer->value.as_external_typed_data.data;
966 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 1004 const int64_t bytes_read = file->Read(data, length);
967 result->SetAt(1, external_array); 1005 if (bytes_read < 0) {
968 return result; 1006 CObject::FreeIOBufferData(io_buffer);
969 } else { 1007 return CObject::NewOSError();
970 CObject::FreeIOBufferData(io_buffer); 1008 }
971 return CObject::NewOSError(); 1009 CObjectExternalUint8Array* external_array =
972 } 1010 new CObjectExternalUint8Array(io_buffer);
973 } else { 1011 external_array->SetLength(bytes_read);
974 return CObject::FileClosedError(); 1012 CObjectArray* result = new CObjectArray(CObject::NewArray(2));
975 } 1013 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
976 } else { 1014 result->SetAt(1, external_array);
977 return CObject::IllegalArgumentError(); 1015 return result;
978 }
979 }
980 return CObject::IllegalArgumentError();
981 } 1016 }
982 1017
983 CObject* File::ReadIntoRequest(const CObjectArray& request) { 1018 CObject* File::ReadIntoRequest(const CObjectArray& request) {
984 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1019 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
985 File* file = CObjectToFilePointer(request[0]); 1020 return CObject::IllegalArgumentError();
986 RefCntReleaseScope<File> rs(file); 1021 }
987 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { 1022 File* file = CObjectToFilePointer(request[0]);
988 if (!file->IsClosed()) { 1023 RefCntReleaseScope<File> rs(file);
989 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); 1024 if ((request.Length() != 2) || !request[1]->IsInt32OrInt64()) {
990 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 1025 return CObject::IllegalArgumentError();
991 ASSERT(io_buffer != NULL); 1026 }
992 uint8_t* data = io_buffer->value.as_external_typed_data.data; 1027 if (file->IsClosed()) {
993 int64_t bytes_read = file->Read(data, length); 1028 return CObject::FileClosedError();
994 if (bytes_read >= 0) { 1029 }
995 CObjectExternalUint8Array* external_array = 1030 const int64_t length = CObjectInt32OrInt64ToInt64(request[1]);
996 new CObjectExternalUint8Array(io_buffer); 1031 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
997 external_array->SetLength(bytes_read); 1032 ASSERT(io_buffer != NULL);
998 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 1033 uint8_t* data = io_buffer->value.as_external_typed_data.data;
999 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 1034 const int64_t bytes_read = file->Read(data, length);
1000 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); 1035 if (bytes_read < 0) {
1001 result->SetAt(2, external_array); 1036 CObject::FreeIOBufferData(io_buffer);
1002 return result; 1037 return CObject::NewOSError();
1003 } else { 1038 }
1004 CObject::FreeIOBufferData(io_buffer); 1039 CObjectExternalUint8Array* external_array =
1005 return CObject::NewOSError(); 1040 new CObjectExternalUint8Array(io_buffer);
1006 } 1041 external_array->SetLength(bytes_read);
1007 } else { 1042 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
1008 return CObject::FileClosedError(); 1043 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
1009 } 1044 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read)));
1010 } else { 1045 result->SetAt(2, external_array);
1011 return CObject::IllegalArgumentError(); 1046 return result;
1012 }
1013 }
1014 return CObject::IllegalArgumentError();
1015 } 1047 }
1016 1048
1017 static int SizeInBytes(Dart_TypedData_Type type) { 1049 static int SizeInBytes(Dart_TypedData_Type type) {
1018 switch (type) { 1050 switch (type) {
1019 case Dart_TypedData_kInt8: 1051 case Dart_TypedData_kInt8:
1020 case Dart_TypedData_kUint8: 1052 case Dart_TypedData_kUint8:
1021 case Dart_TypedData_kUint8Clamped: 1053 case Dart_TypedData_kUint8Clamped:
1022 return 1; 1054 return 1;
1023 case Dart_TypedData_kInt16: 1055 case Dart_TypedData_kInt16:
1024 case Dart_TypedData_kUint16: 1056 case Dart_TypedData_kUint16:
1025 return 2; 1057 return 2;
1026 case Dart_TypedData_kInt32: 1058 case Dart_TypedData_kInt32:
1027 case Dart_TypedData_kUint32: 1059 case Dart_TypedData_kUint32:
1028 case Dart_TypedData_kFloat32: 1060 case Dart_TypedData_kFloat32:
1029 return 4; 1061 return 4;
1030 case Dart_TypedData_kInt64: 1062 case Dart_TypedData_kInt64:
1031 case Dart_TypedData_kUint64: 1063 case Dart_TypedData_kUint64:
1032 case Dart_TypedData_kFloat64: 1064 case Dart_TypedData_kFloat64:
1033 return 8; 1065 return 8;
1034 default: 1066 default:
1035 break; 1067 break;
1036 } 1068 }
1037 UNREACHABLE(); 1069 UNREACHABLE();
1038 return -1; 1070 return -1;
1039 } 1071 }
1040 1072
1041 CObject* File::WriteFromRequest(const CObjectArray& request) { 1073 CObject* File::WriteFromRequest(const CObjectArray& request) {
1042 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1074 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1043 File* file = CObjectToFilePointer(request[0]); 1075 return CObject::IllegalArgumentError();
1044 RefCntReleaseScope<File> rs(file); 1076 }
1045 if ((request.Length() == 4) && 1077 File* file = CObjectToFilePointer(request[0]);
1046 (request[1]->IsTypedData() || request[1]->IsArray()) && 1078 RefCntReleaseScope<File> rs(file);
1047 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { 1079 if ((request.Length() != 4) ||
1048 if (!file->IsClosed()) { 1080 (!request[1]->IsTypedData() && !request[1]->IsArray()) ||
1049 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); 1081 !request[2]->IsInt32OrInt64() || !request[3]->IsInt32OrInt64()) {
1050 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); 1082 return CObject::IllegalArgumentError();
1051 int64_t length = end - start; 1083 }
1052 uint8_t* buffer_start; 1084 if (file->IsClosed()) {
1053 if (request[1]->IsTypedData()) { 1085 return CObject::FileClosedError();
1054 CObjectTypedData typed_data(request[1]); 1086 }
1055 start = start * SizeInBytes(typed_data.Type()); 1087 int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
1056 length = length * SizeInBytes(typed_data.Type()); 1088 int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
1057 buffer_start = typed_data.Buffer() + start; 1089 int64_t length = end - start;
1058 } else { 1090 uint8_t* buffer_start;
1059 CObjectArray array(request[1]); 1091 if (request[1]->IsTypedData()) {
1060 buffer_start = Dart_ScopeAllocate(length); 1092 CObjectTypedData typed_data(request[1]);
1061 for (int i = 0; i < length; i++) { 1093 start = start * SizeInBytes(typed_data.Type());
1062 if (array[i + start]->IsInt32OrInt64()) { 1094 length = length * SizeInBytes(typed_data.Type());
1063 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); 1095 buffer_start = typed_data.Buffer() + start;
1064 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); 1096 } else {
1065 } else { 1097 CObjectArray array(request[1]);
1066 // Unsupported type. 1098 buffer_start = Dart_ScopeAllocate(length);
1067 return CObject::IllegalArgumentError(); 1099 for (int i = 0; i < length; i++) {
1068 } 1100 if (array[i + start]->IsInt32OrInt64()) {
1069 } 1101 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]);
1070 start = 0; 1102 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 { 1103 } else {
1080 return CObject::FileClosedError(); 1104 // Unsupported type.
1105 return CObject::IllegalArgumentError();
1081 } 1106 }
1082 } else {
1083 return CObject::IllegalArgumentError();
1084 } 1107 }
1108 start = 0;
1085 } 1109 }
1086 return CObject::IllegalArgumentError(); 1110 return file->WriteFully(reinterpret_cast<void*>(buffer_start), length)
1111 ? new CObjectInt64(CObject::NewInt64(length))
1112 : CObject::NewOSError();
1087 } 1113 }
1088 1114
1089 CObject* File::CreateLinkRequest(const CObjectArray& request) { 1115 CObject* File::CreateLinkRequest(const CObjectArray& request) {
1090 if ((request.Length() != 2) || !request[0]->IsString() || 1116 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1091 !request[1]->IsString()) {
1092 return CObject::IllegalArgumentError(); 1117 return CObject::IllegalArgumentError();
1093 } 1118 }
1094 CObjectString link_name(request[0]); 1119 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1095 CObjectString target_name(request[1]); 1120 RefCntReleaseScope<Namespace> rs(namespc);
1096 if (File::CreateLink(link_name.CString(), target_name.CString())) { 1121 if (!request[1]->IsString() || !request[2]->IsString()) {
1097 return CObject::True(); 1122 return CObject::IllegalArgumentError();
1098 } else {
1099 return CObject::NewOSError();
1100 } 1123 }
1124 CObjectString link_name(request[1]);
1125 CObjectString target_name(request[2]);
1126 return File::CreateLink(namespc, link_name.CString(), target_name.CString())
1127 ? CObject::True()
1128 : CObject::NewOSError();
1101 } 1129 }
1102 1130
1103 CObject* File::DeleteLinkRequest(const CObjectArray& request) { 1131 CObject* File::DeleteLinkRequest(const CObjectArray& request) {
1104 if ((request.Length() == 1) && request[0]->IsString()) { 1132 if ((request.Length() != 2) || !request[0]->IsIntptr()) {
1105 CObjectString link_path(request[0]); 1133 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 } 1134 }
1113 return CObject::IllegalArgumentError(); 1135 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1136 RefCntReleaseScope<Namespace> rs(namespc);
1137 if (!request[1]->IsString()) {
1138 return CObject::IllegalArgumentError();
1139 }
1140 CObjectString link_path(request[1]);
1141 return File::DeleteLink(namespc, link_path.CString()) ? CObject::True()
1142 : CObject::NewOSError();
1114 } 1143 }
1115 1144
1116 CObject* File::RenameLinkRequest(const CObjectArray& request) { 1145 CObject* File::RenameLinkRequest(const CObjectArray& request) {
1117 if ((request.Length() == 2) && request[0]->IsString() && 1146 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1118 request[1]->IsString()) { 1147 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 } 1148 }
1127 return CObject::IllegalArgumentError(); 1149 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1150 RefCntReleaseScope<Namespace> rs(namespc);
1151 if (!request[1]->IsString() || !request[2]->IsString()) {
1152 return CObject::IllegalArgumentError();
1153 }
1154 CObjectString old_path(request[1]);
1155 CObjectString new_path(request[2]);
1156 return File::RenameLink(namespc, old_path.CString(), new_path.CString())
1157 ? CObject::True()
1158 : CObject::NewOSError();
1128 } 1159 }
1129 1160
1130 CObject* File::LinkTargetRequest(const CObjectArray& request) { 1161 CObject* File::LinkTargetRequest(const CObjectArray& request) {
1131 if ((request.Length() == 1) && request[0]->IsString()) { 1162 if ((request.Length() != 2) || !request[0]->IsIntptr()) {
1132 CObjectString link_path(request[0]); 1163 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 } 1164 }
1141 return CObject::IllegalArgumentError(); 1165 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1166 RefCntReleaseScope<Namespace> rs(namespc);
1167 if (!request[1]->IsString()) {
1168 return CObject::IllegalArgumentError();
1169 }
1170 CObjectString link_path(request[1]);
1171 const char* target = File::LinkTarget(namespc, link_path.CString());
1172 if (target == NULL) {
1173 return CObject::NewOSError();
1174 }
1175 return new CObjectString(CObject::NewString(target));
1142 } 1176 }
1143 1177
1144 CObject* File::TypeRequest(const CObjectArray& request) { 1178 CObject* File::TypeRequest(const CObjectArray& request) {
1145 if ((request.Length() == 2) && request[0]->IsString() && 1179 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1146 request[1]->IsBool()) { 1180 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 } 1181 }
1152 return CObject::IllegalArgumentError(); 1182 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1183 RefCntReleaseScope<Namespace> rs(namespc);
1184 if (!request[1]->IsString() || !request[2]->IsBool()) {
1185 return CObject::IllegalArgumentError();
1186 }
1187 CObjectString path(request[1]);
1188 CObjectBool follow_links(request[2]);
1189 File::Type type =
1190 File::GetType(namespc, path.CString(), follow_links.Value());
1191 return new CObjectInt32(CObject::NewInt32(type));
1153 } 1192 }
1154 1193
1155 CObject* File::IdenticalRequest(const CObjectArray& request) { 1194 CObject* File::IdenticalRequest(const CObjectArray& request) {
1156 if ((request.Length() == 2) && request[0]->IsString() && 1195 if ((request.Length() != 3) || !request[0]->IsIntptr()) {
1157 request[1]->IsString()) { 1196 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 } 1197 }
1170 return CObject::IllegalArgumentError(); 1198 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1199 RefCntReleaseScope<Namespace> rs(namespc);
1200 if (!request[1]->IsString() || !request[2]->IsString()) {
1201 return CObject::IllegalArgumentError();
1202 }
1203 CObjectString path1(request[1]);
1204 CObjectString path2(request[2]);
1205 File::Identical result =
1206 File::AreIdentical(namespc, path1.CString(), path2.CString());
1207 if (result == File::kError) {
1208 return CObject::NewOSError();
1209 }
1210 return (result == File::kIdentical) ? CObject::True() : CObject::False();
1171 } 1211 }
1172 1212
1173 CObject* File::StatRequest(const CObjectArray& request) { 1213 CObject* File::StatRequest(const CObjectArray& request) {
1174 if ((request.Length() == 1) && request[0]->IsString()) { 1214 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1175 int64_t data[File::kStatSize]; 1215 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 } 1216 }
1190 return CObject::IllegalArgumentError(); 1217 Namespace* namespc = CObjectToNamespacePointer(request[0]);
1218 RefCntReleaseScope<Namespace> rs(namespc);
1219 if ((request.Length() != 2) || !request[1]->IsString()) {
1220 return CObject::IllegalArgumentError();
1221 }
1222 int64_t data[File::kStatSize];
1223 CObjectString path(request[1]);
1224 File::Stat(namespc, path.CString(), data);
1225 if (data[File::kType] == File::kDoesNotExist) {
1226 return CObject::NewOSError();
1227 }
1228 CObjectArray* result = new CObjectArray(CObject::NewArray(File::kStatSize));
1229 for (int i = 0; i < File::kStatSize; ++i) {
1230 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i])));
1231 }
1232 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2));
1233 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess)));
1234 wrapper->SetAt(1, result);
1235 return wrapper;
1191 } 1236 }
1192 1237
1193 CObject* File::LockRequest(const CObjectArray& request) { 1238 CObject* File::LockRequest(const CObjectArray& request) {
1194 if ((request.Length() >= 1) && request[0]->IsIntptr()) { 1239 if ((request.Length() < 1) || !request[0]->IsIntptr()) {
1195 File* file = CObjectToFilePointer(request[0]); 1240 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 } 1241 }
1215 return CObject::IllegalArgumentError(); 1242 File* file = CObjectToFilePointer(request[0]);
1243 RefCntReleaseScope<File> rs(file);
1244 if ((request.Length() != 4) || !request[1]->IsInt32OrInt64() ||
1245 !request[2]->IsInt32OrInt64() || !request[3]->IsInt32OrInt64()) {
1246 return CObject::IllegalArgumentError();
1247 }
1248 if (file->IsClosed()) {
1249 return CObject::FileClosedError();
1250 }
1251 const int64_t lock = CObjectInt32OrInt64ToInt64(request[1]);
1252 const int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
1253 const int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
1254 return file->Lock(static_cast<File::LockType>(lock), start, end)
1255 ? CObject::True()
1256 : CObject::NewOSError();
1216 } 1257 }
1217 1258
1218 } // namespace bin 1259 } // namespace bin
1219 } // namespace dart 1260 } // 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