]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/InspectorValues.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / InspectorValues.h
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2014 University of Washington. 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 are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef InspectorValues_h
33 #define InspectorValues_h
34
35 #include <wtf/Assertions.h>
36 #include <wtf/HashMap.h>
37 #include <wtf/RefCounted.h>
38 #include <wtf/Vector.h>
39 #include <wtf/text/StringHash.h>
40 #include <wtf/text/WTFString.h>
41
42 namespace Inspector {
43
44 class InspectorArray;
45 class InspectorArrayBase;
46 class InspectorObject;
47 class InspectorObjectBase;
48
49 class JS_EXPORT_PRIVATE InspectorValue : public RefCounted<InspectorValue> {
50 public:
51 static const int maxDepth = 1000;
52
53 InspectorValue()
54 : m_type(Type::Null) { }
55 virtual ~InspectorValue() { }
56
57 static Ref<InspectorValue> null();
58
59 enum class Type {
60 Null = 0,
61 Boolean,
62 Double,
63 Integer,
64 String,
65 Object,
66 Array
67 };
68
69 Type type() const { return m_type; }
70
71 bool isNull() const { return m_type == Type::Null; }
72
73 virtual bool asBoolean(bool&) const;
74 virtual bool asInteger(int&) const;
75 virtual bool asInteger(unsigned&) const;
76 virtual bool asInteger(long&) const;
77 virtual bool asInteger(long long&) const;
78 virtual bool asInteger(unsigned long&) const;
79 virtual bool asInteger(unsigned long long&) const;
80 virtual bool asDouble(double&) const;
81 virtual bool asDouble(float&) const;
82 virtual bool asString(String&) const;
83 virtual bool asValue(RefPtr<InspectorValue>&);
84 virtual bool asObject(RefPtr<InspectorObject>&);
85 virtual bool asArray(RefPtr<InspectorArray>&);
86
87 static bool parseJSON(const String& jsonInput, RefPtr<InspectorValue>& output);
88
89 String toJSONString() const;
90 virtual void writeJSON(StringBuilder& output) const;
91
92 protected:
93 explicit InspectorValue(Type type) : m_type(type) { }
94
95 private:
96 Type m_type;
97 };
98
99 class JS_EXPORT_PRIVATE InspectorBasicValue : public InspectorValue {
100 public:
101
102 static Ref<InspectorBasicValue> create(bool);
103 static Ref<InspectorBasicValue> create(int);
104 static Ref<InspectorBasicValue> create(double);
105
106 virtual bool asBoolean(bool&) const override;
107 // Numbers from the frontend are always parsed as doubles, so we allow
108 // clients to convert to integral values with this function.
109 virtual bool asInteger(int&) const override;
110 virtual bool asInteger(unsigned&) const override;
111 virtual bool asInteger(long&) const override;
112 virtual bool asInteger(long long&) const override;
113 virtual bool asInteger(unsigned long&) const override;
114 virtual bool asInteger(unsigned long long&) const override;
115 virtual bool asDouble(double&) const override;
116 virtual bool asDouble(float&) const override;
117
118 virtual void writeJSON(StringBuilder& output) const override;
119
120 private:
121 explicit InspectorBasicValue(bool value)
122 : InspectorValue(Type::Boolean)
123 , m_booleanValue(value) { }
124
125 explicit InspectorBasicValue(int value)
126 : InspectorValue(Type::Integer)
127 , m_doubleValue(static_cast<double>(value)) { }
128
129 explicit InspectorBasicValue(double value)
130 : InspectorValue(Type::Double)
131 , m_doubleValue(value) { }
132
133 union {
134 bool m_booleanValue;
135 double m_doubleValue;
136 };
137 };
138
139 class JS_EXPORT_PRIVATE InspectorString : public InspectorValue {
140 public:
141 static Ref<InspectorString> create(const String&);
142 static Ref<InspectorString> create(const char*);
143
144 virtual bool asString(String& output) const override;
145
146 virtual void writeJSON(StringBuilder& output) const override;
147
148 private:
149 explicit InspectorString(const String& value)
150 : InspectorValue(Type::String)
151 , m_stringValue(value) { }
152
153 explicit InspectorString(const char* value)
154 : InspectorValue(Type::String)
155 , m_stringValue(value) { }
156
157 String m_stringValue;
158 };
159
160 class JS_EXPORT_PRIVATE InspectorObjectBase : public InspectorValue {
161 private:
162 typedef HashMap<String, RefPtr<InspectorValue>> Dictionary;
163
164 public:
165 typedef Dictionary::iterator iterator;
166 typedef Dictionary::const_iterator const_iterator;
167
168 InspectorObject* openAccessors();
169
170 protected:
171 virtual ~InspectorObjectBase();
172
173 virtual bool asObject(RefPtr<InspectorObject>& output) override;
174
175 // FIXME: use templates to reduce the amount of duplicated set*() methods.
176 void setBoolean(const String& name, bool);
177 void setInteger(const String& name, int);
178 void setDouble(const String& name, double);
179 void setString(const String& name, const String&);
180 void setValue(const String& name, RefPtr<InspectorValue>&&);
181 void setObject(const String& name, RefPtr<InspectorObjectBase>&&);
182 void setArray(const String& name, RefPtr<InspectorArrayBase>&&);
183
184 iterator find(const String& name);
185 const_iterator find(const String& name) const;
186
187 // FIXME: use templates to reduce the amount of duplicated get*() methods.
188 bool getBoolean(const String& name, bool& output) const;
189 template<class T> bool getDouble(const String& name, T& output) const
190 {
191 RefPtr<InspectorValue> value;
192 if (!getValue(name, value))
193 return false;
194
195 return value->asDouble(output);
196 }
197 template<class T> bool getInteger(const String& name, T& output) const
198 {
199 RefPtr<InspectorValue> value;
200 if (!getValue(name, value))
201 return false;
202
203 return value->asInteger(output);
204 }
205
206 bool getString(const String& name, String& output) const;
207 bool getObject(const String& name, RefPtr<InspectorObject>&) const;
208 bool getArray(const String& name, RefPtr<InspectorArray>&) const;
209 bool getValue(const String& name, RefPtr<InspectorValue>&) const;
210
211 void remove(const String& name);
212
213 virtual void writeJSON(StringBuilder& output) const override;
214
215 iterator begin() { return m_data.begin(); }
216 iterator end() { return m_data.end(); }
217 const_iterator begin() const { return m_data.begin(); }
218 const_iterator end() const { return m_data.end(); }
219
220 int size() const { return m_data.size(); }
221
222 protected:
223 InspectorObjectBase();
224
225 private:
226 Dictionary m_data;
227 Vector<String> m_order;
228 };
229
230 class InspectorObject : public InspectorObjectBase {
231 public:
232 static JS_EXPORT_PRIVATE Ref<InspectorObject> create();
233
234 using InspectorObjectBase::asObject;
235
236 using InspectorObjectBase::setBoolean;
237 using InspectorObjectBase::setInteger;
238 using InspectorObjectBase::setDouble;
239 using InspectorObjectBase::setString;
240 using InspectorObjectBase::setValue;
241 using InspectorObjectBase::setObject;
242 using InspectorObjectBase::setArray;
243
244 using InspectorObjectBase::find;
245 using InspectorObjectBase::getBoolean;
246 using InspectorObjectBase::getInteger;
247 using InspectorObjectBase::getDouble;
248 using InspectorObjectBase::getString;
249 using InspectorObjectBase::getObject;
250 using InspectorObjectBase::getArray;
251 using InspectorObjectBase::getValue;
252
253 using InspectorObjectBase::remove;
254
255 using InspectorObjectBase::begin;
256 using InspectorObjectBase::end;
257
258 using InspectorObjectBase::size;
259 };
260
261
262 class JS_EXPORT_PRIVATE InspectorArrayBase : public InspectorValue {
263 public:
264 typedef Vector<RefPtr<InspectorValue>>::iterator iterator;
265 typedef Vector<RefPtr<InspectorValue>>::const_iterator const_iterator;
266
267 unsigned length() const { return static_cast<unsigned>(m_data.size()); }
268
269 protected:
270 virtual ~InspectorArrayBase();
271
272 virtual bool asArray(RefPtr<InspectorArray>&) override;
273
274 void pushBoolean(bool);
275 void pushInteger(int);
276 void pushDouble(double);
277 void pushString(const String&);
278 void pushValue(RefPtr<InspectorValue>&&);
279 void pushObject(RefPtr<InspectorObjectBase>&&);
280 void pushArray(RefPtr<InspectorArrayBase>&&);
281
282 RefPtr<InspectorValue> get(size_t index) const;
283
284 virtual void writeJSON(StringBuilder& output) const override;
285
286 iterator begin() { return m_data.begin(); }
287 iterator end() { return m_data.end(); }
288 const_iterator begin() const { return m_data.begin(); }
289 const_iterator end() const { return m_data.end(); }
290
291 protected:
292 InspectorArrayBase();
293
294 private:
295 Vector<RefPtr<InspectorValue>> m_data;
296 };
297
298 class InspectorArray : public InspectorArrayBase {
299 public:
300 static JS_EXPORT_PRIVATE Ref<InspectorArray> create();
301
302 using InspectorArrayBase::asArray;
303
304 using InspectorArrayBase::pushBoolean;
305 using InspectorArrayBase::pushInteger;
306 using InspectorArrayBase::pushDouble;
307 using InspectorArrayBase::pushString;
308 using InspectorArrayBase::pushValue;
309 using InspectorArrayBase::pushObject;
310 using InspectorArrayBase::pushArray;
311
312 using InspectorArrayBase::get;
313
314 using InspectorArrayBase::begin;
315 using InspectorArrayBase::end;
316 };
317
318
319 inline InspectorObjectBase::iterator InspectorObjectBase::find(const String& name)
320 {
321 return m_data.find(name);
322 }
323
324 inline InspectorObjectBase::const_iterator InspectorObjectBase::find(const String& name) const
325 {
326 return m_data.find(name);
327 }
328
329 inline void InspectorObjectBase::setBoolean(const String& name, bool value)
330 {
331 setValue(name, InspectorBasicValue::create(value));
332 }
333
334 inline void InspectorObjectBase::setInteger(const String& name, int value)
335 {
336 setValue(name, InspectorBasicValue::create(value));
337 }
338
339 inline void InspectorObjectBase::setDouble(const String& name, double value)
340 {
341 setValue(name, InspectorBasicValue::create(value));
342 }
343
344 inline void InspectorObjectBase::setString(const String& name, const String& value)
345 {
346 setValue(name, InspectorString::create(value));
347 }
348
349 inline void InspectorObjectBase::setValue(const String& name, RefPtr<InspectorValue>&& value)
350 {
351 ASSERT(value);
352 if (m_data.set(name, WTF::move(value)).isNewEntry)
353 m_order.append(name);
354 }
355
356 inline void InspectorObjectBase::setObject(const String& name, RefPtr<InspectorObjectBase>&& value)
357 {
358 ASSERT(value);
359 if (m_data.set(name, WTF::move(value)).isNewEntry)
360 m_order.append(name);
361 }
362
363 inline void InspectorObjectBase::setArray(const String& name, RefPtr<InspectorArrayBase>&& value)
364 {
365 ASSERT(value);
366 if (m_data.set(name, WTF::move(value)).isNewEntry)
367 m_order.append(name);
368 }
369
370 inline void InspectorArrayBase::pushBoolean(bool value)
371 {
372 m_data.append(InspectorBasicValue::create(value));
373 }
374
375 inline void InspectorArrayBase::pushInteger(int value)
376 {
377 m_data.append(InspectorBasicValue::create(value));
378 }
379
380 inline void InspectorArrayBase::pushDouble(double value)
381 {
382 m_data.append(InspectorBasicValue::create(value));
383 }
384
385 inline void InspectorArrayBase::pushString(const String& value)
386 {
387 m_data.append(InspectorString::create(value));
388 }
389
390 inline void InspectorArrayBase::pushValue(RefPtr<InspectorValue>&& value)
391 {
392 ASSERT(value);
393 m_data.append(WTF::move(value));
394 }
395
396 inline void InspectorArrayBase::pushObject(RefPtr<InspectorObjectBase>&& value)
397 {
398 ASSERT(value);
399 m_data.append(WTF::move(value));
400 }
401
402 inline void InspectorArrayBase::pushArray(RefPtr<InspectorArrayBase>&& value)
403 {
404 ASSERT(value);
405 m_data.append(WTF::move(value));
406 }
407
408 } // namespace Inspector
409
410 #endif // !defined(InspectorValues_h)