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

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

Issue 584023004: Service isolate rework (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/io_impl_sources.gypi ('k') | runtime/bin/platform.h » ('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) 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 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 // available here in the case of script snapshot loading. 582 // available here in the case of script snapshot loading.
583 Dart_Handle builtin_lib = 583 Dart_Handle builtin_lib =
584 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 584 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
585 CHECK_RESULT(builtin_lib); 585 CHECK_RESULT(builtin_lib);
586 586
587 // Prepare for script loading by setting up the 'print' and 'timer' 587 // Prepare for script loading by setting up the 'print' and 'timer'
588 // closures and setting up 'package root' for URI resolution. 588 // closures and setting up 'package root' for URI resolution.
589 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); 589 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
590 CHECK_RESULT(result); 590 CHECK_RESULT(result);
591 591
592
593 if (Dart_IsServiceIsolate(isolate)) {
594 // If this is the service isolate, load embedder specific bits and return.
595 if (!VmService::Setup(vm_service_server_ip, vm_service_server_port)) {
596 *error = strdup(VmService::GetErrorMessage());
597 return NULL;
598 }
599 Dart_ExitScope();
600 Dart_ExitIsolate();
601 return isolate;
602 }
603
604 // Load the script.
592 result = DartUtils::LoadScript(script_uri, builtin_lib); 605 result = DartUtils::LoadScript(script_uri, builtin_lib);
593 CHECK_RESULT(result); 606 CHECK_RESULT(result);
594 607
595 // Run event-loop and wait for script loading to complete. 608 // Run event-loop and wait for script loading to complete.
596 result = Dart_RunLoop(); 609 result = Dart_RunLoop();
597 CHECK_RESULT(result); 610 CHECK_RESULT(result);
598 611
599 Platform::SetPackageRoot(package_root); 612 Platform::SetPackageRoot(package_root);
600 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); 613
601 CHECK_RESULT(io_lib_url); 614 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 615
615 // Make the isolate runnable so that it is ready to handle messages. 616 // Make the isolate runnable so that it is ready to handle messages.
616 Dart_ExitScope(); 617 Dart_ExitScope();
617 Dart_ExitIsolate(); 618 Dart_ExitIsolate();
618 bool retval = Dart_IsolateMakeRunnable(isolate); 619 bool retval = Dart_IsolateMakeRunnable(isolate);
619 if (!retval) { 620 if (!retval) {
620 *error = strdup("Invalid isolate state - Unable to make it runnable"); 621 *error = strdup("Invalid isolate state - Unable to make it runnable");
621 Dart_EnterIsolate(isolate); 622 Dart_EnterIsolate(isolate);
622 Dart_ShutdownIsolate(); 623 Dart_ShutdownIsolate();
623 return NULL; 624 return NULL;
(...skipping 15 matching lines...) Expand all
639 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); 640 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate");
640 return NULL; 641 return NULL;
641 } 642 }
642 script_uri = parent_isolate_data->script_url; 643 script_uri = parent_isolate_data->script_url;
643 if (script_uri == NULL) { 644 if (script_uri == NULL) {
644 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); 645 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate");
645 return NULL; 646 return NULL;
646 } 647 }
647 } 648 }
648 if (package_root == NULL) { 649 if (package_root == NULL) {
649 package_root = parent_isolate_data->package_root; 650 if (parent_isolate_data != NULL) {
651 package_root = parent_isolate_data->package_root;
652 } else {
653 package_root = ".";
654 }
650 } 655 }
651 return CreateIsolateAndSetupHelper(script_uri, 656 return CreateIsolateAndSetupHelper(script_uri,
652 main, 657 main,
653 package_root, 658 package_root,
654 error, 659 error,
655 &is_compile_error); 660 &is_compile_error);
656 } 661 }
657 662
658 663
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() { 664 static void PrintVersion() {
704 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); 665 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString());
705 } 666 }
706 667
707 668
708 static void PrintUsage() { 669 static void PrintUsage() {
709 Log::PrintErr( 670 Log::PrintErr(
710 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" 671 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"
711 "\n" 672 "\n"
712 "Executes the Dart script passed as <dart-script-file>.\n" 673 "Executes the Dart script passed as <dart-script-file>.\n"
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 934
974 if (!DartUtils::SetOriginalWorkingDirectory()) { 935 if (!DartUtils::SetOriginalWorkingDirectory()) {
975 OSError err; 936 OSError err;
976 fprintf(stderr, "Error determining current directory: %s\n", err.message()); 937 fprintf(stderr, "Error determining current directory: %s\n", err.message());
977 fflush(stderr); 938 fflush(stderr);
978 exit(kErrorExitCode); 939 exit(kErrorExitCode);
979 } 940 }
980 941
981 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); 942 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
982 943
944 // Start event handler.
945 EventHandler::Start();
946
983 // Initialize the Dart VM. 947 // Initialize the Dart VM.
984 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, 948 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
985 DartUtils::OpenFile, 949 DartUtils::OpenFile,
986 DartUtils::ReadFile, 950 DartUtils::ReadFile,
987 DartUtils::WriteFile, 951 DartUtils::WriteFile,
988 DartUtils::CloseFile, 952 DartUtils::CloseFile,
989 DartUtils::EntropySource, 953 DartUtils::EntropySource)) {
990 CreateServiceIsolate)) {
991 fprintf(stderr, "%s", "VM initialization failed\n"); 954 fprintf(stderr, "%s", "VM initialization failed\n");
992 fflush(stderr); 955 fflush(stderr);
993 exit(kErrorExitCode); 956 exit(kErrorExitCode);
994 } 957 }
995 958
996 // Start event handler. 959
997 EventHandler::Start();
998 960
999 // Start the debugger wire protocol handler if necessary. 961 // Start the debugger wire protocol handler if necessary.
1000 if (start_debugger) { 962 if (start_debugger) {
1001 ASSERT(debug_port >= 0); 963 ASSERT(debug_port >= 0);
1002 bool print_msg = verbose_debug_seen || (debug_port == 0); 964 bool print_msg = verbose_debug_seen || (debug_port == 0);
1003 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); 965 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port);
1004 if (print_msg) { 966 if (print_msg) {
1005 Log::Print("Debugger listening on port %d\n", debug_port); 967 Log::Print("Debugger listening on port %d\n", debug_port);
1006 } 968 }
969 } else {
970 DebuggerConnectionHandler::InitForVmService();
1007 } 971 }
1008 972
1009 ASSERT(Dart_CurrentIsolate() == NULL); 973 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); 974 "io", &ServiceRequestHandler, NULL);
1022 }
1023 ASSERT(Dart_CurrentIsolate() == NULL);
1024 975
1025 // Call CreateIsolateAndSetup which creates an isolate and loads up 976 // Call CreateIsolateAndSetup which creates an isolate and loads up
1026 // the specified application script. 977 // the specified application script.
1027 char* error = NULL; 978 char* error = NULL;
1028 bool is_compile_error = false; 979 bool is_compile_error = false;
1029 char* isolate_name = BuildIsolateName(script_name, "main"); 980 char* isolate_name = BuildIsolateName(script_name, "main");
1030 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, 981 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
1031 "main", 982 "main",
1032 commandline_package_root, 983 commandline_package_root,
1033 &error, 984 &error,
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 exit(Process::GlobalExitCode()); 1111 exit(Process::GlobalExitCode());
1161 } 1112 }
1162 1113
1163 } // namespace bin 1114 } // namespace bin
1164 } // namespace dart 1115 } // namespace dart
1165 1116
1166 int main(int argc, char** argv) { 1117 int main(int argc, char** argv) {
1167 dart::bin::main(argc, argv); 1118 dart::bin::main(argc, argv);
1168 UNREACHABLE(); 1119 UNREACHABLE();
1169 } 1120 }
OLDNEW
« no previous file with comments | « runtime/bin/io_impl_sources.gypi ('k') | runtime/bin/platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698