| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a | 
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "platform/assert.h" | 5 #include "platform/assert.h" | 
| 6 | 6 | 
| 7 #include "include/dart_native_api.h" | 7 #include "include/dart_native_api.h" | 
| 8 #include "vm/dart_entry.h" | 8 #include "vm/dart_entry.h" | 
| 9 #include "vm/debugger.h" | 9 #include "vm/debugger.h" | 
| 10 #include "vm/json_stream.h" | 10 #include "vm/json_stream.h" | 
| 11 #include "vm/message.h" | 11 #include "vm/message.h" | 
| 12 #include "vm/metrics.h" | 12 #include "vm/metrics.h" | 
| 13 #include "vm/object.h" | 13 #include "vm/object.h" | 
| 14 #include "vm/safepoint.h" | 14 #include "vm/safepoint.h" | 
| 15 #include "vm/service.h" | 15 #include "vm/service.h" | 
| 16 #include "vm/service_event.h" | 16 #include "vm/service_event.h" | 
| 17 #include "vm/thread_registry.h" | 17 #include "vm/thread_registry.h" | 
| 18 #include "vm/timeline.h" | 18 #include "vm/timeline.h" | 
| 19 #include "vm/unicode.h" | 19 #include "vm/unicode.h" | 
| 20 | 20 | 
| 21 |  | 
| 22 namespace dart { | 21 namespace dart { | 
| 23 | 22 | 
| 24 #ifndef PRODUCT | 23 #ifndef PRODUCT | 
| 25 | 24 | 
| 26 class MaybeOnStackBuffer { | 25 class MaybeOnStackBuffer { | 
| 27  public: | 26  public: | 
| 28   explicit MaybeOnStackBuffer(intptr_t size) { | 27   explicit MaybeOnStackBuffer(intptr_t size) { | 
| 29     if (size > kOnStackBufferCapacity) { | 28     if (size > kOnStackBufferCapacity) { | 
| 30       p_ = reinterpret_cast<char*>(malloc(size)); | 29       p_ = reinterpret_cast<char*>(malloc(size)); | 
| 31     } else { | 30     } else { | 
| 32       p_ = &buffer_[0]; | 31       p_ = &buffer_[0]; | 
| 33     } | 32     } | 
| 34   } | 33   } | 
| 35   ~MaybeOnStackBuffer() { | 34   ~MaybeOnStackBuffer() { | 
| 36     if (p_ != &buffer_[0]) free(p_); | 35     if (p_ != &buffer_[0]) free(p_); | 
| 37   } | 36   } | 
| 38 | 37 | 
| 39   char* p() { return p_; } | 38   char* p() { return p_; } | 
| 40 | 39 | 
| 41  private: | 40  private: | 
| 42   static const intptr_t kOnStackBufferCapacity = 4096; | 41   static const intptr_t kOnStackBufferCapacity = 4096; | 
| 43   char* p_; | 42   char* p_; | 
| 44   char buffer_[kOnStackBufferCapacity]; | 43   char buffer_[kOnStackBufferCapacity]; | 
| 45 }; | 44 }; | 
| 46 | 45 | 
| 47 |  | 
| 48 void AppendJSONStreamConsumer(Dart_StreamConsumer_State state, | 46 void AppendJSONStreamConsumer(Dart_StreamConsumer_State state, | 
| 49                               const char* stream_name, | 47                               const char* stream_name, | 
| 50                               const uint8_t* buffer, | 48                               const uint8_t* buffer, | 
| 51                               intptr_t buffer_length, | 49                               intptr_t buffer_length, | 
| 52                               void* user_data) { | 50                               void* user_data) { | 
| 53   if ((state == Dart_StreamConsumer_kStart) || | 51   if ((state == Dart_StreamConsumer_kStart) || | 
| 54       (state == Dart_StreamConsumer_kFinish)) { | 52       (state == Dart_StreamConsumer_kFinish)) { | 
| 55     // Ignore. | 53     // Ignore. | 
| 56     return; | 54     return; | 
| 57   } | 55   } | 
| 58   ASSERT(state == Dart_StreamConsumer_kData); | 56   ASSERT(state == Dart_StreamConsumer_kData); | 
| 59   JSONStream* js = reinterpret_cast<JSONStream*>(user_data); | 57   JSONStream* js = reinterpret_cast<JSONStream*>(user_data); | 
| 60   ASSERT(js != NULL); | 58   ASSERT(js != NULL); | 
| 61   js->AppendSerializedObject(buffer, buffer_length); | 59   js->AppendSerializedObject(buffer, buffer_length); | 
| 62 } | 60 } | 
| 63 | 61 | 
| 64 |  | 
| 65 DECLARE_FLAG(bool, trace_service); | 62 DECLARE_FLAG(bool, trace_service); | 
| 66 | 63 | 
| 67 JSONStream::JSONStream(intptr_t buf_size) | 64 JSONStream::JSONStream(intptr_t buf_size) | 
| 68     : open_objects_(0), | 65     : open_objects_(0), | 
| 69       buffer_(buf_size), | 66       buffer_(buf_size), | 
| 70       default_id_zone_(), | 67       default_id_zone_(), | 
| 71       id_zone_(&default_id_zone_), | 68       id_zone_(&default_id_zone_), | 
| 72       reply_port_(ILLEGAL_PORT), | 69       reply_port_(ILLEGAL_PORT), | 
| 73       seq_(NULL), | 70       seq_(NULL), | 
| 74       parameter_keys_(NULL), | 71       parameter_keys_(NULL), | 
| 75       parameter_values_(NULL), | 72       parameter_values_(NULL), | 
| 76       method_(""), | 73       method_(""), | 
| 77       param_keys_(NULL), | 74       param_keys_(NULL), | 
| 78       param_values_(NULL), | 75       param_values_(NULL), | 
| 79       num_params_(0), | 76       num_params_(0), | 
| 80       offset_(0), | 77       offset_(0), | 
| 81       count_(-1) { | 78       count_(-1) { | 
| 82   ObjectIdRing* ring = NULL; | 79   ObjectIdRing* ring = NULL; | 
| 83   Isolate* isolate = Isolate::Current(); | 80   Isolate* isolate = Isolate::Current(); | 
| 84   if (isolate != NULL) { | 81   if (isolate != NULL) { | 
| 85     ring = isolate->object_id_ring(); | 82     ring = isolate->object_id_ring(); | 
| 86   } | 83   } | 
| 87   default_id_zone_.Init(ring, ObjectIdRing::kAllocateId); | 84   default_id_zone_.Init(ring, ObjectIdRing::kAllocateId); | 
| 88 } | 85 } | 
| 89 | 86 | 
| 90 |  | 
| 91 JSONStream::~JSONStream() {} | 87 JSONStream::~JSONStream() {} | 
| 92 | 88 | 
| 93 |  | 
| 94 void JSONStream::Setup(Zone* zone, | 89 void JSONStream::Setup(Zone* zone, | 
| 95                        Dart_Port reply_port, | 90                        Dart_Port reply_port, | 
| 96                        const Instance& seq, | 91                        const Instance& seq, | 
| 97                        const String& method, | 92                        const String& method, | 
| 98                        const Array& param_keys, | 93                        const Array& param_keys, | 
| 99                        const Array& param_values, | 94                        const Array& param_values, | 
| 100                        bool parameters_are_dart_objects) { | 95                        bool parameters_are_dart_objects) { | 
| 101   set_reply_port(reply_port); | 96   set_reply_port(reply_port); | 
| 102   seq_ = &Instance::ZoneHandle(seq.raw()); | 97   seq_ = &Instance::ZoneHandle(seq.raw()); | 
| 103   method_ = method.ToCString(); | 98   method_ = method.ToCString(); | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
| 128     Isolate* isolate = Isolate::Current(); | 123     Isolate* isolate = Isolate::Current(); | 
| 129     ASSERT(isolate != NULL); | 124     ASSERT(isolate != NULL); | 
| 130     const char* isolate_name = isolate->name(); | 125     const char* isolate_name = isolate->name(); | 
| 131     setup_time_micros_ = OS::GetCurrentTimeMicros(); | 126     setup_time_micros_ = OS::GetCurrentTimeMicros(); | 
| 132     OS::Print("[+%" Pd64 "ms] Isolate %s processing service request %s\n", | 127     OS::Print("[+%" Pd64 "ms] Isolate %s processing service request %s\n", | 
| 133               Dart::UptimeMillis(), isolate_name, method_); | 128               Dart::UptimeMillis(), isolate_name, method_); | 
| 134   } | 129   } | 
| 135   buffer_.Printf("{\"jsonrpc\":\"2.0\", \"result\":"); | 130   buffer_.Printf("{\"jsonrpc\":\"2.0\", \"result\":"); | 
| 136 } | 131 } | 
| 137 | 132 | 
| 138 |  | 
| 139 void JSONStream::SetupError() { | 133 void JSONStream::SetupError() { | 
| 140   buffer_.Clear(); | 134   buffer_.Clear(); | 
| 141   buffer_.Printf("{\"jsonrpc\":\"2.0\", \"error\":"); | 135   buffer_.Printf("{\"jsonrpc\":\"2.0\", \"error\":"); | 
| 142 } | 136 } | 
| 143 | 137 | 
| 144 |  | 
| 145 static const char* GetJSONRpcErrorMessage(intptr_t code) { | 138 static const char* GetJSONRpcErrorMessage(intptr_t code) { | 
| 146   switch (code) { | 139   switch (code) { | 
| 147     case kParseError: | 140     case kParseError: | 
| 148       return "Parse error"; | 141       return "Parse error"; | 
| 149     case kInvalidRequest: | 142     case kInvalidRequest: | 
| 150       return "Invalid Request"; | 143       return "Invalid Request"; | 
| 151     case kMethodNotFound: | 144     case kMethodNotFound: | 
| 152       return "Method not found"; | 145       return "Method not found"; | 
| 153     case kInvalidParams: | 146     case kInvalidParams: | 
| 154       return "Invalid params"; | 147       return "Invalid params"; | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 172       return "File system does not exist"; | 165       return "File system does not exist"; | 
| 173     case kFileDoesNotExist: | 166     case kFileDoesNotExist: | 
| 174       return "File does not exist"; | 167       return "File does not exist"; | 
| 175     case kIsolateReloadBarred: | 168     case kIsolateReloadBarred: | 
| 176       return "Isolate cannot be reloaded"; | 169       return "Isolate cannot be reloaded"; | 
| 177     default: | 170     default: | 
| 178       return "Extension error"; | 171       return "Extension error"; | 
| 179   } | 172   } | 
| 180 } | 173 } | 
| 181 | 174 | 
| 182 |  | 
| 183 static void PrintRequest(JSONObject* obj, JSONStream* js) { | 175 static void PrintRequest(JSONObject* obj, JSONStream* js) { | 
| 184   JSONObject jsobj(obj, "request"); | 176   JSONObject jsobj(obj, "request"); | 
| 185   jsobj.AddProperty("method", js->method()); | 177   jsobj.AddProperty("method", js->method()); | 
| 186   { | 178   { | 
| 187     JSONObject params(&jsobj, "params"); | 179     JSONObject params(&jsobj, "params"); | 
| 188     for (intptr_t i = 0; i < js->num_params(); i++) { | 180     for (intptr_t i = 0; i < js->num_params(); i++) { | 
| 189       params.AddProperty(js->GetParamKey(i), js->GetParamValue(i)); | 181       params.AddProperty(js->GetParamKey(i), js->GetParamValue(i)); | 
| 190     } | 182     } | 
| 191   } | 183   } | 
| 192 } | 184 } | 
| 193 | 185 | 
| 194 |  | 
| 195 void JSONStream::PrintError(intptr_t code, const char* details_format, ...) { | 186 void JSONStream::PrintError(intptr_t code, const char* details_format, ...) { | 
| 196   SetupError(); | 187   SetupError(); | 
| 197   JSONObject jsobj(this); | 188   JSONObject jsobj(this); | 
| 198   jsobj.AddProperty("code", code); | 189   jsobj.AddProperty("code", code); | 
| 199   jsobj.AddProperty("message", GetJSONRpcErrorMessage(code)); | 190   jsobj.AddProperty("message", GetJSONRpcErrorMessage(code)); | 
| 200   { | 191   { | 
| 201     JSONObject data(&jsobj, "data"); | 192     JSONObject data(&jsobj, "data"); | 
| 202     PrintRequest(&data, this); | 193     PrintRequest(&data, this); | 
| 203     if (details_format != NULL) { | 194     if (details_format != NULL) { | 
| 204       va_list args; | 195       va_list args; | 
| 205       va_start(args, details_format); | 196       va_start(args, details_format); | 
| 206       intptr_t len = OS::VSNPrint(NULL, 0, details_format, args); | 197       intptr_t len = OS::VSNPrint(NULL, 0, details_format, args); | 
| 207       va_end(args); | 198       va_end(args); | 
| 208 | 199 | 
| 209       char* buffer = Thread::Current()->zone()->Alloc<char>(len + 1); | 200       char* buffer = Thread::Current()->zone()->Alloc<char>(len + 1); | 
| 210       va_list args2; | 201       va_list args2; | 
| 211       va_start(args2, details_format); | 202       va_start(args2, details_format); | 
| 212       OS::VSNPrint(buffer, (len + 1), details_format, args2); | 203       OS::VSNPrint(buffer, (len + 1), details_format, args2); | 
| 213       va_end(args2); | 204       va_end(args2); | 
| 214 | 205 | 
| 215       data.AddProperty("details", buffer); | 206       data.AddProperty("details", buffer); | 
| 216     } | 207     } | 
| 217   } | 208   } | 
| 218 } | 209 } | 
| 219 | 210 | 
| 220 |  | 
| 221 void JSONStream::PostNullReply(Dart_Port port) { | 211 void JSONStream::PostNullReply(Dart_Port port) { | 
| 222   PortMap::PostMessage( | 212   PortMap::PostMessage( | 
| 223       new Message(port, Object::null(), Message::kNormalPriority)); | 213       new Message(port, Object::null(), Message::kNormalPriority)); | 
| 224 } | 214 } | 
| 225 | 215 | 
| 226 |  | 
| 227 static void Finalizer(void* isolate_callback_data, | 216 static void Finalizer(void* isolate_callback_data, | 
| 228                       Dart_WeakPersistentHandle handle, | 217                       Dart_WeakPersistentHandle handle, | 
| 229                       void* buffer) { | 218                       void* buffer) { | 
| 230   free(buffer); | 219   free(buffer); | 
| 231 } | 220 } | 
| 232 | 221 | 
| 233 |  | 
| 234 void JSONStream::PostReply() { | 222 void JSONStream::PostReply() { | 
| 235   ASSERT(seq_ != NULL); | 223   ASSERT(seq_ != NULL); | 
| 236   Dart_Port port = reply_port(); | 224   Dart_Port port = reply_port(); | 
| 237   set_reply_port(ILLEGAL_PORT);  // Prevent double replies. | 225   set_reply_port(ILLEGAL_PORT);  // Prevent double replies. | 
| 238   if (seq_->IsString()) { | 226   if (seq_->IsString()) { | 
| 239     const String& str = String::Cast(*seq_); | 227     const String& str = String::Cast(*seq_); | 
| 240     PrintProperty("id", str.ToCString()); | 228     PrintProperty("id", str.ToCString()); | 
| 241   } else if (seq_->IsInteger()) { | 229   } else if (seq_->IsInteger()) { | 
| 242     const Integer& integer = Integer::Cast(*seq_); | 230     const Integer& integer = Integer::Cast(*seq_); | 
| 243     PrintProperty64("id", integer.AsInt64Value()); | 231     PrintProperty64("id", integer.AsInt64Value()); | 
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 300                 Dart::UptimeMillis(), isolate_name, method_, total_time); | 288                 Dart::UptimeMillis(), isolate_name, method_, total_time); | 
| 301     } else { | 289     } else { | 
| 302       OS::Print("[+%" Pd64 | 290       OS::Print("[+%" Pd64 | 
| 303                 "ms] Isolate %s processed service request %s " | 291                 "ms] Isolate %s processed service request %s " | 
| 304                 "(%" Pd64 "us) FAILED\n", | 292                 "(%" Pd64 "us) FAILED\n", | 
| 305                 Dart::UptimeMillis(), isolate_name, method_, total_time); | 293                 Dart::UptimeMillis(), isolate_name, method_, total_time); | 
| 306     } | 294     } | 
| 307   } | 295   } | 
| 308 } | 296 } | 
| 309 | 297 | 
| 310 |  | 
| 311 const char* JSONStream::LookupParam(const char* key) const { | 298 const char* JSONStream::LookupParam(const char* key) const { | 
| 312   for (int i = 0; i < num_params(); i++) { | 299   for (int i = 0; i < num_params(); i++) { | 
| 313     if (!strcmp(key, param_keys_[i])) { | 300     if (!strcmp(key, param_keys_[i])) { | 
| 314       return param_values_[i]; | 301       return param_values_[i]; | 
| 315     } | 302     } | 
| 316   } | 303   } | 
| 317   return NULL; | 304   return NULL; | 
| 318 } | 305 } | 
| 319 | 306 | 
| 320 |  | 
| 321 bool JSONStream::HasParam(const char* key) const { | 307 bool JSONStream::HasParam(const char* key) const { | 
| 322   ASSERT(key); | 308   ASSERT(key); | 
| 323   return LookupParam(key) != NULL; | 309   return LookupParam(key) != NULL; | 
| 324 } | 310 } | 
| 325 | 311 | 
| 326 |  | 
| 327 bool JSONStream::ParamIs(const char* key, const char* value) const { | 312 bool JSONStream::ParamIs(const char* key, const char* value) const { | 
| 328   ASSERT(key); | 313   ASSERT(key); | 
| 329   ASSERT(value); | 314   ASSERT(value); | 
| 330   const char* key_value = LookupParam(key); | 315   const char* key_value = LookupParam(key); | 
| 331   return (key_value != NULL) && (strcmp(key_value, value) == 0); | 316   return (key_value != NULL) && (strcmp(key_value, value) == 0); | 
| 332 } | 317 } | 
| 333 | 318 | 
| 334 |  | 
| 335 void JSONStream::ComputeOffsetAndCount(intptr_t length, | 319 void JSONStream::ComputeOffsetAndCount(intptr_t length, | 
| 336                                        intptr_t* offset, | 320                                        intptr_t* offset, | 
| 337                                        intptr_t* count) { | 321                                        intptr_t* count) { | 
| 338   // This function is written to avoid adding (count + offset) in case | 322   // This function is written to avoid adding (count + offset) in case | 
| 339   // that triggers an integer overflow. | 323   // that triggers an integer overflow. | 
| 340   *offset = offset_; | 324   *offset = offset_; | 
| 341   if (*offset > length) { | 325   if (*offset > length) { | 
| 342     *offset = length; | 326     *offset = length; | 
| 343   } | 327   } | 
| 344   intptr_t remaining = length - *offset; | 328   intptr_t remaining = length - *offset; | 
| 345   *count = count_; | 329   *count = count_; | 
| 346   if (*count < 0 || *count > remaining) { | 330   if (*count < 0 || *count > remaining) { | 
| 347     *count = remaining; | 331     *count = remaining; | 
| 348   } | 332   } | 
| 349 } | 333 } | 
| 350 | 334 | 
| 351 |  | 
| 352 void JSONStream::AppendSerializedObject(const char* serialized_object) { | 335 void JSONStream::AppendSerializedObject(const char* serialized_object) { | 
| 353   PrintCommaIfNeeded(); | 336   PrintCommaIfNeeded(); | 
| 354   buffer_.AddString(serialized_object); | 337   buffer_.AddString(serialized_object); | 
| 355 } | 338 } | 
| 356 | 339 | 
| 357 |  | 
| 358 void JSONStream::AppendSerializedObject(const uint8_t* buffer, | 340 void JSONStream::AppendSerializedObject(const uint8_t* buffer, | 
| 359                                         intptr_t buffer_length) { | 341                                         intptr_t buffer_length) { | 
| 360   buffer_.AddRaw(buffer, buffer_length); | 342   buffer_.AddRaw(buffer, buffer_length); | 
| 361 } | 343 } | 
| 362 | 344 | 
| 363 void JSONStream::AppendSerializedObject(const char* property_name, | 345 void JSONStream::AppendSerializedObject(const char* property_name, | 
| 364                                         const char* serialized_object) { | 346                                         const char* serialized_object) { | 
| 365   PrintCommaIfNeeded(); | 347   PrintCommaIfNeeded(); | 
| 366   PrintPropertyName(property_name); | 348   PrintPropertyName(property_name); | 
| 367   buffer_.AddString(serialized_object); | 349   buffer_.AddString(serialized_object); | 
| 368 } | 350 } | 
| 369 | 351 | 
| 370 void JSONStream::Clear() { | 352 void JSONStream::Clear() { | 
| 371   buffer_.Clear(); | 353   buffer_.Clear(); | 
| 372   open_objects_ = 0; | 354   open_objects_ = 0; | 
| 373 } | 355 } | 
| 374 | 356 | 
| 375 |  | 
| 376 void JSONStream::OpenObject(const char* property_name) { | 357 void JSONStream::OpenObject(const char* property_name) { | 
| 377   PrintCommaIfNeeded(); | 358   PrintCommaIfNeeded(); | 
| 378   open_objects_++; | 359   open_objects_++; | 
| 379   if (property_name != NULL) { | 360   if (property_name != NULL) { | 
| 380     PrintPropertyName(property_name); | 361     PrintPropertyName(property_name); | 
| 381   } | 362   } | 
| 382   buffer_.AddChar('{'); | 363   buffer_.AddChar('{'); | 
| 383 } | 364 } | 
| 384 | 365 | 
| 385 |  | 
| 386 void JSONStream::CloseObject() { | 366 void JSONStream::CloseObject() { | 
| 387   ASSERT(open_objects_ > 0); | 367   ASSERT(open_objects_ > 0); | 
| 388   open_objects_--; | 368   open_objects_--; | 
| 389   buffer_.AddChar('}'); | 369   buffer_.AddChar('}'); | 
| 390 } | 370 } | 
| 391 | 371 | 
| 392 |  | 
| 393 void JSONStream::OpenArray(const char* property_name) { | 372 void JSONStream::OpenArray(const char* property_name) { | 
| 394   PrintCommaIfNeeded(); | 373   PrintCommaIfNeeded(); | 
| 395   if (property_name != NULL) { | 374   if (property_name != NULL) { | 
| 396     PrintPropertyName(property_name); | 375     PrintPropertyName(property_name); | 
| 397   } | 376   } | 
| 398   open_objects_++; | 377   open_objects_++; | 
| 399   buffer_.AddChar('['); | 378   buffer_.AddChar('['); | 
| 400 } | 379 } | 
| 401 | 380 | 
| 402 |  | 
| 403 void JSONStream::CloseArray() { | 381 void JSONStream::CloseArray() { | 
| 404   ASSERT(open_objects_ > 0); | 382   ASSERT(open_objects_ > 0); | 
| 405   open_objects_--; | 383   open_objects_--; | 
| 406   buffer_.AddChar(']'); | 384   buffer_.AddChar(']'); | 
| 407 } | 385 } | 
| 408 | 386 | 
| 409 |  | 
| 410 void JSONStream::PrintValueNull() { | 387 void JSONStream::PrintValueNull() { | 
| 411   PrintCommaIfNeeded(); | 388   PrintCommaIfNeeded(); | 
| 412   buffer_.Printf("null"); | 389   buffer_.Printf("null"); | 
| 413 } | 390 } | 
| 414 | 391 | 
| 415 void JSONStream::PrintValueBool(bool b) { | 392 void JSONStream::PrintValueBool(bool b) { | 
| 416   PrintCommaIfNeeded(); | 393   PrintCommaIfNeeded(); | 
| 417   buffer_.Printf("%s", b ? "true" : "false"); | 394   buffer_.Printf("%s", b ? "true" : "false"); | 
| 418 } | 395 } | 
| 419 | 396 | 
| 420 |  | 
| 421 void JSONStream::PrintValue(intptr_t i) { | 397 void JSONStream::PrintValue(intptr_t i) { | 
| 422   EnsureIntegerIsRepresentableInJavaScript(static_cast<int64_t>(i)); | 398   EnsureIntegerIsRepresentableInJavaScript(static_cast<int64_t>(i)); | 
| 423   PrintCommaIfNeeded(); | 399   PrintCommaIfNeeded(); | 
| 424   buffer_.Printf("%" Pd "", i); | 400   buffer_.Printf("%" Pd "", i); | 
| 425 } | 401 } | 
| 426 | 402 | 
| 427 |  | 
| 428 void JSONStream::PrintValue64(int64_t i) { | 403 void JSONStream::PrintValue64(int64_t i) { | 
| 429   EnsureIntegerIsRepresentableInJavaScript(i); | 404   EnsureIntegerIsRepresentableInJavaScript(i); | 
| 430   PrintCommaIfNeeded(); | 405   PrintCommaIfNeeded(); | 
| 431   buffer_.Printf("%" Pd64 "", i); | 406   buffer_.Printf("%" Pd64 "", i); | 
| 432 } | 407 } | 
| 433 | 408 | 
| 434 |  | 
| 435 void JSONStream::PrintValueTimeMillis(int64_t millis) { | 409 void JSONStream::PrintValueTimeMillis(int64_t millis) { | 
| 436   EnsureIntegerIsRepresentableInJavaScript(millis); | 410   EnsureIntegerIsRepresentableInJavaScript(millis); | 
| 437   PrintValue64(millis); | 411   PrintValue64(millis); | 
| 438 } | 412 } | 
| 439 | 413 | 
| 440 |  | 
| 441 void JSONStream::PrintValueTimeMicros(int64_t micros) { | 414 void JSONStream::PrintValueTimeMicros(int64_t micros) { | 
| 442   EnsureIntegerIsRepresentableInJavaScript(micros); | 415   EnsureIntegerIsRepresentableInJavaScript(micros); | 
| 443   PrintValue64(micros); | 416   PrintValue64(micros); | 
| 444 } | 417 } | 
| 445 | 418 | 
| 446 |  | 
| 447 void JSONStream::PrintValue(double d) { | 419 void JSONStream::PrintValue(double d) { | 
| 448   PrintCommaIfNeeded(); | 420   PrintCommaIfNeeded(); | 
| 449   buffer_.Printf("%f", d); | 421   buffer_.Printf("%f", d); | 
| 450 } | 422 } | 
| 451 | 423 | 
| 452 |  | 
| 453 static const char base64_digits[65] = | 424 static const char base64_digits[65] = | 
| 454     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 425     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 
| 455 static const char base64_pad = '='; | 426 static const char base64_pad = '='; | 
| 456 | 427 | 
| 457 |  | 
| 458 void JSONStream::PrintValueBase64(const uint8_t* bytes, intptr_t length) { | 428 void JSONStream::PrintValueBase64(const uint8_t* bytes, intptr_t length) { | 
| 459   PrintCommaIfNeeded(); | 429   PrintCommaIfNeeded(); | 
| 460   buffer_.AddChar('"'); | 430   buffer_.AddChar('"'); | 
| 461 | 431 | 
| 462   intptr_t odd_bits = length % 3; | 432   intptr_t odd_bits = length % 3; | 
| 463   intptr_t even_bits = length - odd_bits; | 433   intptr_t even_bits = length - odd_bits; | 
| 464   for (intptr_t i = 0; i < even_bits; i += 3) { | 434   for (intptr_t i = 0; i < even_bits; i += 3) { | 
| 465     intptr_t triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; | 435     intptr_t triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; | 
| 466     buffer_.AddChar(base64_digits[triplet >> 18]); | 436     buffer_.AddChar(base64_digits[triplet >> 18]); | 
| 467     buffer_.AddChar(base64_digits[(triplet >> 12) & 63]); | 437     buffer_.AddChar(base64_digits[(triplet >> 12) & 63]); | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 478     intptr_t triplet = (bytes[even_bits] << 16) | (bytes[even_bits + 1] << 8); | 448     intptr_t triplet = (bytes[even_bits] << 16) | (bytes[even_bits + 1] << 8); | 
| 479     buffer_.AddChar(base64_digits[triplet >> 18]); | 449     buffer_.AddChar(base64_digits[triplet >> 18]); | 
| 480     buffer_.AddChar(base64_digits[(triplet >> 12) & 63]); | 450     buffer_.AddChar(base64_digits[(triplet >> 12) & 63]); | 
| 481     buffer_.AddChar(base64_digits[(triplet >> 6) & 63]); | 451     buffer_.AddChar(base64_digits[(triplet >> 6) & 63]); | 
| 482     buffer_.AddChar(base64_pad); | 452     buffer_.AddChar(base64_pad); | 
| 483   } | 453   } | 
| 484 | 454 | 
| 485   buffer_.AddChar('"'); | 455   buffer_.AddChar('"'); | 
| 486 } | 456 } | 
| 487 | 457 | 
| 488 |  | 
| 489 void JSONStream::PrintValue(const char* s) { | 458 void JSONStream::PrintValue(const char* s) { | 
| 490   PrintCommaIfNeeded(); | 459   PrintCommaIfNeeded(); | 
| 491   buffer_.AddChar('"'); | 460   buffer_.AddChar('"'); | 
| 492   AddEscapedUTF8String(s); | 461   AddEscapedUTF8String(s); | 
| 493   buffer_.AddChar('"'); | 462   buffer_.AddChar('"'); | 
| 494 } | 463 } | 
| 495 | 464 | 
| 496 |  | 
| 497 bool JSONStream::PrintValueStr(const String& s, | 465 bool JSONStream::PrintValueStr(const String& s, | 
| 498                                intptr_t offset, | 466                                intptr_t offset, | 
| 499                                intptr_t count) { | 467                                intptr_t count) { | 
| 500   PrintCommaIfNeeded(); | 468   PrintCommaIfNeeded(); | 
| 501   buffer_.AddChar('"'); | 469   buffer_.AddChar('"'); | 
| 502   bool did_truncate = AddDartString(s, offset, count); | 470   bool did_truncate = AddDartString(s, offset, count); | 
| 503   buffer_.AddChar('"'); | 471   buffer_.AddChar('"'); | 
| 504   return did_truncate; | 472   return did_truncate; | 
| 505 } | 473 } | 
| 506 | 474 | 
| 507 |  | 
| 508 void JSONStream::PrintValueNoEscape(const char* s) { | 475 void JSONStream::PrintValueNoEscape(const char* s) { | 
| 509   PrintCommaIfNeeded(); | 476   PrintCommaIfNeeded(); | 
| 510   buffer_.Printf("%s", s); | 477   buffer_.Printf("%s", s); | 
| 511 } | 478 } | 
| 512 | 479 | 
| 513 |  | 
| 514 void JSONStream::PrintfValue(const char* format, ...) { | 480 void JSONStream::PrintfValue(const char* format, ...) { | 
| 515   PrintCommaIfNeeded(); | 481   PrintCommaIfNeeded(); | 
| 516 | 482 | 
| 517   va_list args; | 483   va_list args; | 
| 518   va_start(args, format); | 484   va_start(args, format); | 
| 519   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 485   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 
| 520   va_end(args); | 486   va_end(args); | 
| 521   MaybeOnStackBuffer mosb(len + 1); | 487   MaybeOnStackBuffer mosb(len + 1); | 
| 522   char* p = mosb.p(); | 488   char* p = mosb.p(); | 
| 523   va_start(args, format); | 489   va_start(args, format); | 
| 524   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 490   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 
| 525   va_end(args); | 491   va_end(args); | 
| 526   ASSERT(len == len2); | 492   ASSERT(len == len2); | 
| 527   buffer_.AddChar('"'); | 493   buffer_.AddChar('"'); | 
| 528   AddEscapedUTF8String(p, len); | 494   AddEscapedUTF8String(p, len); | 
| 529   buffer_.AddChar('"'); | 495   buffer_.AddChar('"'); | 
| 530 } | 496 } | 
| 531 | 497 | 
| 532 |  | 
| 533 void JSONStream::PrintValue(const Object& o, bool ref) { | 498 void JSONStream::PrintValue(const Object& o, bool ref) { | 
| 534   PrintCommaIfNeeded(); | 499   PrintCommaIfNeeded(); | 
| 535   o.PrintJSON(this, ref); | 500   o.PrintJSON(this, ref); | 
| 536 } | 501 } | 
| 537 | 502 | 
| 538 |  | 
| 539 void JSONStream::PrintValue(Breakpoint* bpt) { | 503 void JSONStream::PrintValue(Breakpoint* bpt) { | 
| 540   PrintCommaIfNeeded(); | 504   PrintCommaIfNeeded(); | 
| 541   bpt->PrintJSON(this); | 505   bpt->PrintJSON(this); | 
| 542 } | 506 } | 
| 543 | 507 | 
| 544 |  | 
| 545 void JSONStream::PrintValue(TokenPosition tp) { | 508 void JSONStream::PrintValue(TokenPosition tp) { | 
| 546   PrintCommaIfNeeded(); | 509   PrintCommaIfNeeded(); | 
| 547   PrintValue(tp.value()); | 510   PrintValue(tp.value()); | 
| 548 } | 511 } | 
| 549 | 512 | 
| 550 |  | 
| 551 void JSONStream::PrintValue(const ServiceEvent* event) { | 513 void JSONStream::PrintValue(const ServiceEvent* event) { | 
| 552   PrintCommaIfNeeded(); | 514   PrintCommaIfNeeded(); | 
| 553   event->PrintJSON(this); | 515   event->PrintJSON(this); | 
| 554 } | 516 } | 
| 555 | 517 | 
| 556 |  | 
| 557 void JSONStream::PrintValue(Metric* metric) { | 518 void JSONStream::PrintValue(Metric* metric) { | 
| 558   PrintCommaIfNeeded(); | 519   PrintCommaIfNeeded(); | 
| 559   metric->PrintJSON(this); | 520   metric->PrintJSON(this); | 
| 560 } | 521 } | 
| 561 | 522 | 
| 562 |  | 
| 563 void JSONStream::PrintValue(MessageQueue* queue) { | 523 void JSONStream::PrintValue(MessageQueue* queue) { | 
| 564   PrintCommaIfNeeded(); | 524   PrintCommaIfNeeded(); | 
| 565   queue->PrintJSON(this); | 525   queue->PrintJSON(this); | 
| 566 } | 526 } | 
| 567 | 527 | 
| 568 |  | 
| 569 void JSONStream::PrintValue(Isolate* isolate, bool ref) { | 528 void JSONStream::PrintValue(Isolate* isolate, bool ref) { | 
| 570   PrintCommaIfNeeded(); | 529   PrintCommaIfNeeded(); | 
| 571   isolate->PrintJSON(this, ref); | 530   isolate->PrintJSON(this, ref); | 
| 572 } | 531 } | 
| 573 | 532 | 
| 574 |  | 
| 575 void JSONStream::PrintValue(ThreadRegistry* reg) { | 533 void JSONStream::PrintValue(ThreadRegistry* reg) { | 
| 576   PrintCommaIfNeeded(); | 534   PrintCommaIfNeeded(); | 
| 577   reg->PrintJSON(this); | 535   reg->PrintJSON(this); | 
| 578 } | 536 } | 
| 579 | 537 | 
| 580 |  | 
| 581 void JSONStream::PrintValue(Thread* thread) { | 538 void JSONStream::PrintValue(Thread* thread) { | 
| 582   PrintCommaIfNeeded(); | 539   PrintCommaIfNeeded(); | 
| 583   thread->PrintJSON(this); | 540   thread->PrintJSON(this); | 
| 584 } | 541 } | 
| 585 | 542 | 
| 586 |  | 
| 587 void JSONStream::PrintValue(const TimelineEvent* timeline_event) { | 543 void JSONStream::PrintValue(const TimelineEvent* timeline_event) { | 
| 588   PrintCommaIfNeeded(); | 544   PrintCommaIfNeeded(); | 
| 589   timeline_event->PrintJSON(this); | 545   timeline_event->PrintJSON(this); | 
| 590 } | 546 } | 
| 591 | 547 | 
| 592 |  | 
| 593 void JSONStream::PrintValue(const TimelineEventBlock* timeline_event_block) { | 548 void JSONStream::PrintValue(const TimelineEventBlock* timeline_event_block) { | 
| 594   PrintCommaIfNeeded(); | 549   PrintCommaIfNeeded(); | 
| 595   timeline_event_block->PrintJSON(this); | 550   timeline_event_block->PrintJSON(this); | 
| 596 } | 551 } | 
| 597 | 552 | 
| 598 |  | 
| 599 void JSONStream::PrintValueVM(bool ref) { | 553 void JSONStream::PrintValueVM(bool ref) { | 
| 600   PrintCommaIfNeeded(); | 554   PrintCommaIfNeeded(); | 
| 601   Service::PrintJSONForVM(this, ref); | 555   Service::PrintJSONForVM(this, ref); | 
| 602 } | 556 } | 
| 603 | 557 | 
| 604 |  | 
| 605 void JSONStream::PrintServiceId(const Object& o) { | 558 void JSONStream::PrintServiceId(const Object& o) { | 
| 606   ASSERT(id_zone_ != NULL); | 559   ASSERT(id_zone_ != NULL); | 
| 607   PrintProperty("id", id_zone_->GetServiceId(o)); | 560   PrintProperty("id", id_zone_->GetServiceId(o)); | 
| 608 } | 561 } | 
| 609 | 562 | 
| 610 |  | 
| 611 void JSONStream::PrintPropertyBool(const char* name, bool b) { | 563 void JSONStream::PrintPropertyBool(const char* name, bool b) { | 
| 612   PrintPropertyName(name); | 564   PrintPropertyName(name); | 
| 613   PrintValueBool(b); | 565   PrintValueBool(b); | 
| 614 } | 566 } | 
| 615 | 567 | 
| 616 |  | 
| 617 void JSONStream::PrintProperty(const char* name, intptr_t i) { | 568 void JSONStream::PrintProperty(const char* name, intptr_t i) { | 
| 618   PrintPropertyName(name); | 569   PrintPropertyName(name); | 
| 619   PrintValue(i); | 570   PrintValue(i); | 
| 620 } | 571 } | 
| 621 | 572 | 
| 622 |  | 
| 623 void JSONStream::PrintProperty64(const char* name, int64_t i) { | 573 void JSONStream::PrintProperty64(const char* name, int64_t i) { | 
| 624   PrintPropertyName(name); | 574   PrintPropertyName(name); | 
| 625   PrintValue64(i); | 575   PrintValue64(i); | 
| 626 } | 576 } | 
| 627 | 577 | 
| 628 |  | 
| 629 void JSONStream::PrintPropertyTimeMillis(const char* name, int64_t millis) { | 578 void JSONStream::PrintPropertyTimeMillis(const char* name, int64_t millis) { | 
| 630   PrintProperty64(name, millis); | 579   PrintProperty64(name, millis); | 
| 631 } | 580 } | 
| 632 | 581 | 
| 633 |  | 
| 634 void JSONStream::PrintPropertyTimeMicros(const char* name, int64_t micros) { | 582 void JSONStream::PrintPropertyTimeMicros(const char* name, int64_t micros) { | 
| 635   PrintProperty64(name, micros); | 583   PrintProperty64(name, micros); | 
| 636 } | 584 } | 
| 637 | 585 | 
| 638 |  | 
| 639 void JSONStream::PrintProperty(const char* name, double d) { | 586 void JSONStream::PrintProperty(const char* name, double d) { | 
| 640   PrintPropertyName(name); | 587   PrintPropertyName(name); | 
| 641   PrintValue(d); | 588   PrintValue(d); | 
| 642 } | 589 } | 
| 643 | 590 | 
| 644 |  | 
| 645 void JSONStream::PrintProperty(const char* name, const char* s) { | 591 void JSONStream::PrintProperty(const char* name, const char* s) { | 
| 646   PrintPropertyName(name); | 592   PrintPropertyName(name); | 
| 647   PrintValue(s); | 593   PrintValue(s); | 
| 648 } | 594 } | 
| 649 | 595 | 
| 650 |  | 
| 651 void JSONStream::PrintPropertyBase64(const char* name, | 596 void JSONStream::PrintPropertyBase64(const char* name, | 
| 652                                      const uint8_t* b, | 597                                      const uint8_t* b, | 
| 653                                      intptr_t len) { | 598                                      intptr_t len) { | 
| 654   PrintPropertyName(name); | 599   PrintPropertyName(name); | 
| 655   PrintValueBase64(b, len); | 600   PrintValueBase64(b, len); | 
| 656 } | 601 } | 
| 657 | 602 | 
| 658 |  | 
| 659 bool JSONStream::PrintPropertyStr(const char* name, | 603 bool JSONStream::PrintPropertyStr(const char* name, | 
| 660                                   const String& s, | 604                                   const String& s, | 
| 661                                   intptr_t offset, | 605                                   intptr_t offset, | 
| 662                                   intptr_t count) { | 606                                   intptr_t count) { | 
| 663   PrintPropertyName(name); | 607   PrintPropertyName(name); | 
| 664   return PrintValueStr(s, offset, count); | 608   return PrintValueStr(s, offset, count); | 
| 665 } | 609 } | 
| 666 | 610 | 
| 667 |  | 
| 668 void JSONStream::PrintPropertyNoEscape(const char* name, const char* s) { | 611 void JSONStream::PrintPropertyNoEscape(const char* name, const char* s) { | 
| 669   PrintPropertyName(name); | 612   PrintPropertyName(name); | 
| 670   PrintValueNoEscape(s); | 613   PrintValueNoEscape(s); | 
| 671 } | 614 } | 
| 672 | 615 | 
| 673 |  | 
| 674 void JSONStream::PrintProperty(const char* name, const ServiceEvent* event) { | 616 void JSONStream::PrintProperty(const char* name, const ServiceEvent* event) { | 
| 675   PrintPropertyName(name); | 617   PrintPropertyName(name); | 
| 676   PrintValue(event); | 618   PrintValue(event); | 
| 677 } | 619 } | 
| 678 | 620 | 
| 679 |  | 
| 680 void JSONStream::PrintProperty(const char* name, Breakpoint* bpt) { | 621 void JSONStream::PrintProperty(const char* name, Breakpoint* bpt) { | 
| 681   PrintPropertyName(name); | 622   PrintPropertyName(name); | 
| 682   PrintValue(bpt); | 623   PrintValue(bpt); | 
| 683 } | 624 } | 
| 684 | 625 | 
| 685 |  | 
| 686 void JSONStream::PrintProperty(const char* name, TokenPosition tp) { | 626 void JSONStream::PrintProperty(const char* name, TokenPosition tp) { | 
| 687   PrintPropertyName(name); | 627   PrintPropertyName(name); | 
| 688   PrintValue(tp); | 628   PrintValue(tp); | 
| 689 } | 629 } | 
| 690 | 630 | 
| 691 |  | 
| 692 void JSONStream::PrintProperty(const char* name, Metric* metric) { | 631 void JSONStream::PrintProperty(const char* name, Metric* metric) { | 
| 693   PrintPropertyName(name); | 632   PrintPropertyName(name); | 
| 694   PrintValue(metric); | 633   PrintValue(metric); | 
| 695 } | 634 } | 
| 696 | 635 | 
| 697 |  | 
| 698 void JSONStream::PrintProperty(const char* name, MessageQueue* queue) { | 636 void JSONStream::PrintProperty(const char* name, MessageQueue* queue) { | 
| 699   PrintPropertyName(name); | 637   PrintPropertyName(name); | 
| 700   PrintValue(queue); | 638   PrintValue(queue); | 
| 701 } | 639 } | 
| 702 | 640 | 
| 703 |  | 
| 704 void JSONStream::PrintProperty(const char* name, Isolate* isolate) { | 641 void JSONStream::PrintProperty(const char* name, Isolate* isolate) { | 
| 705   PrintPropertyName(name); | 642   PrintPropertyName(name); | 
| 706   PrintValue(isolate); | 643   PrintValue(isolate); | 
| 707 } | 644 } | 
| 708 | 645 | 
| 709 |  | 
| 710 void JSONStream::PrintProperty(const char* name, ThreadRegistry* reg) { | 646 void JSONStream::PrintProperty(const char* name, ThreadRegistry* reg) { | 
| 711   PrintPropertyName(name); | 647   PrintPropertyName(name); | 
| 712   PrintValue(reg); | 648   PrintValue(reg); | 
| 713 } | 649 } | 
| 714 | 650 | 
| 715 |  | 
| 716 void JSONStream::PrintProperty(const char* name, Thread* thread) { | 651 void JSONStream::PrintProperty(const char* name, Thread* thread) { | 
| 717   PrintPropertyName(name); | 652   PrintPropertyName(name); | 
| 718   PrintValue(thread); | 653   PrintValue(thread); | 
| 719 } | 654 } | 
| 720 | 655 | 
| 721 |  | 
| 722 void JSONStream::PrintProperty(const char* name, | 656 void JSONStream::PrintProperty(const char* name, | 
| 723                                const TimelineEvent* timeline_event) { | 657                                const TimelineEvent* timeline_event) { | 
| 724   PrintPropertyName(name); | 658   PrintPropertyName(name); | 
| 725   PrintValue(timeline_event); | 659   PrintValue(timeline_event); | 
| 726 } | 660 } | 
| 727 | 661 | 
| 728 |  | 
| 729 void JSONStream::PrintProperty(const char* name, | 662 void JSONStream::PrintProperty(const char* name, | 
| 730                                const TimelineEventBlock* timeline_event_block) { | 663                                const TimelineEventBlock* timeline_event_block) { | 
| 731   PrintPropertyName(name); | 664   PrintPropertyName(name); | 
| 732   PrintValue(timeline_event_block); | 665   PrintValue(timeline_event_block); | 
| 733 } | 666 } | 
| 734 | 667 | 
| 735 |  | 
| 736 void JSONStream::PrintfProperty(const char* name, const char* format, ...) { | 668 void JSONStream::PrintfProperty(const char* name, const char* format, ...) { | 
| 737   PrintPropertyName(name); | 669   PrintPropertyName(name); | 
| 738   va_list args; | 670   va_list args; | 
| 739   va_start(args, format); | 671   va_start(args, format); | 
| 740   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 672   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 
| 741   va_end(args); | 673   va_end(args); | 
| 742   MaybeOnStackBuffer mosb(len + 1); | 674   MaybeOnStackBuffer mosb(len + 1); | 
| 743   char* p = mosb.p(); | 675   char* p = mosb.p(); | 
| 744   va_start(args, format); | 676   va_start(args, format); | 
| 745   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 677   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 
| 746   va_end(args); | 678   va_end(args); | 
| 747   ASSERT(len == len2); | 679   ASSERT(len == len2); | 
| 748   buffer_.AddChar('"'); | 680   buffer_.AddChar('"'); | 
| 749   AddEscapedUTF8String(p, len); | 681   AddEscapedUTF8String(p, len); | 
| 750   buffer_.AddChar('"'); | 682   buffer_.AddChar('"'); | 
| 751 } | 683 } | 
| 752 | 684 | 
| 753 |  | 
| 754 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) { | 685 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) { | 
| 755   ASSERT(buffer != NULL); | 686   ASSERT(buffer != NULL); | 
| 756   ASSERT(buffer_length != NULL); | 687   ASSERT(buffer_length != NULL); | 
| 757   *buffer_length = buffer_.length(); | 688   *buffer_length = buffer_.length(); | 
| 758   *buffer = buffer_.Steal(); | 689   *buffer = buffer_.Steal(); | 
| 759 } | 690 } | 
| 760 | 691 | 
| 761 |  | 
| 762 void JSONStream::set_reply_port(Dart_Port port) { | 692 void JSONStream::set_reply_port(Dart_Port port) { | 
| 763   reply_port_ = port; | 693   reply_port_ = port; | 
| 764 } | 694 } | 
| 765 | 695 | 
| 766 |  | 
| 767 intptr_t JSONStream::NumObjectParameters() const { | 696 intptr_t JSONStream::NumObjectParameters() const { | 
| 768   if (parameter_keys_ == NULL) { | 697   if (parameter_keys_ == NULL) { | 
| 769     return 0; | 698     return 0; | 
| 770   } | 699   } | 
| 771   ASSERT(parameter_keys_ != NULL); | 700   ASSERT(parameter_keys_ != NULL); | 
| 772   ASSERT(parameter_values_ != NULL); | 701   ASSERT(parameter_values_ != NULL); | 
| 773   return parameter_keys_->Length(); | 702   return parameter_keys_->Length(); | 
| 774 } | 703 } | 
| 775 | 704 | 
| 776 |  | 
| 777 RawObject* JSONStream::GetObjectParameterKey(intptr_t i) const { | 705 RawObject* JSONStream::GetObjectParameterKey(intptr_t i) const { | 
| 778   ASSERT((i >= 0) && (i < NumObjectParameters())); | 706   ASSERT((i >= 0) && (i < NumObjectParameters())); | 
| 779   return parameter_keys_->At(i); | 707   return parameter_keys_->At(i); | 
| 780 } | 708 } | 
| 781 | 709 | 
| 782 |  | 
| 783 RawObject* JSONStream::GetObjectParameterValue(intptr_t i) const { | 710 RawObject* JSONStream::GetObjectParameterValue(intptr_t i) const { | 
| 784   ASSERT((i >= 0) && (i < NumObjectParameters())); | 711   ASSERT((i >= 0) && (i < NumObjectParameters())); | 
| 785   return parameter_values_->At(i); | 712   return parameter_values_->At(i); | 
| 786 } | 713 } | 
| 787 | 714 | 
| 788 |  | 
| 789 RawObject* JSONStream::LookupObjectParam(const char* c_key) const { | 715 RawObject* JSONStream::LookupObjectParam(const char* c_key) const { | 
| 790   const String& key = String::Handle(String::New(c_key)); | 716   const String& key = String::Handle(String::New(c_key)); | 
| 791   Object& test = Object::Handle(); | 717   Object& test = Object::Handle(); | 
| 792   const intptr_t num_object_parameters = NumObjectParameters(); | 718   const intptr_t num_object_parameters = NumObjectParameters(); | 
| 793   for (intptr_t i = 0; i < num_object_parameters; i++) { | 719   for (intptr_t i = 0; i < num_object_parameters; i++) { | 
| 794     test = GetObjectParameterKey(i); | 720     test = GetObjectParameterKey(i); | 
| 795     if (test.IsString() && String::Cast(test).Equals(key)) { | 721     if (test.IsString() && String::Cast(test).Equals(key)) { | 
| 796       return GetObjectParameterValue(i); | 722       return GetObjectParameterValue(i); | 
| 797     } | 723     } | 
| 798   } | 724   } | 
| 799   return Object::null(); | 725   return Object::null(); | 
| 800 } | 726 } | 
| 801 | 727 | 
| 802 |  | 
| 803 void JSONStream::SetParams(const char** param_keys, | 728 void JSONStream::SetParams(const char** param_keys, | 
| 804                            const char** param_values, | 729                            const char** param_values, | 
| 805                            intptr_t num_params) { | 730                            intptr_t num_params) { | 
| 806   param_keys_ = param_keys; | 731   param_keys_ = param_keys; | 
| 807   param_values_ = param_values; | 732   param_values_ = param_values; | 
| 808   num_params_ = num_params; | 733   num_params_ = num_params; | 
| 809 } | 734 } | 
| 810 | 735 | 
| 811 |  | 
| 812 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { | 736 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { | 
| 813   PrintPropertyName(name); | 737   PrintPropertyName(name); | 
| 814   PrintValue(o, ref); | 738   PrintValue(o, ref); | 
| 815 } | 739 } | 
| 816 | 740 | 
| 817 |  | 
| 818 void JSONStream::PrintPropertyVM(const char* name, bool ref) { | 741 void JSONStream::PrintPropertyVM(const char* name, bool ref) { | 
| 819   PrintPropertyName(name); | 742   PrintPropertyName(name); | 
| 820   PrintValueVM(ref); | 743   PrintValueVM(ref); | 
| 821 } | 744 } | 
| 822 | 745 | 
| 823 |  | 
| 824 void JSONStream::PrintPropertyName(const char* name) { | 746 void JSONStream::PrintPropertyName(const char* name) { | 
| 825   ASSERT(name != NULL); | 747   ASSERT(name != NULL); | 
| 826   PrintCommaIfNeeded(); | 748   PrintCommaIfNeeded(); | 
| 827   buffer_.AddChar('"'); | 749   buffer_.AddChar('"'); | 
| 828   AddEscapedUTF8String(name); | 750   AddEscapedUTF8String(name); | 
| 829   buffer_.AddChar('"'); | 751   buffer_.AddChar('"'); | 
| 830   buffer_.AddChar(':'); | 752   buffer_.AddChar(':'); | 
| 831 } | 753 } | 
| 832 | 754 | 
| 833 |  | 
| 834 void JSONStream::PrintCommaIfNeeded() { | 755 void JSONStream::PrintCommaIfNeeded() { | 
| 835   if (NeedComma()) { | 756   if (NeedComma()) { | 
| 836     buffer_.AddChar(','); | 757     buffer_.AddChar(','); | 
| 837   } | 758   } | 
| 838 } | 759 } | 
| 839 | 760 | 
| 840 |  | 
| 841 bool JSONStream::NeedComma() { | 761 bool JSONStream::NeedComma() { | 
| 842   const char* buffer = buffer_.buf(); | 762   const char* buffer = buffer_.buf(); | 
| 843   intptr_t length = buffer_.length(); | 763   intptr_t length = buffer_.length(); | 
| 844   if (length == 0) { | 764   if (length == 0) { | 
| 845     return false; | 765     return false; | 
| 846   } | 766   } | 
| 847   char ch = buffer[length - 1]; | 767   char ch = buffer[length - 1]; | 
| 848   return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ','); | 768   return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ','); | 
| 849 } | 769 } | 
| 850 | 770 | 
| 851 |  | 
| 852 void JSONStream::EnsureIntegerIsRepresentableInJavaScript(int64_t i) { | 771 void JSONStream::EnsureIntegerIsRepresentableInJavaScript(int64_t i) { | 
| 853 #ifdef DEBUG | 772 #ifdef DEBUG | 
| 854   if (!Utils::IsJavascriptInt(i)) { | 773   if (!Utils::IsJavascriptInt(i)) { | 
| 855     OS::Print( | 774     OS::Print( | 
| 856         "JSONStream::EnsureIntegerIsRepresentableInJavaScript failed on " | 775         "JSONStream::EnsureIntegerIsRepresentableInJavaScript failed on " | 
| 857         "%" Pd64 "\n", | 776         "%" Pd64 "\n", | 
| 858         i); | 777         i); | 
| 859     UNREACHABLE(); | 778     UNREACHABLE(); | 
| 860   } | 779   } | 
| 861 #endif | 780 #endif | 
| 862 } | 781 } | 
| 863 | 782 | 
| 864 |  | 
| 865 void JSONStream::AddEscapedUTF8String(const char* s) { | 783 void JSONStream::AddEscapedUTF8String(const char* s) { | 
| 866   if (s == NULL) { | 784   if (s == NULL) { | 
| 867     return; | 785     return; | 
| 868   } | 786   } | 
| 869   intptr_t len = strlen(s); | 787   intptr_t len = strlen(s); | 
| 870   AddEscapedUTF8String(s, len); | 788   AddEscapedUTF8String(s, len); | 
| 871 } | 789 } | 
| 872 | 790 | 
| 873 |  | 
| 874 void JSONStream::AddEscapedUTF8String(const char* s, intptr_t len) { | 791 void JSONStream::AddEscapedUTF8String(const char* s, intptr_t len) { | 
| 875   if (s == NULL) { | 792   if (s == NULL) { | 
| 876     return; | 793     return; | 
| 877   } | 794   } | 
| 878   const uint8_t* s8 = reinterpret_cast<const uint8_t*>(s); | 795   const uint8_t* s8 = reinterpret_cast<const uint8_t*>(s); | 
| 879   intptr_t i = 0; | 796   intptr_t i = 0; | 
| 880   for (; i < len;) { | 797   for (; i < len;) { | 
| 881     // Extract next UTF8 character. | 798     // Extract next UTF8 character. | 
| 882     int32_t ch = 0; | 799     int32_t ch = 0; | 
| 883     int32_t ch_len = Utf8::Decode(&s8[i], len - i, &ch); | 800     int32_t ch_len = Utf8::Decode(&s8[i], len - i, &ch); | 
| 884     ASSERT(ch_len != 0); | 801     ASSERT(ch_len != 0); | 
| 885     buffer_.EscapeAndAddCodeUnit(ch); | 802     buffer_.EscapeAndAddCodeUnit(ch); | 
| 886     // Move i forward. | 803     // Move i forward. | 
| 887     i += ch_len; | 804     i += ch_len; | 
| 888   } | 805   } | 
| 889   ASSERT(i == len); | 806   ASSERT(i == len); | 
| 890 } | 807 } | 
| 891 | 808 | 
| 892 |  | 
| 893 bool JSONStream::AddDartString(const String& s, | 809 bool JSONStream::AddDartString(const String& s, | 
| 894                                intptr_t offset, | 810                                intptr_t offset, | 
| 895                                intptr_t count) { | 811                                intptr_t count) { | 
| 896   intptr_t length = s.Length(); | 812   intptr_t length = s.Length(); | 
| 897   ASSERT(offset >= 0); | 813   ASSERT(offset >= 0); | 
| 898   if (offset > length) { | 814   if (offset > length) { | 
| 899     offset = length; | 815     offset = length; | 
| 900   } | 816   } | 
| 901   if (!Utils::RangeCheck(offset, count, length)) { | 817   if (!Utils::RangeCheck(offset, count, length)) { | 
| 902     count = length - offset; | 818     count = length - offset; | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 920         } | 836         } | 
| 921       } | 837       } | 
| 922     } else { | 838     } else { | 
| 923       buffer_.EscapeAndAddCodeUnit(code_unit); | 839       buffer_.EscapeAndAddCodeUnit(code_unit); | 
| 924     } | 840     } | 
| 925   } | 841   } | 
| 926   // Return value indicates whether the string is truncated. | 842   // Return value indicates whether the string is truncated. | 
| 927   return (offset > 0) || (limit < length); | 843   return (offset > 0) || (limit < length); | 
| 928 } | 844 } | 
| 929 | 845 | 
| 930 |  | 
| 931 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { | 846 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { | 
| 932   stream_->OpenObject(); | 847   stream_->OpenObject(); | 
| 933 } | 848 } | 
| 934 | 849 | 
| 935 |  | 
| 936 void JSONObject::AddFixedServiceId(const char* format, ...) const { | 850 void JSONObject::AddFixedServiceId(const char* format, ...) const { | 
| 937   // Mark that this id is fixed. | 851   // Mark that this id is fixed. | 
| 938   AddProperty("fixedId", true); | 852   AddProperty("fixedId", true); | 
| 939   // Add the id property. | 853   // Add the id property. | 
| 940   stream_->PrintPropertyName("id"); | 854   stream_->PrintPropertyName("id"); | 
| 941   va_list args; | 855   va_list args; | 
| 942   va_start(args, format); | 856   va_start(args, format); | 
| 943   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 857   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 
| 944   va_end(args); | 858   va_end(args); | 
| 945   MaybeOnStackBuffer mosb(len + 1); | 859   MaybeOnStackBuffer mosb(len + 1); | 
| 946   char* p = mosb.p(); | 860   char* p = mosb.p(); | 
| 947   va_start(args, format); | 861   va_start(args, format); | 
| 948   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 862   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 
| 949   va_end(args); | 863   va_end(args); | 
| 950   ASSERT(len == len2); | 864   ASSERT(len == len2); | 
| 951   stream_->buffer_.AddChar('"'); | 865   stream_->buffer_.AddChar('"'); | 
| 952   stream_->AddEscapedUTF8String(p, len); | 866   stream_->AddEscapedUTF8String(p, len); | 
| 953   stream_->buffer_.AddChar('"'); | 867   stream_->buffer_.AddChar('"'); | 
| 954 } | 868 } | 
| 955 | 869 | 
| 956 |  | 
| 957 void JSONObject::AddLocation(const Script& script, | 870 void JSONObject::AddLocation(const Script& script, | 
| 958                              TokenPosition token_pos, | 871                              TokenPosition token_pos, | 
| 959                              TokenPosition end_token_pos) const { | 872                              TokenPosition end_token_pos) const { | 
| 960   JSONObject location(this, "location"); | 873   JSONObject location(this, "location"); | 
| 961   location.AddProperty("type", "SourceLocation"); | 874   location.AddProperty("type", "SourceLocation"); | 
| 962   location.AddProperty("script", script); | 875   location.AddProperty("script", script); | 
| 963   location.AddProperty("tokenPos", token_pos); | 876   location.AddProperty("tokenPos", token_pos); | 
| 964   if (end_token_pos.IsReal()) { | 877   if (end_token_pos.IsReal()) { | 
| 965     location.AddProperty("endTokenPos", end_token_pos); | 878     location.AddProperty("endTokenPos", end_token_pos); | 
| 966   } | 879   } | 
| 967 } | 880 } | 
| 968 | 881 | 
| 969 |  | 
| 970 void JSONObject::AddLocation(const BreakpointLocation* bpt_loc) const { | 882 void JSONObject::AddLocation(const BreakpointLocation* bpt_loc) const { | 
| 971   ASSERT(bpt_loc->IsResolved()); | 883   ASSERT(bpt_loc->IsResolved()); | 
| 972 | 884 | 
| 973   Zone* zone = Thread::Current()->zone(); | 885   Zone* zone = Thread::Current()->zone(); | 
| 974   Library& library = Library::Handle(zone); | 886   Library& library = Library::Handle(zone); | 
| 975   Script& script = Script::Handle(zone); | 887   Script& script = Script::Handle(zone); | 
| 976   TokenPosition token_pos = TokenPosition::kNoSource; | 888   TokenPosition token_pos = TokenPosition::kNoSource; | 
| 977   bpt_loc->GetCodeLocation(&library, &script, &token_pos); | 889   bpt_loc->GetCodeLocation(&library, &script, &token_pos); | 
| 978   AddLocation(script, token_pos); | 890   AddLocation(script, token_pos); | 
| 979 } | 891 } | 
| 980 | 892 | 
| 981 |  | 
| 982 void JSONObject::AddUnresolvedLocation( | 893 void JSONObject::AddUnresolvedLocation( | 
| 983     const BreakpointLocation* bpt_loc) const { | 894     const BreakpointLocation* bpt_loc) const { | 
| 984   ASSERT(!bpt_loc->IsResolved()); | 895   ASSERT(!bpt_loc->IsResolved()); | 
| 985 | 896 | 
| 986   Zone* zone = Thread::Current()->zone(); | 897   Zone* zone = Thread::Current()->zone(); | 
| 987   Library& library = Library::Handle(zone); | 898   Library& library = Library::Handle(zone); | 
| 988   Script& script = Script::Handle(zone); | 899   Script& script = Script::Handle(zone); | 
| 989   TokenPosition token_pos = TokenPosition::kNoSource; | 900   TokenPosition token_pos = TokenPosition::kNoSource; | 
| 990   bpt_loc->GetCodeLocation(&library, &script, &token_pos); | 901   bpt_loc->GetCodeLocation(&library, &script, &token_pos); | 
| 991 | 902 | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 1002     location.AddProperty("line", bpt_loc->requested_line_number()); | 913     location.AddProperty("line", bpt_loc->requested_line_number()); | 
| 1003     if (bpt_loc->requested_column_number() >= 0) { | 914     if (bpt_loc->requested_column_number() >= 0) { | 
| 1004       location.AddProperty("column", bpt_loc->requested_column_number()); | 915       location.AddProperty("column", bpt_loc->requested_column_number()); | 
| 1005     } | 916     } | 
| 1006   } else { | 917   } else { | 
| 1007     // This unresolved breakpoint was requested at some function entry. | 918     // This unresolved breakpoint was requested at some function entry. | 
| 1008     location.AddProperty("tokenPos", token_pos); | 919     location.AddProperty("tokenPos", token_pos); | 
| 1009   } | 920   } | 
| 1010 } | 921 } | 
| 1011 | 922 | 
| 1012 |  | 
| 1013 void JSONObject::AddPropertyF(const char* name, const char* format, ...) const { | 923 void JSONObject::AddPropertyF(const char* name, const char* format, ...) const { | 
| 1014   stream_->PrintPropertyName(name); | 924   stream_->PrintPropertyName(name); | 
| 1015   va_list args; | 925   va_list args; | 
| 1016   va_start(args, format); | 926   va_start(args, format); | 
| 1017   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 927   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 
| 1018   va_end(args); | 928   va_end(args); | 
| 1019   MaybeOnStackBuffer mosb(len + 1); | 929   MaybeOnStackBuffer mosb(len + 1); | 
| 1020   char* p = mosb.p(); | 930   char* p = mosb.p(); | 
| 1021   va_start(args, format); | 931   va_start(args, format); | 
| 1022   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 932   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 
| 1023   va_end(args); | 933   va_end(args); | 
| 1024   ASSERT(len == len2); | 934   ASSERT(len == len2); | 
| 1025   stream_->buffer_.AddChar('"'); | 935   stream_->buffer_.AddChar('"'); | 
| 1026   stream_->AddEscapedUTF8String(p, len); | 936   stream_->AddEscapedUTF8String(p, len); | 
| 1027   stream_->buffer_.AddChar('"'); | 937   stream_->buffer_.AddChar('"'); | 
| 1028 } | 938 } | 
| 1029 | 939 | 
| 1030 |  | 
| 1031 void JSONArray::AddValueF(const char* format, ...) const { | 940 void JSONArray::AddValueF(const char* format, ...) const { | 
| 1032   stream_->PrintCommaIfNeeded(); | 941   stream_->PrintCommaIfNeeded(); | 
| 1033   va_list args; | 942   va_list args; | 
| 1034   va_start(args, format); | 943   va_start(args, format); | 
| 1035   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 944   intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 
| 1036   va_end(args); | 945   va_end(args); | 
| 1037   MaybeOnStackBuffer mosb(len + 1); | 946   MaybeOnStackBuffer mosb(len + 1); | 
| 1038   char* p = mosb.p(); | 947   char* p = mosb.p(); | 
| 1039   va_start(args, format); | 948   va_start(args, format); | 
| 1040   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 949   intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); | 
| 1041   va_end(args); | 950   va_end(args); | 
| 1042   ASSERT(len == len2); | 951   ASSERT(len == len2); | 
| 1043   stream_->buffer_.AddChar('"'); | 952   stream_->buffer_.AddChar('"'); | 
| 1044   stream_->AddEscapedUTF8String(p, len); | 953   stream_->AddEscapedUTF8String(p, len); | 
| 1045   stream_->buffer_.AddChar('"'); | 954   stream_->buffer_.AddChar('"'); | 
| 1046 } | 955 } | 
| 1047 | 956 | 
| 1048 #endif  // !PRODUCT | 957 #endif  // !PRODUCT | 
| 1049 | 958 | 
| 1050 }  // namespace dart | 959 }  // namespace dart | 
| OLD | NEW | 
|---|