| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 | 9 |
| 10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 void DartUtils::WriteMagicNumber(File* file) { | 470 void DartUtils::WriteMagicNumber(File* file) { |
| 471 // Write a magic number and version information into the snapshot file. | 471 // Write a magic number and version information into the snapshot file. |
| 472 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 472 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
| 473 ASSERT(bytes_written); | 473 ASSERT(bytes_written); |
| 474 } | 474 } |
| 475 | 475 |
| 476 | 476 |
| 477 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 477 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 478 Dart_Handle builtin_lib) { | 478 Dart_Handle builtin_lib) { |
| 479 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 479 Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 480 |
| 481 Dart_Port load_port = Dart_ServiceWaitForLoadPort(); |
| 482 Builtin::SetLoadPort(load_port); |
| 483 |
| 480 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); | 484 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); |
| 481 } | 485 } |
| 482 | 486 |
| 483 | 487 |
| 484 // Callback function, gets called from asynchronous script and library | 488 // Callback function, gets called from asynchronous script and library |
| 485 // reading code when there is an i/o error. | 489 // reading code when there is an i/o error. |
| 486 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { | 490 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { |
| 487 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); | 491 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); |
| 488 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); | 492 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); |
| 489 Dart_Handle error = Dart_GetNativeArgument(args, 2); | 493 Dart_Handle error = Dart_GetNativeArgument(args, 2); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 Dart_Handle res = Dart_FinalizeLoading(true); | 587 Dart_Handle res = Dart_FinalizeLoading(true); |
| 584 if (Dart_IsError(res)) { | 588 if (Dart_IsError(res)) { |
| 585 // TODO(hausner): If compilation/loading errors are supposed to | 589 // TODO(hausner): If compilation/loading errors are supposed to |
| 586 // be observable by the program, we need to mark the bad library | 590 // be observable by the program, we need to mark the bad library |
| 587 // with the error instead of propagating it. | 591 // with the error instead of propagating it. |
| 588 Dart_PropagateError(res); | 592 Dart_PropagateError(res); |
| 589 } | 593 } |
| 590 } | 594 } |
| 591 | 595 |
| 592 | 596 |
| 597 void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, |
| 598 const char* package_root) { |
| 599 Dart_Handle url = NewString(kInternalLibURL); |
| 600 DART_CHECK_VALID(url); |
| 601 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
| 602 DART_CHECK_VALID(internal_lib); |
| 603 |
| 604 // Setup the internal library's 'internalPrint' function. |
| 605 Dart_Handle print = Dart_Invoke( |
| 606 builtin_lib, NewString("_getPrintClosure"), 0, NULL); |
| 607 DART_CHECK_VALID(print); |
| 608 Dart_Handle result = |
| 609 Dart_SetField(internal_lib, NewString("_printClosure"), print); |
| 610 DART_CHECK_VALID(result); |
| 611 |
| 612 if (IsWindowsHost()) { |
| 613 // Set running on Windows flag. |
| 614 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, NULL); |
| 615 DART_CHECK_VALID(result); |
| 616 } |
| 617 |
| 618 // Set current working directory. |
| 619 result = SetWorkingDirectory(builtin_lib); |
| 620 DART_CHECK_VALID(result); |
| 621 |
| 622 // Set up package root if specified. |
| 623 if (package_root != NULL) { |
| 624 result = NewString(package_root); |
| 625 DART_CHECK_VALID(result); |
| 626 const int kNumArgs = 1; |
| 627 Dart_Handle dart_args[kNumArgs]; |
| 628 dart_args[0] = result; |
| 629 result = Dart_Invoke(builtin_lib, |
| 630 NewString("_setPackageRoot"), |
| 631 kNumArgs, |
| 632 dart_args); |
| 633 DART_CHECK_VALID(result); |
| 634 } |
| 635 } |
| 636 |
| 637 |
| 638 void DartUtils::PrepareCoreLibrary(Dart_Handle builtin_lib) { |
| 639 Dart_Handle url = NewString(kCoreLibURL); |
| 640 DART_CHECK_VALID(url); |
| 641 Dart_Handle core_lib = Dart_LookupLibrary(url); |
| 642 DART_CHECK_VALID(core_lib); |
| 643 |
| 644 // Setup the 'Uri.base' getter in dart:core. |
| 645 Dart_Handle uri_base = Dart_Invoke( |
| 646 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); |
| 647 DART_CHECK_VALID(uri_base); |
| 648 Dart_Handle result = Dart_SetField(core_lib, |
| 649 NewString("_uriBaseClosure"), |
| 650 uri_base); |
| 651 DART_CHECK_VALID(result); |
| 652 } |
| 653 |
| 654 |
| 655 void DartUtils::PrepareAsyncLibrary(Dart_Handle async_lib, |
| 656 Dart_Handle io_lib) { |
| 657 Dart_Handle url = NewString(kIsolateLibURL); |
| 658 DART_CHECK_VALID(url); |
| 659 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
| 660 DART_CHECK_VALID(isolate_lib); |
| 661 |
| 662 // Setup the 'timer' factory in dart:async. |
| 663 Dart_Handle timer_closure = |
| 664 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); |
| 665 DART_CHECK_VALID(timer_closure); |
| 666 Dart_Handle args[1]; |
| 667 args[0] = timer_closure; |
| 668 DART_CHECK_VALID(Dart_Invoke( |
| 669 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); |
| 670 |
| 671 // Setup the 'scheduleImmediate' closure in dart:async. |
| 672 Dart_Handle schedule_immediate_closure = |
| 673 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), |
| 674 0, NULL); |
| 675 args[0] = schedule_immediate_closure; |
| 676 DART_CHECK_VALID(Dart_Invoke( |
| 677 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); |
| 678 } |
| 679 |
| 680 |
| 593 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, | 681 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, |
| 594 Dart_Handle builtin_lib) { | 682 Dart_Handle builtin_lib) { |
| 595 // First ensure all required libraries are available. | 683 // First ensure all required libraries are available. |
| 596 Dart_Handle url = NewString(kAsyncLibURL); | 684 Dart_Handle url = NewString(kAsyncLibURL); |
| 597 DART_CHECK_VALID(url); | 685 DART_CHECK_VALID(url); |
| 598 Dart_Handle async_lib = Dart_LookupLibrary(url); | 686 Dart_Handle async_lib = Dart_LookupLibrary(url); |
| 599 DART_CHECK_VALID(async_lib); | 687 DART_CHECK_VALID(async_lib); |
| 600 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 688 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
| 601 | 689 |
| 602 // We need to ensure that all the scripts loaded so far are finalized | 690 // We need to ensure that all the scripts loaded so far are finalized |
| 603 // as we are about to invoke some Dart code below to setup closures. | 691 // as we are about to invoke some Dart code below to setup closures. |
| 604 Dart_Handle result = Dart_FinalizeLoading(false); | 692 Dart_Handle result = Dart_FinalizeLoading(false); |
| 605 DART_CHECK_VALID(result); | 693 DART_CHECK_VALID(result); |
| 606 | 694 |
| 607 // Setup the internal library's 'internalPrint' function. | 695 PrepareBuiltinLibrary(builtin_lib, package_root); |
| 608 Dart_Handle print = Dart_Invoke( | 696 PrepareAsyncLibrary(async_lib, io_lib); |
| 609 builtin_lib, NewString("_getPrintClosure"), 0, NULL); | 697 PrepareCoreLibrary(builtin_lib); |
| 610 url = NewString(kInternalLibURL); | |
| 611 DART_CHECK_VALID(url); | |
| 612 Dart_Handle internal_lib = Dart_LookupLibrary(url); | |
| 613 DART_CHECK_VALID(internal_lib); | |
| 614 result = Dart_SetField(internal_lib, | |
| 615 NewString("_printClosure"), | |
| 616 print); | |
| 617 DART_CHECK_VALID(result); | |
| 618 | 698 |
| 619 // Setup the 'timer' factory. | 699 return result; |
| 620 Dart_Handle timer_closure = | 700 } |
| 621 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); | |
| 622 Dart_Handle args[1]; | |
| 623 args[0] = timer_closure; | |
| 624 DART_CHECK_VALID(Dart_Invoke( | |
| 625 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); | |
| 626 | 701 |
| 627 // Setup the 'scheduleImmediate' closure. | |
| 628 url = NewString(kIsolateLibURL); | |
| 629 DART_CHECK_VALID(url); | |
| 630 Dart_Handle isolate_lib = Dart_LookupLibrary(url); | |
| 631 DART_CHECK_VALID(isolate_lib); | |
| 632 Dart_Handle schedule_immediate_closure = | |
| 633 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), | |
| 634 0, NULL); | |
| 635 args[0] = schedule_immediate_closure; | |
| 636 DART_CHECK_VALID(Dart_Invoke( | |
| 637 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); | |
| 638 | 702 |
| 639 // Setup the corelib 'Uri.base' getter. | 703 void DartUtils::SetupIOLibrary(const char* script_uri) { |
| 640 url = NewString(kCoreLibURL); | 704 Dart_Handle io_lib_url = NewString(kIOLibURL); |
| 641 DART_CHECK_VALID(url); | 705 DART_CHECK_VALID(io_lib_url); |
| 642 Dart_Handle corelib = Dart_LookupLibrary(url); | 706 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
| 643 DART_CHECK_VALID(corelib); | 707 DART_CHECK_VALID(io_lib); |
| 644 Dart_Handle uri_base = Dart_Invoke( | 708 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); |
| 645 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); | 709 DART_CHECK_VALID(platform_type); |
| 646 DART_CHECK_VALID(uri_base); | 710 Dart_Handle script_name = NewString("_nativeScript"); |
| 647 result = Dart_SetField(corelib, | 711 DART_CHECK_VALID(script_name); |
| 648 NewString("_uriBaseClosure"), | 712 Dart_Handle dart_script = NewString(script_uri); |
| 649 uri_base); | 713 DART_CHECK_VALID(dart_script); |
| 650 DART_CHECK_VALID(result); | 714 Dart_Handle set_script_name = |
| 651 | 715 Dart_SetField(platform_type, script_name, dart_script); |
| 652 if (IsWindowsHost()) { | 716 DART_CHECK_VALID(set_script_name); |
| 653 // Set running on Windows flag. | |
| 654 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, NULL); | |
| 655 if (Dart_IsError(result)) { | |
| 656 return result; | |
| 657 } | |
| 658 } | |
| 659 | |
| 660 // Set current working directory. | |
| 661 result = SetWorkingDirectory(builtin_lib); | |
| 662 if (Dart_IsError(result)) { | |
| 663 return result; | |
| 664 } | |
| 665 | |
| 666 // Set up package root if specified. | |
| 667 if (package_root != NULL) { | |
| 668 result = NewString(package_root); | |
| 669 if (!Dart_IsError(result)) { | |
| 670 const int kNumArgs = 1; | |
| 671 Dart_Handle dart_args[kNumArgs]; | |
| 672 dart_args[0] = result; | |
| 673 return Dart_Invoke(builtin_lib, | |
| 674 NewString("_setPackageRoot"), | |
| 675 kNumArgs, | |
| 676 dart_args); | |
| 677 } | |
| 678 } | |
| 679 return result; | |
| 680 } | 717 } |
| 681 | 718 |
| 682 | 719 |
| 683 bool DartUtils::PostNull(Dart_Port port_id) { | 720 bool DartUtils::PostNull(Dart_Port port_id) { |
| 684 // Post a message with just the null object. | 721 // Post a message with just the null object. |
| 685 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 722 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
| 686 } | 723 } |
| 687 | 724 |
| 688 | 725 |
| 689 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 726 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1111 new CObjectString(CObject::NewString(os_error->message())); | 1148 new CObjectString(CObject::NewString(os_error->message())); |
| 1112 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1149 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1113 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1150 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1114 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1151 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1115 result->SetAt(2, error_message); | 1152 result->SetAt(2, error_message); |
| 1116 return result; | 1153 return result; |
| 1117 } | 1154 } |
| 1118 | 1155 |
| 1119 } // namespace bin | 1156 } // namespace bin |
| 1120 } // namespace dart | 1157 } // namespace dart |
| OLD | NEW |