OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // Define all of our flags default values. | 48 // Define all of our flags default values. |
49 #define FLAG_MODE_DEFINE_DEFAULTS | 49 #define FLAG_MODE_DEFINE_DEFAULTS |
50 #include "flag-definitions.h" | 50 #include "flag-definitions.h" |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 // This structure represents a single entry in the flag system, with a pointer | 54 // This structure represents a single entry in the flag system, with a pointer |
55 // to the actual flag, default value, comment, etc. This is designed to be POD | 55 // to the actual flag, default value, comment, etc. This is designed to be POD |
56 // initialized as to avoid requiring static constructors. | 56 // initialized as to avoid requiring static constructors. |
57 struct Flag { | 57 struct Flag { |
58 enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT, | 58 enum FlagType { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING, TYPE_ARGS }; |
59 TYPE_STRING, TYPE_ARGS }; | |
60 | 59 |
61 FlagType type_; // What type of flag, bool, int, or string. | 60 FlagType type_; // What type of flag, bool, int, or string. |
62 const char* name_; // Name of the flag, ex "my_flag". | 61 const char* name_; // Name of the flag, ex "my_flag". |
63 void* valptr_; // Pointer to the global flag variable. | 62 void* valptr_; // Pointer to the global flag variable. |
64 const void* defptr_; // Pointer to the default value. | 63 const void* defptr_; // Pointer to the default value. |
65 const char* cmt_; // A comment about the flags purpose. | 64 const char* cmt_; // A comment about the flags purpose. |
66 bool owns_ptr_; // Does the flag own its string value? | 65 bool owns_ptr_; // Does the flag own its string value? |
67 | 66 |
68 FlagType type() const { return type_; } | 67 FlagType type() const { return type_; } |
69 | 68 |
70 const char* name() const { return name_; } | 69 const char* name() const { return name_; } |
71 | 70 |
72 const char* comment() const { return cmt_; } | 71 const char* comment() const { return cmt_; } |
73 | 72 |
74 bool* bool_variable() const { | 73 bool* bool_variable() const { |
75 ASSERT(type_ == TYPE_BOOL); | 74 ASSERT(type_ == TYPE_BOOL); |
76 return reinterpret_cast<bool*>(valptr_); | 75 return reinterpret_cast<bool*>(valptr_); |
77 } | 76 } |
78 | 77 |
79 Maybe<bool>* maybe_bool_variable() const { | |
80 ASSERT(type_ == TYPE_MAYBE_BOOL); | |
81 return reinterpret_cast<Maybe<bool>*>(valptr_); | |
82 } | |
83 | |
84 int* int_variable() const { | 78 int* int_variable() const { |
85 ASSERT(type_ == TYPE_INT); | 79 ASSERT(type_ == TYPE_INT); |
86 return reinterpret_cast<int*>(valptr_); | 80 return reinterpret_cast<int*>(valptr_); |
87 } | 81 } |
88 | 82 |
89 double* float_variable() const { | 83 double* float_variable() const { |
90 ASSERT(type_ == TYPE_FLOAT); | 84 ASSERT(type_ == TYPE_FLOAT); |
91 return reinterpret_cast<double*>(valptr_); | 85 return reinterpret_cast<double*>(valptr_); |
92 } | 86 } |
93 | 87 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 JSArguments args_default() const { | 126 JSArguments args_default() const { |
133 ASSERT(type_ == TYPE_ARGS); | 127 ASSERT(type_ == TYPE_ARGS); |
134 return *reinterpret_cast<const JSArguments*>(defptr_); | 128 return *reinterpret_cast<const JSArguments*>(defptr_); |
135 } | 129 } |
136 | 130 |
137 // Compare this flag's current value against the default. | 131 // Compare this flag's current value against the default. |
138 bool IsDefault() const { | 132 bool IsDefault() const { |
139 switch (type_) { | 133 switch (type_) { |
140 case TYPE_BOOL: | 134 case TYPE_BOOL: |
141 return *bool_variable() == bool_default(); | 135 return *bool_variable() == bool_default(); |
142 case TYPE_MAYBE_BOOL: | |
143 return maybe_bool_variable()->has_value == false; | |
144 case TYPE_INT: | 136 case TYPE_INT: |
145 return *int_variable() == int_default(); | 137 return *int_variable() == int_default(); |
146 case TYPE_FLOAT: | 138 case TYPE_FLOAT: |
147 return *float_variable() == float_default(); | 139 return *float_variable() == float_default(); |
148 case TYPE_STRING: { | 140 case TYPE_STRING: { |
149 const char* str1 = string_value(); | 141 const char* str1 = string_value(); |
150 const char* str2 = string_default(); | 142 const char* str2 = string_default(); |
151 if (str2 == NULL) return str1 == NULL; | 143 if (str2 == NULL) return str1 == NULL; |
152 if (str1 == NULL) return str2 == NULL; | 144 if (str1 == NULL) return str2 == NULL; |
153 return strcmp(str1, str2) == 0; | 145 return strcmp(str1, str2) == 0; |
154 } | 146 } |
155 case TYPE_ARGS: | 147 case TYPE_ARGS: |
156 return args_variable()->argc() == 0; | 148 return args_variable()->argc() == 0; |
157 } | 149 } |
158 UNREACHABLE(); | 150 UNREACHABLE(); |
159 return true; | 151 return true; |
160 } | 152 } |
161 | 153 |
162 // Set a flag back to it's default value. | 154 // Set a flag back to it's default value. |
163 void Reset() { | 155 void Reset() { |
164 switch (type_) { | 156 switch (type_) { |
165 case TYPE_BOOL: | 157 case TYPE_BOOL: |
166 *bool_variable() = bool_default(); | 158 *bool_variable() = bool_default(); |
167 break; | 159 break; |
168 case TYPE_MAYBE_BOOL: | |
169 *maybe_bool_variable() = Maybe<bool>(); | |
170 break; | |
171 case TYPE_INT: | 160 case TYPE_INT: |
172 *int_variable() = int_default(); | 161 *int_variable() = int_default(); |
173 break; | 162 break; |
174 case TYPE_FLOAT: | 163 case TYPE_FLOAT: |
175 *float_variable() = float_default(); | 164 *float_variable() = float_default(); |
176 break; | 165 break; |
177 case TYPE_STRING: | 166 case TYPE_STRING: |
178 set_string_value(string_default(), false); | 167 set_string_value(string_default(), false); |
179 break; | 168 break; |
180 case TYPE_ARGS: | 169 case TYPE_ARGS: |
181 *args_variable() = args_default(); | 170 *args_variable() = args_default(); |
182 break; | 171 break; |
183 } | 172 } |
184 } | 173 } |
185 }; | 174 }; |
186 | 175 |
187 Flag flags[] = { | 176 Flag flags[] = { |
188 #define FLAG_MODE_META | 177 #define FLAG_MODE_META |
189 #include "flag-definitions.h" | 178 #include "flag-definitions.h" |
190 }; | 179 }; |
191 | 180 |
192 const size_t num_flags = sizeof(flags) / sizeof(*flags); | 181 const size_t num_flags = sizeof(flags) / sizeof(*flags); |
193 | 182 |
194 } // namespace | 183 } // namespace |
195 | 184 |
196 | 185 |
197 static const char* Type2String(Flag::FlagType type) { | 186 static const char* Type2String(Flag::FlagType type) { |
198 switch (type) { | 187 switch (type) { |
199 case Flag::TYPE_BOOL: return "bool"; | 188 case Flag::TYPE_BOOL: return "bool"; |
200 case Flag::TYPE_MAYBE_BOOL: return "maybe_bool"; | |
201 case Flag::TYPE_INT: return "int"; | 189 case Flag::TYPE_INT: return "int"; |
202 case Flag::TYPE_FLOAT: return "float"; | 190 case Flag::TYPE_FLOAT: return "float"; |
203 case Flag::TYPE_STRING: return "string"; | 191 case Flag::TYPE_STRING: return "string"; |
204 case Flag::TYPE_ARGS: return "arguments"; | 192 case Flag::TYPE_ARGS: return "arguments"; |
205 } | 193 } |
206 UNREACHABLE(); | 194 UNREACHABLE(); |
207 return NULL; | 195 return NULL; |
208 } | 196 } |
209 | 197 |
210 | 198 |
211 static SmartArrayPointer<const char> ToString(Flag* flag) { | 199 static SmartArrayPointer<const char> ToString(Flag* flag) { |
212 HeapStringAllocator string_allocator; | 200 HeapStringAllocator string_allocator; |
213 StringStream buffer(&string_allocator); | 201 StringStream buffer(&string_allocator); |
214 switch (flag->type()) { | 202 switch (flag->type()) { |
215 case Flag::TYPE_BOOL: | 203 case Flag::TYPE_BOOL: |
216 buffer.Add("%s", (*flag->bool_variable() ? "true" : "false")); | 204 buffer.Add("%s", (*flag->bool_variable() ? "true" : "false")); |
217 break; | 205 break; |
218 case Flag::TYPE_MAYBE_BOOL: | |
219 buffer.Add("%s", flag->maybe_bool_variable()->has_value | |
220 ? (flag->maybe_bool_variable()->value ? "true" : "false") | |
221 : "unset"); | |
222 break; | |
223 case Flag::TYPE_INT: | 206 case Flag::TYPE_INT: |
224 buffer.Add("%d", *flag->int_variable()); | 207 buffer.Add("%d", *flag->int_variable()); |
225 break; | 208 break; |
226 case Flag::TYPE_FLOAT: | 209 case Flag::TYPE_FLOAT: |
227 buffer.Add("%f", FmtElm(*flag->float_variable())); | 210 buffer.Add("%f", FmtElm(*flag->float_variable())); |
228 break; | 211 break; |
229 case Flag::TYPE_STRING: { | 212 case Flag::TYPE_STRING: { |
230 const char* str = flag->string_value(); | 213 const char* str = flag->string_value(); |
231 buffer.Add("%s", str ? str : "NULL"); | 214 buffer.Add("%s", str ? str : "NULL"); |
232 break; | 215 break; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 } else { | 373 } else { |
391 PrintF(stderr, "Error: unrecognized flag %s\n" | 374 PrintF(stderr, "Error: unrecognized flag %s\n" |
392 "Try --help for options\n", arg); | 375 "Try --help for options\n", arg); |
393 return_code = j; | 376 return_code = j; |
394 break; | 377 break; |
395 } | 378 } |
396 } | 379 } |
397 | 380 |
398 // if we still need a flag value, use the next argument if available | 381 // if we still need a flag value, use the next argument if available |
399 if (flag->type() != Flag::TYPE_BOOL && | 382 if (flag->type() != Flag::TYPE_BOOL && |
400 flag->type() != Flag::TYPE_MAYBE_BOOL && | |
401 flag->type() != Flag::TYPE_ARGS && | 383 flag->type() != Flag::TYPE_ARGS && |
402 value == NULL) { | 384 value == NULL) { |
403 if (i < *argc) { | 385 if (i < *argc) { |
404 value = argv[i++]; | 386 value = argv[i++]; |
405 } else { | 387 } else { |
406 PrintF(stderr, "Error: missing value for flag %s of type %s\n" | 388 PrintF(stderr, "Error: missing value for flag %s of type %s\n" |
407 "Try --help for options\n", | 389 "Try --help for options\n", |
408 arg, Type2String(flag->type())); | 390 arg, Type2String(flag->type())); |
409 return_code = j; | 391 return_code = j; |
410 break; | 392 break; |
411 } | 393 } |
412 } | 394 } |
413 | 395 |
414 // set the flag | 396 // set the flag |
415 char* endp = const_cast<char*>(""); // *endp is only read | 397 char* endp = const_cast<char*>(""); // *endp is only read |
416 switch (flag->type()) { | 398 switch (flag->type()) { |
417 case Flag::TYPE_BOOL: | 399 case Flag::TYPE_BOOL: |
418 *flag->bool_variable() = !is_bool; | 400 *flag->bool_variable() = !is_bool; |
419 break; | 401 break; |
420 case Flag::TYPE_MAYBE_BOOL: | |
421 *flag->maybe_bool_variable() = Maybe<bool>(!is_bool); | |
422 break; | |
423 case Flag::TYPE_INT: | 402 case Flag::TYPE_INT: |
424 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT | 403 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT |
425 break; | 404 break; |
426 case Flag::TYPE_FLOAT: | 405 case Flag::TYPE_FLOAT: |
427 *flag->float_variable() = strtod(value, &endp); | 406 *flag->float_variable() = strtod(value, &endp); |
428 break; | 407 break; |
429 case Flag::TYPE_STRING: | 408 case Flag::TYPE_STRING: |
430 flag->set_string_value(value ? StrDup(value) : NULL, true); | 409 flag->set_string_value(value ? StrDup(value) : NULL, true); |
431 break; | 410 break; |
432 case Flag::TYPE_ARGS: { | 411 case Flag::TYPE_ARGS: { |
433 int start_pos = (value == NULL) ? i : i - 1; | 412 int start_pos = (value == NULL) ? i : i - 1; |
434 int js_argc = *argc - start_pos; | 413 int js_argc = *argc - start_pos; |
435 const char** js_argv = NewArray<const char*>(js_argc); | 414 const char** js_argv = NewArray<const char*>(js_argc); |
436 if (value != NULL) { | 415 if (value != NULL) { |
437 js_argv[0] = StrDup(value); | 416 js_argv[0] = StrDup(value); |
438 } | 417 } |
439 for (int k = i; k < *argc; k++) { | 418 for (int k = i; k < *argc; k++) { |
440 js_argv[k - start_pos] = StrDup(argv[k]); | 419 js_argv[k - start_pos] = StrDup(argv[k]); |
441 } | 420 } |
442 *flag->args_variable() = JSArguments::Create(js_argc, js_argv); | 421 *flag->args_variable() = JSArguments::Create(js_argc, js_argv); |
443 i = *argc; // Consume all arguments | 422 i = *argc; // Consume all arguments |
444 break; | 423 break; |
445 } | 424 } |
446 } | 425 } |
447 | 426 |
448 // handle errors | 427 // handle errors |
449 bool is_bool_type = flag->type() == Flag::TYPE_BOOL || | 428 if ((flag->type() == Flag::TYPE_BOOL && value != NULL) || |
450 flag->type() == Flag::TYPE_MAYBE_BOOL; | 429 (flag->type() != Flag::TYPE_BOOL && is_bool) || |
451 if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) || | |
452 *endp != '\0') { | 430 *endp != '\0') { |
453 PrintF(stderr, "Error: illegal value for flag %s of type %s\n" | 431 PrintF(stderr, "Error: illegal value for flag %s of type %s\n" |
454 "Try --help for options\n", | 432 "Try --help for options\n", |
455 arg, Type2String(flag->type())); | 433 arg, Type2String(flag->type())); |
456 return_code = j; | 434 return_code = j; |
457 break; | 435 break; |
458 } | 436 } |
459 | 437 |
460 // remove the flag & value from the command | 438 // remove the flag & value from the command |
461 if (remove_flags) { | 439 if (remove_flags) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 printf("Options:\n"); | 542 printf("Options:\n"); |
565 for (size_t i = 0; i < num_flags; ++i) { | 543 for (size_t i = 0; i < num_flags; ++i) { |
566 Flag* f = &flags[i]; | 544 Flag* f = &flags[i]; |
567 SmartArrayPointer<const char> value = ToString(f); | 545 SmartArrayPointer<const char> value = ToString(f); |
568 printf(" --%s (%s)\n type: %s default: %s\n", | 546 printf(" --%s (%s)\n type: %s default: %s\n", |
569 f->name(), f->comment(), Type2String(f->type()), *value); | 547 f->name(), f->comment(), Type2String(f->type()), *value); |
570 } | 548 } |
571 } | 549 } |
572 | 550 |
573 | 551 |
574 // static | |
575 void FlagList::EnforceFlagImplications() { | 552 void FlagList::EnforceFlagImplications() { |
576 #define FLAG_MODE_DEFINE_IMPLICATIONS | 553 #define FLAG_MODE_DEFINE_IMPLICATIONS |
577 #include "flag-definitions.h" | 554 #include "flag-definitions.h" |
578 #undef FLAG_MODE_DEFINE_IMPLICATIONS | 555 #undef FLAG_MODE_DEFINE_IMPLICATIONS |
579 } | 556 } |
580 | 557 |
581 } } // namespace v8::internal | 558 } } // namespace v8::internal |
OLD | NEW |