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 <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "include/dart_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 | 539 |
540 #define CHECK_RESULT(result) \ | 540 #define CHECK_RESULT(result) \ |
541 if (Dart_IsError(result)) { \ | 541 if (Dart_IsError(result)) { \ |
542 *error = strdup(Dart_GetError(result)); \ | 542 *error = strdup(Dart_GetError(result)); \ |
543 *is_compile_error = Dart_IsCompilationError(result); \ | 543 *is_compile_error = Dart_IsCompilationError(result); \ |
544 Dart_ExitScope(); \ | 544 Dart_ExitScope(); \ |
545 Dart_ShutdownIsolate(); \ | 545 Dart_ShutdownIsolate(); \ |
546 return NULL; \ | 546 return NULL; \ |
547 } \ | 547 } \ |
548 | 548 |
| 549 |
549 // Returns true on success, false on failure. | 550 // Returns true on success, false on failure. |
550 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, | 551 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
551 const char* main, | 552 const char* main, |
552 const char* package_root, | 553 const char* package_root, |
553 char** error, | 554 char** error, |
554 bool* is_compile_error) { | 555 bool* is_compile_error) { |
555 ASSERT(script_uri != NULL); | 556 ASSERT(script_uri != NULL); |
556 IsolateData* isolate_data = new IsolateData(script_uri, package_root); | 557 IsolateData* isolate_data = new IsolateData(script_uri, package_root); |
557 Dart_Isolate isolate = Dart_CreateIsolate( | 558 Dart_Isolate isolate = NULL; |
558 script_uri, main, snapshot_buffer, isolate_data, error); | 559 |
| 560 isolate = Dart_CreateIsolate(script_uri, |
| 561 main, |
| 562 snapshot_buffer, |
| 563 isolate_data, |
| 564 error); |
| 565 |
559 if (isolate == NULL) { | 566 if (isolate == NULL) { |
560 return NULL; | 567 return NULL; |
561 } | 568 } |
562 | 569 |
563 Dart_EnterScope(); | 570 Dart_EnterScope(); |
564 | 571 |
565 if (snapshot_buffer != NULL) { | 572 if (snapshot_buffer != NULL) { |
566 // Setup the native resolver as the snapshot does not carry it. | 573 // Setup the native resolver as the snapshot does not carry it. |
567 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 574 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
568 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 575 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
569 } | 576 } |
570 | 577 |
| 578 if (Dart_IsServiceIsolate(isolate)) { |
| 579 // If this is the service isolate, load embedder specific bits and return. |
| 580 if (!VmService::Setup(vm_service_server_ip, vm_service_server_port)) { |
| 581 *error = strdup(VmService::GetErrorMessage()); |
| 582 return NULL; |
| 583 } |
| 584 Dart_ExitScope(); |
| 585 Dart_ExitIsolate(); |
| 586 return isolate; |
| 587 } |
| 588 |
| 589 // Load the specified application script into the newly created isolate. |
| 590 |
571 // Set up the library tag handler for this isolate. | 591 // Set up the library tag handler for this isolate. |
572 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 592 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
573 CHECK_RESULT(result); | 593 CHECK_RESULT(result); |
574 | 594 |
575 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | |
576 CHECK_RESULT(result); | |
577 | |
578 // Load the specified application script into the newly created isolate. | |
579 | |
580 // Prepare builtin and its dependent libraries for use to resolve URIs. | 595 // Prepare builtin and its dependent libraries for use to resolve URIs. |
581 // The builtin library is part of the core snapshot and would already be | 596 // The builtin library is part of the core snapshot and would already be |
582 // available here in the case of script snapshot loading. | 597 // available here in the case of script snapshot loading. |
583 Dart_Handle builtin_lib = | 598 Dart_Handle builtin_lib = |
584 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 599 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
585 CHECK_RESULT(builtin_lib); | 600 CHECK_RESULT(builtin_lib); |
586 | 601 |
587 // Prepare for script loading by setting up the 'print' and 'timer' | 602 // Prepare for script loading by setting up the 'print' and 'timer' |
588 // closures and setting up 'package root' for URI resolution. | 603 // closures and setting up 'package root' for URI resolution. |
589 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 604 result = DartUtils::PrepareForScriptLoading(package_root, false, builtin_lib); |
590 CHECK_RESULT(result); | 605 CHECK_RESULT(result); |
591 | 606 |
| 607 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
| 608 CHECK_RESULT(result); |
| 609 |
| 610 // Load the script. |
592 result = DartUtils::LoadScript(script_uri, builtin_lib); | 611 result = DartUtils::LoadScript(script_uri, builtin_lib); |
593 CHECK_RESULT(result); | 612 CHECK_RESULT(result); |
594 | 613 |
595 // Run event-loop and wait for script loading to complete. | 614 // Run event-loop and wait for script loading to complete. |
596 result = Dart_RunLoop(); | 615 result = Dart_RunLoop(); |
597 CHECK_RESULT(result); | 616 CHECK_RESULT(result); |
598 | 617 |
599 Platform::SetPackageRoot(package_root); | 618 Platform::SetPackageRoot(package_root); |
600 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); | 619 |
601 CHECK_RESULT(io_lib_url); | 620 DartUtils::SetupIOLibrary(script_uri); |
602 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | |
603 CHECK_RESULT(io_lib); | |
604 Dart_Handle platform_type = DartUtils::GetDartType(DartUtils::kIOLibURL, | |
605 "_Platform"); | |
606 CHECK_RESULT(platform_type); | |
607 Dart_Handle script_name = DartUtils::NewString("_nativeScript"); | |
608 CHECK_RESULT(script_name); | |
609 Dart_Handle dart_script = DartUtils::NewString(script_uri); | |
610 CHECK_RESULT(dart_script); | |
611 Dart_Handle set_script_name = | |
612 Dart_SetField(platform_type, script_name, dart_script); | |
613 CHECK_RESULT(set_script_name); | |
614 | 621 |
615 // Make the isolate runnable so that it is ready to handle messages. | 622 // Make the isolate runnable so that it is ready to handle messages. |
616 Dart_ExitScope(); | 623 Dart_ExitScope(); |
617 Dart_ExitIsolate(); | 624 Dart_ExitIsolate(); |
618 bool retval = Dart_IsolateMakeRunnable(isolate); | 625 bool retval = Dart_IsolateMakeRunnable(isolate); |
619 if (!retval) { | 626 if (!retval) { |
620 *error = strdup("Invalid isolate state - Unable to make it runnable"); | 627 *error = strdup("Invalid isolate state - Unable to make it runnable"); |
621 Dart_EnterIsolate(isolate); | 628 Dart_EnterIsolate(isolate); |
622 Dart_ShutdownIsolate(); | 629 Dart_ShutdownIsolate(); |
623 return NULL; | 630 return NULL; |
(...skipping 15 matching lines...) Expand all Loading... |
639 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 646 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
640 return NULL; | 647 return NULL; |
641 } | 648 } |
642 script_uri = parent_isolate_data->script_url; | 649 script_uri = parent_isolate_data->script_url; |
643 if (script_uri == NULL) { | 650 if (script_uri == NULL) { |
644 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 651 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
645 return NULL; | 652 return NULL; |
646 } | 653 } |
647 } | 654 } |
648 if (package_root == NULL) { | 655 if (package_root == NULL) { |
649 package_root = parent_isolate_data->package_root; | 656 if (parent_isolate_data != NULL) { |
| 657 package_root = parent_isolate_data->package_root; |
| 658 } else { |
| 659 package_root = "."; |
| 660 } |
650 } | 661 } |
651 return CreateIsolateAndSetupHelper(script_uri, | 662 return CreateIsolateAndSetupHelper(script_uri, |
652 main, | 663 main, |
653 package_root, | 664 package_root, |
654 error, | 665 error, |
655 &is_compile_error); | 666 &is_compile_error); |
656 } | 667 } |
657 | 668 |
658 | 669 |
659 #define CHECK_RESULT(result) \ | |
660 if (Dart_IsError(result)) { \ | |
661 *error = strdup(Dart_GetError(result)); \ | |
662 Dart_ExitScope(); \ | |
663 Dart_ShutdownIsolate(); \ | |
664 return NULL; \ | |
665 } \ | |
666 | |
667 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { | |
668 const char* script_uri = DartUtils::kVMServiceLibURL; | |
669 IsolateData* isolate_data = new IsolateData(script_uri, NULL); | |
670 Dart_Isolate isolate = | |
671 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, | |
672 error); | |
673 if (isolate == NULL) { | |
674 return NULL; | |
675 } | |
676 Dart_EnterScope(); | |
677 if (snapshot_buffer != NULL) { | |
678 // Setup the native resolver as the snapshot does not carry it. | |
679 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | |
680 Builtin::SetNativeResolver(Builtin::kIOLibrary); | |
681 } | |
682 // Set up the library tag handler for this isolate. | |
683 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | |
684 CHECK_RESULT(result); | |
685 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | |
686 CHECK_RESULT(result); | |
687 // Prepare builtin and its dependent libraries for use to resolve URIs. | |
688 Dart_Handle builtin_lib = | |
689 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
690 CHECK_RESULT(builtin_lib); | |
691 // Prepare for script loading by setting up the 'print' and 'timer' | |
692 // closures and setting up 'package root' for URI resolution. | |
693 result = DartUtils::PrepareForScriptLoading(NULL, builtin_lib); | |
694 CHECK_RESULT(result); | |
695 | |
696 Dart_ExitScope(); | |
697 Dart_ExitIsolate(); | |
698 return isolate; | |
699 } | |
700 | |
701 #undef CHECK_RESULT | |
702 | |
703 static void PrintVersion() { | 670 static void PrintVersion() { |
704 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); | 671 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); |
705 } | 672 } |
706 | 673 |
707 | 674 |
708 static void PrintUsage() { | 675 static void PrintUsage() { |
709 Log::PrintErr( | 676 Log::PrintErr( |
710 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" | 677 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" |
711 "\n" | 678 "\n" |
712 "Executes the Dart script passed as <dart-script-file>.\n" | 679 "Executes the Dart script passed as <dart-script-file>.\n" |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 | 940 |
974 if (!DartUtils::SetOriginalWorkingDirectory()) { | 941 if (!DartUtils::SetOriginalWorkingDirectory()) { |
975 OSError err; | 942 OSError err; |
976 fprintf(stderr, "Error determining current directory: %s\n", err.message()); | 943 fprintf(stderr, "Error determining current directory: %s\n", err.message()); |
977 fflush(stderr); | 944 fflush(stderr); |
978 exit(kErrorExitCode); | 945 exit(kErrorExitCode); |
979 } | 946 } |
980 | 947 |
981 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 948 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
982 | 949 |
| 950 // Start event handler. |
| 951 EventHandler::Start(); |
| 952 |
983 // Initialize the Dart VM. | 953 // Initialize the Dart VM. |
984 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 954 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
985 DartUtils::OpenFile, | 955 DartUtils::OpenFile, |
986 DartUtils::ReadFile, | 956 DartUtils::ReadFile, |
987 DartUtils::WriteFile, | 957 DartUtils::WriteFile, |
988 DartUtils::CloseFile, | 958 DartUtils::CloseFile, |
989 DartUtils::EntropySource, | 959 DartUtils::EntropySource)) { |
990 CreateServiceIsolate)) { | |
991 fprintf(stderr, "%s", "VM initialization failed\n"); | 960 fprintf(stderr, "%s", "VM initialization failed\n"); |
992 fflush(stderr); | 961 fflush(stderr); |
993 exit(kErrorExitCode); | 962 exit(kErrorExitCode); |
994 } | 963 } |
995 | 964 |
996 // Start event handler. | 965 |
997 EventHandler::Start(); | |
998 | 966 |
999 // Start the debugger wire protocol handler if necessary. | 967 // Start the debugger wire protocol handler if necessary. |
1000 if (start_debugger) { | 968 if (start_debugger) { |
1001 ASSERT(debug_port >= 0); | 969 ASSERT(debug_port >= 0); |
1002 bool print_msg = verbose_debug_seen || (debug_port == 0); | 970 bool print_msg = verbose_debug_seen || (debug_port == 0); |
1003 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); | 971 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); |
1004 if (print_msg) { | 972 if (print_msg) { |
1005 Log::Print("Debugger listening on port %d\n", debug_port); | 973 Log::Print("Debugger listening on port %d\n", debug_port); |
1006 } | 974 } |
| 975 } else { |
| 976 DebuggerConnectionHandler::InitForVmService(); |
1007 } | 977 } |
1008 | 978 |
1009 ASSERT(Dart_CurrentIsolate() == NULL); | 979 Dart_RegisterIsolateServiceRequestCallback( |
1010 // Start the VM service isolate, if necessary. | |
1011 if (start_vm_service) { | |
1012 if (!start_debugger) { | |
1013 DebuggerConnectionHandler::InitForVmService(); | |
1014 } | |
1015 bool r = VmService::Start(vm_service_server_ip, vm_service_server_port); | |
1016 if (!r) { | |
1017 Log::PrintErr("Could not start VM Service isolate %s\n", | |
1018 VmService::GetErrorMessage()); | |
1019 } | |
1020 Dart_RegisterIsolateServiceRequestCallback( | |
1021 "io", &ServiceRequestHandler, NULL); | 980 "io", &ServiceRequestHandler, NULL); |
1022 } | |
1023 ASSERT(Dart_CurrentIsolate() == NULL); | |
1024 | 981 |
1025 // Call CreateIsolateAndSetup which creates an isolate and loads up | 982 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1026 // the specified application script. | 983 // the specified application script. |
1027 char* error = NULL; | 984 char* error = NULL; |
1028 bool is_compile_error = false; | 985 bool is_compile_error = false; |
1029 char* isolate_name = BuildIsolateName(script_name, "main"); | 986 char* isolate_name = BuildIsolateName(script_name, "main"); |
1030 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 987 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1031 "main", | 988 "main", |
1032 commandline_package_root, | 989 commandline_package_root, |
1033 &error, | 990 &error, |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 exit(Process::GlobalExitCode()); | 1117 exit(Process::GlobalExitCode()); |
1161 } | 1118 } |
1162 | 1119 |
1163 } // namespace bin | 1120 } // namespace bin |
1164 } // namespace dart | 1121 } // namespace dart |
1165 | 1122 |
1166 int main(int argc, char** argv) { | 1123 int main(int argc, char** argv) { |
1167 dart::bin::main(argc, argv); | 1124 dart::bin::main(argc, argv); |
1168 UNREACHABLE(); | 1125 UNREACHABLE(); |
1169 } | 1126 } |
OLD | NEW |