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

Side by Side Diff: runtime/vm/object.cc

Issue 100833005: Add Instance::ToUserCString. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 11375 matching lines...) Expand 10 before | Expand all | Expand 10 after
11386 } 11386 }
11387 const char* kFormat = "Instance of '%s'"; 11387 const char* kFormat = "Instance of '%s'";
11388 const Class& cls = Class::Handle(clazz()); 11388 const Class& cls = Class::Handle(clazz());
11389 AbstractTypeArguments& type_arguments = AbstractTypeArguments::Handle(); 11389 AbstractTypeArguments& type_arguments = AbstractTypeArguments::Handle();
11390 const intptr_t num_type_arguments = cls.NumTypeArguments(); 11390 const intptr_t num_type_arguments = cls.NumTypeArguments();
11391 if (num_type_arguments > 0) { 11391 if (num_type_arguments > 0) {
11392 type_arguments = GetTypeArguments(); 11392 type_arguments = GetTypeArguments();
11393 } 11393 }
11394 const Type& type = 11394 const Type& type =
11395 Type::Handle(Type::New(cls, type_arguments, Scanner::kDummyTokenIndex)); 11395 Type::Handle(Type::New(cls, type_arguments, Scanner::kDummyTokenIndex));
11396 const String& type_name = String::Handle(type.Name()); 11396 const String& type_name = String::Handle(type.UserVisibleName());
11397 // Calculate the size of the string. 11397 // Calculate the size of the string.
11398 intptr_t len = OS::SNPrint(NULL, 0, kFormat, type_name.ToCString()) + 1; 11398 intptr_t len = OS::SNPrint(NULL, 0, kFormat, type_name.ToCString()) + 1;
11399 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); 11399 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
11400 OS::SNPrint(chars, len, kFormat, type_name.ToCString()); 11400 OS::SNPrint(chars, len, kFormat, type_name.ToCString());
11401 return chars; 11401 return chars;
11402 } 11402 }
11403 } 11403 }
11404 11404
11405 11405
11406 const char* Instance::ToUserCString() const {
11407 if (raw() == Object::sentinel().raw()) {
11408 return "<not initialized>";
11409 } else if (raw() == Object::transition_sentinel().raw()) {
11410 return "<being initialized>";
11411 } else {
11412 return ToCString();
11413 }
11414 }
11415
11416
11406 void Instance::PrintToJSONStream(JSONStream* stream, bool ref) const { 11417 void Instance::PrintToJSONStream(JSONStream* stream, bool ref) const {
11407 ObjectIdRing* ring = Isolate::Current()->object_id_ring(); 11418 ObjectIdRing* ring = Isolate::Current()->object_id_ring();
11408 intptr_t id = ring->GetIdForObject(raw()); 11419 intptr_t id = ring->GetIdForObject(raw());
11409 11420
11410 JSONObject jsobj(stream); 11421 JSONObject jsobj(stream);
11411 jsobj.AddProperty("type", JSONType(ref)); 11422 jsobj.AddProperty("type", JSONType(ref));
11412 jsobj.AddProperty("id", id); 11423 jsobj.AddProperty("id", id);
11413 11424
11414 // Set the "preview" property for this instance. 11425 // Set the "preview" property for this instance.
11415 if (IsNull()) { 11426 if (IsNull()) {
(...skipping 2801 matching lines...) Expand 10 before | Expand all | Expand 10 after
14217 } 14228 }
14218 const intptr_t len = Utf8::Length(*this); 14229 const intptr_t len = Utf8::Length(*this);
14219 Zone* zone = Isolate::Current()->current_zone(); 14230 Zone* zone = Isolate::Current()->current_zone();
14220 uint8_t* result = zone->Alloc<uint8_t>(len + 1); 14231 uint8_t* result = zone->Alloc<uint8_t>(len + 1);
14221 ToUTF8(result, len); 14232 ToUTF8(result, len);
14222 result[len] = 0; 14233 result[len] = 0;
14223 return reinterpret_cast<const char*>(result); 14234 return reinterpret_cast<const char*>(result);
14224 } 14235 }
14225 14236
14226 14237
14238 static bool IsAsciiPrintChar(intptr_t codePoint) {
14239 return codePoint >= ' ' && codePoint <= '~';
14240 }
14241
14242
14243 // Does not null-terminate.
14244 intptr_t String::EscapedString(char* buffer, int maxLen) const {
14245 int pos = 0;
14246
14247 CodePointIterator cpi(*this);
14248 while (cpi.Next()) {
14249 int32_t codePoint = cpi.Current();
14250 if (IsSpecialCharacter(codePoint)) {
14251 if (pos + 2 > maxLen) {
14252 return pos;
14253 }
14254 buffer[pos++] = '\\';
14255 buffer[pos++] = SpecialCharacter(codePoint);
14256 } else if (IsAsciiPrintChar(codePoint)) {
14257 buffer[pos++] = codePoint;
14258 } else {
14259 if (pos + 6 > maxLen) {
14260 return pos;
14261 }
14262 pos += OS::SNPrint((buffer + pos), (maxLen - pos), "\\u%04x", codePoint);
14263 }
14264 if (pos == maxLen) {
14265 return pos;
14266 }
14267 }
14268 return pos;
14269 }
14270
14271
14272 intptr_t String::EscapedStringLen(intptr_t tooLong) const {
14273 intptr_t len = 0;
14274
14275 CodePointIterator cpi(*this);
14276 while (cpi.Next()) {
14277 int32_t codePoint = cpi.Current();
14278 if (IsSpecialCharacter(codePoint)) {
14279 len += 2; // e.g. "\n"
14280 } else if (IsAsciiPrintChar(codePoint)) {
14281 len += 1;
14282 } else {
14283 len += 6; // e.g. "\u0000".
14284 }
14285 if (len > tooLong) {
14286 // No point going further.
14287 break;
14288 }
14289 }
14290 return len;
14291 }
14292
14293
14294 const char* String::ToUserCString() const {
14295 const int maxLen = 40;
14296
14297 // Compute the needed length for the buffer.
14298 const intptr_t escapedLen = EscapedStringLen(maxLen);
14299 intptr_t printLen = escapedLen;
14300 intptr_t bufferLen = escapedLen + 2; // +2 for quotes.
14301 if (bufferLen > maxLen) {
14302 bufferLen = maxLen; // Truncate.
14303 printLen = maxLen - 5; // -2 for quotes, -3 for elipsis.
14304 }
14305
14306 // Allocate the buffer.
14307 Zone* zone = Isolate::Current()->current_zone();
14308 char* buffer = zone->Alloc<char>(bufferLen + 1);
14309
14310 // Leading quote.
14311 intptr_t pos = 0;
14312 buffer[pos++] = '\"';
14313
14314 // Print escaped string.
14315 pos += EscapedString((buffer + pos), printLen);
14316
14317 // Trailing quote.
14318 buffer[pos++] = '\"';
14319
14320 if (printLen < escapedLen) {
14321 buffer[pos++] = '.';
14322 buffer[pos++] = '.';
14323 buffer[pos++] = '.';
14324 }
14325 ASSERT(pos <= bufferLen);
14326 buffer[pos++] = '\0';
14327
14328 return buffer;
14329 }
14330
14331
14227 void String::PrintToJSONStream(JSONStream* stream, bool ref) const { 14332 void String::PrintToJSONStream(JSONStream* stream, bool ref) const {
14228 Instance::PrintToJSONStream(stream, ref); 14333 Instance::PrintToJSONStream(stream, ref);
14229 } 14334 }
14230 14335
14231 14336
14232 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { 14337 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const {
14233 ASSERT(array_len >= Utf8::Length(*this)); 14338 ASSERT(array_len >= Utf8::Length(*this));
14234 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len); 14339 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len);
14235 } 14340 }
14236 14341
(...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after
15265 result.SetData(array); 15370 result.SetData(array);
15266 } 15371 }
15267 return result.raw(); 15372 return result.raw();
15268 } 15373 }
15269 15374
15270 15375
15271 const char* GrowableObjectArray::ToCString() const { 15376 const char* GrowableObjectArray::ToCString() const {
15272 if (IsNull()) { 15377 if (IsNull()) {
15273 return "_GrowableList NULL"; 15378 return "_GrowableList NULL";
15274 } 15379 }
15275 const char* format = "_GrowableList len:%" Pd ""; 15380 const char* format = "Instance(length:%" Pd ") of '_GrowableList'";
15276 intptr_t len = OS::SNPrint(NULL, 0, format, Length()) + 1; 15381 intptr_t len = OS::SNPrint(NULL, 0, format, Length()) + 1;
15277 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); 15382 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
15278 OS::SNPrint(chars, len, format, Length()); 15383 OS::SNPrint(chars, len, format, Length());
15279 return chars; 15384 return chars;
15280 } 15385 }
15281 15386
15282 15387
15283 void GrowableObjectArray::PrintToJSONStream(JSONStream* stream, 15388 void GrowableObjectArray::PrintToJSONStream(JSONStream* stream,
15284 bool ref) const { 15389 bool ref) const {
15285 Instance::PrintToJSONStream(stream, ref); 15390 Instance::PrintToJSONStream(stream, ref);
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after
16055 return "_MirrorReference"; 16160 return "_MirrorReference";
16056 } 16161 }
16057 16162
16058 16163
16059 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const { 16164 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const {
16060 Instance::PrintToJSONStream(stream, ref); 16165 Instance::PrintToJSONStream(stream, ref);
16061 } 16166 }
16062 16167
16063 16168
16064 } // namespace dart 16169 } // namespace dart
OLDNEW
« runtime/vm/object.h ('K') | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698