blob: 781f8cd6bf2d5ba1a4401e7b6b177766160ab29d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/string-stream.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/handles-inl.h"
8#include "src/prototype.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
13static const int kMentionedObjectCacheMaxSize = 256;
Steve Blocka7e24c12009-10-30 11:49:00 +000014
15char* HeapStringAllocator::allocate(unsigned bytes) {
16 space_ = NewArray<char>(bytes);
17 return space_;
18}
19
20
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021char* FixedStringAllocator::allocate(unsigned bytes) {
22 CHECK_LE(bytes, length_);
23 return buffer_;
24}
25
26
27char* FixedStringAllocator::grow(unsigned* old) {
28 *old = length_;
29 return buffer_;
30}
31
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033bool StringStream::Put(char c) {
34 if (full()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 DCHECK(length_ < capacity_);
Steve Blocka7e24c12009-10-30 11:49:00 +000036 // Since the trailing '\0' is not accounted for in length_ fullness is
37 // indicated by a difference of 1 between length_ and capacity_. Thus when
38 // reaching a difference of 2 we need to grow the buffer.
39 if (length_ == capacity_ - 2) {
40 unsigned new_capacity = capacity_;
41 char* new_buffer = allocator_->grow(&new_capacity);
42 if (new_capacity > capacity_) {
43 capacity_ = new_capacity;
44 buffer_ = new_buffer;
45 } else {
46 // Reached the end of the available buffer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 DCHECK(capacity_ >= 5);
Steve Blocka7e24c12009-10-30 11:49:00 +000048 length_ = capacity_ - 1; // Indicate fullness of the stream.
49 buffer_[length_ - 4] = '.';
50 buffer_[length_ - 3] = '.';
51 buffer_[length_ - 2] = '.';
52 buffer_[length_ - 1] = '\n';
53 buffer_[length_] = '\0';
54 return false;
55 }
56 }
57 buffer_[length_] = c;
58 buffer_[length_ + 1] = '\0';
59 length_++;
60 return true;
61}
62
63
64// A control character is one that configures a format element. For
65// instance, in %.5s, .5 are control characters.
66static bool IsControlChar(char c) {
67 switch (c) {
68 case '0': case '1': case '2': case '3': case '4': case '5':
69 case '6': case '7': case '8': case '9': case '.': case '-':
70 return true;
71 default:
72 return false;
73 }
74}
75
76
77void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) {
78 // If we already ran out of space then return immediately.
79 if (full()) return;
80 int offset = 0;
81 int elm = 0;
82 while (offset < format.length()) {
83 if (format[offset] != '%' || elm == elms.length()) {
84 Put(format[offset]);
85 offset++;
86 continue;
87 }
88 // Read this formatting directive into a temporary buffer
89 EmbeddedVector<char, 24> temp;
90 int format_length = 0;
91 // Skip over the whole control character sequence until the
92 // format element type
93 temp[format_length++] = format[offset++];
94 while (offset < format.length() && IsControlChar(format[offset]))
95 temp[format_length++] = format[offset++];
96 if (offset >= format.length())
97 return;
98 char type = format[offset];
99 temp[format_length++] = type;
100 temp[format_length] = '\0';
101 offset++;
102 FmtElm current = elms[elm++];
103 switch (type) {
104 case 's': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 DCHECK_EQ(FmtElm::C_STR, current.type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 const char* value = current.data_.u_c_str_;
107 Add(value);
108 break;
109 }
110 case 'w': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 DCHECK_EQ(FmtElm::LC_STR, current.type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 Vector<const uc16> value = *current.data_.u_lc_str_;
113 for (int i = 0; i < value.length(); i++)
114 Put(static_cast<char>(value[i]));
115 break;
116 }
117 case 'o': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 DCHECK_EQ(FmtElm::OBJ, current.type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 Object* obj = current.data_.u_obj_;
120 PrintObject(obj);
121 break;
122 }
123 case 'k': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 DCHECK_EQ(FmtElm::INT, current.type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 int value = current.data_.u_int_;
126 if (0x20 <= value && value <= 0x7F) {
127 Put(value);
128 } else if (value <= 0xff) {
129 Add("\\x%02x", value);
130 } else {
131 Add("\\u%04x", value);
132 }
133 break;
134 }
135 case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': {
136 int value = current.data_.u_int_;
137 EmbeddedVector<char, 24> formatted;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 int length = SNPrintF(formatted, temp.start(), value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 Add(Vector<const char>(formatted.start(), length));
140 break;
141 }
142 case 'f': case 'g': case 'G': case 'e': case 'E': {
143 double value = current.data_.u_double_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 int inf = std::isinf(value);
145 if (inf == -1) {
146 Add("-inf");
147 } else if (inf == 1) {
148 Add("inf");
149 } else if (std::isnan(value)) {
150 Add("nan");
151 } else {
152 EmbeddedVector<char, 28> formatted;
153 SNPrintF(formatted, temp.start(), value);
154 Add(formatted.start());
155 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 break;
157 }
158 case 'p': {
159 void* value = current.data_.u_pointer_;
160 EmbeddedVector<char, 20> formatted;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 SNPrintF(formatted, temp.start(), value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 Add(formatted.start());
163 break;
164 }
165 default:
166 UNREACHABLE();
167 break;
168 }
169 }
170
171 // Verify that the buffer is 0-terminated
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 DCHECK(buffer_[length_] == '\0');
Steve Blocka7e24c12009-10-30 11:49:00 +0000173}
174
175
176void StringStream::PrintObject(Object* o) {
177 o->ShortPrint(this);
178 if (o->IsString()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000179 if (String::cast(o)->length() <= String::kMaxShortPrintLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 return;
181 }
182 } else if (o->IsNumber() || o->IsOddball()) {
183 return;
184 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 if (o->IsHeapObject() && object_print_mode_ == kPrintObjectVerbose) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 HeapObject* ho = HeapObject::cast(o);
187 DebugObjectCache* debug_object_cache = ho->GetIsolate()->
Steve Block44f0eee2011-05-26 01:26:41 +0100188 string_stream_debug_object_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 for (int i = 0; i < debug_object_cache->length(); i++) {
190 if ((*debug_object_cache)[i] == o) {
191 Add("#%d#", i);
192 return;
193 }
194 }
195 if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) {
196 Add("#%d#", debug_object_cache->length());
197 debug_object_cache->Add(HeapObject::cast(o));
198 } else {
199 Add("@%p", o);
200 }
201 }
202}
203
204
205void StringStream::Add(const char* format) {
206 Add(CStrVector(format));
207}
208
209
210void StringStream::Add(Vector<const char> format) {
211 Add(format, Vector<FmtElm>::empty());
212}
213
214
215void StringStream::Add(const char* format, FmtElm arg0) {
216 const char argc = 1;
217 FmtElm argv[argc] = { arg0 };
218 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
219}
220
221
222void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) {
223 const char argc = 2;
224 FmtElm argv[argc] = { arg0, arg1 };
225 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
226}
227
228
229void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
230 FmtElm arg2) {
231 const char argc = 3;
232 FmtElm argv[argc] = { arg0, arg1, arg2 };
233 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
234}
235
236
237void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
238 FmtElm arg2, FmtElm arg3) {
239 const char argc = 4;
240 FmtElm argv[argc] = { arg0, arg1, arg2, arg3 };
241 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
242}
243
244
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
246 FmtElm arg2, FmtElm arg3, FmtElm arg4) {
247 const char argc = 5;
248 FmtElm argv[argc] = { arg0, arg1, arg2, arg3, arg4 };
249 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
250}
251
252
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253base::SmartArrayPointer<const char> StringStream::ToCString() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 char* str = NewArray<char>(length_ + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 MemCopy(str, buffer_, length_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 str[length_] = '\0';
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 return base::SmartArrayPointer<const char>(str);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258}
259
260
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261void StringStream::Log(Isolate* isolate) {
262 LOG(isolate, StringEvent("StackDump", buffer_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000263}
264
265
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266void StringStream::OutputToFile(FILE* out) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 // Dump the output to stdout, but make sure to break it up into
268 // manageable chunks to avoid losing parts of the output in the OS
269 // printing code. This is a problem on Windows in particular; see
270 // the VPrint() function implementations in platform-win32.cc.
271 unsigned position = 0;
272 for (unsigned next; (next = position + 2048) < length_; position = next) {
273 char save = buffer_[next];
274 buffer_[next] = '\0';
Ben Murdochb0fe1622011-05-05 13:52:32 +0100275 internal::PrintF(out, "%s", &buffer_[position]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 buffer_[next] = save;
277 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100278 internal::PrintF(out, "%s", &buffer_[position]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000279}
280
281
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282Handle<String> StringStream::ToString(Isolate* isolate) {
283 return isolate->factory()->NewStringFromUtf8(
284 Vector<const char>(buffer_, length_)).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000285}
286
287
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288void StringStream::ClearMentionedObjectCache(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100289 isolate->set_string_stream_current_security_token(NULL);
290 if (isolate->string_stream_debug_object_cache() == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 isolate->set_string_stream_debug_object_cache(new DebugObjectCache(0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 }
Steve Block44f0eee2011-05-26 01:26:41 +0100293 isolate->string_stream_debug_object_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +0000294}
295
296
297#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299 return object_print_mode_ == kPrintObjectConcise ||
300 isolate->string_stream_debug_object_cache()->length() == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000301}
302#endif
303
304
305bool StringStream::Put(String* str) {
306 return Put(str, 0, str->length());
307}
308
309
310bool StringStream::Put(String* str, int start, int end) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400311 StringCharacterStream stream(str, start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 for (int i = start; i < end && stream.HasMore(); i++) {
313 uint16_t c = stream.GetNext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 if (c >= 127 || c < 32) {
315 c = '?';
316 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 if (!Put(static_cast<char>(c))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 return false; // Output was truncated.
319 }
320 }
321 return true;
322}
323
324
325void StringStream::PrintName(Object* name) {
326 if (name->IsString()) {
327 String* str = String::cast(name);
328 if (str->length() > 0) {
329 Put(str);
330 } else {
331 Add("/* anonymous */");
332 }
333 } else {
334 Add("%o", name);
335 }
336}
337
338
339void StringStream::PrintUsingMap(JSObject* js_object) {
340 Map* map = js_object->map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 if (!js_object->GetHeap()->Contains(map) ||
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 !map->IsHeapObject() ||
343 !map->IsMap()) {
344 Add("<Invalid map>\n");
345 return;
346 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 int real_size = map->NumberOfOwnDescriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 DescriptorArray* descs = map->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 for (int i = 0; i < real_size; i++) {
350 PropertyDetails details = descs->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000351 if (details.type() == DATA) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100352 Object* key = descs->GetKey(i);
353 if (key->IsString() || key->IsNumber()) {
354 int len = 3;
355 if (key->IsString()) {
356 len = String::cast(key)->length();
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100358 for (; len < 18; len++)
359 Put(' ');
360 if (key->IsString()) {
361 Put(String::cast(key));
362 } else {
363 key->ShortPrint();
364 }
365 Add(": ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 FieldIndex index = FieldIndex::ForDescriptor(map, i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400367 if (js_object->IsUnboxedDoubleField(index)) {
368 double value = js_object->RawFastDoublePropertyAt(index);
369 Add("<unboxed double> %.16g\n", FmtElm(value));
370 } else {
371 Object* value = js_object->RawFastPropertyAt(index);
372 Add("%o\n", value);
373 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 }
376 }
377}
378
379
380void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100381 Isolate* isolate = array->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 for (unsigned int i = 0; i < 10 && i < limit; i++) {
383 Object* element = array->get(i);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100384 if (element->IsTheHole(isolate)) continue;
385 for (int len = 1; len < 18; len++) {
386 Put(' ');
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100388 Add("%d: %o\n", i, array->get(i));
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 }
390 if (limit >= 10) {
391 Add(" ...\n");
392 }
393}
394
395
396void StringStream::PrintByteArray(ByteArray* byte_array) {
397 unsigned int limit = byte_array->length();
398 for (unsigned int i = 0; i < 10 && i < limit; i++) {
399 byte b = byte_array->get(i);
400 Add(" %d: %3d 0x%02x", i, b, b);
401 if (b >= ' ' && b <= '~') {
402 Add(" '%c'", b);
403 } else if (b == '\n') {
404 Add(" '\n'");
405 } else if (b == '\r') {
406 Add(" '\r'");
407 } else if (b >= 1 && b <= 26) {
408 Add(" ^%c", b + 'A' - 1);
409 }
410 Add("\n");
411 }
412 if (limit >= 10) {
413 Add(" ...\n");
414 }
415}
416
417
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418void StringStream::PrintMentionedObjectCache(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 if (object_print_mode_ == kPrintObjectConcise) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100420 DebugObjectCache* debug_object_cache =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 isolate->string_stream_debug_object_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 Add("==== Key ============================================\n\n");
423 for (int i = 0; i < debug_object_cache->length(); i++) {
424 HeapObject* printee = (*debug_object_cache)[i];
425 Add(" #%d# %p: ", i, printee);
426 printee->ShortPrint(this);
427 Add("\n");
428 if (printee->IsJSObject()) {
429 if (printee->IsJSValue()) {
430 Add(" value(): %o\n", JSValue::cast(printee)->value());
431 }
432 PrintUsingMap(JSObject::cast(printee));
433 if (printee->IsJSArray()) {
434 JSArray* array = JSArray::cast(printee);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 if (array->HasFastObjectElements()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 unsigned int limit = FixedArray::cast(array->elements())->length();
437 unsigned int length =
438 static_cast<uint32_t>(JSArray::cast(array)->length()->Number());
439 if (length < limit) limit = length;
440 PrintFixedArray(FixedArray::cast(array->elements()), limit);
441 }
442 }
443 } else if (printee->IsByteArray()) {
444 PrintByteArray(ByteArray::cast(printee));
445 } else if (printee->IsFixedArray()) {
446 unsigned int limit = FixedArray::cast(printee)->length();
447 PrintFixedArray(FixedArray::cast(printee), limit);
448 }
449 }
450}
451
452
453void StringStream::PrintSecurityTokenIfChanged(Object* f) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 if (!f->IsHeapObject()) return;
455 HeapObject* obj = HeapObject::cast(f);
456 Isolate* isolate = obj->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100457 Heap* heap = isolate->heap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 if (!heap->Contains(obj)) return;
459 Map* map = obj->map();
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 if (!map->IsHeapObject() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100461 !heap->Contains(map) ||
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 !map->IsMap() ||
463 !f->IsJSFunction()) {
464 return;
465 }
466
467 JSFunction* fun = JSFunction::cast(f);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 Object* perhaps_context = fun->context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 if (perhaps_context->IsHeapObject() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100470 heap->Contains(HeapObject::cast(perhaps_context)) &&
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 perhaps_context->IsContext()) {
472 Context* context = fun->context();
Steve Block44f0eee2011-05-26 01:26:41 +0100473 if (!heap->Contains(context)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 Add("(Function context is outside heap)\n");
475 return;
476 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 Object* token = context->native_context()->security_token();
Steve Block44f0eee2011-05-26 01:26:41 +0100478 if (token != isolate->string_stream_current_security_token()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 Add("Security context: %o\n", token);
Steve Block44f0eee2011-05-26 01:26:41 +0100480 isolate->set_string_stream_current_security_token(token);
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 }
482 } else {
483 Add("(Function context is corrupt)\n");
484 }
485}
486
487
488void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 if (!f->IsHeapObject()) {
490 Add("/* warning: 'function' was not a heap object */ ");
491 return;
492 }
493 Heap* heap = HeapObject::cast(f)->GetHeap();
494 if (!heap->Contains(HeapObject::cast(f))) {
495 Add("/* warning: 'function' was not on the heap */ ");
496 return;
497 }
498 if (!heap->Contains(HeapObject::cast(f)->map())) {
499 Add("/* warning: function's map was not on the heap */ ");
500 return;
501 }
502 if (!HeapObject::cast(f)->map()->IsMap()) {
503 Add("/* warning: function's map was not a valid map */ ");
504 return;
505 }
506 if (f->IsJSFunction()) {
507 JSFunction* fun = JSFunction::cast(f);
508 // Common case: on-stack function present and resolved.
509 PrintPrototype(fun, receiver);
510 *code = fun->code();
511 } else if (f->IsInternalizedString()) {
512 // Unresolved and megamorphic calls: Instead of the function
513 // we have the function name on the stack.
514 PrintName(f);
515 Add("/* unresolved */ ");
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 // Unless this is the frame of a built-in function, we should always have
518 // the callee function or name on the stack. If we don't, we have a
519 // problem or a change of the stack frame layout.
520 Add("%o", f);
521 Add("/* warning: no JSFunction object or function name found */ ");
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 }
523}
524
525
526void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) {
527 Object* name = fun->shared()->name();
528 bool print_name = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 Isolate* isolate = fun->GetIsolate();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100530 if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate) ||
531 receiver->IsTheHole(isolate) || receiver->IsJSProxy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100532 print_name = true;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100533 } else if (isolate->context() != nullptr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100534 if (!receiver->IsJSObject()) {
535 receiver = receiver->GetRootMap(isolate)->prototype();
536 }
537
538 for (PrototypeIterator iter(isolate, JSObject::cast(receiver),
Ben Murdoch61f157c2016-09-16 13:49:30 +0100539 kStartAtReceiver);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100540 !iter.IsAtEnd(); iter.Advance()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100541 if (iter.GetCurrent()->IsJSProxy()) break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000542 Object* key = iter.GetCurrent<JSObject>()->SlowReverseLookup(fun);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100543 if (!key->IsUndefined(isolate)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000544 if (!name->IsString() ||
545 !key->IsString() ||
546 !String::cast(name)->Equals(String::cast(key))) {
547 print_name = true;
548 }
549 if (name->IsString() && String::cast(name)->length() == 0) {
550 print_name = false;
551 }
552 name = key;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100553 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 }
556 }
557 PrintName(name);
558 // Also known as - if the name in the function doesn't match the name under
559 // which it was looked up.
560 if (print_name) {
561 Add("(aka ");
562 PrintName(fun->shared()->name());
563 Put(')');
564 }
565}
566
567
568char* HeapStringAllocator::grow(unsigned* bytes) {
569 unsigned new_bytes = *bytes * 2;
570 // Check for overflow.
571 if (new_bytes <= *bytes) {
572 return space_;
573 }
574 char* new_space = NewArray<char>(new_bytes);
575 if (new_space == NULL) {
576 return space_;
577 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 MemCopy(new_space, space_, *bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 *bytes = new_bytes;
580 DeleteArray(space_);
581 space_ = new_space;
582 return new_space;
583}
584
585
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000586} // namespace internal
587} // namespace v8