]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - replay/EncodedValue.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / replay / EncodedValue.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013 University of Washington. All rights reserved.
3 * Copyright (C) 2014 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 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef EncodedValue_h
29#define EncodedValue_h
30
31#if ENABLE(WEB_REPLAY)
32
33#include "InspectorValues.h"
34#include <wtf/Forward.h>
35#include <wtf/Vector.h>
36
37namespace JSC {
38
39class NondeterministicInputBase;
40template<typename T> struct EncodingTraits;
41
42class EncodedValue final {
43public:
44 explicit EncodedValue(RefPtr<Inspector::InspectorValue>&& value)
45 : m_value(value) { }
46
47 EncodedValue()
48 : m_value(nullptr) { }
49
50 static EncodedValue createObject()
51 {
52 return EncodedValue(Inspector::InspectorObject::create());
53 }
54
55 static EncodedValue createArray()
56 {
57 return EncodedValue(Inspector::InspectorArray::create());
58 }
59
60 static EncodedValue createString(const String& value)
61 {
62 return EncodedValue(Inspector::InspectorString::create(value));
63 }
64
65 static EncodedValue createString(const char* value)
66 {
67 return EncodedValue(Inspector::InspectorString::create(value));
68 }
69
70 template<typename T>
71 void put(const String&, const typename EncodingTraits<T>::DecodedType&);
72
73 template<typename T>
74 void append(const typename EncodingTraits<T>::DecodedType&);
75
76 template<typename T> bool get(const String&, typename EncodingTraits<T>::DecodedType&);
77 template<typename T> bool get(const String&, std::unique_ptr<typename EncodingTraits<T>::DecodedType>&);
78
79 template<typename T> T convertTo();
80
81 JS_EXPORT_PRIVATE RefPtr<Inspector::InspectorObject> asObject();
82 JS_EXPORT_PRIVATE RefPtr<Inspector::InspectorArray> asArray();
83
84private:
85 RefPtr<Inspector::InspectorValue> m_value;
86};
87
88template<> JS_EXPORT_PRIVATE bool EncodedValue::convertTo<bool>();
89template<> JS_EXPORT_PRIVATE double EncodedValue::convertTo<double>();
90template<> JS_EXPORT_PRIVATE float EncodedValue::convertTo<float>();
91template<> JS_EXPORT_PRIVATE int32_t EncodedValue::convertTo<int32_t>();
92template<> JS_EXPORT_PRIVATE int64_t EncodedValue::convertTo<int64_t>();
93template<> JS_EXPORT_PRIVATE uint32_t EncodedValue::convertTo<uint32_t>();
94template<> JS_EXPORT_PRIVATE uint64_t EncodedValue::convertTo<uint64_t>();
95template<> JS_EXPORT_PRIVATE String EncodedValue::convertTo<String>();
96
97template<typename T>
98struct EncodingTraits {
99 typedef T DecodedType;
100
101 static EncodedValue encodeValue(const DecodedType&);
102
103 static bool decodeValue(EncodedValue&, DecodedType&);
104 static bool decodeValue(EncodedValue&, std::unique_ptr<DecodedType>&);
105};
106
107template<typename T, size_t inlineCapacity, typename OverflowHandler>
108struct EncodingTraits<Vector<T, inlineCapacity, OverflowHandler>> {
109 typedef Vector<typename EncodingTraits<T>::DecodedType, inlineCapacity, OverflowHandler> DecodedType;
110
111 static EncodedValue encodeValue(const DecodedType& vectorOfValues)
112 {
113 EncodedValue encodedVector = EncodedValue::createArray();
114 for (const typename EncodingTraits<T>::DecodedType& value : vectorOfValues)
115 encodedVector.append<T>(value);
116
117 return WTF::move(encodedVector);
118 }
119
120 static bool decodeValue(EncodedValue& encodedVector, DecodedType& decodedValue)
121 {
122 RefPtr<Inspector::InspectorArray> inspectorArray = encodedVector.asArray();
123 decodedValue = Vector<typename EncodingTraits<T>::DecodedType, inlineCapacity, OverflowHandler>(inspectorArray->length());
124 for (size_t i = 0; i < inspectorArray->length(); ++i) {
125 EncodedValue encodedElement(inspectorArray->get(i));
126 if (!EncodingTraits<T>::decodeValue(encodedElement, decodedValue.at(i)))
127 return false;
128 }
129 return true;
130 }
131};
132
133template<> struct EncodingTraits<EncodedValue> {
134 typedef EncodedValue DecodedType;
135 // We should never attempt to decode or encode an encoded value,
136 // so encodeValue and decodeValue are intentionally omitted here.
137};
138
139// Specialize byte vectors to use base64 encoding.
140template<> struct EncodingTraits<Vector<char>> {
141 typedef Vector<char> DecodedType;
142 static EncodedValue encodeValue(const DecodedType&);
143 static bool decodeValue(EncodedValue&, DecodedType&);
144};
145
146template<typename T>
147struct ScalarEncodingTraits {
148 typedef T DecodedType;
149
150 static JS_EXPORT_PRIVATE EncodedValue encodeValue(const DecodedType& decodedValue);
151 static bool decodeValue(EncodedValue& encodedValue, DecodedType& decodedValue)
152 {
153 decodedValue = encodedValue.convertTo<DecodedType>();
154 return true;
155 }
156};
157
158template<> struct EncodingTraits<bool> : public ScalarEncodingTraits<bool> { };
159template<> struct EncodingTraits<double> : public ScalarEncodingTraits<double> { };
160template<> struct EncodingTraits<float> : public ScalarEncodingTraits<float> { };
161template<> struct EncodingTraits<int32_t> : public ScalarEncodingTraits<int32_t> { };
162template<> struct EncodingTraits<int64_t> : public ScalarEncodingTraits<int64_t> { };
163template<> struct EncodingTraits<uint32_t> : public ScalarEncodingTraits<uint32_t> { };
164template<> struct EncodingTraits<uint64_t> : public ScalarEncodingTraits<uint64_t> { };
165
166template<> struct EncodingTraits<String> : public ScalarEncodingTraits<String> {
167 static EncodedValue encodeValue(const String& value)
168 {
169 return EncodedValue::createString(value);
170 }
171};
172
173// Base cases for loading and storing values.
174template<> JS_EXPORT_PRIVATE
175void EncodedValue::put<EncodedValue>(const String& key, const typename EncodingTraits<EncodedValue>::DecodedType&);
176
177template<> JS_EXPORT_PRIVATE
178void EncodedValue::append<EncodedValue>(const typename EncodingTraits<EncodedValue>::DecodedType&);
179
180template<> JS_EXPORT_PRIVATE
181bool EncodedValue::get<EncodedValue>(const String& key, typename EncodingTraits<EncodedValue>::DecodedType&);
182
183// Load and store types with an accompanying EncodingTraits implementation.
184template<typename T>
185void EncodedValue::put(const String& key, const typename EncodingTraits<T>::DecodedType& value)
186{
187 EncodedValue encodedValue = EncodingTraits<T>::encodeValue(value);
188 put<EncodedValue>(key, encodedValue);
189}
190
191template<typename T>
192void EncodedValue::append(const typename EncodingTraits<T>::DecodedType& value)
193{
194 EncodedValue encodedValue = EncodingTraits<T>::encodeValue(value);
195 append<EncodedValue>(encodedValue);
196}
197
198template<typename T>
199bool EncodedValue::get(const String& key, typename EncodingTraits<T>::DecodedType& decodedValue)
200{
201 EncodedValue encodedValue;
202 if (!get<EncodedValue>(key, encodedValue))
203 return false;
204
205 return EncodingTraits<T>::decodeValue(encodedValue, decodedValue);
206}
207
208template<typename T>
209bool EncodedValue::get(const String& key, std::unique_ptr<typename EncodingTraits<T>::DecodedType>& decodedValue)
210{
211 EncodedValue encodedValue;
212 if (!get<EncodedValue>(key, encodedValue))
213 return false;
214
215 return EncodingTraits<T>::decodeValue(encodedValue, decodedValue);
216}
217
218} // namespace JSC
219
220using JSC::EncodedValue;
221using JSC::EncodingTraits;
222
223#endif // ENABLE(WEB_REPLAY)
224
225#endif // EncodedValue_h