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 char** error, | 553 char** error, |
554 bool* is_compile_error) { | 554 bool* is_compile_error) { |
555 Dart_Isolate isolate = | 555 ASSERT(script_uri != NULL); |
556 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); | 556 IsolateData* isolate_data = new IsolateData(script_uri, package_root); |
| 557 Dart_Isolate isolate = Dart_CreateIsolate( |
| 558 script_uri, main, snapshot_buffer, isolate_data, error); |
557 if (isolate == NULL) { | 559 if (isolate == NULL) { |
558 return NULL; | 560 return NULL; |
559 } | 561 } |
560 | 562 |
561 Dart_EnterScope(); | 563 Dart_EnterScope(); |
562 | 564 |
563 if (snapshot_buffer != NULL) { | 565 if (snapshot_buffer != NULL) { |
564 // Setup the native resolver as the snapshot does not carry it. | 566 // Setup the native resolver as the snapshot does not carry it. |
565 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 567 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
566 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 568 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
(...skipping 13 matching lines...) Expand all Loading... |
580 // available here in the case of script snapshot loading. | 582 // available here in the case of script snapshot loading. |
581 Dart_Handle builtin_lib = | 583 Dart_Handle builtin_lib = |
582 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 584 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
583 CHECK_RESULT(builtin_lib); | 585 CHECK_RESULT(builtin_lib); |
584 | 586 |
585 // Prepare for script loading by setting up the 'print' and 'timer' | 587 // Prepare for script loading by setting up the 'print' and 'timer' |
586 // closures and setting up 'package root' for URI resolution. | 588 // closures and setting up 'package root' for URI resolution. |
587 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 589 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
588 CHECK_RESULT(result); | 590 CHECK_RESULT(result); |
589 | 591 |
590 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); | 592 result = DartUtils::LoadScript(script_uri, builtin_lib); |
591 ASSERT(isolate_data != NULL); | |
592 ASSERT(isolate_data->script_url != NULL); | |
593 result = DartUtils::LoadScript(isolate_data->script_url, builtin_lib); | |
594 CHECK_RESULT(result); | 593 CHECK_RESULT(result); |
595 | 594 |
596 // Run event-loop and wait for script loading to complete. | 595 // Run event-loop and wait for script loading to complete. |
597 result = Dart_RunLoop(); | 596 result = Dart_RunLoop(); |
598 CHECK_RESULT(result); | 597 CHECK_RESULT(result); |
599 | 598 |
600 Platform::SetPackageRoot(package_root); | 599 Platform::SetPackageRoot(package_root); |
601 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); | 600 Dart_Handle io_lib_url = DartUtils::NewString(DartUtils::kIOLibURL); |
602 CHECK_RESULT(io_lib_url); | 601 CHECK_RESULT(io_lib_url); |
603 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | 602 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
(...skipping 20 matching lines...) Expand all Loading... |
624 return NULL; | 623 return NULL; |
625 } | 624 } |
626 | 625 |
627 return isolate; | 626 return isolate; |
628 } | 627 } |
629 | 628 |
630 #undef CHECK_RESULT | 629 #undef CHECK_RESULT |
631 | 630 |
632 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | 631 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
633 const char* main, | 632 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); |
635 bool is_compile_error = false; | 636 bool is_compile_error = false; |
636 if (script_uri == NULL) { | 637 if (script_uri == NULL) { |
637 if (data == NULL) { | 638 if (data == NULL) { |
638 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 639 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
639 return NULL; | 640 return NULL; |
640 } | 641 } |
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 IsolateData* isolate_data = new IsolateData(script_uri); | 648 if (package_root == NULL) { |
| 649 package_root = parent_isolate_data->package_root; |
| 650 } |
649 return CreateIsolateAndSetupHelper(script_uri, | 651 return CreateIsolateAndSetupHelper(script_uri, |
650 main, | 652 main, |
651 isolate_data, | 653 package_root, |
652 error, | 654 error, |
653 &is_compile_error); | 655 &is_compile_error); |
654 } | 656 } |
655 | 657 |
656 | 658 |
657 #define CHECK_RESULT(result) \ | 659 #define CHECK_RESULT(result) \ |
658 if (Dart_IsError(result)) { \ | 660 if (Dart_IsError(result)) { \ |
659 *error = strdup(Dart_GetError(result)); \ | 661 *error = strdup(Dart_GetError(result)); \ |
660 Dart_ExitScope(); \ | 662 Dart_ExitScope(); \ |
661 Dart_ShutdownIsolate(); \ | 663 Dart_ShutdownIsolate(); \ |
662 return NULL; \ | 664 return NULL; \ |
663 } \ | 665 } \ |
664 | 666 |
665 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { | 667 static Dart_Isolate CreateServiceIsolate(void* data, char** error) { |
666 const char* script_uri = DartUtils::kVMServiceLibURL; | 668 const char* script_uri = DartUtils::kVMServiceLibURL; |
667 IsolateData* isolate_data = new IsolateData(script_uri); | 669 IsolateData* isolate_data = new IsolateData(script_uri, NULL); |
668 Dart_Isolate isolate = | 670 Dart_Isolate isolate = |
669 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, | 671 Dart_CreateIsolate(script_uri, "main", snapshot_buffer, isolate_data, |
670 error); | 672 error); |
671 if (isolate == NULL) { | 673 if (isolate == NULL) { |
672 return NULL; | 674 return NULL; |
673 } | 675 } |
674 Dart_EnterScope(); | 676 Dart_EnterScope(); |
675 if (snapshot_buffer != NULL) { | 677 if (snapshot_buffer != NULL) { |
676 // Setup the native resolver as the snapshot does not carry it. | 678 // Setup the native resolver as the snapshot does not carry it. |
677 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 679 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
678 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 680 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
679 } | 681 } |
680 // Set up the library tag handler for this isolate. | 682 // Set up the library tag handler for this isolate. |
681 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 683 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
682 CHECK_RESULT(result); | 684 CHECK_RESULT(result); |
683 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 685 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
684 CHECK_RESULT(result); | 686 CHECK_RESULT(result); |
685 // Prepare builtin and its dependent libraries for use to resolve URIs. | 687 // Prepare builtin and its dependent libraries for use to resolve URIs. |
686 Dart_Handle builtin_lib = | 688 Dart_Handle builtin_lib = |
687 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 689 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
688 CHECK_RESULT(builtin_lib); | 690 CHECK_RESULT(builtin_lib); |
689 // Prepare for script loading by setting up the 'print' and 'timer' | 691 // Prepare for script loading by setting up the 'print' and 'timer' |
690 // closures and setting up 'package root' for URI resolution. | 692 // closures and setting up 'package root' for URI resolution. |
691 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 693 result = DartUtils::PrepareForScriptLoading(NULL, builtin_lib); |
692 CHECK_RESULT(result); | 694 CHECK_RESULT(result); |
693 | 695 |
694 Dart_ExitScope(); | 696 Dart_ExitScope(); |
695 Dart_ExitIsolate(); | 697 Dart_ExitIsolate(); |
696 return isolate; | 698 return isolate; |
697 } | 699 } |
698 | 700 |
699 #undef CHECK_RESULT | 701 #undef CHECK_RESULT |
700 | 702 |
701 static void PrintVersion() { | 703 static void PrintVersion() { |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 Dart_RegisterIsolateServiceRequestCallback( | 1020 Dart_RegisterIsolateServiceRequestCallback( |
1019 "io", &ServiceRequestHandler, NULL); | 1021 "io", &ServiceRequestHandler, NULL); |
1020 } | 1022 } |
1021 ASSERT(Dart_CurrentIsolate() == NULL); | 1023 ASSERT(Dart_CurrentIsolate() == NULL); |
1022 | 1024 |
1023 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1025 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1024 // the specified application script. | 1026 // the specified application script. |
1025 char* error = NULL; | 1027 char* error = NULL; |
1026 bool is_compile_error = false; | 1028 bool is_compile_error = false; |
1027 char* isolate_name = BuildIsolateName(script_name, "main"); | 1029 char* isolate_name = BuildIsolateName(script_name, "main"); |
1028 IsolateData* isolate_data = new IsolateData(script_name); | |
1029 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1030 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1030 "main", | 1031 "main", |
1031 isolate_data, | 1032 commandline_package_root, |
1032 &error, | 1033 &error, |
1033 &is_compile_error); | 1034 &is_compile_error); |
1034 if (isolate == NULL) { | 1035 if (isolate == NULL) { |
1035 Log::PrintErr("%s\n", error); | 1036 Log::PrintErr("%s\n", error); |
1036 free(error); | 1037 free(error); |
1037 delete [] isolate_name; | 1038 delete [] isolate_name; |
1038 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); | 1039 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); |
1039 } | 1040 } |
1040 delete [] isolate_name; | 1041 delete [] isolate_name; |
1041 | 1042 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1159 exit(Process::GlobalExitCode()); | 1160 exit(Process::GlobalExitCode()); |
1160 } | 1161 } |
1161 | 1162 |
1162 } // namespace bin | 1163 } // namespace bin |
1163 } // namespace dart | 1164 } // namespace dart |
1164 | 1165 |
1165 int main(int argc, char** argv) { | 1166 int main(int argc, char** argv) { |
1166 dart::bin::main(argc, argv); | 1167 dart::bin::main(argc, argv); |
1167 UNREACHABLE(); | 1168 UNREACHABLE(); |
1168 } | 1169 } |
OLD | NEW |