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

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
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(intptr_t maxLen) 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(intptr_t maxLen) const {
14295 // Compute the needed length for the buffer.
14296 const intptr_t escapedLen = EscapedStringLen(maxLen);
14297 intptr_t printLen = escapedLen;
14298 intptr_t bufferLen = escapedLen + 2; // +2 for quotes.
14299 if (bufferLen > maxLen) {
14300 bufferLen = maxLen; // Truncate.
14301 printLen = maxLen - 5; // -2 for quotes, -3 for elipsis.
14302 }
14303
14304 // Allocate the buffer.
14305 Zone* zone = Isolate::Current()->current_zone();
14306 char* buffer = zone->Alloc<char>(bufferLen + 1);
14307
14308 // Leading quote.
14309 intptr_t pos = 0;
14310 buffer[pos++] = '\"';
14311
14312 // Print escaped string.
14313 pos += EscapedString((buffer + pos), printLen);
14314
14315 // Trailing quote.
14316 buffer[pos++] = '\"';
14317
14318 if (printLen < escapedLen) {
14319 buffer[pos++] = '.';
14320 buffer[pos++] = '.';
14321 buffer[pos++] = '.';
14322 }
14323 ASSERT(pos <= bufferLen);
14324 buffer[pos++] = '\0';
14325
14326 return buffer;
14327 }
14328
14329
14227 void String::PrintToJSONStream(JSONStream* stream, bool ref) const { 14330 void String::PrintToJSONStream(JSONStream* stream, bool ref) const {
14228 Instance::PrintToJSONStream(stream, ref); 14331 Instance::PrintToJSONStream(stream, ref);
14229 } 14332 }
14230 14333
14231 14334
14232 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { 14335 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const {
14233 ASSERT(array_len >= Utf8::Length(*this)); 14336 ASSERT(array_len >= Utf8::Length(*this));
14234 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len); 14337 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len);
14235 } 14338 }
14236 14339
(...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after
15265 result.SetData(array); 15368 result.SetData(array);
15266 } 15369 }
15267 return result.raw(); 15370 return result.raw();
15268 } 15371 }
15269 15372
15270 15373
15271 const char* GrowableObjectArray::ToCString() const { 15374 const char* GrowableObjectArray::ToCString() const {
15272 if (IsNull()) { 15375 if (IsNull()) {
15273 return "_GrowableList NULL"; 15376 return "_GrowableList NULL";
15274 } 15377 }
15275 const char* format = "_GrowableList len:%" Pd ""; 15378 const char* format = "Instance(length:%" Pd ") of '_GrowableList'";
15276 intptr_t len = OS::SNPrint(NULL, 0, format, Length()) + 1; 15379 intptr_t len = OS::SNPrint(NULL, 0, format, Length()) + 1;
15277 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); 15380 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
15278 OS::SNPrint(chars, len, format, Length()); 15381 OS::SNPrint(chars, len, format, Length());
15279 return chars; 15382 return chars;
15280 } 15383 }
15281 15384
15282 15385
15283 void GrowableObjectArray::PrintToJSONStream(JSONStream* stream, 15386 void GrowableObjectArray::PrintToJSONStream(JSONStream* stream,
15284 bool ref) const { 15387 bool ref) const {
15285 Instance::PrintToJSONStream(stream, ref); 15388 Instance::PrintToJSONStream(stream, ref);
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after
16055 return "_MirrorReference"; 16158 return "_MirrorReference";
16056 } 16159 }
16057 16160
16058 16161
16059 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const { 16162 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const {
16060 Instance::PrintToJSONStream(stream, ref); 16163 Instance::PrintToJSONStream(stream, ref);
16061 } 16164 }
16062 16165
16063 16166
16064 } // namespace dart 16167 } // namespace dart
OLDNEW
« no previous file with comments | « 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