<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[182331] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/182331">182331</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2015-04-03 14:01:43 -0700 (Fri, 03 Apr 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Some JSC Options refactoring and enhancements.
&lt;https://webkit.org/b/143384&gt;

Rubber stamped by Benjamin Poulain.

Create a better encapsulated Option class to make working with options easier.  This
is a building block towards a JIT policy scaling debugging option I will introduce later.

This work entails:
1. Convert Options::Option into a public class Option (who works closely with Options).
2. Convert Options::EntryType into an enum class Options::Type and make it public.
3. Renamed Options::OPT_&lt;option name&gt; to Options::&lt;option name&gt;ID because it reads better.
4. Add misc methods to class Option to make it more useable.

* runtime/Options.cpp:
(JSC::Options::dumpOption):
(JSC::Option::dump):
(JSC::Option::operator==):
(JSC::Options::Option::dump): Deleted.
(JSC::Options::Option::operator==): Deleted.
* runtime/Options.h:
(JSC::Option::Option):
(JSC::Option::operator!=):
(JSC::Option::name):
(JSC::Option::description):
(JSC::Option::type):
(JSC::Option::isOverridden):
(JSC::Option::defaultOption):
(JSC::Option::boolVal):
(JSC::Option::unsignedVal):
(JSC::Option::doubleVal):
(JSC::Option::int32Val):
(JSC::Option::optionRangeVal):
(JSC::Option::optionStringVal):
(JSC::Option::gcLogLevelVal):
(JSC::Options::Option::Option): Deleted.
(JSC::Options::Option::operator!=): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeOptionscpp">trunk/Source/JavaScriptCore/runtime/Options.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeOptionsh">trunk/Source/JavaScriptCore/runtime/Options.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (182330 => 182331)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-04-03 20:46:11 UTC (rev 182330)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-04-03 21:01:43 UTC (rev 182331)
</span><span class="lines">@@ -1,3 +1,43 @@
</span><ins>+2015-04-03  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        Some JSC Options refactoring and enhancements.
+        &lt;https://webkit.org/b/143384&gt;
+
+        Rubber stamped by Benjamin Poulain.
+
+        Create a better encapsulated Option class to make working with options easier.  This
+        is a building block towards a JIT policy scaling debugging option I will introduce later.
+
+        This work entails:
+        1. Convert Options::Option into a public class Option (who works closely with Options).
+        2. Convert Options::EntryType into an enum class Options::Type and make it public.
+        3. Renamed Options::OPT_&lt;option name&gt; to Options::&lt;option name&gt;ID because it reads better.
+        4. Add misc methods to class Option to make it more useable.
+
+        * runtime/Options.cpp:
+        (JSC::Options::dumpOption):
+        (JSC::Option::dump):
+        (JSC::Option::operator==):
+        (JSC::Options::Option::dump): Deleted.
+        (JSC::Options::Option::operator==): Deleted.
+        * runtime/Options.h:
+        (JSC::Option::Option):
+        (JSC::Option::operator!=):
+        (JSC::Option::name):
+        (JSC::Option::description):
+        (JSC::Option::type):
+        (JSC::Option::isOverridden):
+        (JSC::Option::defaultOption):
+        (JSC::Option::boolVal):
+        (JSC::Option::unsignedVal):
+        (JSC::Option::doubleVal):
+        (JSC::Option::int32Val):
+        (JSC::Option::optionRangeVal):
+        (JSC::Option::optionStringVal):
+        (JSC::Option::gcLogLevelVal):
+        (JSC::Options::Option::Option): Deleted.
+        (JSC::Options::Option::operator!=): Deleted.
+
</ins><span class="cx"> 2015-04-03  Geoffrey Garen  &lt;ggaren@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         JavaScriptCore API should support type checking for Array and Date
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeOptionscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/Options.cpp (182330 => 182331)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/Options.cpp        2015-04-03 20:46:11 UTC (rev 182330)
+++ trunk/Source/JavaScriptCore/runtime/Options.cpp        2015-04-03 21:01:43 UTC (rev 182331)
</span><span class="lines">@@ -207,7 +207,7 @@
</span><span class="cx"> // Realize the names for each of the options:
</span><span class="cx"> const Options::EntryInfo Options::s_optionsInfo[Options::numberOfOptions] = {
</span><span class="cx"> #define FOR_EACH_OPTION(type_, name_, defaultValue_, description_) \
</span><del>-    { #name_, description_, Options::type_##Type },
</del><ins>+    { #name_, description_, Options::Type::type_##Type },
</ins><span class="cx">     JSC_OPTIONS(FOR_EACH_OPTION)
</span><span class="cx"> #undef FOR_EACH_OPTION
</span><span class="cx"> };
</span><span class="lines">@@ -366,80 +366,77 @@
</span><span class="cx">     if (id &gt;= numberOfOptions)
</span><span class="cx">         return; // Illegal option.
</span><span class="cx"> 
</span><del>-    EntryType type = s_optionsInfo[id].type;
-    Option option(type, s_options[id]);
-    Option defaultOption(type, s_defaultOptions[id]);
</del><ins>+    Option option(id);
+    bool wasOverridden = option.isOverridden();
+    bool needsDescription = (level == DumpLevel::Verbose &amp;&amp; option.description());
</ins><span class="cx"> 
</span><del>-    bool wasOverridden = (option != defaultOption);
-    bool needsDescription = (level == DumpLevel::Verbose &amp;&amp; s_optionsInfo[id].description);
-
</del><span class="cx">     if (level == DumpLevel::Overridden &amp;&amp; !wasOverridden)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    fprintf(stream, &quot;%s%s: &quot;, header, s_optionsInfo[id].name);
</del><ins>+    fprintf(stream, &quot;%s%s: &quot;, header, option.name());
</ins><span class="cx">     option.dump(stream);
</span><span class="cx"> 
</span><span class="cx">     if (wasOverridden) {
</span><span class="cx">         fprintf(stream, &quot; (default: &quot;);
</span><del>-        defaultOption.dump(stream);
</del><ins>+        option.defaultOption().dump(stream);
</ins><span class="cx">         fprintf(stream, &quot;)&quot;);
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (needsDescription)
</span><del>-        fprintf(stream, &quot;\n%s   - %s&quot;, header, s_optionsInfo[id].description);
</del><ins>+        fprintf(stream, &quot;   ... %s&quot;, option.description());
</ins><span class="cx"> 
</span><span class="cx">     fprintf(stream, &quot;%s&quot;, footer);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void Options::Option::dump(FILE* stream) const
</del><ins>+void Option::dump(FILE* stream) const
</ins><span class="cx"> {
</span><del>-    switch (m_type) {
-    case boolType:
</del><ins>+    switch (type()) {
+    case Options::Type::boolType:
</ins><span class="cx">         fprintf(stream, &quot;%s&quot;, m_entry.boolVal ? &quot;true&quot; : &quot;false&quot;);
</span><span class="cx">         break;
</span><del>-    case unsignedType:
</del><ins>+    case Options::Type::unsignedType:
</ins><span class="cx">         fprintf(stream, &quot;%u&quot;, m_entry.unsignedVal);
</span><span class="cx">         break;
</span><del>-    case doubleType:
</del><ins>+    case Options::Type::doubleType:
</ins><span class="cx">         fprintf(stream, &quot;%lf&quot;, m_entry.doubleVal);
</span><span class="cx">         break;
</span><del>-    case int32Type:
</del><ins>+    case Options::Type::int32Type:
</ins><span class="cx">         fprintf(stream, &quot;%d&quot;, m_entry.int32Val);
</span><span class="cx">         break;
</span><del>-    case optionRangeType:
</del><ins>+    case Options::Type::optionRangeType:
</ins><span class="cx">         fprintf(stream, &quot;%s&quot;, m_entry.optionRangeVal.rangeString());
</span><span class="cx">         break;
</span><del>-    case optionStringType: {
</del><ins>+    case Options::Type::optionStringType: {
</ins><span class="cx">         const char* option = m_entry.optionStringVal;
</span><span class="cx">         if (!option)
</span><span class="cx">             option = &quot;&quot;;
</span><span class="cx">         fprintf(stream, &quot;%s&quot;, option);
</span><span class="cx">         break;
</span><span class="cx">     }
</span><del>-    case gcLogLevelType: {
</del><ins>+    case Options::Type::gcLogLevelType: {
</ins><span class="cx">         fprintf(stream, &quot;%s&quot;, GCLogging::levelAsString(m_entry.gcLogLevelVal));
</span><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool Options::Option::operator==(const Options::Option&amp; other) const
</del><ins>+bool Option::operator==(const Option&amp; other) const
</ins><span class="cx"> {
</span><del>-    switch (m_type) {
-    case boolType:
</del><ins>+    switch (type()) {
+    case Options::Type::boolType:
</ins><span class="cx">         return m_entry.boolVal == other.m_entry.boolVal;
</span><del>-    case unsignedType:
</del><ins>+    case Options::Type::unsignedType:
</ins><span class="cx">         return m_entry.unsignedVal == other.m_entry.unsignedVal;
</span><del>-    case doubleType:
</del><ins>+    case Options::Type::doubleType:
</ins><span class="cx">         return (m_entry.doubleVal == other.m_entry.doubleVal) || (isnan(m_entry.doubleVal) &amp;&amp; isnan(other.m_entry.doubleVal));
</span><del>-    case int32Type:
</del><ins>+    case Options::Type::int32Type:
</ins><span class="cx">         return m_entry.int32Val == other.m_entry.int32Val;
</span><del>-    case optionRangeType:
</del><ins>+    case Options::Type::optionRangeType:
</ins><span class="cx">         return m_entry.optionRangeVal.rangeString() == other.m_entry.optionRangeVal.rangeString();
</span><del>-    case optionStringType:
</del><ins>+    case Options::Type::optionStringType:
</ins><span class="cx">         return (m_entry.optionStringVal == other.m_entry.optionStringVal)
</span><span class="cx">             || (m_entry.optionStringVal &amp;&amp; other.m_entry.optionStringVal &amp;&amp; !strcmp(m_entry.optionStringVal, other.m_entry.optionStringVal));
</span><del>-    case gcLogLevelType:
</del><ins>+    case Options::Type::gcLogLevelType:
</ins><span class="cx">         return m_entry.gcLogLevelVal == other.m_entry.gcLogLevelVal;
</span><span class="cx">     }
</span><span class="cx">     return false;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeOptionsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/Options.h (182330 => 182331)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/Options.h        2015-04-03 20:46:11 UTC (rev 182330)
+++ trunk/Source/JavaScriptCore/runtime/Options.h        2015-04-03 21:01:43 UTC (rev 182331)
</span><span class="lines">@@ -321,12 +321,21 @@
</span><span class="cx">     // Declare the option IDs:
</span><span class="cx">     enum OptionID {
</span><span class="cx"> #define FOR_EACH_OPTION(type_, name_, defaultValue_, description_) \
</span><del>-        OPT_##name_,
</del><ins>+        name_##ID,
</ins><span class="cx">         JSC_OPTIONS(FOR_EACH_OPTION)
</span><span class="cx"> #undef FOR_EACH_OPTION
</span><span class="cx">         numberOfOptions
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    enum class Type {
+        boolType,
+        unsignedType,
+        doubleType,
+        int32Type,
+        optionRangeType,
+        optionStringType,
+        gcLogLevelType,
+    };
</ins><span class="cx"> 
</span><span class="cx">     static void initialize();
</span><span class="cx"> 
</span><span class="lines">@@ -338,23 +347,13 @@
</span><span class="cx"> 
</span><span class="cx">     // Declare accessors for each option:
</span><span class="cx"> #define FOR_EACH_OPTION(type_, name_, defaultValue_, description_) \
</span><del>-    ALWAYS_INLINE static type_&amp; name_() { return s_options[OPT_##name_].type_##Val; } \
-    ALWAYS_INLINE static type_&amp; name_##Default() { return s_defaultOptions[OPT_##name_].type_##Val; }
</del><ins>+    ALWAYS_INLINE static type_&amp; name_() { return s_options[name_##ID].type_##Val; } \
+    ALWAYS_INLINE static type_&amp; name_##Default() { return s_defaultOptions[name_##ID].type_##Val; }
</ins><span class="cx"> 
</span><span class="cx">     JSC_OPTIONS(FOR_EACH_OPTION)
</span><span class="cx"> #undef FOR_EACH_OPTION
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    enum EntryType {
-        boolType,
-        unsignedType,
-        doubleType,
-        int32Type,
-        optionRangeType,
-        optionStringType,
-        gcLogLevelType,
-    };
-
</del><span class="cx">     // For storing for an option value:
</span><span class="cx">     union Entry {
</span><span class="cx">         bool boolVal;
</span><span class="lines">@@ -366,28 +365,11 @@
</span><span class="cx">         GCLogging::Level gcLogLevelVal;
</span><span class="cx">     };
</span><span class="cx"> 
</span><del>-    class Option {
-    public:
-        Option(EntryType type, Entry&amp; entry)
-            : m_type(type)
-            , m_entry(entry)
-        {
-        }
-
-        void dump(FILE*) const;
-        bool operator==(const Option&amp; other) const;
-        bool operator!=(const Option&amp; other) const { return !(*this == other); }
-
-    private:
-        EntryType m_type;
-        Entry&amp; m_entry;
-    };
-
</del><span class="cx">     // For storing constant meta data about each option:
</span><span class="cx">     struct EntryInfo {
</span><span class="cx">         const char* name;
</span><span class="cx">         const char* description;
</span><del>-        EntryType type;
</del><ins>+        Type type;
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     Options();
</span><span class="lines">@@ -396,8 +378,108 @@
</span><span class="cx">     JS_EXPORTDATA static Entry s_options[numberOfOptions];
</span><span class="cx">     static Entry s_defaultOptions[numberOfOptions];
</span><span class="cx">     static const EntryInfo s_optionsInfo[numberOfOptions];
</span><ins>+
+    friend class Option;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><ins>+class Option {
+public:
+    Option(Options::OptionID id)
+        : m_id(id)
+        , m_entry(Options::s_options[m_id])
+    {
+    }
+    
+    void dump(FILE*) const;
+    bool operator==(const Option&amp; other) const;
+    bool operator!=(const Option&amp; other) const { return !(*this == other); }
+    
+    const char* name() const;
+    const char* description() const;
+    Options::Type type() const;
+    bool isOverridden() const;
+    const Option defaultOption() const;
+    
+    bool&amp; boolVal();
+    unsigned&amp; unsignedVal();
+    double&amp; doubleVal();
+    int32_t&amp; int32Val();
+    OptionRange optionRangeVal();
+    const char* optionStringVal();
+    GCLogging::Level&amp; gcLogLevelVal();
+    
+private:
+    // Only used for constructing default Options.
+    Option(Options::OptionID id, Options::Entry&amp; entry)
+        : m_id(id)
+        , m_entry(entry)
+    {
+    }
+    
+    Options::OptionID m_id;
+    Options::Entry&amp; m_entry;
+};
+
+inline const char* Option::name() const
+{
+    return Options::s_optionsInfo[m_id].name;
+}
+
+inline const char* Option::description() const
+{
+    return Options::s_optionsInfo[m_id].description;
+}
+
+inline Options::Type Option::type() const
+{
+    return Options::s_optionsInfo[m_id].type;
+}
+
+inline bool Option::isOverridden() const
+{
+    return *this != defaultOption();
+}
+
+inline const Option Option::defaultOption() const
+{
+    return Option(m_id, Options::s_defaultOptions[m_id]);
+}
+
+inline bool&amp; Option::boolVal()
+{
+    return m_entry.boolVal;
+}
+
+inline unsigned&amp; Option::unsignedVal()
+{
+    return m_entry.unsignedVal;
+}
+
+inline double&amp; Option::doubleVal()
+{
+    return m_entry.doubleVal;
+}
+
+inline int32_t&amp; Option::int32Val()
+{
+    return m_entry.int32Val;
+}
+
+inline OptionRange Option::optionRangeVal()
+{
+    return m_entry.optionRangeVal;
+}
+
+inline const char* Option::optionStringVal()
+{
+    return m_entry.optionStringVal;
+}
+
+inline GCLogging::Level&amp; Option::gcLogLevelVal()
+{
+    return m_entry.gcLogLevelVal;
+}
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // Options_h
</span></span></pre>
</div>
</div>

</body>
</html>