]>
Commit | Line | Data |
---|---|---|
93a37866 A |
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 "JSCJSValueInlines.h" | |
30 | #include "JSCallbackObject.h" | |
31 | #include "JSCellInlines.h" | |
32 | #include "JSVirtualMachineInternal.h" | |
33 | #include "SlotVisitorInlines.h" | |
34 | #include "Structure.h" | |
35 | #include "StructureInlines.h" | |
36 | ||
37 | #if JSC_OBJC_API_ENABLED | |
38 | ||
39 | class JSAPIWrapperObjectHandleOwner : public JSC::WeakHandleOwner { | |
40 | public: | |
41 | virtual void finalize(JSC::Handle<JSC::Unknown>, void*); | |
42 | virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&); | |
43 | }; | |
44 | ||
45 | static JSAPIWrapperObjectHandleOwner* jsAPIWrapperObjectHandleOwner() | |
46 | { | |
47 | DEFINE_STATIC_LOCAL(JSAPIWrapperObjectHandleOwner, jsWrapperObjectHandleOwner, ()); | |
48 | return &jsWrapperObjectHandleOwner; | |
49 | } | |
50 | ||
51 | void JSAPIWrapperObjectHandleOwner::finalize(JSC::Handle<JSC::Unknown> handle, void*) | |
52 | { | |
53 | JSC::JSAPIWrapperObject* wrapperObject = JSC::jsCast<JSC::JSAPIWrapperObject*>(handle.get().asCell()); | |
54 | if (!wrapperObject->wrappedObject()) | |
55 | return; | |
56 | [static_cast<id>(wrapperObject->wrappedObject()) release]; | |
57 | JSC::WeakSet::deallocate(JSC::WeakImpl::asWeakImpl(handle.slot())); | |
58 | } | |
59 | ||
60 | bool JSAPIWrapperObjectHandleOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, JSC::SlotVisitor& visitor) | |
61 | { | |
62 | JSC::JSAPIWrapperObject* wrapperObject = JSC::jsCast<JSC::JSAPIWrapperObject*>(handle.get().asCell()); | |
63 | // We use the JSGlobalObject when processing weak handles to prevent the situation where using | |
64 | // the same Objective-C object in multiple global objects keeps all of the global objects alive. | |
65 | if (!wrapperObject->wrappedObject()) | |
66 | return false; | |
67 | return JSC::Heap::isMarked(wrapperObject->structure()->globalObject()) && visitor.containsOpaqueRoot(wrapperObject->wrappedObject()); | |
68 | } | |
69 | ||
70 | namespace JSC { | |
71 | ||
72 | template <> const ClassInfo JSCallbackObject<JSAPIWrapperObject>::s_info = { "JSAPIWrapperObject", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackObject) }; | |
73 | ||
74 | template<> const bool JSCallbackObject<JSAPIWrapperObject>::needsDestruction = true; | |
75 | ||
76 | template <> | |
77 | Structure* JSCallbackObject<JSAPIWrapperObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) | |
78 | { | |
79 | return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info); | |
80 | } | |
81 | ||
82 | JSAPIWrapperObject::JSAPIWrapperObject(VM& vm, Structure* structure) | |
83 | : Base(vm, structure) | |
84 | , m_wrappedObject(0) | |
85 | { | |
86 | } | |
87 | ||
88 | void JSAPIWrapperObject::finishCreation(VM& vm) | |
89 | { | |
90 | Base::finishCreation(vm); | |
91 | WeakSet::allocate(this, jsAPIWrapperObjectHandleOwner(), 0); // Balanced in JSAPIWrapperObjectHandleOwner::finalize. | |
92 | } | |
93 | ||
94 | void JSAPIWrapperObject::setWrappedObject(void* wrappedObject) | |
95 | { | |
96 | ASSERT(!m_wrappedObject); | |
97 | m_wrappedObject = [static_cast<id>(wrappedObject) retain]; | |
98 | } | |
99 | ||
100 | void JSAPIWrapperObject::visitChildren(JSCell* cell, JSC::SlotVisitor& visitor) | |
101 | { | |
102 | JSAPIWrapperObject* thisObject = JSC::jsCast<JSAPIWrapperObject*>(cell); | |
103 | COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag); | |
104 | Base::visitChildren(cell, visitor); | |
105 | ||
106 | if (thisObject->wrappedObject()) | |
107 | scanExternalObjectGraph(cell->structure()->globalObject()->vm(), visitor, thisObject->wrappedObject()); | |
108 | } | |
109 | ||
110 | } // namespace JSC | |
111 | ||
112 | #endif // JSC_OBJC_API_ENABLED |