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" |
11 | 11 |
12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
13 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
14 #include "bin/dbg_connection.h" | 14 #include "bin/dbg_connection.h" |
15 #include "bin/directory.h" | 15 #include "bin/directory.h" |
16 #include "bin/eventhandler.h" | 16 #include "bin/eventhandler.h" |
17 #include "bin/extensions.h" | 17 #include "bin/extensions.h" |
18 #include "bin/file.h" | 18 #include "bin/file.h" |
19 #include "bin/isolate_data.h" | 19 #include "bin/isolate_data.h" |
20 #include "bin/log.h" | 20 #include "bin/log.h" |
21 #include "bin/platform.h" | 21 #include "bin/platform.h" |
22 #include "bin/process.h" | 22 #include "bin/process.h" |
23 #include "bin/vmservice_impl.h" | 23 #include "bin/vmservice_impl.h" |
24 #include "platform/globals.h" | 24 #include "platform/globals.h" |
| 25 #include "platform/hashmap.h" |
25 | 26 |
26 namespace dart { | 27 namespace dart { |
27 namespace bin { | 28 namespace bin { |
28 | 29 |
29 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 30 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
30 // it is initialized to NULL. | 31 // it is initialized to NULL. |
31 extern const uint8_t* snapshot_buffer; | 32 extern const uint8_t* snapshot_buffer; |
32 | 33 |
33 // Global state that stores a pointer to the application script snapshot. | 34 // Global state that stores a pointer to the application script snapshot. |
34 static bool generate_script_snapshot = false; | 35 static bool generate_script_snapshot = false; |
(...skipping 30 matching lines...) Expand all Loading... |
65 // Global flag that is used to indicate that we want to print the source code | 66 // Global flag that is used to indicate that we want to print the source code |
66 // for script that is being run. | 67 // for script that is being run. |
67 static bool has_print_script = false; | 68 static bool has_print_script = false; |
68 | 69 |
69 | 70 |
70 // VM Service options. | 71 // VM Service options. |
71 static bool start_vm_service = false; | 72 static bool start_vm_service = false; |
72 static int vm_service_server_port = -1; | 73 static int vm_service_server_port = -1; |
73 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; | 74 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
74 | 75 |
| 76 // The environment provided through the command line using -D options. |
| 77 static dart::HashMap* environment = NULL; |
75 | 78 |
76 static bool IsValidFlag(const char* name, | 79 static bool IsValidFlag(const char* name, |
77 const char* prefix, | 80 const char* prefix, |
78 intptr_t prefix_length) { | 81 intptr_t prefix_length) { |
79 intptr_t name_length = strlen(name); | 82 intptr_t name_length = strlen(name); |
80 return ((name_length > prefix_length) && | 83 return ((name_length > prefix_length) && |
81 (strncmp(name, prefix, prefix_length) == 0)); | 84 (strncmp(name, prefix, prefix_length) == 0)); |
82 } | 85 } |
83 | 86 |
84 | 87 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 static bool ProcessPackageRootOption(const char* arg) { | 128 static bool ProcessPackageRootOption(const char* arg) { |
126 ASSERT(arg != NULL); | 129 ASSERT(arg != NULL); |
127 if (*arg == '\0' || *arg == '-') { | 130 if (*arg == '\0' || *arg == '-') { |
128 return false; | 131 return false; |
129 } | 132 } |
130 package_root = arg; | 133 package_root = arg; |
131 return true; | 134 return true; |
132 } | 135 } |
133 | 136 |
134 | 137 |
| 138 static void* GetHashmapKeyFromString(char* key) { |
| 139 return reinterpret_cast<void*>(key); |
| 140 } |
| 141 |
| 142 static bool ProcessEnvironmentOption(const char* arg) { |
| 143 ASSERT(arg != NULL); |
| 144 if (*arg == '\0') { |
| 145 // Ignore empty -D option. |
| 146 Log::PrintErr("No arguments given to -D option\n"); |
| 147 return true; |
| 148 } |
| 149 if (environment == NULL) { |
| 150 environment = new HashMap(&HashMap::SameStringValue, 4); |
| 151 } |
| 152 // Split the name=value part of the -Dname=value argument. |
| 153 char* name; |
| 154 char* value = NULL; |
| 155 const char* equals_pos = strchr(arg, '='); |
| 156 if (equals_pos == NULL) { |
| 157 // No equal sign (name without value) currently not supported. |
| 158 Log::PrintErr("No value given to -D option\n"); |
| 159 return false; |
| 160 } else { |
| 161 int name_len = equals_pos - arg; |
| 162 if (name_len == 0) { |
| 163 Log::PrintErr("No name given to -D option\n"); |
| 164 return false; |
| 165 } |
| 166 // Split name=value into name and value. |
| 167 name = reinterpret_cast<char*>(malloc(name_len + 1)); |
| 168 strncpy(name, arg, name_len); |
| 169 name[name_len] = '\0'; |
| 170 value = strdup(equals_pos + 1); |
| 171 } |
| 172 HashMap::Entry* entry = environment->Lookup( |
| 173 GetHashmapKeyFromString(name), HashMap::StringHash(name), true); |
| 174 ASSERT(entry != NULL); // Lookup adds an entry if key not found. |
| 175 entry->value = value; |
| 176 return true; |
| 177 } |
| 178 |
| 179 |
135 static bool ProcessCompileAllOption(const char* arg) { | 180 static bool ProcessCompileAllOption(const char* arg) { |
136 ASSERT(arg != NULL); | 181 ASSERT(arg != NULL); |
137 if (*arg != '\0') { | 182 if (*arg != '\0') { |
138 return false; | 183 return false; |
139 } | 184 } |
140 has_compile_all = true; | 185 has_compile_all = true; |
141 return true; | 186 return true; |
142 } | 187 } |
143 | 188 |
144 | 189 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 const char* option_name; | 284 const char* option_name; |
240 bool (*process)(const char* option); | 285 bool (*process)(const char* option); |
241 } main_options[] = { | 286 } main_options[] = { |
242 // Standard options shared with dart2js. | 287 // Standard options shared with dart2js. |
243 { "--version", ProcessVersionOption }, | 288 { "--version", ProcessVersionOption }, |
244 { "--help", ProcessHelpOption }, | 289 { "--help", ProcessHelpOption }, |
245 { "-h", ProcessHelpOption }, | 290 { "-h", ProcessHelpOption }, |
246 { "--verbose", ProcessVerboseOption }, | 291 { "--verbose", ProcessVerboseOption }, |
247 { "-v", ProcessVerboseOption }, | 292 { "-v", ProcessVerboseOption }, |
248 { "--package-root=", ProcessPackageRootOption }, | 293 { "--package-root=", ProcessPackageRootOption }, |
| 294 { "-D", ProcessEnvironmentOption }, |
249 // VM specific options to the standalone dart program. | 295 // VM specific options to the standalone dart program. |
250 { "--break-at=", ProcessBreakpointOption }, | 296 { "--break-at=", ProcessBreakpointOption }, |
251 { "--compile_all", ProcessCompileAllOption }, | 297 { "--compile_all", ProcessCompileAllOption }, |
252 { "--debug", ProcessDebugOption }, | 298 { "--debug", ProcessDebugOption }, |
253 { "--snapshot=", ProcessGenScriptSnapshotOption }, | 299 { "--snapshot=", ProcessGenScriptSnapshotOption }, |
254 { "--print-script", ProcessPrintScriptOption }, | 300 { "--print-script", ProcessPrintScriptOption }, |
255 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, | 301 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, |
256 { "--enable-vm-service", ProcessEnableVmServiceOption }, | 302 { "--enable-vm-service", ProcessEnableVmServiceOption }, |
257 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, | 303 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, |
258 { NULL, NULL } | 304 { NULL, NULL } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 #define CHECK_RESULT(result) \ | 441 #define CHECK_RESULT(result) \ |
396 if (Dart_IsError(result)) { \ | 442 if (Dart_IsError(result)) { \ |
397 *error = strdup(Dart_GetError(result)); \ | 443 *error = strdup(Dart_GetError(result)); \ |
398 *is_compile_error = Dart_IsCompilationError(result); \ | 444 *is_compile_error = Dart_IsCompilationError(result); \ |
399 Dart_ExitScope(); \ | 445 Dart_ExitScope(); \ |
400 Dart_ShutdownIsolate(); \ | 446 Dart_ShutdownIsolate(); \ |
401 return NULL; \ | 447 return NULL; \ |
402 } \ | 448 } \ |
403 | 449 |
404 | 450 |
| 451 static Dart_Handle EnvironmentCallback(Dart_Handle name) { |
| 452 uint8_t* utf8_array; |
| 453 intptr_t utf8_len; |
| 454 Dart_Handle result = Dart_Null(); |
| 455 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len); |
| 456 if (Dart_IsError(handle)) { |
| 457 handle = Dart_ThrowException( |
| 458 DartUtils::NewDartArgumentError(Dart_GetError(handle))); |
| 459 } else { |
| 460 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1)); |
| 461 memmove(name_chars, utf8_array, utf8_len); |
| 462 name_chars[utf8_len] = '\0'; |
| 463 const char* value = NULL; |
| 464 if (environment != NULL) { |
| 465 HashMap::Entry* entry = environment->Lookup( |
| 466 GetHashmapKeyFromString(name_chars), |
| 467 HashMap::StringHash(name_chars), |
| 468 false); |
| 469 if (entry != NULL) { |
| 470 value = reinterpret_cast<char*>(entry->value); |
| 471 } |
| 472 } |
| 473 if (value != NULL) { |
| 474 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value), |
| 475 strlen(value)); |
| 476 } |
| 477 free(name_chars); |
| 478 } |
| 479 return result; |
| 480 } |
| 481 |
| 482 |
405 // Returns true on success, false on failure. | 483 // Returns true on success, false on failure. |
406 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, | 484 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
407 const char* main, | 485 const char* main, |
408 void* data, | 486 void* data, |
409 char** error, | 487 char** error, |
410 bool* is_compile_error) { | 488 bool* is_compile_error) { |
411 Dart_Isolate isolate = | 489 Dart_Isolate isolate = |
412 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); | 490 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
413 if (isolate == NULL) { | 491 if (isolate == NULL) { |
414 return NULL; | 492 return NULL; |
415 } | 493 } |
416 | 494 |
417 Dart_EnterScope(); | 495 Dart_EnterScope(); |
418 | 496 |
419 if (snapshot_buffer != NULL) { | 497 if (snapshot_buffer != NULL) { |
420 // Setup the native resolver as the snapshot does not carry it. | 498 // Setup the native resolver as the snapshot does not carry it. |
421 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 499 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
422 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 500 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
423 } | 501 } |
424 | 502 |
425 // Set up the library tag handler for this isolate. | 503 // Set up the library tag handler for this isolate. |
426 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 504 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
427 CHECK_RESULT(result); | 505 CHECK_RESULT(result); |
428 | 506 |
| 507 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
| 508 CHECK_RESULT(result); |
| 509 |
429 // Load the specified application script into the newly created isolate. | 510 // Load the specified application script into the newly created isolate. |
430 | 511 |
431 // Prepare builtin and its dependent libraries for use to resolve URIs. | 512 // Prepare builtin and its dependent libraries for use to resolve URIs. |
432 // The builtin library is part of the core snapshot and would already be | 513 // The builtin library is part of the core snapshot and would already be |
433 // available here in the case of script snapshot loading. | 514 // available here in the case of script snapshot loading. |
434 Dart_Handle builtin_lib = | 515 Dart_Handle builtin_lib = |
435 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 516 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
436 CHECK_RESULT(builtin_lib); | 517 CHECK_RESULT(builtin_lib); |
437 | 518 |
438 // Prepare for script loading by setting up the 'print' and 'timer' | 519 // Prepare for script loading by setting up the 'print' and 'timer' |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 // Terminate process exit-code handler. | 980 // Terminate process exit-code handler. |
900 Process::TerminateExitCodeHandler(); | 981 Process::TerminateExitCodeHandler(); |
901 | 982 |
902 Dart_Cleanup(); | 983 Dart_Cleanup(); |
903 | 984 |
904 // Free copied argument strings if converted. | 985 // Free copied argument strings if converted. |
905 if (argv_converted) { | 986 if (argv_converted) { |
906 for (int i = 0; i < argc; i++) free(argv[i]); | 987 for (int i = 0; i < argc; i++) free(argv[i]); |
907 } | 988 } |
908 | 989 |
| 990 // Free environment if any. |
| 991 if (environment != NULL) { |
| 992 for (HashMap::Entry* p = environment->Start(); |
| 993 p != NULL; |
| 994 p = environment->Next(p)) { |
| 995 free(p->key); |
| 996 free(p->value); |
| 997 } |
| 998 free(environment); |
| 999 } |
| 1000 |
909 return Process::GlobalExitCode(); | 1001 return Process::GlobalExitCode(); |
910 } | 1002 } |
911 | 1003 |
912 } // namespace bin | 1004 } // namespace bin |
913 } // namespace dart | 1005 } // namespace dart |
914 | 1006 |
915 int main(int argc, char** argv) { | 1007 int main(int argc, char** argv) { |
916 return dart::bin::main(argc, argv); | 1008 return dart::bin::main(argc, argv); |
917 } | 1009 } |
OLD | NEW |