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* package_root = NULL; | 57 static const char* commandline_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 package_root = arg; | 139 commandline_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 void* data, | 552 const char* package_root, |
553 IsolateData* isolate_data, | |
553 char** error, | 554 char** error, |
554 bool* is_compile_error) { | 555 bool* is_compile_error) { |
turnidge
2014/09/09 15:46:41
Thanks Anders.
I think I want to change my earlie
Anders Johnsen
2014/09/10 05:42:17
Done.
| |
555 Dart_Isolate isolate = | 556 Dart_Isolate isolate = Dart_CreateIsolate( |
556 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); | 557 script_uri, main, snapshot_buffer, isolate_data, error); |
557 if (isolate == NULL) { | 558 if (isolate == NULL) { |
558 return NULL; | 559 return NULL; |
559 } | 560 } |
560 | 561 |
561 Dart_EnterScope(); | 562 Dart_EnterScope(); |
562 | 563 |
563 if (snapshot_buffer != NULL) { | 564 if (snapshot_buffer != NULL) { |
564 // Setup the native resolver as the snapshot does not carry it. | 565 // Setup the native resolver as the snapshot does not carry it. |
565 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 566 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
566 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 567 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
567 } | 568 } |
568 | 569 |
569 // Set up the library tag handler for this isolate. | 570 // Set up the library tag handler for this isolate. |
570 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 571 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
571 CHECK_RESULT(result); | 572 CHECK_RESULT(result); |
572 | 573 |
573 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 574 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
574 CHECK_RESULT(result); | 575 CHECK_RESULT(result); |
575 | 576 |
576 // Load the specified application script into the newly created isolate. | 577 // Load the specified application script into the newly created isolate. |
577 | 578 |
578 // Prepare builtin and its dependent libraries for use to resolve URIs. | 579 // Prepare builtin and its dependent libraries for use to resolve URIs. |
579 // The builtin library is part of the core snapshot and would already be | 580 // The builtin library is part of the core snapshot and would already be |
580 // available here in the case of script snapshot loading. | 581 // available here in the case of script snapshot loading. |
581 Dart_Handle builtin_lib = | 582 Dart_Handle builtin_lib = |
582 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 583 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
583 CHECK_RESULT(builtin_lib); | 584 CHECK_RESULT(builtin_lib); |
584 | 585 |
586 ASSERT(isolate_data != NULL); | |
587 ASSERT(isolate_data->script_url != NULL); | |
turnidge
2014/09/09 15:46:41
Remove if you follow the above recommendation.
Anders Johnsen
2014/09/10 05:42:17
Done.
| |
588 | |
585 // Prepare for script loading by setting up the 'print' and 'timer' | 589 // Prepare for script loading by setting up the 'print' and 'timer' |
586 // closures and setting up 'package root' for URI resolution. | 590 // closures and setting up 'package root' for URI resolution. |
587 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 591 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
588 CHECK_RESULT(result); | 592 CHECK_RESULT(result); |
589 | 593 |
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); | 594 result = DartUtils::LoadScript(isolate_data->script_url, builtin_lib); |
turnidge
2014/09/09 15:46:41
Use script_uri instead of isolate_data->script_url
Anders Johnsen
2014/09/10 05:42:17
Done.
| |
594 CHECK_RESULT(result); | 595 CHECK_RESULT(result); |
595 | 596 |
596 // Run event-loop and wait for script loading to complete. | 597 // Run event-loop and wait for script loading to complete. |
597 result = Dart_RunLoop(); | 598 result = Dart_RunLoop(); |
598 CHECK_RESULT(result); | 599 CHECK_RESULT(result); |
599 | 600 |
600 Platform::SetPackageRoot(package_root); | 601 Platform::SetPackageRoot(package_root); |
601 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); | 602 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); |
602 CHECK_RESULT(io_lib_url); | 603 CHECK_RESULT(io_lib_url); |
603 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | 604 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
(...skipping 20 matching lines...) Expand all Loading... | |
624 return NULL; | 625 return NULL; |
625 } | 626 } |
626 | 627 |
627 return isolate; | 628 return isolate; |
628 } | 629 } |
629 | 630 |
630 #undef CHECK_RESULT | 631 #undef CHECK_RESULT |
631 | 632 |
632 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | 633 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
633 const char* main, | 634 const char* main, |
635 const char* package_root, | |
634 void* data, char** error) { | 636 void* data, char** error) { |
637 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); | |
635 bool is_compile_error = false; | 638 bool is_compile_error = false; |
636 if (script_uri == NULL) { | 639 if (script_uri == NULL) { |
637 if (data == NULL) { | 640 if (data == NULL) { |
638 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 641 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
639 return NULL; | 642 return NULL; |
640 } | 643 } |
641 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); | |
642 script_uri = parent_isolate_data->script_url; | 644 script_uri = parent_isolate_data->script_url; |
643 if (script_uri == NULL) { | 645 if (script_uri == NULL) { |
644 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 646 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
645 return NULL; | 647 return NULL; |
646 } | 648 } |
647 } | 649 } |
648 IsolateData* isolate_data = new IsolateData(script_uri); | 650 if (package_root == NULL) { |
651 package_root = parent_isolate_data->package_root; | |
652 } | |
653 IsolateData* isolate_data = new IsolateData(script_uri, package_root); | |
turnidge
2014/09/09 15:46:41
Creation moves into helper function.
Anders Johnsen
2014/09/10 05:42:16
Done.
| |
649 return CreateIsolateAndSetupHelper(script_uri, | 654 return CreateIsolateAndSetupHelper(script_uri, |
650 main, | 655 main, |
656 package_root, | |
651 isolate_data, | 657 isolate_data, |
652 error, | 658 error, |
653 &is_compile_error); | 659 &is_compile_error); |
654 } | 660 } |
655 | 661 |
656 | 662 |
657 #define CHECK_RESULT(result) \ | 663 #define CHECK_RESULT(result) \ |
658 if (Dart_IsError(result)) { \ | 664 if (Dart_IsError(result)) { \ |
659 *error = strdup(Dart_GetError(result)); \ | 665 *error = strdup(Dart_GetError(result)); \ |
660 Dart_ExitScope(); \ | 666 Dart_ExitScope(); \ |
661 Dart_ShutdownIsolate(); \ | 667 Dart_ShutdownIsolate(); \ |
662 return NULL; \ | 668 return NULL; \ |
663 } \ | 669 } \ |
664 | 670 |
665 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { | 671 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { |
666 const char* script_uri = DartUtils::kVMServiceLibURL; | 672 const char* script_uri = DartUtils::kVMServiceLibURL; |
667 IsolateData* isolate_data = new IsolateData(script_uri); | 673 IsolateData* isolate_data = new IsolateData(script_uri, NULL); |
668 Dart_Isolate isolate = | 674 Dart_Isolate isolate = |
669 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, | 675 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, |
670 error); | 676 error); |
671 if (isolate == NULL) { | 677 if (isolate == NULL) { |
672 return NULL; | 678 return NULL; |
673 } | 679 } |
674 Dart_EnterScope(); | 680 Dart_EnterScope(); |
675 if (snapshot_buffer != NULL) { | 681 if (snapshot_buffer != NULL) { |
676 // Setup the native resolver as the snapshot does not carry it. | 682 // Setup the native resolver as the snapshot does not carry it. |
677 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 683 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
678 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 684 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
679 } | 685 } |
680 // Set up the library tag handler for this isolate. | 686 // Set up the library tag handler for this isolate. |
681 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 687 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
682 CHECK_RESULT(result); | 688 CHECK_RESULT(result); |
683 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 689 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
684 CHECK_RESULT(result); | 690 CHECK_RESULT(result); |
685 // Prepare builtin and its dependent libraries for use to resolve URIs. | 691 // Prepare builtin and its dependent libraries for use to resolve URIs. |
686 Dart_Handle builtin_lib = | 692 Dart_Handle builtin_lib = |
687 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 693 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
688 CHECK_RESULT(builtin_lib); | 694 CHECK_RESULT(builtin_lib); |
689 // Prepare for script loading by setting up the 'print' and 'timer' | 695 // Prepare for script loading by setting up the 'print' and 'timer' |
690 // closures and setting up 'package root' for URI resolution. | 696 // closures and setting up 'package root' for URI resolution. |
691 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 697 result = DartUtils::PrepareForScriptLoading(NULL, builtin_lib); |
692 CHECK_RESULT(result); | 698 CHECK_RESULT(result); |
693 Platform::SetPackageRoot(package_root); | 699 Platform::SetPackageRoot(commandline_package_root); |
694 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); | 700 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); |
695 CHECK_RESULT(io_lib_url); | 701 CHECK_RESULT(io_lib_url); |
696 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | 702 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
697 CHECK_RESULT(io_lib); | 703 CHECK_RESULT(io_lib); |
698 Dart_Handle platform_type = DartUtils::GetDartType(DartUtils::kIOLibURL, | 704 Dart_Handle platform_type = DartUtils::GetDartType(DartUtils::kIOLibURL, |
699 "_Platform"); | 705 "_Platform"); |
700 CHECK_RESULT(platform_type); | 706 CHECK_RESULT(platform_type); |
701 Dart_Handle script_name = DartUtils::NewString("_nativeScript"); | 707 Dart_Handle script_name = DartUtils::NewString("_nativeScript"); |
702 CHECK_RESULT(script_name); | 708 CHECK_RESULT(script_name); |
703 Dart_Handle dart_script = DartUtils::NewString(script_uri); | 709 Dart_Handle dart_script = DartUtils::NewString(script_uri); |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1032 Dart_RegisterIsolateServiceRequestCallback( | 1038 Dart_RegisterIsolateServiceRequestCallback( |
1033 "io", &ServiceRequestHandler, NULL); | 1039 "io", &ServiceRequestHandler, NULL); |
1034 } | 1040 } |
1035 ASSERT(Dart_CurrentIsolate() == NULL); | 1041 ASSERT(Dart_CurrentIsolate() == NULL); |
1036 | 1042 |
1037 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1043 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1038 // the specified application script. | 1044 // the specified application script. |
1039 char* error = NULL; | 1045 char* error = NULL; |
1040 bool is_compile_error = false; | 1046 bool is_compile_error = false; |
1041 char* isolate_name = BuildIsolateName(script_name, "main"); | 1047 char* isolate_name = BuildIsolateName(script_name, "main"); |
1042 IsolateData* isolate_data = new IsolateData(script_name); | 1048 IsolateData* isolate_data = new IsolateData(script_name, |
1049 commandline_package_root); | |
turnidge
2014/09/09 15:46:41
Creation moves into helper function.
Anders Johnsen
2014/09/10 05:42:17
Done.
| |
1043 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1050 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1044 "main", | 1051 "main", |
1052 commandline_package_root, | |
1045 isolate_data, | 1053 isolate_data, |
1046 &error, | 1054 &error, |
1047 &is_compile_error); | 1055 &is_compile_error); |
1048 if (isolate == NULL) { | 1056 if (isolate == NULL) { |
1049 Log::PrintErr("%s\n", error); | 1057 Log::PrintErr("%s\n", error); |
1050 free(error); | 1058 free(error); |
1051 delete [] isolate_name; | 1059 delete [] isolate_name; |
1052 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); | 1060 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); |
1053 } | 1061 } |
1054 delete [] isolate_name; | 1062 delete [] isolate_name; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1173 exit(Process::GlobalExitCode()); | 1181 exit(Process::GlobalExitCode()); |
1174 } | 1182 } |
1175 | 1183 |
1176 } // namespace bin | 1184 } // namespace bin |
1177 } // namespace dart | 1185 } // namespace dart |
1178 | 1186 |
1179 int main(int argc, char** argv) { | 1187 int main(int argc, char** argv) { |
1180 dart::bin::main(argc, argv); | 1188 dart::bin::main(argc, argv); |
1181 UNREACHABLE(); | 1189 UNREACHABLE(); |
1182 } | 1190 } |
OLD | NEW |