]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - replay/EncodedValue.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / replay / EncodedValue.cpp
... / ...
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#include "config.h"
29#include "EncodedValue.h"
30
31#if ENABLE(WEB_REPLAY)
32
33#include "InspectorValues.h"
34#include <wtf/text/Base64.h>
35
36using namespace Inspector;
37
38namespace JSC {
39
40RefPtr<InspectorObject> EncodedValue::asObject()
41{
42 RefPtr<InspectorObject> result;
43 bool castSucceeded = m_value->asObject(result);
44 ASSERT_UNUSED(castSucceeded, castSucceeded);
45
46 return result;
47}
48
49RefPtr<InspectorArray> EncodedValue::asArray()
50{
51 RefPtr<InspectorArray> result;
52 bool castSucceeded = m_value->asArray(result);
53 ASSERT_UNUSED(castSucceeded, castSucceeded);
54
55 return result;
56}
57
58EncodedValue EncodingTraits<Vector<char>>::encodeValue(const Vector<char>& buffer)
59{
60 return EncodedValue::createString(base64Encode(buffer));
61}
62
63bool EncodingTraits<Vector<char>>::decodeValue(EncodedValue& encodedBuffer, Vector<char>& decodedValue)
64{
65 return base64Decode(encodedBuffer.convertTo<String>(), decodedValue);
66}
67
68template<> EncodedValue ScalarEncodingTraits<bool>::encodeValue(const bool& value)
69{
70 return EncodedValue(InspectorBasicValue::create(value));
71}
72
73template<> EncodedValue ScalarEncodingTraits<double>::encodeValue(const double& value)
74{
75 return EncodedValue(InspectorBasicValue::create(value));
76}
77
78template<> EncodedValue ScalarEncodingTraits<float>::encodeValue(const float& value)
79{
80 return EncodedValue(InspectorBasicValue::create((double)value));
81}
82
83template<> EncodedValue ScalarEncodingTraits<int32_t>::encodeValue(const int32_t& value)
84{
85 return EncodedValue(InspectorBasicValue::create((double)value));
86}
87
88template<> EncodedValue ScalarEncodingTraits<int64_t>::encodeValue(const int64_t& value)
89{
90 return EncodedValue(InspectorBasicValue::create((double)value));
91}
92
93template<> EncodedValue ScalarEncodingTraits<uint32_t>::encodeValue(const uint32_t& value)
94{
95 return EncodedValue(InspectorBasicValue::create((double)value));
96}
97
98template<> EncodedValue ScalarEncodingTraits<uint64_t>::encodeValue(const uint64_t& value)
99{
100 return EncodedValue(InspectorBasicValue::create((double)value));
101}
102
103template<> bool EncodedValue::convertTo<bool>()
104{
105 bool result;
106 bool castSucceeded = m_value->asBoolean(result);
107 ASSERT_UNUSED(castSucceeded, castSucceeded);
108
109 return result;
110}
111
112template<> double EncodedValue::convertTo<double>()
113{
114 double result;
115 bool castSucceeded = m_value->asDouble(result);
116 ASSERT_UNUSED(castSucceeded, castSucceeded);
117
118 return result;
119}
120
121template<> float EncodedValue::convertTo<float>()
122{
123 float result;
124 bool castSucceeded = m_value->asDouble(result);
125 ASSERT_UNUSED(castSucceeded, castSucceeded);
126
127 return result;
128}
129
130template<> int32_t EncodedValue::convertTo<int32_t>()
131{
132 int32_t result;
133 bool castSucceeded = m_value->asInteger(result);
134 ASSERT_UNUSED(castSucceeded, castSucceeded);
135
136 return result;
137}
138
139template<> int64_t EncodedValue::convertTo<int64_t>()
140{
141 int64_t result;
142 bool castSucceeded = m_value->asInteger(result);
143 ASSERT_UNUSED(castSucceeded, castSucceeded);
144
145 return result;
146}
147
148template<> uint32_t EncodedValue::convertTo<uint32_t>()
149{
150 uint32_t result;
151 bool castSucceeded = m_value->asInteger(result);
152 ASSERT_UNUSED(castSucceeded, castSucceeded);
153
154 return result;
155}
156
157template<> uint64_t EncodedValue::convertTo<uint64_t>()
158{
159 uint64_t result;
160 bool castSucceeded = m_value->asInteger(result);
161 ASSERT_UNUSED(castSucceeded, castSucceeded);
162
163 return result;
164}
165
166template<> String EncodedValue::convertTo<String>()
167{
168 String result;
169 bool castSucceeded = m_value->asString(result);
170 ASSERT_UNUSED(castSucceeded, castSucceeded);
171
172 return result;
173}
174
175template<>
176void EncodedValue::put<EncodedValue>(const String& key, const typename EncodingTraits<EncodedValue>::DecodedType& value)
177{
178 asObject()->setValue(key, value.m_value.copyRef());
179}
180
181template<>
182void EncodedValue::append<EncodedValue>(const typename EncodingTraits<EncodedValue>::DecodedType& value)
183{
184 asArray()->pushValue(value.m_value.copyRef());
185}
186
187template<>
188bool EncodedValue::get<EncodedValue>(const String& key, typename EncodingTraits<EncodedValue>::DecodedType& decodedValue)
189{
190 RefPtr<Inspector::InspectorValue> value;
191 if (!asObject()->getValue(key, value))
192 return false;
193
194 decodedValue = EncodedValue(WTF::move(value));
195 return true;
196}
197
198
199}; // namespace JSC
200
201#endif // ENABLE(WEB_REPLAY)