Web Inspector: CodeGeneratorInspector.py: protect typed API from C++ implicit float...
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 May 2012 08:03:24 +0000 (08:03 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 May 2012 08:03:24 +0000 (08:03 +0000)
https://bugs.webkit.org/show_bug.cgi?id=87183

Patch by Peter Rybin <peter.rybin@gmail.com> on 2012-05-28
Reviewed by Yury Semikhatsky.

An intermediate C++ class is introduced that uses C++ template technique to control actual type
of its constructor argument.
All input parameters of type "int" now have type ExactlyInt.
All usage sites are fixed accordingly.

* inspector/CodeGeneratorInspector.py:
(TypeModel.RefPtrBased):
(TypeModel.Enum):
(TypeModel.ValueType):
(TypeModel.ValueType.get_opt_output_type_):
(TypeModel.ValueType.ValueOptional.get_command_return_pass_model):
(TypeModel.ExactlyInt):
(TypeModel.ExactlyInt.__init__):
(TypeModel.ExactlyInt.get_input_param_type_text):
(TypeModel.ExactlyInt.get_opt_output_type_):
(TypeModel.init_class):
(ExactlyInt):
* inspector/InspectorAgent.cpp:
(WebCore::InspectorAgent::enable):
(WebCore::InspectorAgent::didCreateWorker):
(WebCore::InspectorAgent::didDestroyWorker):
(WebCore::InspectorAgent::evaluateForTestInFrontend):
* inspector/InspectorApplicationCacheAgent.cpp:
(WebCore::InspectorApplicationCacheAgent::updateApplicationCacheStatus):
(WebCore::InspectorApplicationCacheAgent::getFramesWithManifests):
* inspector/InspectorDOMAgent.cpp:
(WebCore::InspectorDOMAgent::buildObjectForNode):
* inspector/InspectorMemoryAgent.cpp:
(WebCore::jsHeapInfo):
(WebCore::InspectorMemoryAgent::getProcessMemoryDistribution):
* inspector/PageRuntimeAgent.cpp:
(WebCore::PageRuntimeAgent::notifyContextCreated):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@118652 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/inspector/CodeGeneratorInspector.py
Source/WebCore/inspector/InspectorAgent.cpp
Source/WebCore/inspector/InspectorApplicationCacheAgent.cpp
Source/WebCore/inspector/InspectorDOMAgent.cpp
Source/WebCore/inspector/InspectorMemoryAgent.cpp
Source/WebCore/inspector/PageRuntimeAgent.cpp

index aca95bf..df3260b 100644 (file)
@@ -1,3 +1,43 @@
+2012-05-28  Peter Rybin  <peter.rybin@gmail.com>
+
+        Web Inspector: CodeGeneratorInspector.py: protect typed API from C++ implicit float to int cast
+        https://bugs.webkit.org/show_bug.cgi?id=87183
+
+        Reviewed by Yury Semikhatsky.
+
+        An intermediate C++ class is introduced that uses C++ template technique to control actual type
+        of its constructor argument.
+        All input parameters of type "int" now have type ExactlyInt. 
+        All usage sites are fixed accordingly.
+
+        * inspector/CodeGeneratorInspector.py:
+        (TypeModel.RefPtrBased):
+        (TypeModel.Enum):
+        (TypeModel.ValueType):
+        (TypeModel.ValueType.get_opt_output_type_):
+        (TypeModel.ValueType.ValueOptional.get_command_return_pass_model):
+        (TypeModel.ExactlyInt):
+        (TypeModel.ExactlyInt.__init__):
+        (TypeModel.ExactlyInt.get_input_param_type_text):
+        (TypeModel.ExactlyInt.get_opt_output_type_):
+        (TypeModel.init_class):
+        (ExactlyInt):
+        * inspector/InspectorAgent.cpp:
+        (WebCore::InspectorAgent::enable):
+        (WebCore::InspectorAgent::didCreateWorker):
+        (WebCore::InspectorAgent::didDestroyWorker):
+        (WebCore::InspectorAgent::evaluateForTestInFrontend):
+        * inspector/InspectorApplicationCacheAgent.cpp:
+        (WebCore::InspectorApplicationCacheAgent::updateApplicationCacheStatus):
+        (WebCore::InspectorApplicationCacheAgent::getFramesWithManifests):
+        * inspector/InspectorDOMAgent.cpp:
+        (WebCore::InspectorDOMAgent::buildObjectForNode):
+        * inspector/InspectorMemoryAgent.cpp:
+        (WebCore::jsHeapInfo):
+        (WebCore::InspectorMemoryAgent::getProcessMemoryDistribution):
+        * inspector/PageRuntimeAgent.cpp:
+        (WebCore::PageRuntimeAgent::notifyContextCreated):
+
 2012-05-28  Kentaro Hara  <haraken@chromium.org>
 
         Unreviewed. Rebaselined run-binding-tests results.
index ddae7a2..aaeb9aa 100755 (executable)
@@ -622,7 +622,7 @@ class CommandReturnPassModel:
 
 
 class TypeModel:
-    class RefPtrBased:
+    class RefPtrBased(object):
         def __init__(self, class_name):
             self.class_name = class_name
             self.optional = False
@@ -646,7 +646,7 @@ class TypeModel:
         def get_event_setter_expression_pattern():
             return "%s"
 
-    class Enum:
+    class Enum(object):
         def __init__(self, base_type_name):
             self.type_name = base_type_name + "::Enum"
 
@@ -679,7 +679,7 @@ class TypeModel:
         def get_event_setter_expression_pattern():
             return "%s"
 
-    class ValueType:
+    class ValueType(object):
         def __init__(self, type_name, is_heavy):
             self.type_name = type_name
             self.is_heavy = is_heavy
@@ -696,6 +696,9 @@ class TypeModel:
             else:
                 return self.type_name
 
+        def get_opt_output_type_(self):
+            return self.type_name
+
         @staticmethod
         def get_event_setter_expression_pattern():
             return "%s"
@@ -708,7 +711,7 @@ class TypeModel:
                 return self
 
             def get_command_return_pass_model(self):
-                return CommandReturnPassModel.OptOutput(self.base.type_name)
+                return CommandReturnPassModel.OptOutput(self.base.get_opt_output_type_())
 
             def get_input_param_type_text(self):
                 return "const %s* const" % self.base.type_name
@@ -717,12 +720,22 @@ class TypeModel:
             def get_event_setter_expression_pattern():
                 return "*%s"
 
+    class ExactlyInt(ValueType):
+        def __init__(self):
+            TypeModel.ValueType.__init__(self, "int", False)
+
+        def get_input_param_type_text(self):
+            return "TypeBuilder::ExactlyInt"
+
+        def get_opt_output_type_(self):
+            return "TypeBuilder::ExactlyInt"
+
     @classmethod
     def init_class(cls):
         cls.Bool = cls.ValueType("bool", False)
-        cls.Int = cls.ValueType("int", False)
+        cls.Int = cls.ExactlyInt()
         cls.Number = cls.ValueType("double", False)
-        cls.String = cls.ValueType("String", True)
+        cls.String = cls.ValueType("String", True,)
         cls.Object = cls.RefPtrBased("InspectorObject")
         cls.Array = cls.RefPtrBased("InspectorArray")
         cls.Any = cls.RefPtrBased("InspectorValue")
@@ -2224,6 +2237,30 @@ private:
 };
 
 
