Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(894)

Side by Side Diff: sky/engine/core/inspector/CodeGeneratorInspectorStrings.py

Issue 723773002: Bring back ScriptDebugServer and associated machinery (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2013 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 # THis file contains string resources for CodeGeneratorInspector.
30 # Its syntax is a Python syntax subset, suitable for manual parsing.
31
32 frontend_domain_class = (
33 """ class $domainClassName {
34 public:
35 $domainClassName(InspectorFrontendChannel* inspectorFrontendChannel) : m _inspectorFrontendChannel(inspectorFrontendChannel) { }
36 ${frontendDomainMethodDeclarations}
37 void flush() { m_inspectorFrontendChannel->flush(); }
38 private:
39 InspectorFrontendChannel* m_inspectorFrontendChannel;
40 };
41
42 $domainClassName* $domainFieldName() { return &m_$domainFieldName; }
43
44 """)
45
46 backend_method = (
47 """void InspectorBackendDispatcherImpl::${domainName}_$methodName(long callId, J SONObject*$requestMessageObject, JSONArray* protocolErrors)
48 {
49 if (!$agentField)
50 protocolErrors->pushString("${domainName} handler is not available.");
51 $methodCode
52 if (protocolErrors->length()) {
53 reportProtocolError(&callId, InvalidParams, String::format(InvalidParams FormatString, commandName($commandNameIndex)), protocolErrors);
54 return;
55 }
56 $agentCallParamsDeclaration
57 $agentField->$methodName($agentCallParams);
58 $responseCook
59 sendResponse(callId, $sendResponseCallParams);
60 }
61 """)
62
63 frontend_method = ("""void InspectorFrontend::$domainName::$eventName($parameter s)
64 {
65 RefPtr<JSONObject> jsonMessage = JSONObject::create();
66 jsonMessage->setString("method", "$domainName.$eventName");
67 $code if (m_inspectorFrontendChannel)
68 m_inspectorFrontendChannel->sendMessageToFrontend(jsonMessage.release()) ;
69 }
70 """)
71
72 callback_main_methods = (
73 """InspectorBackendDispatcher::$agentName::$callbackName::$callbackName(PassRefP tr<InspectorBackendDispatcherImpl> backendImpl, int id) : CallbackBase(backendIm pl, id) {}
74
75 void InspectorBackendDispatcher::$agentName::$callbackName::sendSuccess($paramet ers)
76 {
77 RefPtr<JSONObject> jsonMessage = JSONObject::create();
78 $code sendIfActive(jsonMessage, ErrorString(), PassRefPtr<JSONValue>());
79 }
80 """)
81
82 callback_failure_method = (
83 """void InspectorBackendDispatcher::$agentName::$callbackName::sendFailure(const ErrorString& error, $parameter)
84 {
85 ASSERT(error.length());
86 RefPtr<JSONValue> errorDataValue;
87 if (error) {
88 errorDataValue = $argument;
89 }
90 sendIfActive(nullptr, error, errorDataValue.release());
91 }
92 """)
93
94
95 frontend_h = (
96 """#ifndef InspectorFrontend_h
97 #define InspectorFrontend_h
98
99 #include "InspectorTypeBuilder.h"
100 #include "core/inspector/InspectorFrontendChannel.h"
101 #include "platform/JSONValues.h"
102 #include "wtf/PassRefPtr.h"
103 #include "wtf/text/WTFString.h"
104
105 namespace blink {
106
107 typedef String ErrorString;
108
109 class InspectorFrontend {
110 public:
111 InspectorFrontend(InspectorFrontendChannel*);
112 InspectorFrontendChannel* channel() { return m_inspectorFrontendChannel; }
113
114 $domainClassList
115 private:
116 InspectorFrontendChannel* m_inspectorFrontendChannel;
117 ${fieldDeclarations}};
118
119 } // namespace blink
120 #endif // !defined(InspectorFrontend_h)
121 """)
122
123 backend_h = (
124 """#ifndef InspectorBackendDispatcher_h
125 #define InspectorBackendDispatcher_h
126
127 #include "InspectorTypeBuilder.h"
128
129 #include "platform/heap/Handle.h"
130 #include "wtf/PassRefPtr.h"
131 #include "wtf/RefCounted.h"
132 #include "wtf/text/WTFString.h"
133
134 namespace blink {
135
136 class JSONObject;
137 class JSONArray;
138 class InspectorFrontendChannel;
139
140 typedef String ErrorString;
141
142 class InspectorBackendDispatcherImpl;
143
144 class InspectorBackendDispatcher: public RefCounted<InspectorBackendDispatcher> {
145 public:
146 static PassRefPtr<InspectorBackendDispatcher> create(InspectorFrontendChanne l* inspectorFrontendChannel);
147 virtual ~InspectorBackendDispatcher() { }
148 virtual void trace(Visitor*) { }
149
150 class CallbackBase: public RefCounted<CallbackBase> {
151 public:
152 CallbackBase(PassRefPtr<InspectorBackendDispatcherImpl> backendImpl, int id);
153 virtual ~CallbackBase();
154 virtual void trace(Visitor*);
155 void sendFailure(const ErrorString&);
156 bool isActive();
157
158 protected:
159 void sendIfActive(PassRefPtr<JSONObject> partialMessage, const ErrorStri ng& invocationError, PassRefPtr<JSONValue> errorData);
160
161 private:
162 void disable() { m_alreadySent = true; }
163
164 RefPtr<InspectorBackendDispatcherImpl> m_backendImpl;
165 int m_id;
166 bool m_alreadySent;
167
168 friend class InspectorBackendDispatcherImpl;
169 };
170
171 $agentInterfaces
172 $virtualSetters
173
174 virtual void clearFrontend() = 0;
175
176 enum CommonErrorCode {
177 ParseError = 0,
178 InvalidRequest,
179 MethodNotFound,
180 InvalidParams,
181 InternalError,
182 ServerError,
183 LastEntry,
184 };
185
186 void reportProtocolError(const long* const callId, CommonErrorCode, const St ring& errorMessage) const;
187 virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<JSONValue> data) const = 0;
188 virtual void dispatch(const String& message) = 0;
189 static bool getCommandName(const String& message, String* result);
190
191 enum MethodNames {
192 $methodNamesEnumContent
193
194 kMethodNamesEnumSize
195 };
196
197 static const char* commandName(MethodNames);
198
199 private:
200 static const char commandNames[];
201 static const size_t commandNamesIndex[];
202 };
203
204 } // namespace blink
205 #endif // !defined(InspectorBackendDispatcher_h)
206
207
208 """)
209
210 backend_cpp = (
211 """
212
213 #include "config.h"
214 #include "InspectorBackendDispatcher.h"
215
216 #include "core/inspector/InspectorFrontendChannel.h"
217 #include "core/inspector/JSONParser.h"
218 #include "platform/JSONValues.h"
219 #include "wtf/text/CString.h"
220 #include "wtf/text/WTFString.h"
221
222 namespace blink {
223
224 const char InspectorBackendDispatcher::commandNames[] = {
225 $methodNameDeclarations
226 };
227
228 const size_t InspectorBackendDispatcher::commandNamesIndex[] = {
229 $methodNameDeclarationsIndex
230 };
231
232 const char* InspectorBackendDispatcher::commandName(MethodNames index) {
233 COMPILE_ASSERT(static_cast<int>(kMethodNamesEnumSize) == WTF_ARRAY_LENGTH(co mmandNamesIndex), command_name_array_problem);
234 return commandNames + commandNamesIndex[index];
235 }
236
237 class InspectorBackendDispatcherImpl : public InspectorBackendDispatcher {
238 public:
239 InspectorBackendDispatcherImpl(InspectorFrontendChannel* inspectorFrontendCh annel)
240 : m_inspectorFrontendChannel(inspectorFrontendChannel)
241 $constructorInit
242 { }
243
244 virtual void clearFrontend() { m_inspectorFrontendChannel = 0; }
245 virtual void dispatch(const String& message);
246 virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<JSONValue> data) const;
247 using InspectorBackendDispatcher::reportProtocolError;
248
249 void sendResponse(long callId, const ErrorString& invocationError, PassRefPt r<JSONValue> errorData, PassRefPtr<JSONObject> result);
250 bool isActive() { return m_inspectorFrontendChannel; }
251
252 $setters
253 private:
254 $methodDeclarations
255
256 InspectorFrontendChannel* m_inspectorFrontendChannel;
257 $fieldDeclarations
258
259 template<typename R, typename V, typename V0>
260 static R getPropertyValueImpl(JSONObject* object, const char* name, bool* va lueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(JSONVal ue*, V*), const char* type_name);
261
262 static int getInt(JSONObject* object, const char* name, bool* valueFound, JS ONArray* protocolErrors);
263 static double getDouble(JSONObject* object, const char* name, bool* valueFou nd, JSONArray* protocolErrors);
264 static String getString(JSONObject* object, const char* name, bool* valueFou nd, JSONArray* protocolErrors);
265 static bool getBoolean(JSONObject* object, const char* name, bool* valueFoun d, JSONArray* protocolErrors);
266 static PassRefPtr<JSONObject> getObject(JSONObject* object, const char* name , bool* valueFound, JSONArray* protocolErrors);
267 static PassRefPtr<JSONArray> getArray(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors);
268
269 void sendResponse(long callId, ErrorString invocationError, PassRefPtr<JSONO bject> result)
270 {
271 sendResponse(callId, invocationError, RefPtr<JSONValue>(), result);
272 }
273 void sendResponse(long callId, ErrorString invocationError)
274 {
275 sendResponse(callId, invocationError, RefPtr<JSONValue>(), JSONObject::c reate());
276 }
277 static const char InvalidParamsFormatString[];
278 };
279
280 const char InspectorBackendDispatcherImpl::InvalidParamsFormatString[] = "Some a rguments of method '%s' can't be processed";
281
282 $methods
283
284 PassRefPtr<InspectorBackendDispatcher> InspectorBackendDispatcher::create(Inspec torFrontendChannel* inspectorFrontendChannel)
285 {
286 return adoptRef(new InspectorBackendDispatcherImpl(inspectorFrontendChannel) );
287 }
288
289
290 void InspectorBackendDispatcherImpl::dispatch(const String& message)
291 {
292 RefPtr<InspectorBackendDispatcher> protect(this);
293 typedef void (InspectorBackendDispatcherImpl::*CallHandler)(long callId, JSO NObject* messageObject, JSONArray* protocolErrors);
294 typedef HashMap<String, CallHandler> DispatchMap;
295 DEFINE_STATIC_LOCAL(DispatchMap, dispatchMap, );
296 long callId = 0;
297
298 if (dispatchMap.isEmpty()) {
299 static const CallHandler handlers[] = {
300 $messageHandlers
301 };
302 for (size_t i = 0; i < kMethodNamesEnumSize; ++i)
303 dispatchMap.add(commandName(static_cast<MethodNames>(i)), handlers[i ]);
304 }
305
306 RefPtr<JSONValue> parsedMessage = parseJSON(message);
307 if (!parsedMessage) {
308 reportProtocolError(0, ParseError, "Message must be in JSON format");
309 return;
310 }
311
312 RefPtr<JSONObject> messageObject = parsedMessage->asObject();
313 if (!messageObject) {
314 reportProtocolError(0, InvalidRequest, "Message must be a JSONified obje ct");
315 return;
316 }
317
318 RefPtr<JSONValue> callIdValue = messageObject->get("id");
319 if (!callIdValue) {
320 reportProtocolError(0, InvalidRequest, "'id' property was not found");
321 return;
322 }
323
324 if (!callIdValue->asNumber(&callId)) {
325 reportProtocolError(0, InvalidRequest, "The type of 'id' property must b e number");
326 return;
327 }
328
329 RefPtr<JSONValue> methodValue = messageObject->get("method");
330 if (!methodValue) {
331 reportProtocolError(&callId, InvalidRequest, "'method' property wasn't f ound");
332 return;
333 }
334
335 String method;
336 if (!methodValue->asString(&method)) {
337 reportProtocolError(&callId, InvalidRequest, "The type of 'method' prope rty must be string");
338 return;
339 }
340
341 HashMap<String, CallHandler>::iterator it = dispatchMap.find(method);
342 if (it == dispatchMap.end()) {
343 reportProtocolError(&callId, MethodNotFound, "'" + method + "' wasn't fo und");
344 return;
345 }
346
347 RefPtr<JSONArray> protocolErrors = JSONArray::create();
348 ((*this).*it->value)(callId, messageObject.get(), protocolErrors.get());
349 }
350
351 void InspectorBackendDispatcherImpl::sendResponse(long callId, const ErrorString & invocationError, PassRefPtr<JSONValue> errorData, PassRefPtr<JSONObject> resul t)
352 {
353 if (invocationError.length()) {
354 reportProtocolError(&callId, ServerError, invocationError, errorData);
355 return;
356 }
357
358 RefPtr<JSONObject> responseMessage = JSONObject::create();
359 responseMessage->setNumber("id", callId);
360 responseMessage->setObject("result", result);
361 if (m_inspectorFrontendChannel)
362 m_inspectorFrontendChannel->sendMessageToFrontend(responseMessage.releas e());
363 }
364
365 void InspectorBackendDispatcher::reportProtocolError(const long* const callId, C ommonErrorCode code, const String& errorMessage) const
366 {
367 reportProtocolError(callId, code, errorMessage, PassRefPtr<JSONValue>());
368 }
369
370 void InspectorBackendDispatcherImpl::reportProtocolError(const long* const callI d, CommonErrorCode code, const String& errorMessage, PassRefPtr<JSONValue> data) const
371 {
372 DEFINE_STATIC_LOCAL(Vector<int>,s_commonErrors,);
373 if (!s_commonErrors.size()) {
374 s_commonErrors.insert(ParseError, -32700);
375 s_commonErrors.insert(InvalidRequest, -32600);
376 s_commonErrors.insert(MethodNotFound, -32601);
377 s_commonErrors.insert(InvalidParams, -32602);
378 s_commonErrors.insert(InternalError, -32603);
379 s_commonErrors.insert(ServerError, -32000);
380 }
381 ASSERT(code >=0);
382 ASSERT((unsigned)code < s_commonErrors.size());
383 ASSERT(s_commonErrors[code]);
384 RefPtr<JSONObject> error = JSONObject::create();
385 error->setNumber("code", s_commonErrors[code]);
386 error->setString("message", errorMessage);
387 ASSERT(error);
388 if (data)
389 error->setValue("data", data);
390 RefPtr<JSONObject> message = JSONObject::create();
391 message->setObject("error", error);
392 if (callId)
393 message->setNumber("id", *callId);
394 else
395 message->setValue("id", JSONValue::null());
396 if (m_inspectorFrontendChannel)
397 m_inspectorFrontendChannel->sendMessageToFrontend(message.release());
398 }
399
400 template<typename R, typename V, typename V0>
401 R InspectorBackendDispatcherImpl::getPropertyValueImpl(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(JSONValue*, V*), const char* type_name)
402 {
403 ASSERT(protocolErrors);
404
405 if (valueFound)
406 *valueFound = false;
407
408 V value = initial_value;
409
410 if (!object) {
411 if (!valueFound) {
412 // Required parameter in missing params container.
413 protocolErrors->pushString(String::format("'params' object must cont ain required parameter '%s' with type '%s'.", name, type_name));
414 }
415 return value;
416 }
417
418 JSONObject::const_iterator end = object->end();
419 JSONObject::const_iterator valueIterator = object->find(name);
420
421 if (valueIterator == end) {
422 if (!valueFound)
423 protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name, type_name));
424 return value;
425 }
426
427 if (!as_method(valueIterator->value.get(), &value))
428 protocolErrors->pushString(String::format("Parameter '%s' has wrong type . It must be '%s'.", name, type_name));
429 else
430 if (valueFound)
431 *valueFound = true;
432 return value;
433 }
434
435 struct AsMethodBridges {
436 static bool asInt(JSONValue* value, int* output) { return value->asNumber(ou tput); }
437 static bool asDouble(JSONValue* value, double* output) { return value->asNum ber(output); }
438 static bool asString(JSONValue* value, String* output) { return value->asStr ing(output); }
439 static bool asBoolean(JSONValue* value, bool* output) { return value->asBool ean(output); }
440 static bool asObject(JSONValue* value, RefPtr<JSONObject>* output) { return value->asObject(output); }
441 static bool asArray(JSONValue* value, RefPtr<JSONArray>* output) { return va lue->asArray(output); }
442 };
443
444 int InspectorBackendDispatcherImpl::getInt(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors)
445 {
446 return getPropertyValueImpl<int, int, int>(object, name, valueFound, protoco lErrors, 0, AsMethodBridges::asInt, "Number");
447 }
448
449 double InspectorBackendDispatcherImpl::getDouble(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors)
450 {
451 return getPropertyValueImpl<double, double, double>(object, name, valueFound , protocolErrors, 0, AsMethodBridges::asDouble, "Number");
452 }
453
454 String InspectorBackendDispatcherImpl::getString(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors)
455 {
456 return getPropertyValueImpl<String, String, String>(object, name, valueFound , protocolErrors, "", AsMethodBridges::asString, "String");
457 }
458
459 bool InspectorBackendDispatcherImpl::getBoolean(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors)
460 {
461 return getPropertyValueImpl<bool, bool, bool>(object, name, valueFound, prot ocolErrors, false, AsMethodBridges::asBoolean, "Boolean");
462 }
463
464 PassRefPtr<JSONObject> InspectorBackendDispatcherImpl::getObject(JSONObject* obj ect, const char* name, bool* valueFound, JSONArray* protocolErrors)
465 {
466 return getPropertyValueImpl<PassRefPtr<JSONObject>, RefPtr<JSONObject>, JSON Object*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asObject, "Object");
467 }
468
469 PassRefPtr<JSONArray> InspectorBackendDispatcherImpl::getArray(JSONObject* objec t, const char* name, bool* valueFound, JSONArray* protocolErrors)
470 {
471 return getPropertyValueImpl<PassRefPtr<JSONArray>, RefPtr<JSONArray>, JSONAr ray*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asArray, "Ar ray");
472 }
473
474 bool InspectorBackendDispatcher::getCommandName(const String& message, String* r esult)
475 {
476 RefPtr<JSONValue> value = parseJSON(message);
477 if (!value)
478 return false;
479
480 RefPtr<JSONObject> object = value->asObject();
481 if (!object)
482 return false;
483
484 if (!object->getString("method", result))
485 return false;
486
487 return true;
488 }
489
490 InspectorBackendDispatcher::CallbackBase::CallbackBase(PassRefPtr<InspectorBacke ndDispatcherImpl> backendImpl, int id)
491 : m_backendImpl(backendImpl), m_id(id), m_alreadySent(false) {}
492
493 InspectorBackendDispatcher::CallbackBase::~CallbackBase() {}
494
495 void InspectorBackendDispatcher::CallbackBase::trace(Visitor* visitor)
496 {
497 visitor->trace(m_backendImpl);
498 }
499
500 void InspectorBackendDispatcher::CallbackBase::sendFailure(const ErrorString& er ror)
501 {
502 ASSERT(error.length());
503 sendIfActive(nullptr, error, PassRefPtr<JSONValue>());
504 }
505
506 bool InspectorBackendDispatcher::CallbackBase::isActive()
507 {
508 return !m_alreadySent && m_backendImpl->isActive();
509 }
510
511 void InspectorBackendDispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObjec t> partialMessage, const ErrorString& invocationError, PassRefPtr<JSONValue> err orData)
512 {
513 if (m_alreadySent)
514 return;
515 m_backendImpl->sendResponse(m_id, invocationError, errorData, partialMessage );
516 m_alreadySent = true;
517 }
518
519 } // namespace blink
520
521 """)
522
523 frontend_cpp = (
524 """
525
526 #include "config.h"
527 #include "InspectorFrontend.h"
528
529 #include "core/inspector/InspectorFrontendChannel.h"
530 #include "platform/JSONValues.h"
531 #include "wtf/text/CString.h"
532 #include "wtf/text/WTFString.h"
533
534 namespace blink {
535
536 InspectorFrontend::InspectorFrontend(InspectorFrontendChannel* inspectorFrontend Channel)
537 : m_inspectorFrontendChannel(inspectorFrontendChannel)
538 , $constructorInit
539 {
540 }
541
542 $methods
543
544 } // namespace blink
545
546 """)
547
548 typebuilder_h = (
549 """
550 #ifndef InspectorTypeBuilder_h
551 #define InspectorTypeBuilder_h
552
553 #include "platform/JSONValues.h"
554 #include "wtf/Assertions.h"
555 #include "wtf/PassRefPtr.h"
556
557 namespace blink {
558
559 namespace TypeBuilder {
560
561 template<typename T>
562 class OptOutput {
563 public:
564 OptOutput() : m_assigned(false) { }
565
566 void operator=(T value)
567 {
568 m_value = value;
569 m_assigned = true;
570 }
571
572 bool isAssigned() { return m_assigned; }
573
574 T getValue()
575 {
576 ASSERT(isAssigned());
577 return m_value;
578 }
579
580 private:
581 T m_value;
582 bool m_assigned;
583
584 WTF_MAKE_NONCOPYABLE(OptOutput);
585 };
586
587
588 // A small transient wrapper around int type, that can be used as a funciton par ameter type
589 // cleverly disallowing C++ implicit casts from float or double.
590 class ExactlyInt {
591 public:
592 template<typename T>
593 ExactlyInt(T t) : m_value(cast_to_int<T>(t)) {}
594
595 ExactlyInt() {}
596
597 operator int() { return m_value; }
598 private:
599 int m_value;
600
601 template<typename T>
602 static int cast_to_int(T) { return T::default_case_cast_is_not_supported(); }
603 };
604
605 template<>
606 inline int ExactlyInt::cast_to_int<int>(int i) { return i; }
607
608 template<>
609 inline int ExactlyInt::cast_to_int<unsigned int>(unsigned int i) { return i; }
610
611 class RuntimeCastHelper {
612 public:
613 #if $validatorIfdefName
614 template<JSONValue::Type TYPE>
615 static void assertType(JSONValue* value)
616 {
617 ASSERT(value->type() == TYPE);
618 }
619 static void assertAny(JSONValue*);
620 static void assertInt(JSONValue* value);
621 #endif
622 };
623
624
625 // This class provides "Traits" type for the input type T. It is programmed usin g C++ template specialization
626 // technique. By default it simply takes "ItemTraits" type from T, but it doesn' t work with the base types.
627 template<typename T>
628 struct ArrayItemHelper {
629 typedef typename T::ItemTraits Traits;
630 };
631
632 template<typename T>
633 class Array : public JSONArrayBase {
634 private:
635 Array() { }
636
637 JSONArray* openAccessors() {
638 COMPILE_ASSERT(sizeof(JSONArray) == sizeof(Array<T>), cannot_cast);
639 return static_cast<JSONArray*>(static_cast<JSONArrayBase*>(this));
640 }
641
642 public:
643 void addItem(PassRefPtr<T> value)
644 {
645 ArrayItemHelper<T>::Traits::pushRefPtr(this->openAccessors(), value);
646 }
647
648 void addItem(T value)
649 {
650 ArrayItemHelper<T>::Traits::pushRaw(this->openAccessors(), value);
651 }
652
653 static PassRefPtr<Array<T> > create()
654 {
655 return adoptRef(new Array<T>());
656 }
657
658 static PassRefPtr<Array<T> > runtimeCast(PassRefPtr<JSONValue> value)
659 {
660 RefPtr<JSONArray> array;
661 bool castRes = value->asArray(&array);
662 ASSERT_UNUSED(castRes, castRes);
663 #if $validatorIfdefName
664 assertCorrectValue(array.get());
665 #endif // $validatorIfdefName
666 COMPILE_ASSERT(sizeof(Array<T>) == sizeof(JSONArray), type_cast_problem) ;
667 return static_cast<Array<T>*>(static_cast<JSONArrayBase*>(array.get()));
668 }
669
670 void concat(PassRefPtr<Array<T> > array)
671 {
672 return ArrayItemHelper<T>::Traits::concat(this->openAccessors(), array-> openAccessors());
673 }
674
675 #if $validatorIfdefName
676 static void assertCorrectValue(JSONValue* value)
677 {
678 RefPtr<JSONArray> array;
679 bool castRes = value->asArray(&array);
680 ASSERT_UNUSED(castRes, castRes);
681 for (unsigned i = 0; i < array->length(); i++)
682 ArrayItemHelper<T>::Traits::template assertCorrectValue<T>(array->ge t(i).get());
683 }
684
685 #endif // $validatorIfdefName
686 };
687
688 struct StructItemTraits {
689 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
690 {
691 array->pushValue(value);
692 }
693
694 static void concat(JSONArray* array, JSONArray* anotherArray)
695 {
696 for (JSONArray::iterator it = anotherArray->begin(); it != anotherArray- >end(); ++it)
697 array->pushValue(*it);
698 }
699
700 #if $validatorIfdefName
701 template<typename T>
702 static void assertCorrectValue(JSONValue* value) {
703 T::assertCorrectValue(value);
704 }
705 #endif // $validatorIfdefName
706 };
707
708 template<>
709 struct ArrayItemHelper<String> {
710 struct Traits {
711 static void pushRaw(JSONArray* array, const String& value)
712 {
713 array->pushString(value);
714 }
715
716 #if $validatorIfdefName
717 template<typename T>
718 static void assertCorrectValue(JSONValue* value) {
719 RuntimeCastHelper::assertType<JSONValue::TypeString>(value);
720 }
721 #endif // $validatorIfdefName
722 };
723 };
724
725 template<>
726 struct ArrayItemHelper<int> {
727 struct Traits {
728 static void pushRaw(JSONArray* array, int value)
729 {
730 array->pushInt(value);
731 }
732
733 #if $validatorIfdefName
734 template<typename T>
735 static void assertCorrectValue(JSONValue* value) {
736 RuntimeCastHelper::assertInt(value);
737 }
738 #endif // $validatorIfdefName
739 };
740 };
741
742 template<>
743 struct ArrayItemHelper<double> {
744 struct Traits {
745 static void pushRaw(JSONArray* array, double value)
746 {
747 array->pushNumber(value);
748 }
749
750 #if $validatorIfdefName
751 template<typename T>
752 static void assertCorrectValue(JSONValue* value) {
753 RuntimeCastHelper::assertType<JSONValue::TypeNumber>(value);
754 }
755 #endif // $validatorIfdefName
756 };
757 };
758
759 template<>
760 struct ArrayItemHelper<bool> {
761 struct Traits {
762 static void pushRaw(JSONArray* array, bool value)
763 {
764 array->pushBoolean(value);
765 }
766
767 #if $validatorIfdefName
768 template<typename T>
769 static void assertCorrectValue(JSONValue* value) {
770 RuntimeCastHelper::assertType<JSONValue::TypeBoolean>(value);
771 }
772 #endif // $validatorIfdefName
773 };
774 };
775
776 template<>
777 struct ArrayItemHelper<JSONValue> {
778 struct Traits {
779 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
780 {
781 array->pushValue(value);
782 }
783
784 #if $validatorIfdefName
785 template<typename T>
786 static void assertCorrectValue(JSONValue* value) {
787 RuntimeCastHelper::assertAny(value);
788 }
789 #endif // $validatorIfdefName
790 };
791 };
792
793 template<>
794 struct ArrayItemHelper<JSONObject> {
795 struct Traits {
796 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
797 {
798 array->pushValue(value);
799 }
800
801 #if $validatorIfdefName
802 template<typename T>
803 static void assertCorrectValue(JSONValue* value) {
804 RuntimeCastHelper::assertType<JSONValue::TypeObject>(value);
805 }
806 #endif // $validatorIfdefName
807 };
808 };
809
810 template<>
811 struct ArrayItemHelper<JSONArray> {
812 struct Traits {
813 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONArray> value)
814 {
815 array->pushArray(value);
816 }
817
818 #if $validatorIfdefName
819 template<typename T>
820 static void assertCorrectValue(JSONValue* value) {
821 RuntimeCastHelper::assertType<JSONValue::TypeArray>(value);
822 }
823 #endif // $validatorIfdefName
824 };
825 };
826
827 template<typename T>
828 struct ArrayItemHelper<TypeBuilder::Array<T> > {
829 struct Traits {
830 static void pushRefPtr(JSONArray* array, PassRefPtr<TypeBuilder::Array<T > > value)
831 {
832 array->pushValue(value);
833 }
834
835 #if $validatorIfdefName
836 template<typename S>
837 static void assertCorrectValue(JSONValue* value) {
838 S::assertCorrectValue(value);
839 }
840 #endif // $validatorIfdefName
841 };
842 };
843
844 ${forwards}
845
846 String getEnumConstantValue(int code);
847
848 ${typeBuilders}
849 } // namespace TypeBuilder
850
851
852 } // namespace blink
853
854 #endif // !defined(InspectorTypeBuilder_h)
855
856 """)
857
858 typebuilder_cpp = (
859 """
860
861 #include "config.h"
862
863 #include "InspectorTypeBuilder.h"
864 #include "wtf/text/CString.h"
865
866 namespace blink {
867
868 namespace TypeBuilder {
869
870 const char* const enum_constant_values[] = {
871 $enumConstantValues};
872
873 String getEnumConstantValue(int code) {
874 return enum_constant_values[code];
875 }
876
877 } // namespace TypeBuilder
878
879 $implCode
880
881 #if $validatorIfdefName
882
883 void TypeBuilder::RuntimeCastHelper::assertAny(JSONValue*)
884 {
885 // No-op.
886 }
887
888
889 void TypeBuilder::RuntimeCastHelper::assertInt(JSONValue* value)
890 {
891 double v;
892 bool castRes = value->asNumber(&v);
893 ASSERT_UNUSED(castRes, castRes);
894 ASSERT(static_cast<double>(static_cast<int>(v)) == v);
895 }
896
897 $validatorCode
898
899 #endif // $validatorIfdefName
900
901 } // namespace blink
902
903 """)
904
905 param_container_access_code = """
906 RefPtr<JSONObject> paramsContainer = requestMessageObject->getObject("params ");
907 JSONObject* paramsContainerPtr = paramsContainer.get();
908 """
909
910 class_binding_builder_part_1 = (
911 """ AllFieldsSet = %s
912 };
913
914 template<int STATE>
915 class Builder {
916 private:
917 RefPtr<JSONObject> m_result;
918
919 template<int STEP> Builder<STATE | STEP>& castState()
920 {
921 return *reinterpret_cast<Builder<STATE | STEP>*>(this);
922 }
923
924 Builder(PassRefPtr</*%s*/JSONObject> ptr)
925 {
926 COMPILE_ASSERT(STATE == NoFieldsSet, builder_created_in_non_init_sta te);
927 m_result = ptr;
928 }
929 friend class %s;
930 public:
931 """)
932
933 class_binding_builder_part_2 = ("""
934 Builder<STATE | %s>& set%s(%s value)
935 {
936 COMPILE_ASSERT(!(STATE & %s), property_%s_already_set);
937 m_result->set%s("%s", %s);
938 return castState<%s>();
939 }
940 """)
941
942 class_binding_builder_part_3 = ("""
943 operator RefPtr<%s>& ()
944 {
945 COMPILE_ASSERT(STATE == AllFieldsSet, result_is_not_ready);
946 COMPILE_ASSERT(sizeof(%s) == sizeof(JSONObject), cannot_cast);
947 return *reinterpret_cast<RefPtr<%s>*>(&m_result);
948 }
949
950 PassRefPtr<%s> release()
951 {
952 return RefPtr<%s>(*this).release();
953 }
954 };
955
956 """)
957
958 class_binding_builder_part_4 = (
959 """ static Builder<NoFieldsSet> create()
960 {
961 return Builder<NoFieldsSet>(JSONObject::create());
962 }
963 """)
OLDNEW
« no previous file with comments | « sky/engine/core/inspector/CodeGeneratorInspector.py ('k') | sky/engine/core/inspector/Inspector-1.1.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698