The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / flags.cc @ f230a1cf

History | View | Annotate | Download (16.6 KB)

1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27

    
28
#include <ctype.h>
29
#include <stdlib.h>
30

    
31
#include "v8.h"
32

    
33
#include "platform.h"
34
#include "smart-pointers.h"
35
#include "string-stream.h"
36

    
37
#if V8_TARGET_ARCH_ARM
38
#include "arm/assembler-arm-inl.h"
39
#endif
40

    
41
namespace v8 {
42
namespace internal {
43

    
44
// Define all of our flags.
45
#define FLAG_MODE_DEFINE
46
#include "flag-definitions.h"
47

    
48
// Define all of our flags default values.
49
#define FLAG_MODE_DEFINE_DEFAULTS
50
#include "flag-definitions.h"
51

    
52
namespace {
53

    
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
56
// initialized as to avoid requiring static constructors.
57
struct Flag {
58
  enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT,
59
                  TYPE_STRING, TYPE_ARGS };
60

    
61
  FlagType type_;           // What type of flag, bool, int, or string.
62
  const char* name_;        // Name of the flag, ex "my_flag".
63
  void* valptr_;            // Pointer to the global flag variable.
64
  const void* defptr_;      // Pointer to the default value.
65
  const char* cmt_;         // A comment about the flags purpose.
66
  bool owns_ptr_;           // Does the flag own its string value?
67

    
68
  FlagType type() const { return type_; }
69

    
70
  const char* name() const { return name_; }
71

    
72
  const char* comment() const { return cmt_; }
73

    
74
  bool* bool_variable() const {
75
    ASSERT(type_ == TYPE_BOOL);
76
    return reinterpret_cast<bool*>(valptr_);
77
  }
78

    
79
  MaybeBoolFlag* maybe_bool_variable() const {
80
    ASSERT(type_ == TYPE_MAYBE_BOOL);
81
    return reinterpret_cast<MaybeBoolFlag*>(valptr_);
82
  }
83

    
84
  int* int_variable() const {
85
    ASSERT(type_ == TYPE_INT);
86
    return reinterpret_cast<int*>(valptr_);
87
  }
88

    
89
  double* float_variable() const {
90
    ASSERT(type_ == TYPE_FLOAT);
91
    return reinterpret_cast<double*>(valptr_);
92
  }
93

    
94
  const char* string_value() const {
95
    ASSERT(type_ == TYPE_STRING);
96
    return *reinterpret_cast<const char**>(valptr_);
97
  }
98

    
99
  void set_string_value(const char* value, bool owns_ptr) {
100
    ASSERT(type_ == TYPE_STRING);
101
    const char** ptr = reinterpret_cast<const char**>(valptr_);
102
    if (owns_ptr_ && *ptr != NULL) DeleteArray(*ptr);
103
    *ptr = value;
104
    owns_ptr_ = owns_ptr;
105
  }
106

    
107
  JSArguments* args_variable() const {
108
    ASSERT(type_ == TYPE_ARGS);
109
    return reinterpret_cast<JSArguments*>(valptr_);
110
  }
111

    
112
  bool bool_default() const {
113
    ASSERT(type_ == TYPE_BOOL);
114
    return *reinterpret_cast<const bool*>(defptr_);
115
  }
116

    
117
  int int_default() const {
118
    ASSERT(type_ == TYPE_INT);
119
    return *reinterpret_cast<const int*>(defptr_);
120
  }
121

    
122
  double float_default() const {
123
    ASSERT(type_ == TYPE_FLOAT);
124
    return *reinterpret_cast<const double*>(defptr_);
125
  }
126

    
127
  const char* string_default() const {
128
    ASSERT(type_ == TYPE_STRING);
129
    return *reinterpret_cast<const char* const *>(defptr_);
130
  }
131

    
132
  JSArguments args_default() const {
133
    ASSERT(type_ == TYPE_ARGS);
134
    return *reinterpret_cast<const JSArguments*>(defptr_);
135
  }
136

    
137
  // Compare this flag's current value against the default.
138
  bool IsDefault() const {
139
    switch (type_) {
140
      case TYPE_BOOL:
141
        return *bool_variable() == bool_default();
142
      case TYPE_MAYBE_BOOL:
143
        return maybe_bool_variable()->has_value == false;
144
      case TYPE_INT:
145
        return *int_variable() == int_default();
146
      case TYPE_FLOAT:
147
        return *float_variable() == float_default();
148
      case TYPE_STRING: {
149
        const char* str1 = string_value();
150
        const char* str2 = string_default();
151
        if (str2 == NULL) return str1 == NULL;
152
        if (str1 == NULL) return str2 == NULL;
153
        return strcmp(str1, str2) == 0;
154
      }
155
      case TYPE_ARGS:
156
        return args_variable()->argc == 0;
157
    }
158
    UNREACHABLE();
159
    return true;
160
  }
161

    
162
  // Set a flag back to it's default value.
163
  void Reset() {
164
    switch (type_) {
165
      case TYPE_BOOL:
166
        *bool_variable() = bool_default();
167
        break;
168
      case TYPE_MAYBE_BOOL:
169
        *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
170
        break;
171
      case TYPE_INT:
172
        *int_variable() = int_default();
173
        break;
174
      case TYPE_FLOAT:
175
        *float_variable() = float_default();
176
        break;
177
      case TYPE_STRING:
178
        set_string_value(string_default(), false);
179
        break;
180
      case TYPE_ARGS:
181
        *args_variable() = args_default();
182
        break;
183
    }
184
  }
185
};
186

    
187
Flag flags[] = {
188
#define FLAG_MODE_META
189
#include "flag-definitions.h"
190
};
191

    
192
const size_t num_flags = sizeof(flags) / sizeof(*flags);
193

    
194
}  // namespace
195

    
196

    
197
static const char* Type2String(Flag::FlagType type) {
198
  switch (type) {
199
    case Flag::TYPE_BOOL: return "bool";
200
    case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
201
    case Flag::TYPE_INT: return "int";
202
    case Flag::TYPE_FLOAT: return "float";
203
    case Flag::TYPE_STRING: return "string";
204
    case Flag::TYPE_ARGS: return "arguments";
205
  }
206
  UNREACHABLE();
207
  return NULL;
208
}
209

    
210

    
211
static SmartArrayPointer<const char> ToString(Flag* flag) {
212
  HeapStringAllocator string_allocator;
213
  StringStream buffer(&string_allocator);
214
  switch (flag->type()) {
215
    case Flag::TYPE_BOOL:
216
      buffer.Add("%s", (*flag->bool_variable() ? "true" : "false"));
217
      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:
224
      buffer.Add("%d", *flag->int_variable());
225
      break;
226
    case Flag::TYPE_FLOAT:
227
      buffer.Add("%f", FmtElm(*flag->float_variable()));
228
      break;
229
    case Flag::TYPE_STRING: {
230
      const char* str = flag->string_value();
231
      buffer.Add("%s", str ? str : "NULL");
232
      break;
233
    }
234
    case Flag::TYPE_ARGS: {
235
      JSArguments args = *flag->args_variable();
236
      if (args.argc > 0) {
237
        buffer.Add("%s",  args[0]);
238
        for (int i = 1; i < args.argc; i++) {
239
          buffer.Add(" %s", args[i]);
240
        }
241
      }
242
      break;
243
    }
244
  }
245
  return buffer.ToCString();
246
}
247

    
248

    
249
// static
250
List<const char*>* FlagList::argv() {
251
  List<const char*>* args = new List<const char*>(8);
252
  Flag* args_flag = NULL;
253
  for (size_t i = 0; i < num_flags; ++i) {
254
    Flag* f = &flags[i];
255
    if (!f->IsDefault()) {
256
      if (f->type() == Flag::TYPE_ARGS) {
257
        ASSERT(args_flag == NULL);
258
        args_flag = f;  // Must be last in arguments.
259
        continue;
260
      }
261
      HeapStringAllocator string_allocator;
262
      StringStream buffer(&string_allocator);
263
      if (f->type() != Flag::TYPE_BOOL || *(f->bool_variable())) {
264
        buffer.Add("--%s", f->name());
265
      } else {
266
        buffer.Add("--no%s", f->name());
267
      }
268
      args->Add(buffer.ToCString().Detach());
269
      if (f->type() != Flag::TYPE_BOOL) {
270
        args->Add(ToString(f).Detach());
271
      }
272
    }
273
  }
274
  if (args_flag != NULL) {
275
    HeapStringAllocator string_allocator;
276
    StringStream buffer(&string_allocator);
277
    buffer.Add("--%s", args_flag->name());
278
    args->Add(buffer.ToCString().Detach());
279
    JSArguments jsargs = *args_flag->args_variable();
280
    for (int j = 0; j < jsargs.argc; j++) {
281
      args->Add(StrDup(jsargs[j]));
282
    }
283
  }
284
  return args;
285
}
286

    
287

    
288
inline char NormalizeChar(char ch) {
289
  return ch == '_' ? '-' : ch;
290
}
291

    
292

    
293
// Helper function to parse flags: Takes an argument arg and splits it into
294
// a flag name and flag value (or NULL if they are missing). is_bool is set
295
// if the arg started with "-no" or "--no". The buffer may be used to NUL-
296
// terminate the name, it must be large enough to hold any possible name.
297
static void SplitArgument(const char* arg,
298
                          char* buffer,
299
                          int buffer_size,
300
                          const char** name,
301
                          const char** value,
302
                          bool* is_bool) {
303
  *name = NULL;
304
  *value = NULL;
305
  *is_bool = false;
306

    
307
  if (arg != NULL && *arg == '-') {
308
    // find the begin of the flag name
309
    arg++;  // remove 1st '-'
310
    if (*arg == '-') {
311
      arg++;  // remove 2nd '-'
312
      if (arg[0] == '\0') {
313
        const char* kJSArgumentsFlagName = "js_arguments";
314
        *name = kJSArgumentsFlagName;
315
        return;
316
      }
317
    }
318
    if (arg[0] == 'n' && arg[1] == 'o') {
319
      arg += 2;  // remove "no"
320
      if (NormalizeChar(arg[0]) == '-') arg++;  // remove dash after "no".
321
      *is_bool = true;
322
    }
323
    *name = arg;
324

    
325
    // find the end of the flag name
326
    while (*arg != '\0' && *arg != '=')
327
      arg++;
328

    
329
    // get the value if any
330
    if (*arg == '=') {
331
      // make a copy so we can NUL-terminate flag name
332
      size_t n = arg - *name;
333
      CHECK(n < static_cast<size_t>(buffer_size));  // buffer is too small
334
      OS::MemCopy(buffer, *name, n);
335
      buffer[n] = '\0';
336
      *name = buffer;
337
      // get the value
338
      *value = arg + 1;
339
    }
340
  }
341
}
342

    
343

    
344
static bool EqualNames(const char* a, const char* b) {
345
  for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
346
    if (a[i] == '\0') {
347
      return true;
348
    }
349
  }
350
  return false;
351
}
352

    
353

    
354
static Flag* FindFlag(const char* name) {
355
  for (size_t i = 0; i < num_flags; ++i) {
356
    if (EqualNames(name, flags[i].name()))
357
      return &flags[i];
358
  }
359
  return NULL;
360
}
361

    
362

    
363
// static
364
int FlagList::SetFlagsFromCommandLine(int* argc,
365
                                      char** argv,
366
                                      bool remove_flags) {
367
  int return_code = 0;
368
  // parse arguments
369
  for (int i = 1; i < *argc;) {
370
    int j = i;  // j > 0
371
    const char* arg = argv[i++];
372

    
373
    // split arg into flag components
374
    char buffer[1*KB];
375
    const char* name;
376
    const char* value;
377
    bool is_bool;
378
    SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
379

    
380
    if (name != NULL) {
381
      // lookup the flag
382
      Flag* flag = FindFlag(name);
383
      if (flag == NULL) {
384
        if (remove_flags) {
385
          // We don't recognize this flag but since we're removing
386
          // the flags we recognize we assume that the remaining flags
387
          // will be processed somewhere else so this flag might make
388
          // sense there.
389
          continue;
390
        } else {
391
          PrintF(stderr, "Error: unrecognized flag %s\n"
392
                 "Try --help for options\n", arg);
393
          return_code = j;
394
          break;
395
        }
396
      }
397

    
398
      // if we still need a flag value, use the next argument if available
399
      if (flag->type() != Flag::TYPE_BOOL &&
400
          flag->type() != Flag::TYPE_MAYBE_BOOL &&
401
          flag->type() != Flag::TYPE_ARGS &&
402
          value == NULL) {
403
        if (i < *argc) {
404
          value = argv[i++];
405
        } else {
406
          PrintF(stderr, "Error: missing value for flag %s of type %s\n"
407
                 "Try --help for options\n",
408
                 arg, Type2String(flag->type()));
409
          return_code = j;
410
          break;
411
        }
412
      }
413

    
414
      // set the flag
415
      char* endp = const_cast<char*>("");  // *endp is only read
416
      switch (flag->type()) {
417
        case Flag::TYPE_BOOL:
418
          *flag->bool_variable() = !is_bool;
419
          break;
420
        case Flag::TYPE_MAYBE_BOOL:
421
          *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool);
422
          break;
423
        case Flag::TYPE_INT:
424
          *flag->int_variable() = strtol(value, &endp, 10);  // NOLINT
425
          break;
426
        case Flag::TYPE_FLOAT:
427
          *flag->float_variable() = strtod(value, &endp);
428
          break;
429
        case Flag::TYPE_STRING:
430
          flag->set_string_value(value ? StrDup(value) : NULL, true);
431
          break;
432
        case Flag::TYPE_ARGS: {
433
          int start_pos = (value == NULL) ? i : i - 1;
434
          int js_argc = *argc - start_pos;
435
          const char** js_argv = NewArray<const char*>(js_argc);
436
          if (value != NULL) {
437
            js_argv[0] = StrDup(value);
438
          }
439
          for (int k = i; k < *argc; k++) {
440
            js_argv[k - start_pos] = StrDup(argv[k]);
441
          }
442
          *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
443
          i = *argc;  // Consume all arguments
444
          break;
445
        }
446
      }
447

    
448
      // handle errors
449
      bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
450
          flag->type() == Flag::TYPE_MAYBE_BOOL;
451
      if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) ||
452
          *endp != '\0') {
453
        PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
454
               "Try --help for options\n",
455
               arg, Type2String(flag->type()));