+// A small transient wrapper around int type, that can be used as a funciton parameter type
+// cleverly disallowing C++ implicit casts from float or double.
+class ExactlyInt {
+public:
+    template<typename T>
+    ExactlyInt(T t) : m_value(cast_to_int<T>(t)) {}
+
+    ExactlyInt() {}
+
+    operator int() { return m_value; }
+private:
+    int m_value;
+
+    template<typename T>
+    static int cast_to_int(T t) { return T::default_case_cast_is_not_supported(); }
+};
+
+template<>
+inline int ExactlyInt::cast_to_int<int>(int i) { return i; }
+
+template<>
+inline int ExactlyInt::cast_to_int<unsigned int>(unsigned int i) { return i; }
+
+
 // This class provides "Traits" type for the input type T. It is programmed using C++ template specialization
 // technique. By default it simply takes "ItemTraits" type from T, but it doesn't work with the base types.
 template<typename T>
index 35e5314..b3aa3bc 100644 (file)
@@ -126,7 +126,7 @@ void InspectorAgent::enable(ErrorString*)
     WorkersMap::iterator workersEnd = m_workers.end();
     for (WorkersMap::iterator it = m_workers.begin(); it != workersEnd; ++it) {
         InspectorWorkerResource* worker = it->second.get();
-        m_frontend->inspector()->didCreateWorker(worker->id(), worker->url(), worker->isSharedWorker());
+        m_frontend->inspector()->didCreateWorker(static_cast<int>(worker->id()), worker->url(), worker->isSharedWorker());
     }
 #endif
 
@@ -134,7 +134,7 @@ void InspectorAgent::enable(ErrorString*)
         inspect(m_pendingInspectData.first, m_pendingInspectData.second);
 
     for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it)
-        m_frontend->inspector()->evaluateForTestInFrontend((*it).first, (*it).second);
+        m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
     m_pendingEvaluateTestCommands.clear();
 }
 
