1/*
2 * Copyright (C) 2007-2019 Apple Inc. All rights reserved.
3 * Copyright (C) 2018 Yusuke Suzuki <utatane.tea@gmail.com>.
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. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "JSDOMBinding.h"
30#include "JSNode.h"
31
32namespace JSC {
33namespace JSCastingHelpers {
34
35template<>
36struct InheritsTraits<WebCore::JSNode> {
37 template<typename From>
38 static inline bool inherits(VM&, From* from)
39 {
40 return from->type() >= WebCore::JSNodeType;
41 }
42};
43
44} // namespace JSCastingHelpers
45} // namespace JSC
46
47namespace WebCore {
48
49WEBCORE_EXPORT JSC::JSValue createWrapper(JSC::ExecState*, JSDOMGlobalObject*, Ref<Node>&&);
50WEBCORE_EXPORT JSC::JSObject* getOutOfLineCachedWrapper(JSDOMGlobalObject*, Node&);
51
52inline JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, Node& node)
53{
54 if (LIKELY(globalObject->worldIsNormal())) {
55 if (auto* wrapper = node.wrapper())
56 return wrapper;
57 } else {
58 if (auto* wrapper = getOutOfLineCachedWrapper(globalObject, node))
59 return wrapper;
60 }
61
62 return createWrapper(exec, globalObject, node);
63}
64
65// In the C++ DOM, a node tree survives as long as there is a reference to its
66// root. In the JavaScript DOM, a node tree survives as long as there is a
67// reference to any node in the tree. To model the JavaScript DOM on top of
68// the C++ DOM, we ensure that the root of every tree has a JavaScript wrapper.
69void willCreatePossiblyOrphanedTreeByRemovalSlowCase(Node* root);
70inline void willCreatePossiblyOrphanedTreeByRemoval(Node* root)
71{
72 if (root->wrapper())
73 return;
74
75 if (!root->hasChildNodes())
76 return;
77
78 willCreatePossiblyOrphanedTreeByRemovalSlowCase(root);
79}
80
81inline void* root(Node* node)
82{
83 return node ? node->opaqueRoot() : nullptr;
84}
85
86inline void* root(Node& node)
87{
88 return root(&node);
89}
90
91ALWAYS_INLINE JSC::JSValue JSNode::nodeType(JSC::ExecState&) const
92{
93 return JSC::jsNumber(static_cast<uint8_t>(type()) & JSNodeTypeMask);
94}
95
96} // namespace WebCore
97