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