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

Side by Side Diff: src/flags.cc

Issue 10294: * Added d8 flag "--" (alias "--js-arguments") (Closed)
Patch Set: Addressed review comments 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') | src/log.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 13 matching lines...) Expand all
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include <ctype.h> 28 #include <ctype.h>
29 #include <stdlib.h> 29 #include <stdlib.h>
30 30
31 #include "v8.h" 31 #include "v8.h"
32 32
33 #include "platform.h" 33 #include "platform.h"
34 #include "smart-pointer.h"
35 #include "string-stream.h"
36
34 37
35 namespace v8 { namespace internal { 38 namespace v8 { namespace internal {
36 39
37 // Define all of our flags. 40 // Define all of our flags.
38 #define FLAG_MODE_DEFINE 41 #define FLAG_MODE_DEFINE
39 #include "flag-definitions.h" 42 #include "flag-definitions.h"
40 43
41 // Define all of our flags default values. 44 // Define all of our flags default values.
42 #define FLAG_MODE_DEFINE_DEFAULTS 45 #define FLAG_MODE_DEFINE_DEFAULTS
43 #include "flag-definitions.h" 46 #include "flag-definitions.h"
44 47
45 namespace { 48 namespace {
46 49
47 // This structure represents a single entry in the flag system, with a pointer 50 // 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 51 // to the actual flag, default value, comment, etc. This is designed to be POD
49 // initialized as to avoid requiring static constructors. 52 // initialized as to avoid requiring static constructors.
50 struct Flag { 53 struct Flag {
51 enum FlagType { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING }; 54 enum FlagType { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING, TYPE_ARGS };
52 55
53 FlagType type_; // What type of flag, bool, int, or string. 56 FlagType type_; // What type of flag, bool, int, or string.
54 const char* name_; // Name of the flag, ex "my_flag". 57 const char* name_; // Name of the flag, ex "my_flag".
55 void* valptr_; // Pointer to the global flag variable. 58 void* valptr_; // Pointer to the global flag variable.
56 const void* defptr_; // Pointer to the default value. 59 const void* defptr_; // Pointer to the default value.
57 const char* cmt_; // A comment about the flags purpose. 60 const char* cmt_; // A comment about the flags purpose.
58 61
59 FlagType type() const { return type_; } 62 FlagType type() const { return type_; }
60 63
61 const char* name() const { return name_; } 64 const char* name() const { return name_; }
(...skipping 13 matching lines...) Expand all
75 double* float_variable() const { 78 double* float_variable() const {
76 ASSERT(type_ == TYPE_FLOAT); 79 ASSERT(type_ == TYPE_FLOAT);
77 return reinterpret_cast<double*>(valptr_); 80 return reinterpret_cast<double*>(valptr_);
78 } 81 }
79 82
80 const char** string_variable() const { 83 const char** string_variable() const {
81 ASSERT(type_ == TYPE_STRING); 84 ASSERT(type_ == TYPE_STRING);
82 return reinterpret_cast<const char**>(valptr_); 85 return reinterpret_cast<const char**>(valptr_);
83 } 86 }
84 87
88 JSArguments* args_variable() const {
89 ASSERT(type_ == TYPE_ARGS);
90 return reinterpret_cast<JSArguments*>(valptr_);
91 }
92
85 bool bool_default() const { 93 bool bool_default() const {
86 ASSERT(type_ == TYPE_BOOL); 94 ASSERT(type_ == TYPE_BOOL);
87 return *reinterpret_cast<const bool*>(defptr_); 95 return *reinterpret_cast<const bool*>(defptr_);
88 } 96 }
89 97
90 int int_default() const { 98 int int_default() const {
91 ASSERT(type_ == TYPE_INT); 99 ASSERT(type_ == TYPE_INT);
92 return *reinterpret_cast<const int*>(defptr_); 100 return *reinterpret_cast<const int*>(defptr_);
93 } 101 }
94 102
95 double float_default() const { 103 double float_default() const {
96 ASSERT(type_ == TYPE_FLOAT); 104 ASSERT(type_ == TYPE_FLOAT);
97 return *reinterpret_cast<const double*>(defptr_); 105 return *reinterpret_cast<const double*>(defptr_);
98 } 106 }
99 107
100 const char* string_default() const { 108 const char* string_default() const {
101 ASSERT(type_ == TYPE_STRING); 109 ASSERT(type_ == TYPE_STRING);
102 return *reinterpret_cast<const char* const *>(defptr_); 110 return *reinterpret_cast<const char* const *>(defptr_);
103 } 111 }
104 112
113 JSArguments args_default() const {
114 ASSERT(type_ == TYPE_ARGS);
115 return *reinterpret_cast<const JSArguments*>(defptr_);
116 }
117
105 // Compare this flag's current value against the default. 118 // Compare this flag's current value against the default.
106 bool IsDefault() const { 119 bool IsDefault() const {
107 switch (type_) { 120 switch (type_) {
108 case TYPE_BOOL: 121 case TYPE_BOOL:
109 return *bool_variable() == bool_default(); 122 return *bool_variable() == bool_default();
110 case TYPE_INT: 123 case TYPE_INT:
111 return *int_variable() == int_default(); 124 return *int_variable() == int_default();
112 case TYPE_FLOAT: 125 case TYPE_FLOAT:
113 return *float_variable() == float_default(); 126 return *float_variable() == float_default();
114 case TYPE_STRING: 127 case TYPE_STRING: {
115 const char* str1 = *string_variable(); 128 const char* str1 = *string_variable();
116 const char* str2 = string_default(); 129 const char* str2 = string_default();
117 if (str2 == NULL) return str1 == NULL; 130 if (str2 == NULL) return str1 == NULL;
118 if (str1 == NULL) return str2 == NULL; 131 if (str1 == NULL) return str2 == NULL;
119 return strcmp(str1, str2) == 0; 132 return strcmp(str1, str2) == 0;
133 }
134 case TYPE_ARGS:
135 return args_variable()->argc() == 0;
120 } 136 }
121 UNREACHABLE(); 137 UNREACHABLE();
122 return true; 138 return true;
123 } 139 }
124 140
125 // Set a flag back to it's default value. 141 // Set a flag back to it's default value.
126 void Reset() { 142 void Reset() {
127 switch (type_) { 143 switch (type_) {
128 case TYPE_BOOL: 144 case TYPE_BOOL:
129 *bool_variable() = bool_default(); 145 *bool_variable() = bool_default();
130 break; 146 break;
131 case TYPE_INT: 147 case TYPE_INT:
132 *int_variable() = int_default(); 148 *int_variable() = int_default();
133 break; 149 break;
134 case TYPE_FLOAT: 150 case TYPE_FLOAT:
135 *float_variable() = float_default(); 151 *float_variable() = float_default();
136 break; 152 break;
137 case TYPE_STRING: 153 case TYPE_STRING:
138 *string_variable() = string_default(); 154 *string_variable() = string_default();
139 break; 155 break;
156 case TYPE_ARGS:
157 *args_variable() = args_default();
158 break;
140 } 159 }
141 } 160 }
142 }; 161 };
143 162
144 Flag flags[] = { 163 Flag flags[] = {
145 #define FLAG_MODE_META 164 #define FLAG_MODE_META
146 #include "flag-definitions.h" 165 #include "flag-definitions.h"
147 }; 166 };
148 167
149 const size_t num_flags = sizeof(flags) / sizeof(*flags); 168 const size_t num_flags = sizeof(flags) / sizeof(*flags);
150 169
151 } // namespace 170 } // namespace
152 171
153 172
154 static const char* Type2String(Flag::FlagType type) { 173 static const char* Type2String(Flag::FlagType type) {
155 switch (type) { 174 switch (type) {
156 case Flag::TYPE_BOOL: return "bool"; 175 case Flag::TYPE_BOOL: return "bool";
157 case Flag::TYPE_INT: return "int"; 176 case Flag::TYPE_INT: return "int";
158 case Flag::TYPE_FLOAT: return "float"; 177 case Flag::TYPE_FLOAT: return "float";
159 case Flag::TYPE_STRING: return "string"; 178 case Flag::TYPE_STRING: return "string";
179 case Flag::TYPE_ARGS: return "arguments";
160 } 180 }
161 UNREACHABLE(); 181 UNREACHABLE();
162 return NULL; 182 return NULL;
163 } 183 }
164 184
165 185
166 static char* ToString(Flag* flag) { 186 static SmartPointer<const char> ToString(Flag* flag) {
167 Vector<char> value; 187 HeapStringAllocator string_allocator;
188 StringStream buffer(&string_allocator);
168 switch (flag->type()) { 189 switch (flag->type()) {
169 case Flag::TYPE_BOOL: 190 case Flag::TYPE_BOOL:
170 value = Vector<char>::New(6); 191 buffer.Add("%s", (*flag->bool_variable() ? "true" : "false"));
171 OS::SNPrintF(value, "%s", (*flag->bool_variable() ? "true" : "false"));
172 break; 192 break;
173 case Flag::TYPE_INT: 193 case Flag::TYPE_INT:
174 value = Vector<char>::New(12); 194 buffer.Add("%d", *flag->int_variable());
175 OS::SNPrintF(value, "%d", *flag->int_variable());
176 break; 195 break;
177 case Flag::TYPE_FLOAT: 196 case Flag::TYPE_FLOAT:
178 value = Vector<char>::New(20); 197 buffer.Add("%f", FmtElm(*flag->float_variable()));
179 OS::SNPrintF(value, "%f", *flag->float_variable());
180 break; 198 break;
181 case Flag::TYPE_STRING: 199 case Flag::TYPE_STRING: {
182 const char* str = *flag->string_variable(); 200 const char* str = *flag->string_variable();
183 if (str) { 201 buffer.Add("%s", str ? str : "NULL");
184 int length = strlen(str) + 1; 202 break;
185 value = Vector<char>::New(length); 203 }
186 OS::SNPrintF(value, "%s", str); 204 case Flag::TYPE_ARGS: {
187 } else { 205 JSArguments args = *flag->args_variable();
188 value = Vector<char>::New(5); 206 if (args.argc() > 0) {
189 OS::SNPrintF(value, "NULL"); 207 buffer.Add("%s", args[0]);
208 for (int i = 1; i < args.argc(); i++) {
209 buffer.Add(" %s", args[i]);
210 }
190 } 211 }
191 break; 212 break;
213 }
192 } 214 }
193 ASSERT(!value.is_empty()); 215 return buffer.ToCString();
194 return value.start();
195 } 216 }
196 217
197 218
198 // static 219 // static
199 List<char *>* FlagList::argv() { 220 List<const char*>* FlagList::argv() {
200 List<char *>* args = new List<char*>(8); 221 List<const char*>* args = new List<const char*>(8);
222 Flag* args_flag = NULL;
201 for (size_t i = 0; i < num_flags; ++i) { 223 for (size_t i = 0; i < num_flags; ++i) {
202 Flag* f = &flags[i]; 224 Flag* f = &flags[i];
203 if (!f->IsDefault()) { 225 if (!f->IsDefault()) {
204 Vector<char> cmdline_flag; 226 if (f->type() == Flag::TYPE_ARGS) {
227 ASSERT(args_flag == NULL);
228 args_flag = f; // Must be last in arguments.
229 continue;
230 }
231 HeapStringAllocator string_allocator;
232 StringStream buffer(&string_allocator);
205 if (f->type() != Flag::TYPE_BOOL || *(f->bool_variable())) { 233 if (f->type() != Flag::TYPE_BOOL || *(f->bool_variable())) {
206 int length = strlen(f->name()) + 2 + 1; 234 buffer.Add("--%s", f->name());
207 cmdline_flag = Vector<char>::New(length);
208 OS::SNPrintF(cmdline_flag, "--%s", f->name());
209 } else { 235 } else {
210 int length = strlen(f->name()) + 4 + 1; 236 buffer.Add("--no%s", f->name());
211 cmdline_flag = Vector<char>::New(length);
212 OS::SNPrintF(cmdline_flag, "--no%s", f->name());
213 } 237 }
214 args->Add(cmdline_flag.start()); 238 args->Add(buffer.ToCString().Detach());
215 if (f->type() != Flag::TYPE_BOOL) { 239 if (f->type() != Flag::TYPE_BOOL) {
216 args->Add(ToString(f)); 240 args->Add(ToString(f).Detach());
217 } 241 }
218 } 242 }
219 } 243 }
220 244 if (args_flag != NULL) {
245 HeapStringAllocator string_allocator;
246 StringStream buffer(&string_allocator);
247 buffer.Add("--%s", args_flag->name());
248 args->Add(buffer.ToCString().Detach());
249 JSArguments jsargs = *args_flag->args_variable();
250 for (int j = 0; j < jsargs.argc(); j++) {
251 args->Add(reinterpret_cast<const char*>(strdup(jsargs[j])));
252 }
253 }
221 return args; 254 return args;
222 } 255 }
223 256
224 257
225 // Helper function to parse flags: Takes an argument arg and splits it into 258 // Helper function to parse flags: Takes an argument arg and splits it into
226 // a flag name and flag value (or NULL if they are missing). is_bool is set 259 // a flag name and flag value (or NULL if they are missing). is_bool is set
227 // if the arg started with "-no" or "--no". The buffer may be used to NUL- 260 // if the arg started with "-no" or "--no". The buffer may be used to NUL-
228 // terminate the name, it must be large enough to hold any possible name. 261 // terminate the name, it must be large enough to hold any possible name.
229 static void SplitArgument(const char* arg, 262 static void SplitArgument(const char* arg,
230 char* buffer, 263 char* buffer,
231 int buffer_size, 264 int buffer_size,
232 const char** name, 265 const char** name,
233 const char** value, 266 const char** value,
234 bool* is_bool) { 267 bool* is_bool) {
235 *name = NULL; 268 *name = NULL;
236 *value = NULL; 269 *value = NULL;
237 *is_bool = false; 270 *is_bool = false;
238 271
239 if (*arg == '-') { 272 if (*arg == '-') {
240 // find the begin of the flag name 273 // find the begin of the flag name
241 arg++; // remove 1st '-' 274 arg++; // remove 1st '-'
242 if (*arg == '-') 275 if (*arg == '-') {
243 arg++; // remove 2nd '-' 276 arg++; // remove 2nd '-'
277 if (arg[0] == '\0') {
278 const char* kJSArgumentsFlagName = "js_arguments";
279 *name = kJSArgumentsFlagName;
280 return;
281 }
282 }
244 if (arg[0] == 'n' && arg[1] == 'o') { 283 if (arg[0] == 'n' && arg[1] == 'o') {
245 arg += 2; // remove "no" 284 arg += 2; // remove "no"
246 *is_bool = true; 285 *is_bool = true;
247 } 286 }
248 *name = arg; 287 *name = arg;
249 288
250 // find the end of the flag name 289 // find the end of the flag name
251 while (*arg != '\0' && *arg != '=') 290 while (*arg != '\0' && *arg != '=')
252 arg++; 291 arg++;
253 292
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 // sense there. 356 // sense there.
318 continue; 357 continue;
319 } else { 358 } else {
320 fprintf(stderr, "Error: unrecognized flag %s\n" 359 fprintf(stderr, "Error: unrecognized flag %s\n"
321 "Try --help for options\n", arg); 360 "Try --help for options\n", arg);
322 return j; 361 return j;
323 } 362 }
324 } 363 }
325 364
326 // if we still need a flag value, use the next argument if available 365 // if we still need a flag value, use the next argument if available
327 if (flag->type() != Flag::TYPE_BOOL && value == NULL) { 366 if (flag->type() != Flag::TYPE_BOOL &&
367 flag->type() != Flag::TYPE_ARGS &&
368 value == NULL) {
328 if (i < *argc) { 369 if (i < *argc) {
329 value = argv[i++]; 370 value = argv[i++];
330 } else { 371 } else {
331 fprintf(stderr, "Error: missing value for flag %s of type %s\n" 372 fprintf(stderr, "Error: missing value for flag %s of type %s\n"
332 "Try --help for options\n", 373 "Try --help for options\n",
333 arg, Type2String(flag->type())); 374 arg, Type2String(flag->type()));
334 return j; 375 return j;
335 } 376 }
336 } 377 }
337 378
338 // set the flag 379 // set the flag
339 char* endp = const_cast<char*>(""); // *endp is only read 380 char* endp = const_cast<char*>(""); // *endp is only read
340 switch (flag->type()) { 381 switch (flag->type()) {
341 case Flag::TYPE_BOOL: 382 case Flag::TYPE_BOOL:
342 *flag->bool_variable() = !is_bool; 383 *flag->bool_variable() = !is_bool;
343 break; 384 break;
344 case Flag::TYPE_INT: 385 case Flag::TYPE_INT:
345 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT 386 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT
346 break; 387 break;
347 case Flag::TYPE_FLOAT: 388 case Flag::TYPE_FLOAT:
348 *flag->float_variable() = strtod(value, &endp); 389 *flag->float_variable() = strtod(value, &endp);
349 break; 390 break;
350 case Flag::TYPE_STRING: 391 case Flag::TYPE_STRING:
351 *flag->string_variable() = value; 392 *flag->string_variable() = value;
352 break; 393 break;
394 case Flag::TYPE_ARGS: {
395 int start_pos = (value == NULL) ? i : i - 1;
396 int js_argc = *argc - start_pos;
397 const char** js_argv = NewArray<const char*>(js_argc);
398 if (value != NULL) {
399 js_argv[0] = value;
400 }
401 for (int k = i; k < *argc; k++) {
402 js_argv[k - start_pos] = argv[k];
403 }
404 *flag->args_variable() = JSArguments(js_argc, js_argv);
405 i = *argc; // Consume all arguments
406 break;
407 }
353 } 408 }
354 409
355 // handle errors 410 // handle errors
356 if ((flag->type() == Flag::TYPE_BOOL && value != NULL) || 411 if ((flag->type() == Flag::TYPE_BOOL && value != NULL) ||
357 (flag->type() != Flag::TYPE_BOOL && is_bool) || 412 (flag->type() != Flag::TYPE_BOOL && is_bool) ||
358 *endp != '\0') { 413 *endp != '\0') {
359 fprintf(stderr, "Error: illegal value for flag %s of type %s\n" 414 fprintf(stderr, "Error: illegal value for flag %s of type %s\n"
360 "Try --help for options\n", 415 "Try --help for options\n",
361 arg, Type2String(flag->type())); 416 arg, Type2String(flag->type()));
362 return j; 417 return j;
363 } 418 }
364 419
365 // remove the flag & value from the command 420 // remove the flag & value from the command
366 if (remove_flags) 421 if (remove_flags) {
367 while (j < i) 422 while (j < i) {
368 argv[j++] = NULL; 423 argv[j++] = NULL;
424 }
425 }
369 } 426 }
370 } 427 }
371 428
372 // shrink the argument list 429 // shrink the argument list
373 if (remove_flags) { 430 if (remove_flags) {
374 int j = 1; 431 int j = 1;
375 for (int i = 1; i < *argc; i++) { 432 for (int i = 1; i < *argc; i++) {
376 if (argv[i] != NULL) 433 if (argv[i] != NULL)
377 argv[j++] = argv[i]; 434 argv[j++] = argv[i];
378 } 435 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 printf(" run JavaScript scripts in file1, file2, ..., filek\n"); 517 printf(" run JavaScript scripts in file1, file2, ..., filek\n");
461 printf(" shell [options]\n"); 518 printf(" shell [options]\n");
462 printf(" shell [options] --shell\n"); 519 printf(" shell [options] --shell\n");
463 printf(" run an interactive JavaScript shell"); 520 printf(" run an interactive JavaScript shell");
464 printf(" d8 [options] file\n"); 521 printf(" d8 [options] file\n");
465 printf(" d8 [options]\n"); 522 printf(" d8 [options]\n");
466 printf(" run the new debugging shell\n\n"); 523 printf(" run the new debugging shell\n\n");
467 printf("Options:\n"); 524 printf("Options:\n");
468 for (size_t i = 0; i < num_flags; ++i) { 525 for (size_t i = 0; i < num_flags; ++i) {
469 Flag* f = &flags[i]; 526 Flag* f = &flags[i];
470 char* value = ToString(f); 527 SmartPointer<const char> value = ToString(f);
471 printf(" --%s (%s)\n type: %s default: %s\n", 528 printf(" --%s (%s)\n type: %s default: %s\n",
472 f->name(), f->comment(), Type2String(f->type()), value); 529 f->name(), f->comment(), Type2String(f->type()), *value);
473 DeleteArray(value);
474 } 530 }
475 } 531 }
476 532
533 JSArguments::JSArguments()
534 : argc_(0), argv_(NULL) {}
535 JSArguments::JSArguments(int argc, const char** argv)
536 : argc_(argc), argv_(argv) {}
537 int JSArguments::argc() const { return argc_; }
538 const char** JSArguments::argv() { return argv_; }
539 const char*& JSArguments::operator[](int idx) { return argv_[idx]; }
540 JSArguments& JSArguments::operator=(JSArguments args) {
541 argc_ = args.argc_;
542 argv_ = args.argv_;
543 return *this;
544 }
545
546
477 } } // namespace v8::internal 547 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/flags.h ('k') | src/log.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698