1 | /* |
2 | * Copyright (C) 2011-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "JSCConfig.h" |
29 | #include "JSExportMacros.h" |
30 | #include <stdint.h> |
31 | #include <stdio.h> |
32 | #include <wtf/PrintStream.h> |
33 | #include <wtf/StdLibExtras.h> |
34 | |
35 | namespace WTF { |
36 | class StringBuilder; |
37 | } |
38 | using WTF::StringBuilder; |
39 | |
40 | namespace JSC { |
41 | |
42 | class Options { |
43 | public: |
44 | enum class DumpLevel : uint8_t { |
45 | None = 0, |
46 | Overridden, |
47 | All, |
48 | Verbose |
49 | }; |
50 | |
51 | enum class Availability : uint8_t { |
52 | Normal = 0, |
53 | Restricted, |
54 | Configurable |
55 | }; |
56 | |
57 | #define DECLARE_OPTION_ID(type_, name_, defaultValue_, availability_, description_) \ |
58 | name_##ID, |
59 | |
60 | enum ID : uint16_t { |
61 | FOR_EACH_JSC_OPTION(DECLARE_OPTION_ID) |
62 | }; |
63 | #undef DECLARE_OPTION_ID |
64 | |
65 | enum class Type : uint8_t { |
66 | Bool, |
67 | Unsigned, |
68 | Double, |
69 | Int32, |
70 | Size, |
71 | OptionRange, |
72 | OptionString, |
73 | GCLogLevel, |
74 | }; |
75 | |
76 | JS_EXPORT_PRIVATE static void initialize(); |
77 | |
78 | // Parses a string of options where each option is of the format "--<optionName>=<value>" |
79 | // and are separated by a space. The leading "--" is optional and will be ignored. |
80 | JS_EXPORT_PRIVATE static bool setOptions(const char* optionsList); |
81 | |
82 | // Parses a single command line option in the format "<optionName>=<value>" |
83 | // (no spaces allowed) and set the specified option if appropriate. |
84 | JS_EXPORT_PRIVATE static bool setOption(const char* arg); |
85 | |
86 | JS_EXPORT_PRIVATE static void dumpAllOptions(FILE*, DumpLevel, const char* title = nullptr); |
87 | JS_EXPORT_PRIVATE static void dumpAllOptionsInALine(StringBuilder&); |
88 | |
89 | JS_EXPORT_PRIVATE static void ensureOptionsAreCoherent(); |
90 | |
91 | #define DECLARE_OPTION_ACCESSORS(type_, name_, defaultValue_, availability_, description_) \ |
92 | ALWAYS_INLINE static OptionsStorage::type_& name_() { return g_jscConfig.options.name_; } \ |
93 | ALWAYS_INLINE static OptionsStorage::type_& name_##Default() { return g_jscConfig.options.name_##Default; } |
94 | |
95 | FOR_EACH_JSC_OPTION(DECLARE_OPTION_ACCESSORS) |
96 | #undef DECLARE_OPTION_ACCESSORS |
97 | |
98 | static bool isAvailable(ID, Availability); |
99 | |
100 | private: |
101 | struct ConstMetaData { |
102 | const char* name; |
103 | const char* description; |
104 | Type type; |
105 | Availability availability; |
106 | uint16_t offsetOfOption; |
107 | uint16_t offsetOfOptionDefault; |
108 | }; |
109 | |
110 | Options(); |
111 | |
112 | enum DumpDefaultsOption { |
113 | DontDumpDefaults, |
114 | DumpDefaults |
115 | }; |
116 | static void dumpOptionsIfNeeded(); |
117 | static void dumpAllOptions(StringBuilder&, DumpLevel, const char* title, |
118 | const char* separator, const char* , const char* , DumpDefaultsOption); |
119 | static void dumpOption(StringBuilder&, DumpLevel, ID, |
120 | const char* , const char* , DumpDefaultsOption); |
121 | |
122 | static bool setOptionWithoutAlias(const char* arg); |
123 | static bool setAliasedOption(const char* arg); |
124 | static bool overrideAliasedOptionWithHeuristic(const char* name); |
125 | |
126 | inline static void* addressOfOption(Options::ID); |
127 | inline static void* addressOfOptionDefault(Options::ID); |
128 | |
129 | static const ConstMetaData s_constMetaData[NumberOfOptions]; |
130 | |
131 | friend struct OptionReader; |
132 | }; |
133 | |
134 | } // namespace JSC |
135 | |