nativewrapper-finalizers
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 110 d03021664afc7183bb3f54432dedaffc441e77ec
permissions -rw-r--r--
State as of now

diff --git a/js/src/xpconnect/src/XPCNativeWrapper.cpp b/js/src/xpconnect/src/XPCNativeWrapper.cpp
--- a/js/src/xpconnect/src/XPCNativeWrapper.cpp
+++ b/js/src/xpconnect/src/XPCNativeWrapper.cpp
@@ -66,9 +66,6 @@ JS_STATIC_DLL_CALLBACK(JSBool)
 JS_STATIC_DLL_CALLBACK(JSBool)
 XPC_NW_Convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
 
-JS_STATIC_DLL_CALLBACK(void)
-XPC_NW_Finalize(JSContext *cx, JSObject *obj);
-
 JS_STATIC_DLL_CALLBACK(JSBool)
 XPC_NW_CheckAccess(JSContext *cx, JSObject *obj, jsval id,
                    JSAccessMode mode, jsval *vp);
@@ -107,7 +104,7 @@ JSExtendedClass XPCNativeWrapper::sXPC_N
     XPC_NW_AddProperty, XPC_NW_DelProperty,
     XPC_NW_GetProperty, XPC_NW_SetProperty,
     XPC_NW_Enumerate,   (JSResolveOp)XPC_NW_NewResolve,
-    XPC_NW_Convert,     XPC_NW_Finalize,
+    XPC_NW_Convert,     JS_FinalizeStub,
     nsnull,             XPC_NW_CheckAccess,
     XPC_NW_Call,        XPC_NW_Construct,
     nsnull,             XPC_NW_HasInstance,
@@ -652,20 +649,6 @@ XPC_NW_Convert(JSContext *cx, JSObject *
   return JS_TRUE;
 }
 
-JS_STATIC_DLL_CALLBACK(void)
-XPC_NW_Finalize(JSContext *cx, JSObject *obj)
-{
-  // We must not use obj's private data here since it's likely that it
-  // has already been finalized.
-  XPCJSRuntime *rt = nsXPConnect::GetRuntime();
-
-  {
-    // scoped lock
-    XPCAutoLock lock(rt->GetMapLock());
-    rt->GetExplicitNativeWrapperMap()->Remove(obj);
-  }
-}
-
 JS_STATIC_DLL_CALLBACK(JSBool)
 XPC_NW_CheckAccess(JSContext *cx, JSObject *obj, jsval id,
                    JSAccessMode mode, jsval *vp)
diff --git a/js/src/xpconnect/src/xpcjsruntime.cpp b/js/src/xpconnect/src/xpcjsruntime.cpp
--- a/js/src/xpconnect/src/xpcjsruntime.cpp
+++ b/js/src/xpconnect/src/xpcjsruntime.cpp
@@ -187,8 +187,9 @@ JSBool XPCJSRuntime::GCCallback(JSContex
                 XPCWrappedNativeScope::FinishedMarkPhaseOfGC(cx, self);
 
                 self->mNativeScriptableSharedMap->UnmapDying();
-
                 self->mClassInfo2NativeSetMap->UnmapDyingSets();
+                self->mExplicitNativeWrapperMap->UnmapDying();
+                self->mWrappedJSClassMap->UnmapDying();
 
                 self->mDoingFinalization = JS_TRUE;
                 break;
diff --git a/js/src/xpconnect/src/xpcmaps.cpp b/js/src/xpconnect/src/xpcmaps.cpp
--- a/js/src/xpconnect/src/xpcmaps.cpp
+++ b/js/src/xpconnect/src/xpcmaps.cpp
@@ -255,6 +255,19 @@ IID2WrappedJSClassMap::~IID2WrappedJSCla
         JS_DHashTableDestroy(mTable);
 }
 
+// static
+JSDHashOperator
+IID2WrappedJSClassMap::DyingUnmapper(JSDHashTable *table,
+                                     JSDHashEntryHdr *hdr,
+                                     uint32 number,
+                                     void *arg)
+{
+    Entry *entry = static_cast<Entry*>(hdr);
+    if (!NS_GetGC()->GetMark(entry->value))
+        return JS_DHASH_REMOVE;
+
+    return JS_DHASH_NEXT;
+}
 
 /***************************************************************************/
 // implement IID2NativeInterfaceMap...
@@ -730,6 +743,21 @@ XPCNativeWrapperMap::~XPCNativeWrapperMa
         JS_DHashTableDestroy(mTable);
 }
 
+//static
+JSDHashOperator
+XPCNativeWrapperMap::DyingUnmapper(JSDHashTable *table,
+                                   JSDHashEntryHdr *hdr,
+                                   uint32 number,
+                                   void *arg)
+{
+    JSDHashEntryStub *entry = (JSDHashEntryStub*) hdr;
+
+    if (!NS_GetGC()->GetMark(entry->key))
+        return JS_DHASH_REMOVE;
+
+    return JS_DHASH_NEXT;
+}
+
 /***************************************************************************/
 // implement WrappedNative2WrapperMap...
 
diff --git a/js/src/xpconnect/src/xpcmaps.h b/js/src/xpconnect/src/xpcmaps.h
--- a/js/src/xpconnect/src/xpcmaps.h
+++ b/js/src/xpconnect/src/xpcmaps.h
@@ -278,10 +278,20 @@ public:
     inline uint32 Enumerate(JSDHashEnumerator f, void *arg)
         {return JS_DHashTableEnumerate(mTable, f, arg);}
 
+
+    inline void UnmapDying()
+    {
+        JS_DHashTableEnumerate(mTable, DyingUnmapper, nsnull);
+    }
+
     ~IID2WrappedJSClassMap();
 private:
-    IID2WrappedJSClassMap();    // no implementation
     IID2WrappedJSClassMap(int size);
+
+    static JSDHashOperator DyingUnmapper(JSDHashTable *table,
+                                         JSDHashEntryHdr *hdr,
+                                         uint32 number,
+                                         void *arg);
 private:
     JSDHashTable *mTable;
 };
@@ -711,10 +721,20 @@ public:
     inline uint32 Enumerate(JSDHashEnumerator f, void *arg)
         {return JS_DHashTableEnumerate(mTable, f, arg);}
 
+    inline void UnmapDying()
+    {
+        JS_DHashTableEnumerate(mTable, DyingUnmapper, nsnull);
+    }
+
     ~XPCNativeWrapperMap();
 private:
-    XPCNativeWrapperMap();    // no implementation
     XPCNativeWrapperMap(int size);
+
+    static JSDHashOperator DyingUnmapper(JSDHashTable *table,
+                                         JSDHashEntryHdr *hdr,
+                                         uint32 number,
+                                         void *arg);
+
 private:
     JSDHashTable *mTable;
 };
diff --git a/js/src/xpconnect/src/xpcwrappedjsclass.cpp b/js/src/xpconnect/src/xpcwrappedjsclass.cpp
--- a/js/src/xpconnect/src/xpcwrappedjsclass.cpp
+++ b/js/src/xpconnect/src/xpcwrappedjsclass.cpp
@@ -213,11 +213,6 @@ nsXPCWrappedJSClass::~nsXPCWrappedJSClas
 {
     if(mDescriptors && mDescriptors != &zero_methods_descriptor)
         delete [] mDescriptors;
-    if(mRuntime)
-    {   // scoped lock
-        XPCAutoLock lock(mRuntime->GetMapLock());
-        mRuntime->GetWrappedJSClassMap()->Remove(this);
-    }
     if(mName)
         nsMemory::Free(mName);
     NS_IF_RELEASE(mInfo);