]> git.saurik.com Git - apple/javascriptcore.git/blob - API/JSAPIWrapperObject.mm
JavaScriptCore-7600.1.4.15.12.tar.gz
[apple/javascriptcore.git] / API / JSAPIWrapperObject.mm
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "JSAPIWrapperObject.h"
28
29 #include "DelayedReleaseScope.h"
30 #include "JSCInlines.h"
31 #include "JSCallbackObject.h"
32 #include "JSVirtualMachineInternal.h"
33 #include "Structure.h"
34
35 #if JSC_OBJC_API_ENABLED
36
37 class JSAPIWrapperObjectHandleOwner : public JSC::WeakHandleOwner {
38 public:
39 virtual void finalize(JSC::Handle<JSC::Unknown>, void*) override;
40 virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&) override;
41 };
42
43 static JSAPIWrapperObjectHandleOwner* jsAPIWrapperObjectHandleOwner()
44 {
45 DEPRECATED_DEFINE_STATIC_LOCAL(JSAPIWrapperObjectHandleOwner, jsWrapperObjectHandleOwner, ());
46 return &jsWrapperObjectHandleOwner;
47 }
48
49 void JSAPIWrapperObjectHandleOwner::finalize(JSC::Handle<JSC::Unknown> handle, void*)
50 {
51 JSC::JSAPIWrapperObject* wrapperObject = JSC::jsCast<JSC::JSAPIWrapperObject*>(handle.get().asCell());
52 if (!wrapperObject->wrappedObject())
53 return;
54
55 JSC::Heap::heap(wrapperObject)->releaseSoon(adoptNS(static_cast<id>(wrapperObject->wrappedObject())));
56 JSC::WeakSet::deallocate(JSC::WeakImpl::asWeakImpl(handle.slot()));
57 }
58
59 bool JSAPIWrapperObjectHandleOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, JSC::SlotVisitor& visitor)
60 {
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())
65 return false;
66 return JSC::Heap::isMarked(wrapperObject->structure()->globalObject()) && visitor.containsOpaqueRoot(wrapperObject->wrappedObject());
67 }
68
69 namespace JSC {
70
71 template <> const ClassInfo JSCallbackObject<JSAPIWrapperObject>::s_info = { "JSAPIWrapperObject", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackObject) };
72
73 template<> const bool JSCallbackObject<JSAPIWrapperObject>::needsDestruction = true;
74
75 template <>
76 Structure* JSCallbackObject<JSAPIWrapperObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
77 {
78 return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info);
79 }
80
81 JSAPIWrapperObject::JSAPIWrapperObject(VM& vm, Structure* structure)
82 : Base(vm, structure)
83 , m_wrappedObject(0)
84 {
85 }
86
87 void JSAPIWrapperObject::finishCreation(VM& vm)
88 {
89 Base::finishCreation(vm);
90 WeakSet::allocate(this, jsAPIWrapperObjectHandleOwner(), 0); // Balanced in JSAPIWrapperObjectHandleOwner::finalize.
91 }
92
93 void JSAPIWrapperObject::setWrappedObject(void* wrappedObject)
94 {
95 ASSERT(!m_wrappedObject);
96 m_wrappedObject = [static_cast<id>(wrappedObject) retain];
97 }
98
99 void JSAPIWrapperObject::visitChildren(JSCell* cell, JSC::SlotVisitor& visitor)
100 {
101 JSAPIWrapperObject* thisObject = JSC::jsCast<JSAPIWrapperObject*>(cell);
102 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
103 Base::visitChildren(cell, visitor);
104
105 if (thisObject->wrappedObject())
106 scanExternalObjectGraph(cell->structure()->globalObject()->vm(), visitor, thisObject->wrappedObject());
107 }
108
109 } // namespace JSC
110
111 #endif // JSC_OBJC_API_ENABLED