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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
* Copyright (C) 2014 University of Washington. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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
* OWNER 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 InspectorValues_h
#define InspectorValues_h
#include "JSExportMacros.h"
#include <wtf/Assertions.h>
#include <wtf/HashMap.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
namespace Inspector {
class InspectorArray;
class InspectorArrayBase;
class InspectorObject;
class InspectorObjectBase;
class JS_EXPORT_PRIVATE InspectorValue : public RefCounted<InspectorValue> {
public:
static const int maxDepth = 1000;
InspectorValue()
: m_type(Type::Null) { }
virtual ~InspectorValue() { }
static Ref<InspectorValue> null();
enum class Type {
Null = 0,
Boolean,
Double,
Integer,
String,
Object,
Array
};
Type type() const { return m_type; }
bool isNull() const { return m_type == Type::Null; }
virtual bool asBoolean(bool&) const;
virtual bool asInteger(int&) const;
virtual bool asInteger(unsigned&) const;
virtual bool asInteger(long&) const;
virtual bool asInteger(long long&) const;
virtual bool asInteger(unsigned long&) const;
virtual bool asInteger(unsigned long long&) const;
virtual bool asDouble(double&) const;
virtual bool asDouble(float&) const;
virtual bool asString(String&) const;
virtual bool asValue(RefPtr<InspectorValue>&);
virtual bool asObject(RefPtr<InspectorObject>&);
virtual bool asArray(RefPtr<InspectorArray>&);
static bool parseJSON(const String& jsonInput, RefPtr<InspectorValue>& output);
String toJSONString() const;
virtual void writeJSON(StringBuilder& output) const;
protected:
explicit InspectorValue(Type type) : m_type(type) { }
private:
Type m_type;
};
class JS_EXPORT_PRIVATE InspectorBasicValue : public InspectorValue {
public:
static Ref<InspectorBasicValue> create(bool);
static Ref<InspectorBasicValue> create(int);
static Ref<InspectorBasicValue> create(double);
virtual bool asBoolean(bool&) const override;
// Numbers from the frontend are always parsed as doubles, so we allow
// clients to convert to integral values with this function.
virtual bool asInteger(int&) const override;
virtual bool asInteger(unsigned&) const override;
virtual bool asInteger(long&) const override;
virtual bool asInteger(long long&) const override;
virtual bool asInteger(unsigned long&) const override;
virtual bool asInteger(unsigned long long&) const override;
virtual bool asDouble(double&) const override;
virtual bool asDouble(float&) const override;
virtual void writeJSON(StringBuilder& output) const override;
private:
explicit InspectorBasicValue(bool value)
: InspectorValue(Type::Boolean)
, m_booleanValue(value) { }
explicit InspectorBasicValue(int value)
: InspectorValue(Type::Integer)
, m_doubleValue(static_cast<double>(value)) { }
explicit InspectorBasicValue(double value)
: InspectorValue(Type::Double)
, m_doubleValue(value) { }
union {
bool m_booleanValue;
double m_doubleValue;
};
};
class JS_EXPORT_PRIVATE InspectorString : public InspectorValue {
public:
static Ref<InspectorString> create(const String&);
static Ref<InspectorString> create(const char*);
virtual bool asString(String& output) const override;
virtual void writeJSON(StringBuilder& output) const override;
private:
explicit InspectorString(const String& value)
: InspectorValue(Type::String)
, m_stringValue(value) { }
explicit InspectorString(const char* value)
: InspectorValue(Type::String)
, m_stringValue(value) { }
String m_stringValue;
};
class JS_EXPORT_PRIVATE InspectorObjectBase : public InspectorValue {
private:
typedef HashMap<String, RefPtr<InspectorValue>> Dictionary;
public:
typedef Dictionary::iterator iterator;
typedef Dictionary::const_iterator const_iterator;
InspectorObject* openAccessors();
protected:
virtual ~InspectorObjectBase();
virtual bool asObject(RefPtr<InspectorObject>& output) override;
// FIXME: use templates to reduce the amount of duplicated set*() methods.
void setBoolean(const String& name, bool);
void setInteger(const String& name, int);
void setDouble(const String& name, double);
void setString(const String& name, const String&);
void setValue(const String& name, RefPtr<InspectorValue>&&);
void setObject(const String& name, RefPtr<InspectorObjectBase>&&);
void setArray(const String& name, RefPtr<InspectorArrayBase>&&);
iterator find(const String& name);
const_iterator find(const String& name) const;
// FIXME: use templates to reduce the amount of duplicated get*() methods.
bool getBoolean(const String& name, bool& output) const;
template<class T> bool getDouble(const String& name, T& output) const
{
RefPtr<InspectorValue> value;
if (!getValue(name, value))
return false;
return value->asDouble(output);
}
template<class T> bool getInteger(const String& name, T& output) const
{
RefPtr<InspectorValue> value;
if (!getValue(name, value))
return false;
return value->asInteger(output);
}
bool getString(const String& name, String& output) const;
bool getObject(const String& name, RefPtr<InspectorObject>&) const;
bool getArray(const String& name, RefPtr<InspectorArray>&) const;
bool getValue(const String& name, RefPtr<InspectorValue>&) const;
void remove(const String& name);
virtual void writeJSON(StringBuilder& output) const override;
iterator begin() { return m_data.begin(); }
iterator end() { return m_data.end(); }
const_iterator begin() const { return m_data.begin(); }
const_iterator end() const { return m_data.end(); }
int size() const { return m_data.size(); }
protected:
InspectorObjectBase();
private:
Dictionary m_data;
Vector<String> m_order;
};
class InspectorObject : public InspectorObjectBase {
public:
static JS_EXPORT_PRIVATE Ref<InspectorObject> create();
using InspectorObjectBase::asObject;
using InspectorObjectBase::setBoolean;
using InspectorObjectBase::setInteger;
using InspectorObjectBase::setDouble;
using InspectorObjectBase::setString;
using InspectorObjectBase::setValue;
using InspectorObjectBase::setObject;
using InspectorObjectBase::setArray;
using InspectorObjectBase::find;
using InspectorObjectBase::getBoolean;
using InspectorObjectBase::getInteger;
using InspectorObjectBase::getDouble;
using InspectorObjectBase::getString;
using InspectorObjectBase::getObject;
using InspectorObjectBase::getArray;
using InspectorObjectBase::getValue;
using InspectorObjectBase::remove;
using InspectorObjectBase::begin;
using InspectorObjectBase::end;
using InspectorObjectBase::size;
};
class JS_EXPORT_PRIVATE InspectorArrayBase : public InspectorValue {
public:
typedef Vector<RefPtr<InspectorValue>>::iterator iterator;
typedef Vector<RefPtr<InspectorValue>>::const_iterator const_iterator;
unsigned length() const { return static_cast<unsigned>(m_data.size()); }
protected:
virtual ~InspectorArrayBase();
virtual bool asArray(RefPtr<InspectorArray>&) override;
void pushBoolean(bool);
void pushInteger(int);
void pushDouble(double);
void pushString(const String&);
void pushValue(RefPtr<InspectorValue>&&);
void pushObject(RefPtr<InspectorObjectBase>&&);
void pushArray(RefPtr<InspectorArrayBase>&&);
RefPtr<InspectorValue> get(size_t index) const;
virtual void writeJSON(StringBuilder& output) const override;
iterator begin() { return m_data.begin(); }
iterator end() { return m_data.end(); }
const_iterator begin() const { return m_data.begin(); }
const_iterator end() const { return m_data.end(); }
protected:
InspectorArrayBase();
private:
Vector<RefPtr<InspectorValue>> m_data;
};
class InspectorArray : public InspectorArrayBase {
public:
static JS_EXPORT_PRIVATE Ref<InspectorArray> create();
using InspectorArrayBase::asArray;
using InspectorArrayBase::pushBoolean;
using InspectorArrayBase::pushInteger;
using InspectorArrayBase::pushDouble;
using InspectorArrayBase::pushString;
using InspectorArrayBase::pushValue;
using InspectorArrayBase::pushObject;
using InspectorArrayBase::pushArray;
using InspectorArrayBase::get;
using InspectorArrayBase::begin;
using InspectorArrayBase::end;
};
inline InspectorObjectBase::iterator InspectorObjectBase::find(const String& name)
{
return m_data.find(name);
}
inline InspectorObjectBase::const_iterator InspectorObjectBase::find(const String& name) const
{
return m_data.find(name);
}
inline void InspectorObjectBase::setBoolean(const String& name, bool value)
{
setValue(name, InspectorBasicValue::create(value));
}
inline void InspectorObjectBase::setInteger(const String& name, int value)
{
setValue(name, InspectorBasicValue::create(value));
}
inline void InspectorObjectBase::setDouble(const String& name, double value)
{
setValue(name, InspectorBasicValue::create(value));
}
inline void InspectorObjectBase::setString(const String& name, const String& value)
{
setValue(name, InspectorString::create(value));
}
inline void InspectorObjectBase::setValue(const String& name, RefPtr<InspectorValue>&& value)
{
ASSERT(value);
if (m_data.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void InspectorObjectBase::setObject(const String& name, RefPtr<InspectorObjectBase>&& value)
{
ASSERT(value);
if (m_data.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void InspectorObjectBase::setArray(const String& name, RefPtr<InspectorArrayBase>&& value)
{
ASSERT(value);
if (m_data.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void InspectorArrayBase::pushBoolean(bool value)
{
m_data.append(InspectorBasicValue::create(value));
}
inline void InspectorArrayBase::pushInteger(int value)
{
m_data.append(InspectorBasicValue::create(value));
}
inline void InspectorArrayBase::pushDouble(double value)
{
m_data.append(InspectorBasicValue::create(value));
}
inline void InspectorArrayBase::pushString(const String& value)
{
m_data.append(InspectorString::create(value));
}
inline void InspectorArrayBase::pushValue(RefPtr<InspectorValue>&& value)
{
ASSERT(value);
m_data.append(WTFMove(value));
}
inline void InspectorArrayBase::pushObject(RefPtr<InspectorObjectBase>&& value)
{
ASSERT(value);
m_data.append(WTFMove(value));
}
inline void InspectorArrayBase::pushArray(RefPtr<InspectorArrayBase>&& value)
{
ASSERT(value);
m_data.append(WTFMove(value));
}
} // namespace Inspector
#endif // !defined(InspectorValues_h)
|