456
        return_code = j;
457
        break;
458
      }
459

    
460
      // remove the flag & value from the command
461
      if (remove_flags) {
462
        while (j < i) {
463
          argv[j++] = NULL;
464
        }
465
      }
466
    }
467
  }
468

    
469
  // shrink the argument list
470
  if (remove_flags) {
471
    int j = 1;
472
    for (int i = 1; i < *argc; i++) {
473
      if (argv[i] != NULL)
474
        argv[j++] = argv[i];
475
    }
476
    *argc = j;
477
  }
478

    
479
  if (FLAG_help) {
480
    PrintHelp();
481
    exit(0);
482
  }
483
  // parsed all flags successfully
484
  return return_code;
485
}
486

    
487

    
488
static char* SkipWhiteSpace(char* p) {
489
  while (*p != '\0' && isspace(*p) != 0) p++;
490
  return p;
491
}
492

    
493

    
494
static char* SkipBlackSpace(char* p) {
495
  while (*p != '\0' && isspace(*p) == 0) p++;
496
  return p;
497
}
498

    
499

    
500
// static
501
int FlagList::SetFlagsFromString(const char* str, int len) {
502
  // make a 0-terminated copy of str
503
  ScopedVector<char> copy0(len + 1);
504
  OS::MemCopy(copy0.start(), str, len);
505
  copy0[len] = '\0';
506

    
507
  // strip leading white space
508
  char* copy = SkipWhiteSpace(copy0.start());
509

    
510
  // count the number of 'arguments'
511
  int argc = 1;  // be compatible with SetFlagsFromCommandLine()
512
  for (char* p = copy; *p != '\0'; argc++) {
513
    p = SkipBlackSpace(p);
514
    p = SkipWhiteSpace(p);
515
  }
516

    
517
  // allocate argument array
518
  ScopedVector<char*> argv(argc);
519

    
520
  // split the flags string into arguments
521
  argc = 1;  // be compatible with SetFlagsFromCommandLine()
522
  for (char* p = copy; *p != '\0'; argc++) {
523
    argv[argc] = p;
524
    p = SkipBlackSpace(p);
525
    if (*p != '\0') *p++ = '\0';  // 0-terminate argument
526
    p = SkipWhiteSpace(p);
527
  }
528

    
529
  // set the flags
530
  int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
531

    
532
  return result;
533
}
534

    
535

    
536
// static
537
void FlagList::ResetAllFlags() {
538
  for (size_t i = 0; i < num_flags; ++i) {
539
    flags[i].Reset();
540
  }
541
}
542

    
543

    
544
// static
545
void FlagList::PrintHelp() {
546
#if V8_TARGET_ARCH_ARM
547
  CpuFeatures::PrintTarget();
548
  CpuFeatures::Probe();
549
  CpuFeatures::PrintFeatures();
550
#endif  // V8_TARGET_ARCH_ARM
551

    
552
  printf("Usage:\n");
553
  printf("  shell [options] -e string\n");
554
  printf("    execute string in V8\n");
555
  printf("  shell [options] file1 file2 ... filek\n");
556
  printf("    run JavaScript scripts in file1, file2, ..., filek\n");
557
  printf("  shell [options]\n");
558
  printf("  shell [options] --shell [file1 file2 ... filek]\n");
559
  printf("    run an interactive JavaScript shell\n");
560
  printf("  d8 [options] file1 file2 ... filek\n");
561
  printf("  d8 [options]\n");
562
  printf("  d8 [options] --shell [file1 file2 ... filek]\n");
563
  printf("    run the new debugging shell\n\n");
564
  printf("Options:\n");
565
  for (size_t i = 0; i < num_flags; ++i) {
566
    Flag* f = &flags[i];
567
    SmartArrayPointer<const char> value = ToString(f);
568
    printf("  --%s (%s)\n        type: %s  default: %s\n",
569
           f->name(), f->comment(), Type2String(f->type()), *value);
570
  }
571
}
572

    
573

    
574
// static
575
void FlagList::EnforceFlagImplications() {
576
#define FLAG_MODE_DEFINE_IMPLICATIONS
577
#include "flag-definitions.h"
578
#undef FLAG_MODE_DEFINE_IMPLICATIONS
579
}
580

    
581
} }  // namespace v8::internal