| 1 | /* |
| 2 | * Copyright (C) 2018 Igalia S.L. |
| 3 | * Copyright (C) 2013-2018 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "JSAPIWrapperObject.h" |
| 29 | |
| 30 | #include "JSCGLibWrapperObject.h" |
| 31 | #include "JSCInlines.h" |
| 32 | #include "JSCallbackObject.h" |
| 33 | #include "Structure.h" |
| 34 | #include <wtf/NeverDestroyed.h> |
| 35 | |
| 36 | class JSAPIWrapperObjectHandleOwner : public JSC::WeakHandleOwner { |
| 37 | public: |
| 38 | void finalize(JSC::Handle<JSC::Unknown>, void*) override; |
| 39 | bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&, const char**) override; |
| 40 | }; |
| 41 | |
| 42 | static JSAPIWrapperObjectHandleOwner* jsAPIWrapperObjectHandleOwner() |
| 43 | { |
| 44 | static NeverDestroyed<JSAPIWrapperObjectHandleOwner> jsWrapperObjectHandleOwner; |
| 45 | return &jsWrapperObjectHandleOwner.get(); |
| 46 | } |
| 47 | |
| 48 | void JSAPIWrapperObjectHandleOwner::finalize(JSC::Handle<JSC::Unknown> handle, void*) |
| 49 | { |
| 50 | auto* wrapperObject = static_cast<JSC::JSAPIWrapperObject*>(handle.get().asCell()); |
| 51 | if (!wrapperObject->wrappedObject()) |
| 52 | return; |
| 53 | |
| 54 | JSC::Heap::heap(wrapperObject)->releaseSoon(std::unique_ptr<JSC::JSCGLibWrapperObject>(static_cast<JSC::JSCGLibWrapperObject*>(wrapperObject->wrappedObject()))); |
| 55 | JSC::WeakSet::deallocate(JSC::WeakImpl::asWeakImpl(handle.slot())); |
| 56 | } |
| 57 | |
| 58 | bool JSAPIWrapperObjectHandleOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, JSC::SlotVisitor& visitor, const char**) |
| 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 wrapped object in multiple global objects keeps all of the global objects alive. |
| 63 | if (!wrapperObject->wrappedObject()) |
| 64 | return false; |
| 65 | return visitor.vm().heap.isMarked(wrapperObject->structure()->globalObject()) && visitor.containsOpaqueRoot(wrapperObject->wrappedObject()); |
| 66 | } |
| 67 | |
| 68 | namespace JSC { |
| 69 | |
| 70 | template <> const ClassInfo JSCallbackObject<JSAPIWrapperObject>::s_info = { "JSAPIWrapperObject" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCallbackObject) }; |
| 71 | |
| 72 | template<> const bool JSCallbackObject<JSAPIWrapperObject>::needsDestruction = true; |
| 73 | |
| 74 | template <> |
| 75 | Structure* JSCallbackObject<JSAPIWrapperObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) |
| 76 | { |
| 77 | return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info); |
| 78 | } |
| 79 | |
| 80 | JSAPIWrapperObject::JSAPIWrapperObject(VM& vm, Structure* structure) |
| 81 | : Base(vm, structure) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void JSAPIWrapperObject::finishCreation(VM& vm) |
| 86 | { |
| 87 | Base::finishCreation(vm); |
| 88 | WeakSet::allocate(this, jsAPIWrapperObjectHandleOwner(), 0); // Balanced in JSAPIWrapperObjectHandleOwner::finalize. |
| 89 | } |
| 90 | |
| 91 | void JSAPIWrapperObject::setWrappedObject(void* wrappedObject) |
| 92 | { |
| 93 | ASSERT(!m_wrappedObject); |
| 94 | m_wrappedObject = wrappedObject; |
| 95 | } |
| 96 | |
| 97 | void JSAPIWrapperObject::visitChildren(JSCell* cell, JSC::SlotVisitor& visitor) |
| 98 | { |
| 99 | Base::visitChildren(cell, visitor); |
| 100 | } |
| 101 | |
| 102 | } // namespace JSC |
| 103 | |