v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
1// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_FLAGS_FLAGS_H_
6#define V8_FLAGS_FLAGS_H_
7
8#include <optional>
9
10#include "src/common/globals.h"
11
12#if V8_ENABLE_WEBASSEMBLY
13// Include the wasm-limits.h header for some default values of Wasm flags.
14// This can be reverted once we can use designated initializations (C++20) for
15// {v8_flags} (defined in flags.cc) instead of specifying the default values in
16// the header and using the default constructor.
18#endif
19
20namespace v8::internal {
21
22// The value of a single flag (this is the type of all v8_flags.* fields).
23template <typename T>
24class FlagValue {
25 // {FlagValue} types will be memory-protected in {FlagList::FreezeFlags}.
26 // We currently allow the following types to be used for flags:
27 // - Arithmetic types like bool, int, size_t, double; those will trivially be
28 // protected.
29 // - std::optional<bool>, which is basically a POD, and can also be
30 // protected.
31 // - const char*, for which we currently do not protect the actual string
32 // value. TODO(12887): Also protect the string storage.
33 //
34 // Other types can be added as needed, after checking that memory protection
35 // works for them.
36 static_assert(std::is_same_v<std::decay_t<T>, T>);
37 static_assert(std::is_arithmetic_v<T> ||
38 std::is_same_v<std::optional<bool>, T> ||
39 std::is_same_v<const char*, T>);
40
41 public:
43 explicit constexpr FlagValue(T value) : value_(value) {}
44
45 // Implicitly convert to a {T}. Not marked {constexpr} so we do not get
46 // compiler warnings about dead code (when checking readonly flags).
47 operator T() const { return value_; }
48
49 // Explicitly convert to a {T} via {value()}. This is {constexpr} so we can
50 // use it for computing other constants.
51 constexpr T value() const { return value_; }
52
53 // Assign a new value (defined below).
54 inline FlagValue<T>& operator=(T new_value);
55
56 private:
58};
59
60// Declare a struct to hold all of our flags.
61struct alignas(kMinimumOSPageSize) FlagValues {
62 FlagValues() = default;
63 // No copying, moving, or assigning. This is a singleton struct.
64 FlagValues(const FlagValues&) = delete;
66 FlagValues& operator=(const FlagValues&) = delete;
68
69#define FLAG_MODE_DECLARE
70#include "src/flags/flag-definitions.h" // NOLINT(build/include)
71#undef FLAG_MODE_DECLARE
72};
73
74V8_EXPORT_PRIVATE extern FlagValues v8_flags;
75
76// The global list of all flags.
78 public:
80 public:
81 enum ExitBehavior : bool { kExit = true, kDontExit = false };
82
83 explicit HelpOptions(ExitBehavior exit_behavior = kExit,
84 const char* usage = nullptr)
85 : exit_behavior_(exit_behavior), usage_(usage) {}
86
87 bool ShouldExit() { return exit_behavior_ == kExit; }
88 bool HasUsage() { return usage_ != nullptr; }
89 const char* usage() { return usage_; }
90
91 private:
93 const char* usage_;
94 };
95
96 // Set the flag values by parsing the command line. If remove_flags is
97 // set, the recognized flags and associated values are removed from (argc,
98 // argv) and only unknown arguments remain. Returns 0 if no error occurred.
99 // Otherwise, returns the argv index > 0 for the argument where an error
100 // occurred. In that case, (argc, argv) will remain unchanged independent of
101 // the remove_flags value, and no assumptions about flag settings should be
102 // made. If exit_behavior is set to Exit and --help has been specified on the
103 // command line, then the usage string will be printed, if it was specified,
104 // followed by the help flag and then the process will exit. Otherwise the
105 // flag help will be displayed but execution will continue.
106 //
107 // The following syntax for flags is accepted (both '-' and '--' are ok):
108 //
109 // --flag (bool flags only)
110 // --no-flag (bool flags only)
111 // --flag=value (non-bool flags only, no spaces around '=')
112 // --flag value (non-bool flags only)
113 // -- (capture all remaining args in JavaScript)
114 static int SetFlagsFromCommandLine(
115 int* argc, char** argv, bool remove_flags,
117
118 // Set the flag values by parsing the string str. Splits string into argc
119 // substrings argv[], each of which consisting of non-white-space chars,
120 // and then calls SetFlagsFromCommandLine() and returns its result.
121 static int SetFlagsFromString(const char* str, size_t len);
122
123 // Freeze the current flag values (disallow changes via the API).
124 static void FreezeFlags();
125
126 // Returns true if the flags are currently frozen.
127 static bool IsFrozen();
128
129 // Free dynamically allocated memory of strings. This is called during
130 // teardown; flag values cannot be used afterwards any more.
131 static void ReleaseDynamicAllocations();
132
133 // Print help to stdout with flags, types, and default values.
134 static void PrintHelp();
135
136 static void PrintValues();
137
138 // Prints JS and Wasm feature flags, categorized by in-progress, staging, and
139 // shipping, as JSON. Used by scripts to clean up flags in test files.
140 static void PrintFeatureFlagsJSON();
141
142 // Reset some contradictory flags provided on the command line during
143 // fuzzing.
144 static void ResolveContradictionsWhenFuzzing();
145
146 // Set flags as consequence of being implied by another flag.
147 static void EnforceFlagImplications();
148
149 // Hash of flags (to quickly determine mismatching flag expectations).
150 // This hash is calculated during V8::Initialize and cached.
151 static uint32_t Hash();
152
153 private:
154 // Reset the flag hash on flag changes. This is a private method called from
155 // {FlagValue<T>::operator=}; there should be no need to call it from any
156 // other place.
157 static void ResetFlagHash();
158
159 // Make {FlagValue<T>} a friend, so it can call {ResetFlagHash()}.
160 template <typename T>
161 friend class FlagValue;
162};
163
164template <typename T>
166 if (new_value != value_) {
167 FlagList::ResetFlagHash();
168 value_ = new_value;
169 }
170 return *this;
171}
172
173} // namespace v8::internal
174
175#endif // V8_FLAGS_FLAGS_H_
#define T
HelpOptions(ExitBehavior exit_behavior=kExit, const char *usage=nullptr)
Definition flags.h:83
constexpr FlagValue(T value)
Definition flags.h:43
constexpr T value() const
Definition flags.h:51
FlagValue< T > & operator=(T new_value)
Definition flags.h:165
Register const value_
V8_EXPORT_PRIVATE FlagValues v8_flags
#define V8_EXPORT_PRIVATE
Definition macros.h:460
FlagValues & operator=(const FlagValues &)=delete
FlagValues(const FlagValues &)=delete
FlagValues & operator=(FlagValues &&)=delete
FlagValues(FlagValues &&)=delete