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

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

Issue 125103004: Move service into VM (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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/gen_snapshot.cc ('k') | runtime/bin/resources.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 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 } 417 }
418 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value); 418 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value);
419 if (Dart_IsError(result)) { 419 if (Dart_IsError(result)) {
420 return result; 420 return result;
421 } 421 }
422 } 422 }
423 return dart_arguments; 423 return dart_arguments;
424 } 424 }
425 425
426 426
427 #define CHECK_RESULT(result) \
428 if (Dart_IsError(result)) { \
429 *error = strdup(Dart_GetError(result)); \
430 *is_compile_error = Dart_IsCompilationError(result); \
431 Dart_ExitScope(); \
432 Dart_ShutdownIsolate(); \
433 return NULL; \
434 } \
435
436
437 static Dart_Handle EnvironmentCallback(Dart_Handle name) { 427 static Dart_Handle EnvironmentCallback(Dart_Handle name) {
438 uint8_t* utf8_array; 428 uint8_t* utf8_array;
439 intptr_t utf8_len; 429 intptr_t utf8_len;
440 Dart_Handle result = Dart_Null(); 430 Dart_Handle result = Dart_Null();
441 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len); 431 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len);
442 if (Dart_IsError(handle)) { 432 if (Dart_IsError(handle)) {
443 handle = Dart_ThrowException( 433 handle = Dart_ThrowException(
444 DartUtils::NewDartArgumentError(Dart_GetError(handle))); 434 DartUtils::NewDartArgumentError(Dart_GetError(handle)));
445 } else { 435 } else {
446 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1)); 436 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1));
(...skipping 12 matching lines...) Expand all
459 if (value != NULL) { 449 if (value != NULL) {
460 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value), 450 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value),
461 strlen(value)); 451 strlen(value));
462 } 452 }
463 free(name_chars); 453 free(name_chars);
464 } 454 }
465 return result; 455 return result;
466 } 456 }
467 457
468 458
459 #define CHECK_RESULT(result) \
460 if (Dart_IsError(result)) { \
461 *error = strdup(Dart_GetError(result)); \
462 *is_compile_error = Dart_IsCompilationError(result); \
463 Dart_ExitScope(); \
464 Dart_ShutdownIsolate(); \
465 return NULL; \
466 } \
467
469 // Returns true on success, false on failure. 468 // Returns true on success, false on failure.
470 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, 469 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
471 const char* main, 470 const char* main,
472 void* data, 471 void* data,
473 char** error, 472 char** error,
474 bool* is_compile_error) { 473 bool* is_compile_error) {
475 Dart_Isolate isolate = 474 Dart_Isolate isolate =
476 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); 475 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error);
477 if (isolate == NULL) { 476 if (isolate == NULL) {
478 return NULL; 477 return NULL;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 char errbuf[256]; 516 char errbuf[256];
518 snprintf(errbuf, sizeof(errbuf), 517 snprintf(errbuf, sizeof(errbuf),
519 "Expected a library when loading script: %s", 518 "Expected a library when loading script: %s",
520 script_uri); 519 script_uri);
521 *error = strdup(errbuf); 520 *error = strdup(errbuf);
522 Dart_ExitScope(); 521 Dart_ExitScope();
523 Dart_ShutdownIsolate(); 522 Dart_ShutdownIsolate();
524 return NULL; 523 return NULL;
525 } 524 }
526 525
526
527 Platform::SetPackageRoot(package_root); 527 Platform::SetPackageRoot(package_root);
528 Dart_Handle io_lib_url = DartUtils::NewString("dart:io"); 528 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL);
529 CHECK_RESULT(io_lib_url); 529 CHECK_RESULT(io_lib_url);
530 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); 530 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url);
531 CHECK_RESULT(io_lib); 531 CHECK_RESULT(io_lib);
532 Dart_Handle platform_class_name = DartUtils::NewString("Platform"); 532 Dart_Handle platform_type = DartUtils::GetDartType(DartUtils::kIOLibURL,
533 CHECK_RESULT(platform_class_name); 533 "Platform");
534 Dart_Handle platform_type =
535 Dart_GetType(io_lib, platform_class_name, 0, NULL);
536 CHECK_RESULT(platform_type); 534 CHECK_RESULT(platform_type);
537 Dart_Handle script_name_name = DartUtils::NewString("_nativeScript"); 535 Dart_Handle script_name = DartUtils::NewString("_nativeScript");
538 CHECK_RESULT(script_name_name); 536 CHECK_RESULT(script_name);
539 Dart_Handle dart_script = DartUtils::NewString(script_uri); 537 Dart_Handle dart_script = DartUtils::NewString(script_uri);
540 CHECK_RESULT(dart_script); 538 CHECK_RESULT(dart_script);
541 Dart_Handle set_script_name = 539 Dart_Handle set_script_name =
542 Dart_SetField(platform_type, script_name_name, dart_script); 540 Dart_SetField(platform_type, script_name, dart_script);
543 CHECK_RESULT(set_script_name); 541 CHECK_RESULT(set_script_name);
544 542
545 VmService::SendIsolateStartupMessage();
546
547 // Make the isolate runnable so that it is ready to handle messages. 543 // Make the isolate runnable so that it is ready to handle messages.
548 Dart_ExitScope(); 544 Dart_ExitScope();
549 Dart_ExitIsolate(); 545 Dart_ExitIsolate();
550 bool retval = Dart_IsolateMakeRunnable(isolate); 546 bool retval = Dart_IsolateMakeRunnable(isolate);
551 if (!retval) { 547 if (!retval) {
552 *error = strdup("Invalid isolate state - Unable to make it runnable"); 548 *error = strdup("Invalid isolate state - Unable to make it runnable");
553 Dart_EnterIsolate(isolate); 549 Dart_EnterIsolate(isolate);
554 Dart_ShutdownIsolate(); 550 Dart_ShutdownIsolate();
555 return NULL; 551 return NULL;
556 } 552 }
557 553
558 return isolate; 554 return isolate;
559 } 555 }
560 556
557 #undef CHECK_RESULT
561 558
562 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, 559 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri,
563 const char* main, 560 const char* main,
564 void* data, char** error) { 561 void* data, char** error) {
565 bool is_compile_error = false; 562 bool is_compile_error = false;
566 if (script_uri == NULL) { 563 if (script_uri == NULL) {
567 if (data == NULL) { 564 if (data == NULL) {
568 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); 565 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate");
569 return NULL; 566 return NULL;
570 } 567 }
571 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); 568 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data);
572 script_uri = parent_isolate_data->script_url; 569 script_uri = parent_isolate_data->script_url;
573 if (script_uri == NULL) { 570 if (script_uri == NULL) {
574 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); 571 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate");
575 return NULL; 572 return NULL;
576 } 573 }
577 } 574 }
578 IsolateData* isolate_data = new IsolateData(script_uri); 575 IsolateData* isolate_data = new IsolateData(script_uri);
579 return CreateIsolateAndSetupHelper(script_uri, 576 return CreateIsolateAndSetupHelper(script_uri,
580 main, 577 main,
581 isolate_data, 578 isolate_data,
582 error, 579 error,
583 &is_compile_error); 580 &is_compile_error);
584 } 581 }
585 582
586 583
584 #define CHECK_RESULT(result) \
585 if (Dart_IsError(result)) { \
586 *error = strdup(Dart_GetError(result)); \
587 Dart_ExitScope(); \
588 Dart_ShutdownIsolate(); \
589 return NULL; \
590 } \
591
592 static Dart_Isolate CreateServiceIsolate(void* data, char** error) {
593 const char* script_uri = DartUtils::kVMServiceLibURL;
594 IsolateData* isolate_data = new IsolateData(script_uri);
595 Dart_Isolate isolate =
596 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data,
597 error);
598 if (isolate == NULL) {
599 return NULL;
600 }
601 Dart_EnterScope();
602 if (snapshot_buffer != NULL) {
603 // Setup the native resolver as the snapshot does not carry it.
604 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
605 Builtin::SetNativeResolver(Builtin::kIOLibrary);
606 }
607 // Set up the library tag handler for this isolate.
608 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
609 CHECK_RESULT(result);
610 result = Dart_SetEnvironmentCallback(EnvironmentCallback);
611 CHECK_RESULT(result);
612 // Prepare builtin and its dependent libraries for use to resolve URIs.
613 Dart_Handle builtin_lib =
614 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
615 CHECK_RESULT(builtin_lib);
616 // Prepare for script loading by setting up the 'print' and 'timer'
617 // closures and setting up 'package root' for URI resolution.
618 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
619 CHECK_RESULT(result);
620 Platform::SetPackageRoot(package_root);
621 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL);
622 CHECK_RESULT(io_lib_url);
623 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url);
624 CHECK_RESULT(io_lib);
625 Dart_Handle platform_type = DartUtils::GetDartType(DartUtils::kIOLibURL,
626 "Platform");
627 CHECK_RESULT(platform_type);
628 Dart_Handle script_name = DartUtils::NewString("_nativeScript");
629 CHECK_RESULT(script_name);
630 Dart_Handle dart_script = DartUtils::NewString(script_uri);
631 CHECK_RESULT(dart_script);
632 Dart_Handle set_script_name =
633 Dart_SetField(platform_type, script_name, dart_script);
634 CHECK_RESULT(set_script_name);
635 Dart_ExitScope();
636 Dart_ExitIsolate();
637 return isolate;
638 }
639
640 #undef CHECK_RESULT
641
587 static void PrintVersion() { 642 static void PrintVersion() {
588 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); 643 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString());
589 } 644 }
590 645
591 646
592 static void PrintUsage() { 647 static void PrintUsage() {
593 Log::PrintErr( 648 Log::PrintErr(
594 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" 649 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"
595 "\n" 650 "\n"
596 "Executes the Dart script passed as <dart-script-file>.\n" 651 "Executes the Dart script passed as <dart-script-file>.\n"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 if (!Dart_IsError(error)) { 764 if (!Dart_IsError(error)) {
710 return; 765 return;
711 } 766 }
712 const int exit_code = Dart_IsCompilationError(error) ? 767 const int exit_code = Dart_IsCompilationError(error) ?
713 kCompilationErrorExitCode : kErrorExitCode; 768 kCompilationErrorExitCode : kErrorExitCode;
714 ErrorExit(exit_code, "%s\n", Dart_GetError(error)); 769 ErrorExit(exit_code, "%s\n", Dart_GetError(error));
715 } 770 }
716 771
717 772
718 static void ShutdownIsolate(void* callback_data) { 773 static void ShutdownIsolate(void* callback_data) {
719 VmService::VmServiceShutdownCallback(callback_data);
720 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 774 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data);
721 delete isolate_data; 775 delete isolate_data;
722 } 776 }
723 777
724 778
725 static Dart_Handle GenerateScriptSource() { 779 static Dart_Handle GenerateScriptSource() {
726 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary()); 780 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary());
727 if (Dart_IsError(library_url)) { 781 if (Dart_IsError(library_url)) {
728 return library_url; 782 return library_url;
729 } 783 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 } 859 }
806 860
807 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); 861 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
808 862
809 // Initialize the Dart VM. 863 // Initialize the Dart VM.
810 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, 864 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
811 DartUtils::OpenFile, 865 DartUtils::OpenFile,
812 DartUtils::ReadFile, 866 DartUtils::ReadFile,
813 DartUtils::WriteFile, 867 DartUtils::WriteFile,
814 DartUtils::CloseFile, 868 DartUtils::CloseFile,
815 DartUtils::EntropySource)) { 869 DartUtils::EntropySource,
870 CreateServiceIsolate)) {
816 fprintf(stderr, "%s", "VM initialization failed\n"); 871 fprintf(stderr, "%s", "VM initialization failed\n");
817 fflush(stderr); 872 fflush(stderr);
818 exit(kErrorExitCode); 873 exit(kErrorExitCode);
819 } 874 }
820 875
821 // Start the debugger wire protocol handler if necessary. 876 // Start the debugger wire protocol handler if necessary.
822 if (start_debugger) { 877 if (start_debugger) {
823 ASSERT(debug_port >= 0); 878 ASSERT(debug_port >= 0);
824 bool print_msg = verbose_debug_seen || (debug_port == 0); 879 bool print_msg = verbose_debug_seen || (debug_port == 0);
825 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); 880 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port);
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
985 exit(Process::GlobalExitCode()); 1040 exit(Process::GlobalExitCode());
986 } 1041 }
987 1042
988 } // namespace bin 1043 } // namespace bin
989 } // namespace dart 1044 } // namespace dart
990 1045
991 int main(int argc, char** argv) { 1046 int main(int argc, char** argv) {
992 dart::bin::main(argc, argv); 1047 dart::bin::main(argc, argv);
993 UNREACHABLE(); 1048 UNREACHABLE();
994 } 1049 }
OLDNEW
« no previous file with comments | « runtime/bin/gen_snapshot.cc ('k') | runtime/bin/resources.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698