1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#pragma once
23
24#include "JSDOMGlobalObject.h"
25#include "NodeConstants.h"
26#include <JavaScriptCore/JSDestructibleObject.h>
27
28namespace WebCore {
29
30class ScriptExecutionContext;
31
32// JSC allows us to extend JSType. If the highest bit is set, we can add any Object types and they are
33// recognized as OtherObj in JSC. And we encode Node type into JSType if the given JSType is subclass of Node.
34// offset | 7 | 6 | 5 4 | 3 2 1 0 |
35// value | 1 | 0 | Non-node DOM types |
36// If the given JSType is a subclass of Node, the format is the following.
37// offset | 7 | 6 | 5 4 | 3 2 1 0 |
38// value | 1 | 1 | Kind | NodeType |
39static const uint8_t JSNodeTypeMask = 0b00001111;
40
41static const uint8_t JSDOMWrapperType = 0b10000000;
42static const uint8_t JSEventType = 0b10000001;
43static const uint8_t JSNodeType = 0b11000000;
44static const uint8_t JSTextNodeType = JSNodeType | NodeConstants::TEXT_NODE;
45static const uint8_t JSProcessingInstructionNodeType = JSNodeType | NodeConstants::PROCESSING_INSTRUCTION_NODE;
46static const uint8_t JSDocumentTypeNodeType = JSNodeType | NodeConstants::DOCUMENT_TYPE_NODE;
47static const uint8_t JSDocumentFragmentNodeType = JSNodeType | NodeConstants::DOCUMENT_FRAGMENT_NODE;
48static const uint8_t JSDocumentWrapperType = JSNodeType | NodeConstants::DOCUMENT_NODE;
49static const uint8_t JSCommentNodeType = JSNodeType | NodeConstants::COMMENT_NODE;
50static const uint8_t JSCDATASectionNodeType = JSNodeType | NodeConstants::CDATA_SECTION_NODE;
51static const uint8_t JSAttrNodeType = JSNodeType | NodeConstants::ATTRIBUTE_NODE;
52static const uint8_t JSElementType = 0b11010000 | NodeConstants::ELEMENT_NODE;
53
54static_assert(JSDOMWrapperType > JSC::LastJSCObjectType, "JSC::JSType offers the highest bit.");
55static_assert(NodeConstants::LastNodeType <= JSNodeTypeMask, "NodeType should be represented in 4bit.");
56
57class JSDOMObject : public JSC::JSDestructibleObject {
58public:
59 typedef JSC::JSDestructibleObject Base;
60 static constexpr bool isDOMWrapper = false;
61
62 JSDOMGlobalObject* globalObject() const { return JSC::jsCast<JSDOMGlobalObject*>(JSC::JSNonFinalObject::globalObject()); }
63 ScriptExecutionContext* scriptExecutionContext() const { return globalObject()->scriptExecutionContext(); }
64
65protected:
66 WEBCORE_EXPORT JSDOMObject(JSC::Structure*, JSC::JSGlobalObject&);
67};
68
69WEBCORE_EXPORT JSC::CompleteSubspace* outputConstraintSubspaceFor(JSC::VM&);
70WEBCORE_EXPORT JSC::CompleteSubspace* globalObjectOutputConstraintSubspaceFor(JSC::VM&);
71
72template<typename ImplementationClass> class JSDOMWrapper : public JSDOMObject {
73public:
74 typedef JSDOMObject Base;
75 typedef ImplementationClass DOMWrapped;
76 static constexpr bool isDOMWrapper = true;
77
78 ImplementationClass& wrapped() const { return m_wrapped; }
79 static ptrdiff_t offsetOfWrapped() { return OBJECT_OFFSETOF(JSDOMWrapper<ImplementationClass>, m_wrapped); }
80
81protected:
82 JSDOMWrapper(JSC::Structure* structure, JSC::JSGlobalObject& globalObject, Ref<ImplementationClass>&& impl)
83 : Base(structure, globalObject)
84 , m_wrapped(WTFMove(impl)) { }
85
86private:
87 Ref<ImplementationClass> m_wrapped;
88};
89
90template<typename ImplementationClass> struct JSDOMWrapperConverterTraits;
91
92template<typename JSClass, typename Enable = void>
93struct JSDOMObjectInspector {
94public:
95 static constexpr bool isSimpleWrapper = false;
96 static constexpr bool isComplexWrapper = false;
97 static constexpr bool isBuiltin = true;
98};
99
100template<typename JSClass>
101struct JSDOMObjectInspector<JSClass, typename std::enable_if<JSClass::isDOMWrapper>::type> {
102private:
103 template<typename T> static constexpr auto test(int) -> decltype(T::create(), bool()) { return true; }
104 template<typename T> static constexpr bool test(...) { return false; }
105
106public:
107 static constexpr bool isSimpleWrapper = test<typename JSClass::DOMWrapped>(0);
108 static constexpr bool isComplexWrapper = !isSimpleWrapper;
109 static constexpr bool isBuiltin = false;
110};
111
112JSC::JSValue cloneAcrossWorlds(JSC::ExecState&, const JSDOMObject& owner, JSC::JSValue);
113
114} // namespace WebCore
115