Title: [129933] trunk/Source/WebCore
Revision
129933
Author
fsam...@chromium.org
Date
2012-09-28 12:38:42 -0700 (Fri, 28 Sep 2012)

Log Message

[V8] Make v8NPObjectMap per Context
https://bugs.webkit.org/show_bug.cgi?id=97703

Reviewed by Adam Barth.

V8NPObject is a V8Object wrapper for use by the npruntime.

staticV8NPObjectMap is used for keeping record of V8NPObjects as they are created and destroyed
to ensure that an existing V8NPObject wrapper is returned for a V8Object in npCreateV8ScriptObject.

Once a context is gone, the NPObjects for the context are no longer valid and that record keeping
no longer makes sense so we clear the map.

However, because the map was static, it existed for all pages across contexts. Clearing the
map if one context is gone should not impact the V8NPObject map of other contexts.

Thus, this patch makes the V8NPObject map per context.

* bindings/v8/NPV8Object.cpp:
(WebCore::freeV8NPObject):
(WebCore::npCreateV8ScriptObject):
* bindings/v8/V8PerContextData.h:
(WebCore):
(WebCore::V8PerContextData::v8NPObjectMap):
(V8PerContextData):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (129932 => 129933)


--- trunk/Source/WebCore/ChangeLog	2012-09-28 19:37:51 UTC (rev 129932)
+++ trunk/Source/WebCore/ChangeLog	2012-09-28 19:38:42 UTC (rev 129933)
@@ -1,3 +1,31 @@
+2012-09-28  Fady Samuel  <fsam...@chromium.org>
+
+        [V8] Make v8NPObjectMap per Context
+        https://bugs.webkit.org/show_bug.cgi?id=97703
+
+        Reviewed by Adam Barth.
+
+        V8NPObject is a V8Object wrapper for use by the npruntime.
+
+        staticV8NPObjectMap is used for keeping record of V8NPObjects as they are created and destroyed
+        to ensure that an existing V8NPObject wrapper is returned for a V8Object in npCreateV8ScriptObject.
+        
+        Once a context is gone, the NPObjects for the context are no longer valid and that record keeping
+        no longer makes sense so we clear the map.
+
+        However, because the map was static, it existed for all pages across contexts. Clearing the
+        map if one context is gone should not impact the V8NPObject map of other contexts.
+
+        Thus, this patch makes the V8NPObject map per context.
+
+        * bindings/v8/NPV8Object.cpp:
+        (WebCore::freeV8NPObject):
+        (WebCore::npCreateV8ScriptObject):
+        * bindings/v8/V8PerContextData.h:
+        (WebCore):
+        (WebCore::V8PerContextData::v8NPObjectMap):
+        (V8PerContextData):
+
 2012-09-28  Alberto Garcia  <agar...@igalia.com>
 
         TextureMapperGL: fix -Wsign-compare compilation warning.

Modified: trunk/Source/WebCore/bindings/v8/NPV8Object.cpp (129932 => 129933)


--- trunk/Source/WebCore/bindings/v8/NPV8Object.cpp	2012-09-28 19:37:51 UTC (rev 129932)
+++ trunk/Source/WebCore/bindings/v8/NPV8Object.cpp	2012-09-28 19:38:42 UTC (rev 129933)
@@ -55,9 +55,6 @@
     return &typeInfo;
 }
 
-typedef Vector<V8NPObject*> V8NPObjectVector;
-typedef HashMap<int, V8NPObjectVector> V8NPObjectMap;
-
 static v8::Local<v8::Context> toV8Context(NPP npp, NPObject* npObject)
 {
     V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
@@ -67,12 +64,6 @@
     return ScriptController::mainWorldContext(object->rootObject->frame());
 }
 
