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 configuration provided through the command line using -D options. | |
Ivan Posva
2013/10/30 20:44:43
Please change all mentions of "configuration" or "
Søren Gjesse
2013/10/30 21:24:34
Done. That should be the last, the raw patch file
| |
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 ProcessDefineOption(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", ProcessDefineOption }, | |
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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 #define CHECK_RESULT(result) \ | 472 #define CHECK_RESULT(result) \ |
427 if (Dart_IsError(result)) { \ | 473 if (Dart_IsError(result)) { \ |
428 *error = strdup(Dart_GetError(result)); \ | 474 *error = strdup(Dart_GetError(result)); \ |
429 *is_compile_error = Dart_IsCompilationError(result); \ | 475 *is_compile_error = Dart_IsCompilationError(result); \ |
430 Dart_ExitScope(); \ | 476 Dart_ExitScope(); \ |
431 Dart_ShutdownIsolate(); \ | 477 Dart_ShutdownIsolate(); \ |
432 return NULL; \ | 478 return NULL; \ |
433 } \ | 479 } \ |
434 | 480 |
435 | 481 |
482 static Dart_Handle ConfigCallback(Dart_Handle name) { | |
483 uint8_t* utf8_array; | |
484 intptr_t utf8_len; | |
485 Dart_Handle result = Dart_Null(); | |
486 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len); | |
487 if (Dart_IsError(handle)) { | |
488 handle = Dart_ThrowException( | |
489 DartUtils::NewDartArgumentError(Dart_GetError(handle))); | |
490 } else { | |
491 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1)); | |
Ivan Posva
2013/10/30 20:44:43
LEAK
Søren Gjesse
2013/10/30 21:24:34
Done.
| |
492 memmove(name_chars, utf8_array, utf8_len); | |
493 name_chars[utf8_len] = '\0'; | |
494 const char* value = NULL; | |
495 if (environment != NULL) { | |
496 HashMap::Entry* entry = environment->Lookup( | |
497 GetHashmapKeyFromString(name_chars), | |
498 HashMap::StringHash(name_chars), | |
499 false); | |
500 if (entry != NULL) { | |
501 value = reinterpret_cast<char*>(entry->value); | |
502 } | |
503 } | |
504 if (value != NULL) { | |
505 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value), | |
506 strlen(value)); | |
507 } | |
508 } | |
509 return result; | |
510 } | |
511 | |
512 | |
436 // Returns true on success, false on failure. | 513 // Returns true on success, false on failure. |
437 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, | 514 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
438 const char* main, | 515 const char* main, |
439 void* data, | 516 void* data, |
440 char** error, | 517 char** error, |
441 bool* is_compile_error) { | 518 bool* is_compile_error) { |
442 Dart_Isolate isolate = | 519 Dart_Isolate isolate = |
443 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); | 520 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
444 if (isolate == NULL) { | 521 if (isolate == NULL) { |
445 return NULL; | 522 return NULL; |
446 } | 523 } |
447 | 524 |
448 Dart_EnterScope(); | 525 Dart_EnterScope(); |
449 | 526 |
450 if (snapshot_buffer != NULL) { | 527 if (snapshot_buffer != NULL) { |
451 // Setup the native resolver as the snapshot does not carry it. | 528 // Setup the native resolver as the snapshot does not carry it. |
452 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 529 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
453 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 530 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
454 } | 531 } |
455 | 532 |
456 // Set up the library tag handler for this isolate. | 533 // Set up the library tag handler for this isolate. |
457 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 534 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
458 CHECK_RESULT(result); | 535 CHECK_RESULT(result); |
459 | 536 |
537 result = Dart_SetConfigCallback(ConfigCallback); | |
538 CHECK_RESULT(result); | |
539 | |
460 // Load the specified application script into the newly created isolate. | 540 // Load the specified application script into the newly created isolate. |
461 | 541 |
462 // Prepare builtin and its dependent libraries for use to resolve URIs. | 542 // Prepare builtin and its dependent libraries for use to resolve URIs. |
463 // The builtin library is part of the core snapshot and would already be | 543 // The builtin library is part of the core snapshot and would already be |
464 // available here in the case of script snapshot loading. | 544 // available here in the case of script snapshot loading. |
465 Dart_Handle builtin_lib = | 545 Dart_Handle builtin_lib = |
466 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 546 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
467 CHECK_RESULT(builtin_lib); | 547 CHECK_RESULT(builtin_lib); |
468 | 548 |
469 // Prepare for script loading by setting up the 'print' and 'timer' | 549 // Prepare for script loading by setting up the 'print' and 'timer' |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
944 | 1024 |
945 return Process::GlobalExitCode(); | 1025 return Process::GlobalExitCode(); |
946 } | 1026 } |
947 | 1027 |
948 } // namespace bin | 1028 } // namespace bin |
949 } // namespace dart | 1029 } // namespace dart |
950 | 1030 |
951 int main(int argc, char** argv) { | 1031 int main(int argc, char** argv) { |
952 return dart::bin::main(argc, argv); | 1032 return dart::bin::main(argc, argv); |
953 } | 1033 } |
OLD | NEW |