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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // and listen for a debugger to connect. | 47 // and listen for a debugger to connect. |
48 static bool start_debugger = false; | 48 static bool start_debugger = false; |
49 static const char* debug_ip = NULL; | 49 static const char* debug_ip = NULL; |
50 static int debug_port = -1; | 50 static int debug_port = -1; |
51 static const char* DEFAULT_DEBUG_IP = "127.0.0.1"; | 51 static const char* DEFAULT_DEBUG_IP = "127.0.0.1"; |
52 static const int DEFAULT_DEBUG_PORT = 5858; | 52 static const int DEFAULT_DEBUG_PORT = 5858; |
53 | 53 |
54 // Value of the --package-root flag. | 54 // Value of the --package-root flag. |
55 // (This pointer points into an argv buffer and does not need to be | 55 // (This pointer points into an argv buffer and does not need to be |
56 // free'd.) | 56 // free'd.) |
57 static const char* commandline_package_root = NULL; | 57 static const char* package_root = NULL; |
58 | 58 |
59 | 59 |
60 // Global flag that is used to indicate that we want to compile all the | 60 // Global flag that is used to indicate that we want to compile all the |
61 // dart functions and not run anything. | 61 // dart functions and not run anything. |
62 static bool has_compile_all = false; | 62 static bool has_compile_all = false; |
63 | 63 |
64 // Global flag that is used to indicate that we want to print the source code | 64 // Global flag that is used to indicate that we want to print the source code |
65 // for script that is being run. | 65 // for script that is being run. |
66 static bool has_print_script = false; | 66 static bool has_print_script = false; |
67 | 67 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 return true; | 129 return true; |
130 } | 130 } |
131 | 131 |
132 | 132 |
133 static bool ProcessPackageRootOption(const char* arg, | 133 static bool ProcessPackageRootOption(const char* arg, |
134 CommandLineOptions* vm_options) { | 134 CommandLineOptions* vm_options) { |
135 ASSERT(arg != NULL); | 135 ASSERT(arg != NULL); |
136 if (*arg == '\0' || *arg == '-') { | 136 if (*arg == '\0' || *arg == '-') { |
137 return false; | 137 return false; |
138 } | 138 } |
139 commandline_package_root = arg; | 139 package_root = arg; |
140 return true; | 140 return true; |
141 } | 141 } |
142 | 142 |
143 | 143 |
144 static void* GetHashmapKeyFromString(char* key) { | 144 static void* GetHashmapKeyFromString(char* key) { |
145 return reinterpret_cast<void*>(key); | 145 return reinterpret_cast<void*>(key); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 static bool ExtractPortAndIP(const char *option_value, | 149 static bool ExtractPortAndIP(const char *option_value, |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 // Returns true on success, false on failure. | 549 // Returns true on success, false on failure. |
550 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, | 550 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
551 const char* main, | 551 const char* main, |
552 const char* package_root, | 552 void* data, |
553 char** error, | 553 char** error, |
554 bool* is_compile_error) { | 554 bool* is_compile_error) { |
555 ASSERT(script_uri != NULL); | 555 Dart_Isolate isolate = |
556 IsolateData* isolate_data = new IsolateData(script_uri, package_root); | 556 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
557 Dart_Isolate isolate = Dart_CreateIsolate( | |
558 script_uri, main, snapshot_buffer, isolate_data, error); | |
559 if (isolate == NULL) { | 557 if (isolate == NULL) { |
560 return NULL; | 558 return NULL; |
561 } | 559 } |
562 | 560 |
563 Dart_EnterScope(); | 561 Dart_EnterScope(); |
564 | 562 |
565 if (snapshot_buffer != NULL) { | 563 if (snapshot_buffer != NULL) { |
566 // Setup the native resolver as the snapshot does not carry it. | 564 // Setup the native resolver as the snapshot does not carry it. |
567 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 565 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
568 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 566 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
(...skipping 13 matching lines...) Expand all Loading... |
582 // available here in the case of script snapshot loading. | 580 // available here in the case of script snapshot loading. |
583 Dart_Handle builtin_lib = | 581 Dart_Handle builtin_lib = |
584 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 582 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
585 CHECK_RESULT(builtin_lib); | 583 CHECK_RESULT(builtin_lib); |
586 | 584 |
587 // Prepare for script loading by setting up the 'print' and 'timer' | 585 // Prepare for script loading by setting up the 'print' and 'timer' |
588 // closures and setting up 'package root' for URI resolution. | 586 // closures and setting up 'package root' for URI resolution. |
589 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 587 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
590 CHECK_RESULT(result); | 588 CHECK_RESULT(result); |
591 | 589 |
592 result = DartUtils::LoadScript(script_uri, builtin_lib); | 590 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); |
| 591 ASSERT(isolate_data != NULL); |
| 592 ASSERT(isolate_data->script_url != NULL); |
| 593 result = DartUtils::LoadScript(isolate_data->script_url, builtin_lib); |
593 CHECK_RESULT(result); | 594 CHECK_RESULT(result); |
594 | 595 |
595 // Run event-loop and wait for script loading to complete. | 596 // Run event-loop and wait for script loading to complete. |
596 result = Dart_RunLoop(); | 597 result = Dart_RunLoop(); |
597 CHECK_RESULT(result); | 598 CHECK_RESULT(result); |
598 | 599 |
599 Platform::SetPackageRoot(package_root); | 600 Platform::SetPackageRoot(package_root); |
600 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); | 601 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); |
601 CHECK_RESULT(io_lib_url); | 602 CHECK_RESULT(io_lib_url); |
602 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | 603 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
(...skipping 20 matching lines...) Expand all Loading... |
623 return NULL; | 624 return NULL; |
624 } | 625 } |
625 | 626 |
626 return isolate; | 627 return isolate; |
627 } | 628 } |
628 | 629 |
629 #undef CHECK_RESULT | 630 #undef CHECK_RESULT |
630 | 631 |
631 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | 632 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
632 const char* main, | 633 const char* main, |
633 const char* package_root, | |
634 void* data, char** error) { | 634 void* data, char** error) { |
635 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); | |
636 bool is_compile_error = false; | 635 bool is_compile_error = false; |
637 if (script_uri == NULL) { | 636 if (script_uri == NULL) { |
638 if (data == NULL) { | 637 if (data == NULL) { |
639 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 638 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
640 return NULL; | 639 return NULL; |
641 } | 640 } |
| 641 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); |
642 script_uri = parent_isolate_data->script_url; | 642 script_uri = parent_isolate_data->script_url; |
643 if (script_uri == NULL) { | 643 if (script_uri == NULL) { |
644 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 644 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
645 return NULL; | 645 return NULL; |
646 } | 646 } |
647 } | 647 } |
648 if (package_root == NULL) { | 648 IsolateData* isolate_data = new IsolateData(script_uri); |
649 package_root = parent_isolate_data->package_root; | |
650 } | |
651 return CreateIsolateAndSetupHelper(script_uri, | 649 return CreateIsolateAndSetupHelper(script_uri, |
652 main, | 650 main, |
653 package_root, | 651 isolate_data, |
654 error, | 652 error, |
655 &is_compile_error); | 653 &is_compile_error); |
656 } | 654 } |
657 | 655 |
658 | 656 |
659 #define CHECK_RESULT(result) \ | 657 #define CHECK_RESULT(result) \ |
660 if (Dart_IsError(result)) { \ | 658 if (Dart_IsError(result)) { \ |
661 *error = strdup(Dart_GetError(result)); \ | 659 *error = strdup(Dart_GetError(result)); \ |
662 Dart_ExitScope(); \ | 660 Dart_ExitScope(); \ |
663 Dart_ShutdownIsolate(); \ | 661 Dart_ShutdownIsolate(); \ |
664 return NULL; \ | 662 return NULL; \ |
665 } \ | 663 } \ |
666 | 664 |
667 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { | 665 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { |
668 const char* script_uri = DartUtils::kVMServiceLibURL; | 666 const char* script_uri = DartUtils::kVMServiceLibURL; |
669 IsolateData* isolate_data = new IsolateData(script_uri, NULL); | 667 IsolateData* isolate_data = new IsolateData(script_uri); |
670 Dart_Isolate isolate = | 668 Dart_Isolate isolate = |
671 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, | 669 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, |
672 error); | 670 error); |
673 if (isolate == NULL) { | 671 if (isolate == NULL) { |
674 return NULL; | 672 return NULL; |
675 } | 673 } |
676 Dart_EnterScope(); | 674 Dart_EnterScope(); |
677 if (snapshot_buffer != NULL) { | 675 if (snapshot_buffer != NULL) { |
678 // Setup the native resolver as the snapshot does not carry it. | 676 // Setup the native resolver as the snapshot does not carry it. |
679 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 677 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
680 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 678 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
681 } | 679 } |
682 // Set up the library tag handler for this isolate. | 680 // Set up the library tag handler for this isolate. |
683 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 681 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
684 CHECK_RESULT(result); | 682 CHECK_RESULT(result); |
685 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 683 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
686 CHECK_RESULT(result); | 684 CHECK_RESULT(result); |
687 // Prepare builtin and its dependent libraries for use to resolve URIs. | 685 // Prepare builtin and its dependent libraries for use to resolve URIs. |
688 Dart_Handle builtin_lib = | 686 Dart_Handle builtin_lib = |
689 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 687 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
690 CHECK_RESULT(builtin_lib); | 688 CHECK_RESULT(builtin_lib); |
691 // Prepare for script loading by setting up the 'print' and 'timer' | 689 // Prepare for script loading by setting up the 'print' and 'timer' |
692 // closures and setting up 'package root' for URI resolution. | 690 // closures and setting up 'package root' for URI resolution. |
693 result = DartUtils::PrepareForScriptLoading(NULL, builtin_lib); | 691 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
694 CHECK_RESULT(result); | 692 CHECK_RESULT(result); |
695 | 693 |
696 Dart_ExitScope(); | 694 Dart_ExitScope(); |
697 Dart_ExitIsolate(); | 695 Dart_ExitIsolate(); |
698 return isolate; | 696 return isolate; |
699 } | 697 } |
700 | 698 |
701 #undef CHECK_RESULT | 699 #undef CHECK_RESULT |
702 | 700 |
703 static void PrintVersion() { | 701 static void PrintVersion() { |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 Dart_RegisterIsolateServiceRequestCallback( | 1018 Dart_RegisterIsolateServiceRequestCallback( |
1021 "io", &ServiceRequestHandler, NULL); | 1019 "io", &ServiceRequestHandler, NULL); |
1022 } | 1020 } |
1023 ASSERT(Dart_CurrentIsolate() == NULL); | 1021 ASSERT(Dart_CurrentIsolate() == NULL); |
1024 | 1022 |
1025 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1023 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1026 // the specified application script. | 1024 // the specified application script. |
1027 char* error = NULL; | 1025 char* error = NULL; |
1028 bool is_compile_error = false; | 1026 bool is_compile_error = false; |
1029 char* isolate_name = BuildIsolateName(script_name, "main"); | 1027 char* isolate_name = BuildIsolateName(script_name, "main"); |
| 1028 IsolateData* isolate_data = new IsolateData(script_name); |
1030 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1029 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1031 "main", | 1030 "main", |
1032 commandline_package_root, | 1031 isolate_data, |
1033 &error, | 1032 &error, |
1034 &is_compile_error); | 1033 &is_compile_error); |
1035 if (isolate == NULL) { | 1034 if (isolate == NULL) { |
1036 Log::PrintErr("%s\n", error); | 1035 Log::PrintErr("%s\n", error); |
1037 free(error); | 1036 free(error); |
1038 delete [] isolate_name; | 1037 delete [] isolate_name; |
1039 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); | 1038 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); |
1040 } | 1039 } |
1041 delete [] isolate_name; | 1040 delete [] isolate_name; |
1042 | 1041 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 exit(Process::GlobalExitCode()); | 1159 exit(Process::GlobalExitCode()); |
1161 } | 1160 } |
1162 | 1161 |
1163 } // namespace bin | 1162 } // namespace bin |
1164 } // namespace dart | 1163 } // namespace dart |
1165 | 1164 |
1166 int main(int argc, char** argv) { | 1165 int main(int argc, char** argv) { |
1167 dart::bin::main(argc, argv); | 1166 dart::bin::main(argc, argv); |
1168 UNREACHABLE(); | 1167 UNREACHABLE(); |
1169 } | 1168 } |
OLD | NEW |