2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "JSAPIWrapperObject.h"
29 #include "DelayedReleaseScope.h"
30 #include "JSCInlines.h"
31 #include "JSCallbackObject.h"
32 #include "JSVirtualMachineInternal.h"
33 #include "Structure.h"
35 #if JSC_OBJC_API_ENABLED
37 class JSAPIWrapperObjectHandleOwner : public JSC::WeakHandleOwner {
39 virtual void finalize(JSC::Handle<JSC::Unknown>, void*) override;
40 virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&) override;
43 static JSAPIWrapperObjectHandleOwner* jsAPIWrapperObjectHandleOwner()
45 DEPRECATED_DEFINE_STATIC_LOCAL(JSAPIWrapperObjectHandleOwner, jsWrapperObjectHandleOwner, ());
46 return &jsWrapperObjectHandleOwner;
49 void JSAPIWrapperObjectHandleOwner::finalize(JSC::Handle<JSC::Unknown> handle, void*)
51 JSC::JSAPIWrapperObject* wrapperObject = JSC::jsCast<JSC::JSAPIWrapperObject*>(handle.get().asCell());
52 if (!wrapperObject->wrappedObject())
55 JSC::Heap::heap(wrapperObject)->releaseSoon(adoptNS(static_cast<id>(wrapperObject->wrappedObject())));
56 JSC::WeakSet::deallocate(JSC::WeakImpl::asWeakImpl(handle.slot()));
59 bool JSAPIWrapperObjectHandleOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, JSC::SlotVisitor& visitor)
61 JSC::JSAPIWrapperObject* wrapperObject = JSC::jsCast<JSC::JSAPIWrapperObject*>(handle.get().asCell());
62 // We use the JSGlobalObject when processing weak handles to prevent the situation where using
63 // the same Objective-C object in multiple global objects keeps all of the global objects alive.
64 if (!wrapperObject->wrappedObject())
66 return JSC::Heap::isMarked(wrapperObject->structure()->globalObject()) && visitor.containsOpaqueRoot(wrapperObject->wrappedObject());
71 template <> const ClassInfo JSCallbackObject<JSAPIWrapperObject>::s_info = { "JSAPIWrapperObject", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackObject) };
73 template<> const bool JSCallbackObject<JSAPIWrapperObject>::needsDestruction = true;
76 Structure* JSCallbackObject<JSAPIWrapperObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
78 return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info);
81 JSAPIWrapperObject::JSAPIWrapperObject(VM& vm, Structure* structure)
87 void JSAPIWrapperObject::finishCreation(VM& vm)
89 Base::finishCreation(vm);
90 WeakSet::allocate(this, jsAPIWrapperObjectHandleOwner(), 0); // Balanced in JSAPIWrapperObjectHandleOwner::finalize.
93 void JSAPIWrapperObject::setWrappedObject(void* wrappedObject)
95 ASSERT(!m_wrappedObject);
96 m_wrappedObject = [static_cast<id>(wrappedObject) retain];
99 void JSAPIWrapperObject::visitChildren(JSCell* cell, JSC::SlotVisitor& visitor)
101 JSAPIWrapperObject* thisObject = JSC::jsCast<JSAPIWrapperObject*>(cell);
102 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
103 Base::visitChildren(cell, visitor);
105 if (thisObject->wrappedObject())
106 scanExternalObjectGraph(cell->structure()->globalObject()->vm(), visitor, thisObject->wrappedObject());
111 #endif // JSC_OBJC_API_ENABLED