1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* Copyright (C) 2012 University of Washington. All rights reserved.
* Copyright (C) 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MemoizedDOMResult_h
#define MemoizedDOMResult_h
#if ENABLE(WEB_REPLAY)
#include <replay/EncodedValue.h>
#include <replay/NondeterministicInput.h>
#include <wtf/TypeCasts.h>
namespace WebCore {
class SerializedScriptValue;
typedef int ExceptionCode;
// Add new memoized ctypes here. The first argument is the enum value,
// which cannot conflict with built-in primitive types. The second is
// the actual C type that is used to specialize CTypeTraits. New enum
// values should also be added to the definition in WebInputs.json.
#define FOR_EACH_MEMOIZED_CTYPE(macro) \
macro(Boolean, bool) \
macro(Int, int) \
macro(String, String) \
macro(Unsigned, unsigned) \
\
// end of FOR_EACH_MEMOIZED_CTYPE
// We encode this enum so that we can recover MemoizedType when decoding the input
// and then call the correct specialized MemoizedDOMResult<T> constructor.
enum class EncodedCType {
#define CREATE_ENUM_VALUE(name, type) name,
FOR_EACH_MEMOIZED_CTYPE(CREATE_ENUM_VALUE)
#undef CREATE_ENUM_VALUE
};
class MemoizedDOMResultBase : public NondeterministicInputBase {
public:
MemoizedDOMResultBase(const String& attribute, EncodedCType ctype, ExceptionCode exceptionCode = 0)
: m_attribute(attribute)
, m_ctype(ctype)
, m_exceptionCode(exceptionCode) { }
virtual ~MemoizedDOMResultBase() { }
static std::unique_ptr<MemoizedDOMResultBase> createFromEncodedResult(const String& attribute, EncodedCType, EncodedValue, ExceptionCode);
template<typename T>
bool convertTo(T& decodedValue);
virtual EncodedValue encodedResult() const = 0;
virtual InputQueue queue() const final override { return InputQueue::ScriptMemoizedData; }
virtual const String& type() const final override;
const String& attribute() const { return m_attribute; }
EncodedCType ctype() const { return m_ctype; }
ExceptionCode exceptionCode() const { return m_exceptionCode; }
private:
String m_attribute;
EncodedCType m_ctype;
ExceptionCode m_exceptionCode;
};
template<typename T>
struct CTypeTraits {
static bool decode(EncodedValue& encodedValue, T& decodedValue)
{
return EncodingTraits<T>::decodeValue(encodedValue, decodedValue);
}
};
#define CREATE_CTYPE_TRAITS(_name, _type) \
template<> \
struct CTypeTraits<_type> { \
typedef _type CType; \
static const EncodedCType encodedType = EncodedCType::_name; \
}; \
FOR_EACH_MEMOIZED_CTYPE(CREATE_CTYPE_TRAITS)
#undef CREATE_CTYPE_TRAITS
template<typename MemoizedType>
class MemoizedDOMResult final : public MemoizedDOMResultBase {
public:
MemoizedDOMResult(const String& attribute, typename CTypeTraits<MemoizedType>::CType result, ExceptionCode exceptionCode)
: MemoizedDOMResultBase(attribute, CTypeTraits<MemoizedType>::encodedType, exceptionCode)
, m_result(result) { }
virtual ~MemoizedDOMResult() { }
virtual EncodedValue encodedResult() const override
{
return EncodingTraits<MemoizedType>::encodeValue(m_result);
}
typename CTypeTraits<MemoizedType>::CType result() const { return m_result; }
private:
typename CTypeTraits<MemoizedType>::CType m_result;
};
// This is used by clients of the memoized DOM result to get out the memoized
// value without performing a cast to MemoizedDOMResult<T> and calling result().
template<typename T>
bool MemoizedDOMResultBase::convertTo(T& convertedValue)
{
// Type tag doesn't match; fail to decode the value.
if (m_ctype != CTypeTraits<T>::encodedType)
return false;
MemoizedDOMResult<T>& castedResult = static_cast<MemoizedDOMResult<T>&>(*this);
convertedValue = castedResult.result();
return true;
}
} // namespace WebCore
using WebCore::MemoizedDOMResultBase;
namespace JSC {
template<>
struct InputTraits<MemoizedDOMResultBase> {
static InputQueue queue() { return InputQueue::ScriptMemoizedData; }
static const String& type();
static void encode(EncodedValue&, const MemoizedDOMResultBase& input);
static bool decode(EncodedValue&, std::unique_ptr<MemoizedDOMResultBase>& input);
};
} // namespace JSC
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MemoizedDOMResultBase)
static bool isType(const NondeterministicInputBase& input) { return input.type() == InputTraits<WebCore::MemoizedDOMResultBase>::type(); }
SPECIALIZE_TYPE_TRAITS_END()
#endif // ENABLE(WEB_REPLAY)
#endif // MemoizedDOMResult_h
|