]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSAPIWrapperObject.mm
JavaScriptCore-7600.1.4.9.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
A
29#include "DelayedReleaseScope.h"
30#include "JSCInlines.h"
93a37866 31#include "JSCallbackObject.h"
93a37866 32#include "JSVirtualMachineInternal.h"
93a37866 33#include "Structure.h"
93a37866
A
34
35#if JSC_OBJC_API_ENABLED
36
37class JSAPIWrapperObjectHandleOwner : public JSC::WeakHandleOwner {
38public:
81345200
A
39 virtual void finalize(JSC::Handle<JSC::Unknown>, void*) override;
40 virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&) override;
93a37866
A
41};
42
43static JSAPIWrapperObjectHandleOwner* jsAPIWrapperObjectHandleOwner()
44{
81345200 45 DEPRECATED_DEFINE_STATIC_LOCAL(JSAPIWrapperObjectHandleOwner, jsWrapperObjectHandleOwner, ());
93a37866
A
46 return &jsWrapperObjectHandleOwner;
47}
48
49void 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;
81345200
A
54
55 JSC::Heap::heap(wrapperObject)->releaseSoon(adoptNS(static_cast<id>(wrapperObject->wrappedObject())));
93a37866
A
56 JSC::WeakSet::deallocate(JSC::WeakImpl::asWeakImpl(handle.slot()));
57}
58
59bool 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
69namespace JSC {
70
71template <> const ClassInfo JSCallbackObject<JSAPIWrapperObject>::s_info = { "JSAPIWrapperObject", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackObject) };
72
73template<> const bool JSCallbackObject<JSAPIWrapperObject>::needsDestruction = true;
74
75template <>
76Structure* JSCallbackObject<JSAPIWrapperObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
77{
78 return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info);
79}
80
81JSAPIWrapperObject::JSAPIWrapperObject(VM& vm, Structure* structure)
82 : Base(vm, structure)
83 , m_wrappedObject(0)
84{
85}
86
87void JSAPIWrapperObject::finishCreation(VM& vm)
88{
89 Base::finishCreation(vm);
90 WeakSet::allocate(this, jsAPIWrapperObjectHandleOwner(), 0); // Balanced in JSAPIWrapperObjectHandleOwner::finalize.
91}
92
93void JSAPIWrapperObject::setWrappedObject(void* wrappedObject)
94{
95 ASSERT(!m_wrappedObject);
96 m_wrappedObject = [static_cast<id>(wrappedObject) retain];
97}
98
99void 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