+- (void)dealloc
+{
+ JSVirtualMachine *virtualMachine = [[[self value] context] virtualMachine];
+ if (virtualMachine) {
+ NSMapTable *copy = [m_owners copy];
+ for (id owner in [copy keyEnumerator]) {
+ size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
+ while (count--)
+ [virtualMachine removeManagedReference:self withOwner:owner];
+ }
+ [copy release];
+ }
+
+ [self disconnectValue];
+ [m_owners release];
+ [super dealloc];
+}
+
+- (void)didAddOwner:(id)owner
+{
+ size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
+ NSMapInsert(m_owners, owner, reinterpret_cast<void*>(count + 1));
+}
+
+- (void)didRemoveOwner:(id)owner
+{
+ size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
+
+ if (!count)
+ return;
+
+ if (count == 1) {
+ NSMapRemove(m_owners, owner);
+ return;
+ }
+
+ NSMapInsert(m_owners, owner, reinterpret_cast<void*>(count - 1));
+}
+