Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: runtime/bin/main.cc

Issue 50983002: Implement fromEnvironment on bool, int and String (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/include/dart_api.h » ('j') | runtime/include/dart_api.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 // Global flag that is used to indicate that we want to print the source code 65 // Global flag that is used to indicate that we want to print the source code
66 // for script that is being run. 66 // for script that is being run.
67 static bool has_print_script = false; 67 static bool has_print_script = false;
68 68
69 69
70 // VM Service options. 70 // VM Service options.
71 static bool start_vm_service = false; 71 static bool start_vm_service = false;
72 static int vm_service_server_port = -1; 72 static int vm_service_server_port = -1;
73 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; 73 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181;
74 74
75 // The configuration provided through the command line using -D options.
76 static HashMap* configuration = NULL;
Ivan Posva 2013/10/29 21:42:59 environment to match the renamed const factory con
Søren Gjesse 2013/10/30 11:44:02 Done.
75 77
76 static bool IsValidFlag(const char* name, 78 static bool IsValidFlag(const char* name,
77 const char* prefix, 79 const char* prefix,
78 intptr_t prefix_length) { 80 intptr_t prefix_length) {
79 intptr_t name_length = strlen(name); 81 intptr_t name_length = strlen(name);
80 return ((name_length > prefix_length) && 82 return ((name_length > prefix_length) &&
81 (strncmp(name, prefix, prefix_length) == 0)); 83 (strncmp(name, prefix, prefix_length) == 0));
82 } 84 }
83 85
84 86
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 static bool ProcessPackageRootOption(const char* arg) { 127 static bool ProcessPackageRootOption(const char* arg) {
126 ASSERT(arg != NULL); 128 ASSERT(arg != NULL);
127 if (*arg == '\0' || *arg == '-') { 129 if (*arg == '\0' || *arg == '-') {
128 return false; 130 return false;
129 } 131 }
130 package_root = arg; 132 package_root = arg;
131 return true; 133 return true;
132 } 134 }
133 135
134 136
137 static bool SameStringValue(void* key1, void* key2) {
138 return strcmp(reinterpret_cast<char*>(key1),
139 reinterpret_cast<char*>(key2)) == 0;
140 }
141
142 static void* GetHashmapKeyFromString(char* key) {
143 return reinterpret_cast<void*>(key);
144 }
145
146 static uint32_t GetHashmapHashFromString(char* key) {
147 return key == NULL ? 0 : *key;
Ivan Posva 2013/10/29 21:42:59 Why bother with a hash table if there are only 26
Søren Gjesse 2013/10/30 11:44:02 Added a real hash function to platform/hashmap.h a
148 }
149
150 static bool ProcessDefineOption(const char* arg) {
151 ASSERT(arg != NULL);
152 if (*arg == '\0') {
153 return false;
154 }
155 if (configuration == NULL) configuration = new HashMap(&SameStringValue, 4);
Ivan Posva 2013/10/29 21:42:59 if (...) { environment = ... }
Søren Gjesse 2013/10/30 11:44:02 Done.
156 char* name;
Ivan Posva 2013/10/29 21:42:59 Please add comments about what you are doing here.
Søren Gjesse 2013/10/30 11:44:02 Done.
157 char* value = NULL;
158 const char* equals_pos = strchr(arg, '=');
159 if (equals_pos == NULL) {
160 name = strdup(arg);
161 } else {
162 int name_len = equals_pos - arg;
163 if (name_len == 0) return false;
Ivan Posva 2013/10/29 21:42:59 if (...) { return false; }
Søren Gjesse 2013/10/30 11:44:02 Done.
164 name = reinterpret_cast<char*>(malloc(name_len + 1));
165 strncpy(name, arg, name_len);
166 *(name + name_len) = '\0';
Ivan Posva 2013/10/29 21:42:59 name[name_len] = '\0';
Søren Gjesse 2013/10/30 11:44:02 Done.
167 if (*(equals_pos + 1) != '\0') {
168 value = strdup(equals_pos + 1);
169 }
170 }
171 HashMap::Entry* entry = configuration->Lookup(
172 GetHashmapKeyFromString(name), GetHashmapHashFromString(name), true);
173 ASSERT(entry != NULL);
Ivan Posva 2013/10/29 21:42:59 In a debug VM you will fail, but in a release VM y
Søren Gjesse 2013/10/30 11:44:02 When Lookup is called with the last argument set t
174 entry->value = value != NULL ? value : const_cast<char*>("");
Ivan Posva 2013/10/29 21:42:59 (value != NULL) ?
Søren Gjesse 2013/10/30 11:44:02 That was wrong. A non-existent value is now passed
175 return true;
176 }
177
178
135 static bool ProcessCompileAllOption(const char* arg) { 179 static bool ProcessCompileAllOption(const char* arg) {
136 ASSERT(arg != NULL); 180 ASSERT(arg != NULL);
137 if (*arg != '\0') { 181 if (*arg != '\0') {
138 return false; 182 return false;
139 } 183 }
140 has_compile_all = true; 184 has_compile_all = true;
141 return true; 185 return true;
142 } 186 }
143 187
144 188
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 const char* option_name; 273 const char* option_name;
230 bool (*process)(const char* option); 274 bool (*process)(const char* option);
231 } main_options[] = { 275 } main_options[] = {
232 // Standard options shared with dart2js. 276 // Standard options shared with dart2js.
233 { "--version", ProcessVersionOption }, 277 { "--version", ProcessVersionOption },
234 { "--help", ProcessHelpOption }, 278 { "--help", ProcessHelpOption },
235 { "-h", ProcessHelpOption }, 279 { "-h", ProcessHelpOption },
236 { "--verbose", ProcessVerboseOption }, 280 { "--verbose", ProcessVerboseOption },
237 { "-v", ProcessVerboseOption }, 281 { "-v", ProcessVerboseOption },
238 { "--package-root=", ProcessPackageRootOption }, 282 { "--package-root=", ProcessPackageRootOption },
283 { "-D", ProcessDefineOption },
239 // VM specific options to the standalone dart program. 284 // VM specific options to the standalone dart program.
240 { "--break-at=", ProcessBreakpointOption }, 285 { "--break-at=", ProcessBreakpointOption },
241 { "--compile_all", ProcessCompileAllOption }, 286 { "--compile_all", ProcessCompileAllOption },
242 { "--debug", ProcessDebugOption }, 287 { "--debug", ProcessDebugOption },
243 { "--snapshot=", ProcessGenScriptSnapshotOption }, 288 { "--snapshot=", ProcessGenScriptSnapshotOption },
244 { "--print-script", ProcessPrintScriptOption }, 289 { "--print-script", ProcessPrintScriptOption },
245 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, 290 { "--check-function-fingerprints", ProcessFingerprintedFunctions },
246 { "--enable-vm-service", ProcessEnableVmServiceOption }, 291 { "--enable-vm-service", ProcessEnableVmServiceOption },
247 { NULL, NULL } 292 { NULL, NULL }
248 }; 293 };
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 #define CHECK_RESULT(result) \ 460 #define CHECK_RESULT(result) \
416 if (Dart_IsError(result)) { \ 461 if (Dart_IsError(result)) { \
417 *error = strdup(Dart_GetError(result)); \ 462 *error = strdup(Dart_GetError(result)); \
418 *is_compile_error = Dart_IsCompilationError(result); \ 463 *is_compile_error = Dart_IsCompilationError(result); \
419 Dart_ExitScope(); \ 464 Dart_ExitScope(); \
420 Dart_ShutdownIsolate(); \ 465 Dart_ShutdownIsolate(); \
421 return NULL; \ 466 return NULL; \
422 } \ 467 } \
423 468
424 469
470 static Dart_Handle ConfigCallback(Dart_ConfigType type, Dart_Handle name) {
471 uint8_t* utf8_array;
472 intptr_t utf8_len;
473 Dart_Handle result = Dart_Null();
474 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len);
475 if (Dart_IsError(handle)) {
476 handle = Dart_ThrowException(
477 DartUtils::NewDartArgumentError(Dart_GetError(handle)));
478 } else {
479 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1));
480 memmove(name_chars, utf8_array, utf8_len);
481 name_chars[utf8_len] = '\0';
482 const char* value = NULL;
483 if (configuration != NULL) {
484 HashMap::Entry* entry = configuration->Lookup(
485 GetHashmapKeyFromString(name_chars),
486 GetHashmapHashFromString(name_chars),
487 false);
488 if (entry != NULL) {
489 value = reinterpret_cast<char*>(entry->value);
490 }
491 }
492 if (value != NULL) {
493 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value),
494 strlen(value));
495 }
496 }
497 return result;
498 }
499
500
425 // Returns true on success, false on failure. 501 // Returns true on success, false on failure.
426 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, 502 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
427 const char* main, 503 const char* main,
428 void* data, 504 void* data,
429 char** error, 505 char** error,
430 bool* is_compile_error) { 506 bool* is_compile_error) {
431 Dart_Isolate isolate = 507 Dart_Isolate isolate =
432 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); 508 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error);
433 if (isolate == NULL) { 509 if (isolate == NULL) {
434 return NULL; 510 return NULL;
435 } 511 }
436 512
437 Dart_EnterScope(); 513 Dart_EnterScope();
438 514
439 if (snapshot_buffer != NULL) { 515 if (snapshot_buffer != NULL) {
440 // Setup the native resolver as the snapshot does not carry it. 516 // Setup the native resolver as the snapshot does not carry it.
441 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); 517 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
442 Builtin::SetNativeResolver(Builtin::kIOLibrary); 518 Builtin::SetNativeResolver(Builtin::kIOLibrary);
443 } 519 }
444 520
445 // Set up the library tag handler for this isolate. 521 // Set up the library tag handler for this isolate.
446 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 522 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
447 CHECK_RESULT(result); 523 CHECK_RESULT(result);
448 524
525 result = Dart_SetConfigCallback(ConfigCallback);
526 CHECK_RESULT(result);
527
449 // Load the specified application script into the newly created isolate. 528 // Load the specified application script into the newly created isolate.
450 529
451 // Prepare builtin and its dependent libraries for use to resolve URIs. 530 // Prepare builtin and its dependent libraries for use to resolve URIs.
452 // The builtin library is part of the core snapshot and would already be 531 // The builtin library is part of the core snapshot and would already be
453 // available here in the case of script snapshot loading. 532 // available here in the case of script snapshot loading.
454 Dart_Handle builtin_lib = 533 Dart_Handle builtin_lib =
455 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 534 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
456 CHECK_RESULT(builtin_lib); 535 CHECK_RESULT(builtin_lib);
457 536
458 // Prepare for script loading by setting up the 'print' and 'timer' 537 // Prepare for script loading by setting up the 'print' and 'timer'
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 1012
934 return Process::GlobalExitCode(); 1013 return Process::GlobalExitCode();
935 } 1014 }
936 1015
937 } // namespace bin 1016 } // namespace bin
938 } // namespace dart 1017 } // namespace dart
939 1018
940 int main(int argc, char** argv) { 1019 int main(int argc, char** argv) {
941 return dart::bin::main(argc, argv); 1020 return dart::bin::main(argc, argv);
942 } 1021 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_api.h » ('j') | runtime/include/dart_api.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698