@@ -163,7 +163,7 @@ void InspectorAgent::didCreateWorker(intptr_t id, const String& url, bool isShar
     m_workers.set(id, workerResource);
 #if ENABLE(JAVASCRIPT_DEBUGGER)
     if (m_inspectedPage && m_frontend && m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
-        m_frontend->inspector()->didCreateWorker(id, url, isSharedWorker);
+        m_frontend->inspector()->didCreateWorker(static_cast<int>(id), url, isSharedWorker);
 #endif
 }
 
@@ -177,7 +177,7 @@ void InspectorAgent::didDestroyWorker(intptr_t id)
         return;
 #if ENABLE(JAVASCRIPT_DEBUGGER)
     if (m_inspectedPage && m_frontend && m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
-        m_frontend->inspector()->didDestroyWorker(id);
+        m_frontend->inspector()->didDestroyWorker(static_cast<int>(id));
 #endif
     m_workers.remove(workerResource);
 }
@@ -186,7 +186,7 @@ void InspectorAgent::didDestroyWorker(intptr_t id)
 void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
 {
     if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
-        m_frontend->inspector()->evaluateForTestInFrontend(callId, script);
+        m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>(callId), script);
     else
         m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
 }
index f70535f..2e7c7d9 100644 (file)
@@ -95,7 +95,7 @@ void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
 
     String manifestURL = info.m_manifest.string();
-    m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, status);
+    m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
 }
 
 void InspectorApplicationCacheAgent::networkStateChanged()
@@ -121,7 +121,7 @@ void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr
             RefPtr<TypeBuilder::ApplicationCache::FrameWithManifest> value = TypeBuilder::ApplicationCache::FrameWithManifest::create()
                 .setFrameId(m_pageAgent->frameId(frame))
                 .setManifestURL(manifestURL)
-                .setStatus(host->status());
+                .setStatus(static_cast<int>(host->status()));
             result->addItem(value);
         }
     }
index 7ca0461..ac4df98 100644 (file)
@@ -1219,7 +1219,7 @@ PassRefPtr<TypeBuilder::DOM::Node> InspectorDOMAgent::buildObjectForNode(Node* n
 
     RefPtr<TypeBuilder::DOM::Node> value = TypeBuilder::DOM::Node::create()
         .setNodeId(id)
-        .setNodeType(node->nodeType())
+        .setNodeType(static_cast<int>(node->nodeType()))
         .setNodeName(nodeName)
         .setLocalName(localName)
         .setNodeValue(nodeValue);
index cf05d9b..2900775 100644 (file)
@@ -325,11 +325,11 @@ static PassRefPtr<WebCore::TypeBuilder::Memory::MemoryBlock> jsHeapInfo()
     ScriptGCEvent::getHeapSize(usedJSHeapSize, totalJSHeapSize, jsHeapSizeLimit);
 
     RefPtr<WebCore::TypeBuilder::Memory::MemoryBlock> totalJsHeap = WebCore::TypeBuilder::Memory::MemoryBlock::create().setName(MemoryBlockName::totalJsHeap);
-    totalJsHeap->setSize(totalJSHeapSize);
+    totalJsHeap->setSize(static_cast<int>(totalJSHeapSize));
 
     RefPtr<TypeBuilder::Array<WebCore::TypeBuilder::Memory::MemoryBlock> > children = TypeBuilder::Array<WebCore::TypeBuilder::Memory::MemoryBlock>::create();
     RefPtr<WebCore::TypeBuilder::Memory::MemoryBlock> usedJsHeap = WebCore::TypeBuilder::Memory::MemoryBlock::create().setName(MemoryBlockName::usedJsHeap);
-    usedJsHeap->setSize(usedJSHeapSize);
+    usedJsHeap->setSize(static_cast<int>(usedJSHeapSize));
     children->addItem(usedJsHeap);
 
     totalJsHeap->setChildren(children);
@@ -344,7 +344,7 @@ void InspectorMemoryAgent::getProcessMemoryDistribution(ErrorString*, RefPtr<Web
     PlatformSupport::getProcessMemorySize(&privateBytes, &sharedBytes);
 #endif
     processMemory = WebCore::TypeBuilder::Memory::MemoryBlock::create().setName(MemoryBlockName::processPrivateMemory);
-    processMemory->setSize(privateBytes);
+    processMemory->setSize(static_cast<int>(privateBytes));
 
     RefPtr<TypeBuilder::Array<WebCore::TypeBuilder::Memory::MemoryBlock> > children = TypeBuilder::Array<WebCore::TypeBuilder::Memory::MemoryBlock>::create();
     children->addItem(jsHeapInfo());
index 4e9f21a..0d28f7a 100644 (file)
@@ -154,7 +154,7 @@ void PageRuntimeAgent::notifyContextCreated(const String& frameId, ScriptState*
     long executionContextId = injectedScriptManager()->injectedScriptIdFor(scriptState);
     String name = securityOrigin ? securityOrigin->toString() : "";
     m_frontend->isolatedContextCreated(ExecutionContextDescription::create()
-        .setId(executionContextId)
+        .setId(static_cast<int>(executionContextId))
         .setIsPageContext(isPageContext)
         .setName(name)
         .setFrameId(frameId)