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

Side by Side Diff: src/flags.cc

Issue 10252: * Added d8 flag "--" (alias "--js-arguments"). (Closed)
Patch Set: Addressed reviews, added tests Created 12 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
« no previous file with comments | « src/flags.h ('k') | test/cctest/test-flags.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 30 matching lines...) Expand all
41 // Define all of our flags default values. 41 // Define all of our flags default values.
42 #define FLAG_MODE_DEFINE_DEFAULTS 42 #define FLAG_MODE_DEFINE_DEFAULTS
43 #include "flag-definitions.h" 43 #include "flag-definitions.h"
44 44
45 namespace { 45 namespace {
46 46
47 // This structure represents a single entry in the flag system, with a pointer 47 // This structure represents a single entry in the flag system, with a pointer
48 // to the actual flag, default value, comment, etc. This is designed to be POD 48 // to the actual flag, default value, comment, etc. This is designed to be POD
49 // initialized as to avoid requiring static constructors. 49 // initialized as to avoid requiring static constructors.
50 struct Flag { 50 struct Flag {
51 enum FlagType { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING }; 51 enum FlagType { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING, TYPE_ARGS };
52 52
53 FlagType type_; // What type of flag, bool, int, or string. 53 FlagType type_; // What type of flag, bool, int, or string.
54 const char* name_; // Name of the flag, ex "my_flag". 54 const char* name_; // Name of the flag, ex "my_flag".
55 void* valptr_; // Pointer to the global flag variable. 55 void* valptr_; // Pointer to the global flag variable.
56 const void* defptr_; // Pointer to the default value. 56 const void* defptr_; // Pointer to the default value.
57 const char* cmt_; // A comment about the flags purpose. 57 const char* cmt_; // A comment about the flags purpose.
58 58
59 FlagType type() const { return type_; } 59 FlagType type() const { return type_; }
60 60
61 const char* name() const { return name_; } 61 const char* name() const { return name_; }
(...skipping 13 matching lines...) Expand all
75 double* float_variable() const { 75 double* float_variable() const {
76 ASSERT(type_ == TYPE_FLOAT); 76 ASSERT(type_ == TYPE_FLOAT);
77 return reinterpret_cast<double*>(valptr_); 77 return reinterpret_cast<double*>(valptr_);
78 } 78 }
79 79
80 const char** string_variable() const { 80 const char** string_variable() const {
81 ASSERT(type_ == TYPE_STRING); 81 ASSERT(type_ == TYPE_STRING);
82 return reinterpret_cast<const char**>(valptr_); 82 return reinterpret_cast<const char**>(valptr_);
83 } 83 }
84 84
85 JSArguments* args_variable() const {
86 ASSERT(type_ == TYPE_ARGS);
87 return reinterpret_cast<JSArguments*>(valptr_);
88 }
89
85 bool bool_default() const { 90 bool bool_default() const {
86 ASSERT(type_ == TYPE_BOOL); 91 ASSERT(type_ == TYPE_BOOL);
87 return *reinterpret_cast<const bool*>(defptr_); 92 return *reinterpret_cast<const bool*>(defptr_);
88 } 93 }
89 94
90 int int_default() const { 95 int int_default() const {
91 ASSERT(type_ == TYPE_INT); 96 ASSERT(type_ == TYPE_INT);
92 return *reinterpret_cast<const int*>(defptr_); 97 return *reinterpret_cast<const int*>(defptr_);
93 } 98 }
94 99
95 double float_default() const { 100 double float_default() const {
96 ASSERT(type_ == TYPE_FLOAT); 101 ASSERT(type_ == TYPE_FLOAT);
97 return *reinterpret_cast<const double*>(defptr_); 102 return *reinterpret_cast<const double*>(defptr_);
98 } 103 }
99 104
100 const char* string_default() const { 105 const char* string_default() const {
101 ASSERT(type_ == TYPE_STRING); 106 ASSERT(type_ == TYPE_STRING);
102 return *reinterpret_cast<const char* const *>(defptr_); 107 return *reinterpret_cast<const char* const *>(defptr_);
103 } 108 }
104 109
110 JSArguments args_default() const {
111 ASSERT(type_ == TYPE_ARGS);
112 return *reinterpret_cast<const JSArguments*>(defptr_);
113 }
114
105 // Compare this flag's current value against the default. 115 // Compare this flag's current value against the default.
106 bool IsDefault() const { 116 bool IsDefault() const {
107 switch (type_) { 117 switch (type_) {
108 case TYPE_BOOL: 118 case TYPE_BOOL:
109 return *bool_variable() == bool_default(); 119 return *bool_variable() == bool_default();
110 case TYPE_INT: 120 case TYPE_INT:
111 return *int_variable() == int_default(); 121 return *int_variable() == int_default();
112 case TYPE_FLOAT: 122 case TYPE_FLOAT:
113 return *float_variable() == float_default(); 123 return *float_variable() == float_default();
114 case TYPE_STRING: 124 case TYPE_STRING: {
115 const char* str1 = *string_variable(); 125 const char* str1 = *string_variable();
116 const char* str2 = string_default(); 126 const char* str2 = string_default();
117 if (str2 == NULL) return str1 == NULL; 127 if (str2 == NULL) return str1 == NULL;
118 if (str1 == NULL) return str2 == NULL; 128 if (str1 == NULL) return str2 == NULL;
119 return strcmp(str1, str2) == 0; 129 return strcmp(str1, str2) == 0;
130 }
131 case TYPE_ARGS:
132 return args_variable()->argc() == 0;
120 } 133 }
121 UNREACHABLE(); 134 UNREACHABLE();
122 return true; 135 return true;
123 } 136 }
124 137
125 // Set a flag back to it's default value. 138 // Set a flag back to it's default value.
126 void Reset() { 139 void Reset() {
127 switch (type_) { 140 switch (type_) {
128 case TYPE_BOOL: 141 case TYPE_BOOL:
129 *bool_variable() = bool_default(); 142 *bool_variable() = bool_default();
130 break; 143 break;
131 case TYPE_INT: 144 case TYPE_INT:
132 *int_variable() = int_default(); 145 *int_variable() = int_default();
133 break; 146 break;
134 case TYPE_FLOAT: 147 case TYPE_FLOAT:
135 *float_variable() = float_default(); 148 *float_variable() = float_default();
136 break; 149 break;
137 case TYPE_STRING: 150 case TYPE_STRING:
138 *string_variable() = string_default(); 151 *string_variable() = string_default();
139 break; 152 break;
153 case TYPE_ARGS:
154 *args_variable() = args_default();
155 break;
140 } 156 }
141 } 157 }
142 }; 158 };
143 159
144 Flag flags[] = { 160 Flag flags[] = {
145 #define FLAG_MODE_META 161 #define FLAG_MODE_META
146 #include "flag-definitions.h" 162 #include "flag-definitions.h"
147 }; 163 };
148 164
149 const size_t num_flags = sizeof(flags) / sizeof(*flags); 165 const size_t num_flags = sizeof(flags) / sizeof(*flags);
150 166
151 } // namespace 167 } // namespace
152 168
153 169
154 static const char* Type2String(Flag::FlagType type) { 170 static const char* Type2String(Flag::FlagType type) {
155 switch (type) { 171 switch (type) {
156 case Flag::TYPE_BOOL: return "bool"; 172 case Flag::TYPE_BOOL: return "bool";
157 case Flag::TYPE_INT: return "int"; 173 case Flag::TYPE_INT: return "int";
158 case Flag::TYPE_FLOAT: return "float"; 174 case Flag::TYPE_FLOAT: return "float";
159 case Flag::TYPE_STRING: return "string"; 175 case Flag::TYPE_STRING: return "string";
176 case Flag::TYPE_ARGS: return "arguments";
160 } 177 }
161 UNREACHABLE(); 178 UNREACHABLE();
162 return NULL; 179 return NULL;
163 } 180 }
164 181
165 182
166 static char* ToString(Flag* flag) { 183 static char* ToString(Flag* flag) {
167 Vector<char> value; 184 Vector<char> value;
168 switch (flag->type()) { 185 switch (flag->type()) {
169 case Flag::TYPE_BOOL: 186 case Flag::TYPE_BOOL:
170 value = Vector<char>::New(6); 187 value = Vector<char>::New(6);
171 OS::SNPrintF(value, "%s", (*flag->bool_variable() ? "true" : "false")); 188 OS::SNPrintF(value, "%s", (*flag->bool_variable() ? "true" : "false"));
172 break; 189 break;
173 case Flag::TYPE_INT: 190 case Flag::TYPE_INT:
174 value = Vector<char>::New(12); 191 value = Vector<char>::New(12);
175 OS::SNPrintF(value, "%d", *flag->int_variable()); 192 OS::SNPrintF(value, "%d", *flag->int_variable());
176 break; 193 break;
177 case Flag::TYPE_FLOAT: 194 case Flag::TYPE_FLOAT:
178 value = Vector<char>::New(20); 195 value = Vector<char>::New(20);
179 OS::SNPrintF(value, "%f", *flag->float_variable()); 196 OS::SNPrintF(value, "%f", *flag->float_variable());
180 break; 197 break;
181 case Flag::TYPE_STRING: 198 case Flag::TYPE_STRING: {
182 const char* str = *flag->string_variable(); 199 const char* str = *flag->string_variable();
183 if (str) { 200 if (str) {
184 int length = strlen(str) + 1; 201 int length = strlen(str) + 1;
185 value = Vector<char>::New(length); 202 value = Vector<char>::New(length);
186 OS::SNPrintF(value, "%s", str); 203 OS::SNPrintF(value, "%s", str);
187 } else { 204 } else {
188 value = Vector<char>::New(5); 205 value = Vector<char>::New(5);
189 OS::SNPrintF(value, "NULL"); 206 OS::SNPrintF(value, "NULL");
190 } 207 }
191 break; 208 break;
209 }
210 case Flag::TYPE_ARGS: {
211 JSArguments args = *flag->args_variable();
212 if (args.argc() == 0) {
213 value = Vector<char>::New(0);
214 break;
215 }
216 int len = args.argc() - 1;
217 for (int i = 0; i < args.argc(); i++) {
218 len += strlen(args[i]);
219 }
220 value = Vector<char>::New(len);
221 for (int i = 0; i < args.argc(); i++) {
222 if (i > 0) {
223 OS::SNPrintF(value, " ");
224 }
225 OS::SNPrintF(value, "%s", args[i]);
226 }
227 break;
228 }
192 } 229 }
193 ASSERT(!value.is_empty()); 230 ASSERT(!value.is_empty());
194 return value.start(); 231 return value.start();
195 } 232 }
196 233
197 234
198 // static 235 // static
199 List<char *>* FlagList::argv() { 236 List<char *>* FlagList::argv() {
200 List<char *>* args = new List<char*>(8); 237 List<char *>* args = new List<char*>(8);
201 for (size_t i = 0; i < num_flags; ++i) { 238 for (size_t i = 0; i < num_flags; ++i) {
(...skipping 30 matching lines...) Expand all
232 const char** name, 269 const char** name,
233 const char** value, 270 const char** value,
234 bool* is_bool) { 271 bool* is_bool) {
235 *name = NULL; 272 *name = NULL;
236 *value = NULL; 273 *value = NULL;
237 *is_bool = false; 274 *is_bool = false;
238 275
239 if (*arg == '-') { 276 if (*arg == '-') {
240 // find the begin of the flag name 277 // find the begin of the flag name
241 arg++; // remove 1st '-' 278 arg++; // remove 1st '-'
242 if (*arg == '-') 279 if (*arg == '-') {
243 arg++; // remove 2nd '-' 280 arg++; // remove 2nd '-'
281 if (arg[0] == '\0') {
282 const char* kJSArgumentsFlagName = "js_arguments";
283 *name = kJSArgumentsFlagName;
284 return;
285 }
286 }
244 if (arg[0] == 'n' && arg[1] == 'o') { 287 if (arg[0] == 'n' && arg[1] == 'o') {
245 arg += 2; // remove "no" 288 arg += 2; // remove "no"
246 *is_bool = true; 289 *is_bool = true;
247 } 290 }
248 *name = arg; 291 *name = arg;
249 292
250 // find the end of the flag name 293 // find the end of the flag name
251 while (*arg != '\0' && *arg != '=') 294 while (*arg != '\0' && *arg != '=')
252 arg++; 295 arg++;
253 296
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 // sense there. 360 // sense there.
318 continue; 361 continue;
319 } else { 362 } else {
320 fprintf(stderr, "Error: unrecognized flag %s\n" 363 fprintf(stderr, "Error: unrecognized flag %s\n"
321 "Try --help for options\n", arg); 364 "Try --help for options\n", arg);
322 return j; 365 return j;
323 } 366 }
324 } 367 }
325 368
326 // if we still need a flag value, use the next argument if available 369 // if we still need a flag value, use the next argument if available
327 if (flag->type() != Flag::TYPE_BOOL && value == NULL) { 370 if (flag->type() != Flag::TYPE_BOOL &&
371 flag->type() != Flag::TYPE_ARGS &&
372 value == NULL) {
328 if (i < *argc) { 373 if (i < *argc) {
329 value = argv[i++]; 374 value = argv[i++];
330 } else { 375 } else {
331 fprintf(stderr, "Error: missing value for flag %s of type %s\n" 376 fprintf(stderr, "Error: missing value for flag %s of type %s\n"
332 "Try --help for options\n", 377 "Try --help for options\n",
333 arg, Type2String(flag->type())); 378 arg, Type2String(flag->type()));
334 return j; 379 return j;
335 } 380 }
336 } 381 }
337 382
338 // set the flag 383 // set the flag
339 char* endp = const_cast<char*>(""); // *endp is only read 384 char* endp = const_cast<char*>(""); // *endp is only read
340 switch (flag->type()) { 385 switch (flag->type()) {
341 case Flag::TYPE_BOOL: 386 case Flag::TYPE_BOOL:
342 *flag->bool_variable() = !is_bool; 387 *flag->bool_variable() = !is_bool;
343 break; 388 break;
344 case Flag::TYPE_INT: 389 case Flag::TYPE_INT:
345 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT 390 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT
346 break; 391 break;
347 case Flag::TYPE_FLOAT: 392 case Flag::TYPE_FLOAT:
348 *flag->float_variable() = strtod(value, &endp); 393 *flag->float_variable() = strtod(value, &endp);
349 break; 394 break;
350 case Flag::TYPE_STRING: 395 case Flag::TYPE_STRING:
351 *flag->string_variable() = value; 396 *flag->string_variable() = value;
352 break; 397 break;
398 case Flag::TYPE_ARGS:
399 int start_pos = (value == NULL) ? i : i - 1;
400 int js_argc = *argc - start_pos;
401 const char** js_argv = new const char*[js_argc];
402 if (value != NULL) {
403 js_argv[0] = value;
404 }
405 for (int k = i; k < *argc; k++) {
406 js_argv[k - start_pos] = argv[k];
407 }
408 *flag->args_variable() = JSArguments(js_argc, js_argv);
409 i = *argc; // Consume all arguments
410 break;
353 } 411 }
354 412
355 // handle errors 413 // handle errors
356 if ((flag->type() == Flag::TYPE_BOOL && value != NULL) || 414 if ((flag->type() == Flag::TYPE_BOOL && value != NULL) ||
357 (flag->type() != Flag::TYPE_BOOL && is_bool) || 415 (flag->type() != Flag::TYPE_BOOL && is_bool) ||
358 *endp != '\0') { 416 *endp != '\0') {
359 fprintf(stderr, "Error: illegal value for flag %s of type %s\n" 417 fprintf(stderr, "Error: illegal value for flag %s of type %s\n"
360 "Try --help for options\n", 418 "Try --help for options\n",
361 arg, Type2String(flag->type())); 419 arg, Type2String(flag->type()));
362 return j; 420 return j;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 printf("Options:\n"); 525 printf("Options:\n");
468 for (size_t i = 0; i < num_flags; ++i) { 526 for (size_t i = 0; i < num_flags; ++i) {
469 Flag* f = &flags[i]; 527 Flag* f = &flags[i];
470 char* value = ToString(f); 528 char* value = ToString(f);
471 printf(" --%s (%s)\n type: %s default: %s\n", 529 printf(" --%s (%s)\n type: %s default: %s\n",
472 f->name(), f->comment(), Type2String(f->type()), value); 530 f->name(), f->comment(), Type2String(f->type()), value);
473 DeleteArray(value); 531 DeleteArray(value);
474 } 532 }
475 } 533 }
476 534
535 JSArguments::JSArguments()
536 : argc_(0), argv_(NULL) {}
537 JSArguments::JSArguments(int argc, const char** argv)
538 : argc_(argc), argv_(argv) {}
539 int JSArguments::argc() const { return argc_; }
540 const char** JSArguments::argv() { return argv_; }
541 const char*& JSArguments::operator[](int idx) { return argv_[idx]; }
542 JSArguments& JSArguments::operator=(JSArguments args) {
543 argc_ = args.argc_;
544 argv_ = args.argv_;
545 return *this;
546 }
547
548
477 } } // namespace v8::internal 549 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/flags.h ('k') | test/cctest/test-flags.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698