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 | |
28 | namespace WebCore { |
29 | |
30 | class 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 | |
39 | static const uint8_t JSNodeTypeMask = 0b00001111; |
40 | |
41 | static const uint8_t JSDOMWrapperType = 0b10000000; |
42 | static const uint8_t JSEventType = 0b10000001; |
43 | static const uint8_t JSNodeType = 0b11000000; |
44 | static const uint8_t JSTextNodeType = JSNodeType | NodeConstants::TEXT_NODE; |
45 | static const uint8_t JSProcessingInstructionNodeType = JSNodeType | NodeConstants::PROCESSING_INSTRUCTION_NODE; |
46 | static const uint8_t JSDocumentTypeNodeType = JSNodeType | NodeConstants::DOCUMENT_TYPE_NODE; |
47 | static const uint8_t JSDocumentFragmentNodeType = JSNodeType | NodeConstants::DOCUMENT_FRAGMENT_NODE; |
48 | static const uint8_t JSDocumentWrapperType = JSNodeType | NodeConstants::DOCUMENT_NODE; |
49 | static const uint8_t = JSNodeType | NodeConstants::COMMENT_NODE; |
50 | static const uint8_t JSCDATASectionNodeType = JSNodeType | NodeConstants::CDATA_SECTION_NODE; |
51 | static const uint8_t JSAttrNodeType = JSNodeType | NodeConstants::ATTRIBUTE_NODE; |
52 | static const uint8_t JSElementType = 0b11010000 | NodeConstants::ELEMENT_NODE; |
53 | |
54 | static_assert(JSDOMWrapperType > JSC::LastJSCObjectType, "JSC::JSType offers the highest bit." ); |
55 | static_assert(NodeConstants::LastNodeType <= JSNodeTypeMask, "NodeType should be represented in 4bit." ); |
56 | |
57 | class JSDOMObject : public JSC::JSDestructibleObject { |
58 | public: |
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 | |
65 | protected: |
66 | WEBCORE_EXPORT JSDOMObject(JSC::Structure*, JSC::JSGlobalObject&); |
67 | }; |
68 | |
69 | WEBCORE_EXPORT JSC::CompleteSubspace* outputConstraintSubspaceFor(JSC::VM&); |
70 | WEBCORE_EXPORT JSC::CompleteSubspace* globalObjectOutputConstraintSubspaceFor(JSC::VM&); |
71 | |
72 | template<typename ImplementationClass> class JSDOMWrapper : public JSDOMObject { |
73 | public: |
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 | |
81 | protected: |
82 | JSDOMWrapper(JSC::Structure* structure, JSC::JSGlobalObject& globalObject, Ref<ImplementationClass>&& impl) |
83 | : Base(structure, globalObject) |
84 | , m_wrapped(WTFMove(impl)) { } |
85 | |
86 | private: |
87 | Ref<ImplementationClass> m_wrapped; |
88 | }; |
89 | |
90 | template<typename ImplementationClass> struct JSDOMWrapperConverterTraits; |
91 | |
92 | template<typename JSClass, typename Enable = void> |
93 | struct JSDOMObjectInspector { |
94 | public: |
95 | static constexpr bool isSimpleWrapper = false; |
96 | static constexpr bool isComplexWrapper = false; |
97 | static constexpr bool isBuiltin = true; |
98 | }; |
99 | |
100 | template<typename JSClass> |
101 | struct JSDOMObjectInspector<JSClass, typename std::enable_if<JSClass::isDOMWrapper>::type> { |
102 | private: |
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 | |
106 | public: |
107 | static constexpr bool isSimpleWrapper = test<typename JSClass::DOMWrapped>(0); |
108 | static constexpr bool isComplexWrapper = !isSimpleWrapper; |
109 | static constexpr bool isBuiltin = false; |
110 | }; |
111 | |
112 | JSC::JSValue cloneAcrossWorlds(JSC::ExecState&, const JSDOMObject& owner, JSC::JSValue); |
113 | |
114 | } // namespace WebCore |
115 | |