-static V8NPObjectMap* staticV8NPObjectMap()
-{
-    DEFINE_STATIC_LOCAL(V8NPObjectMap, v8npObjectMap, ());
-    return &v8npObjectMap;
-}
-
 // FIXME: Comments on why use malloc and free.
 static NPObject* allocV8NPObject(NPP, NPClass*)
 {
@@ -82,25 +73,24 @@
 static void freeV8NPObject(NPObject* npObject)
 {
     V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
-    if (int v8ObjectHash = v8NpObject->v8Object->GetIdentityHash()) {
-        V8NPObjectMap::iterator iter = staticV8NPObjectMap()->find(v8ObjectHash);
-        if (iter != staticV8NPObjectMap()->end()) {
-            V8NPObjectVector& objects = iter->second;
-            for (size_t index = 0; index < objects.size(); ++index) {
-                if (objects.at(index) == v8NpObject) {
-                    objects.remove(index);
-                    break;
-                }
+    v8::HandleScope scope;
+    ASSERT(!v8NpObject->v8Object->CreationContext().IsEmpty());
+    if (V8PerContextData* perContextData = V8PerContextData::from(v8NpObject->v8Object->CreationContext())) {
+        V8NPObjectMap* v8NPObjectMap = perContextData->v8NPObjectMap();
+        int v8ObjectHash = v8NpObject->v8Object->GetIdentityHash();
+        ASSERT(v8ObjectHash);
+        V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash);
+        ASSERT(iter != v8NPObjectMap->end());
+        V8NPObjectVector& objects = iter->second;
+        for (size_t index = 0; index < objects.size(); ++index) {
+            if (objects.at(index) == v8NpObject) {
+                objects.remove(index);
+                break;
             }
-            if (objects.isEmpty())
-                staticV8NPObjectMap()->remove(v8ObjectHash);
-        } else
-            ASSERT_NOT_REACHED();
-    } else {
-        ASSERT(!v8::Context::InContext());
-        staticV8NPObjectMap()->clear();
+        }
+        if (objects.isEmpty())
+            v8NPObjectMap->remove(v8ObjectHash);
     }
-
     v8NpObject->v8Object.Dispose();
     free(v8NpObject);
 }
@@ -155,8 +145,9 @@
 
     int v8ObjectHash = object->GetIdentityHash();
     ASSERT(v8ObjectHash);
-    V8NPObjectMap::iterator iter = staticV8NPObjectMap()->find(v8ObjectHash);
-    if (iter != staticV8NPObjectMap()->end()) {
+    V8NPObjectMap* v8NPObjectMap = V8PerContextData::from(object->CreationContext())->v8NPObjectMap();
+    V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash);
+    if (iter != v8NPObjectMap->end()) {
         V8NPObjectVector& objects = iter->second;
         for (size_t index = 0; index < objects.size(); ++index) {
             V8NPObject* v8npObject = objects.at(index);
@@ -167,7 +158,7 @@
             }
         }
     } else {
-        iter = staticV8NPObjectMap()->set(v8ObjectHash, V8NPObjectVector()).iterator;
+        iter = v8NPObjectMap->set(v8ObjectHash, V8NPObjectVector()).iterator;
     }
 
     V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass));

Modified: trunk/Source/WebCore/bindings/v8/V8PerContextData.h (129932 => 129933)


--- trunk/Source/WebCore/bindings/v8/V8PerContextData.h	2012-09-28 19:37:51 UTC (rev 129932)
+++ trunk/Source/WebCore/bindings/v8/V8PerContextData.h	2012-09-28 19:38:42 UTC (rev 129933)
@@ -36,9 +36,14 @@
 #include <v8.h>
 #include <wtf/HashMap.h>
 #include <wtf/PassOwnPtr.h>
+#include <wtf/Vector.h>
 
 namespace WebCore {
 
+struct V8NPObject;
+typedef WTF::Vector<V8NPObject*> V8NPObjectVector;
+typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap;
+
 class V8PerContextData {
 public:
     static PassOwnPtr<V8PerContextData> create(v8::Persistent<v8::Context> context)
@@ -72,6 +77,11 @@
         return constructorForTypeSlowCase(type);
     }
 
+    V8NPObjectMap* v8NPObjectMap()
+    {
+        return &m_v8NPObjectMap;
+    }
+
 private:
     explicit V8PerContextData(v8::Persistent<v8::Context> context)
         : m_context(context)
@@ -91,6 +101,8 @@
     typedef WTF::HashMap<WrapperTypeInfo*, v8::Persistent<v8::Function> > ConstructorMap;
     ConstructorMap m_constructorMap;
 
+    V8NPObjectMap m_v8NPObjectMap;
+
     v8::Persistent<v8::Context> m_context;
     ScopedPersistent<v8::Value> m_errorPrototype;
     ScopedPersistent<v8::Value> m_objectPrototype;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to