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

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: Add support for negative ints. Add more tests. 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') | sdk/lib/core/bool.dart » ('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* environment = NULL;
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 void* GetHashmapKeyFromString(char* key) {
138 return reinterpret_cast<void*>(key);
139 }
140
141 static bool ProcessDefineOption(const char* arg) {
142 ASSERT(arg != NULL);
143 if (*arg == '\0') {
144 // Ignore empty -D option.
145 Log::PrintErr("No arguments given to -D option\n");
146 return true;
147 }
148 if (environment == NULL) {
149 environment = new HashMap(&HashMap::SameStringValue, 4);
150 }
151 // Split the name=value part of the -Dname=value argument.
152 char* name;
153 char* value = NULL;
154 const char* equals_pos = strchr(arg, '=');
155 if (equals_pos == NULL) {
156 // No equal sign (name without value) currently not supported.
157 Log::PrintErr("No value given to -D option\n");
158 return false;
159 } else {
160 int name_len = equals_pos - arg;
161 if (name_len == 0) {
162 Log::PrintErr("No name given to -D option\n");
163 return false;
164 }
165 // Split name=value into name and value.
166 name = reinterpret_cast<char*>(malloc(name_len + 1));
167 strncpy(name, arg, name_len);
168 name[name_len] = '\0';
169 value = strdup(equals_pos + 1);
170 }
171 HashMap::Entry* entry = environment->Lookup(
172 GetHashmapKeyFromString(name), HashMap::StringHash(name), true);
173 ASSERT(entry != NULL); // Lookup adds an entry if key not found.
174 entry->value = value;
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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 const char* option_name; 283 const char* option_name;
240 bool (*process)(const char* option); 284 bool (*process)(const char* option);
241 } main_options[] = { 285 } main_options[] = {
242 // Standard options shared with dart2js. 286 // Standard options shared with dart2js.
243 { "--version", ProcessVersionOption }, 287 { "--version", ProcessVersionOption },
244 { "--help", ProcessHelpOption }, 288 { "--help", ProcessHelpOption },
245 { "-h", ProcessHelpOption }, 289 { "-h", ProcessHelpOption },
246 { "--verbose", ProcessVerboseOption }, 290 { "--verbose", ProcessVerboseOption },
247 { "-v", ProcessVerboseOption }, 291 { "-v", ProcessVerboseOption },
248 { "--package-root=", ProcessPackageRootOption }, 292 { "--package-root=", ProcessPackageRootOption },
293 { "-D", ProcessDefineOption },
249 // VM specific options to the standalone dart program. 294 // VM specific options to the standalone dart program.
250 { "--break-at=", ProcessBreakpointOption }, 295 { "--break-at=", ProcessBreakpointOption },
251 { "--compile_all", ProcessCompileAllOption }, 296 { "--compile_all", ProcessCompileAllOption },
252 { "--debug", ProcessDebugOption }, 297 { "--debug", ProcessDebugOption },
253 { "--snapshot=", ProcessGenScriptSnapshotOption }, 298 { "--snapshot=", ProcessGenScriptSnapshotOption },
254 { "--print-script", ProcessPrintScriptOption }, 299 { "--print-script", ProcessPrintScriptOption },
255 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, 300 { "--check-function-fingerprints", ProcessFingerprintedFunctions },
256 { "--enable-vm-service", ProcessEnableVmServiceOption }, 301 { "--enable-vm-service", ProcessEnableVmServiceOption },
257 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, 302 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption },
258 { NULL, NULL } 303 { NULL, NULL }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 #define CHECK_RESULT(result) \ 471 #define CHECK_RESULT(result) \
427 if (Dart_IsError(result)) { \ 472 if (Dart_IsError(result)) { \
428 *error = strdup(Dart_GetError(result)); \ 473 *error = strdup(Dart_GetError(result)); \
429 *is_compile_error = Dart_IsCompilationError(result); \ 474 *is_compile_error = Dart_IsCompilationError(result); \
430 Dart_ExitScope(); \ 475 Dart_ExitScope(); \
431 Dart_ShutdownIsolate(); \ 476 Dart_ShutdownIsolate(); \
432 return NULL; \ 477 return NULL; \
433 } \ 478 } \
434 479
435 480
481 static Dart_Handle ConfigCallback(Dart_Handle name) {
482 uint8_t* utf8_array;
483 intptr_t utf8_len;
484 Dart_Handle result = Dart_Null();
485 Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len);
486 if (Dart_IsError(handle)) {
487 handle = Dart_ThrowException(
488 DartUtils::NewDartArgumentError(Dart_GetError(handle)));
489 } else {
490 char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1));
491 memmove(name_chars, utf8_array, utf8_len);
492 name_chars[utf8_len] = '\0';
493 const char* value = NULL;
494 if (environment != NULL) {
495 HashMap::Entry* entry = environment->Lookup(
496 GetHashmapKeyFromString(name_chars),
497 HashMap::StringHash(name_chars),
498 false);
499 if (entry != NULL) {
500 value = reinterpret_cast<char*>(entry->value);
501 }
502 }
503 if (value != NULL) {
504 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value),
505 strlen(value));
506 }
507 }
508 return result;
509 }
510
511
436 // Returns true on success, false on failure. 512 // Returns true on success, false on failure.
437 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, 513 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
438 const char* main, 514 const char* main,
439 void* data, 515 void* data,
440 char** error, 516 char** error,
441 bool* is_compile_error) { 517 bool* is_compile_error) {
442 Dart_Isolate isolate = 518 Dart_Isolate isolate =
443 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); 519 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error);
444 if (isolate == NULL) { 520 if (isolate == NULL) {
445 return NULL; 521 return NULL;
446 } 522 }
447 523
448 Dart_EnterScope(); 524 Dart_EnterScope();
449 525
450 if (snapshot_buffer != NULL) { 526 if (snapshot_buffer != NULL) {
451 // Setup the native resolver as the snapshot does not carry it. 527 // Setup the native resolver as the snapshot does not carry it.
452 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); 528 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
453 Builtin::SetNativeResolver(Builtin::kIOLibrary); 529 Builtin::SetNativeResolver(Builtin::kIOLibrary);
454 } 530 }
455 531
456 // Set up the library tag handler for this isolate. 532 // Set up the library tag handler for this isolate.
457 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 533 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
458 CHECK_RESULT(result); 534 CHECK_RESULT(result);
459 535
536 result = Dart_SetConfigCallback(ConfigCallback);
537 CHECK_RESULT(result);
538
460 // Load the specified application script into the newly created isolate. 539 // Load the specified application script into the newly created isolate.
461 540
462 // Prepare builtin and its dependent libraries for use to resolve URIs. 541 // 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 542 // The builtin library is part of the core snapshot and would already be
464 // available here in the case of script snapshot loading. 543 // available here in the case of script snapshot loading.
465 Dart_Handle builtin_lib = 544 Dart_Handle builtin_lib =
466 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 545 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
467 CHECK_RESULT(builtin_lib); 546 CHECK_RESULT(builtin_lib);
468 547
469 // Prepare for script loading by setting up the 'print' and 'timer' 548 // Prepare for script loading by setting up the 'print' and 'timer'
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 1023
945 return Process::GlobalExitCode(); 1024 return Process::GlobalExitCode();
946 } 1025 }
947 1026
948 } // namespace bin 1027 } // namespace bin
949 } // namespace dart 1028 } // namespace dart
950 1029
951 int main(int argc, char** argv) { 1030 int main(int argc, char** argv) {
952 return dart::bin::main(argc, argv); 1031 return dart::bin::main(argc, argv);
953 } 1032 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_api.h » ('j') | sdk/lib/core/bool.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698