OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/execution.h" | 8 #include "src/execution.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/string-builder.h" | 10 #include "src/string-builder.h" |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 Handle<String> arg = args[i++]; | 360 Handle<String> arg = args[i++]; |
361 builder.AppendString(arg); | 361 builder.AppendString(arg); |
362 } | 362 } |
363 } else { | 363 } else { |
364 builder.AppendCharacter(*c); | 364 builder.AppendCharacter(*c); |
365 } | 365 } |
366 } | 366 } |
367 | 367 |
368 return builder.Finish(); | 368 return builder.Finish(); |
369 } | 369 } |
| 370 |
| 371 |
| 372 MaybeHandle<String> ErrorToStringHelper::Stringify(Isolate* isolate, |
| 373 Handle<JSObject> error) { |
| 374 VisitedScope scope(this, error); |
| 375 if (scope.has_visited()) return isolate->factory()->empty_string(); |
| 376 |
| 377 Handle<String> name; |
| 378 Handle<String> message; |
| 379 Handle<Name> internal_key = isolate->factory()->internal_error_symbol(); |
| 380 Handle<String> message_string = |
| 381 isolate->factory()->NewStringFromStaticChars("message"); |
| 382 Handle<String> name_string = isolate->factory()->name_string(); |
| 383 LookupIterator internal_error_lookup( |
| 384 error, internal_key, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 385 LookupIterator message_lookup( |
| 386 error, message_string, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 387 LookupIterator name_lookup(error, name_string, |
| 388 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 389 |
| 390 // Find out whether an internally created error object is on the prototype |
| 391 // chain. If the name property is found on a holder prior to the internally |
| 392 // created error object, use that name property. Otherwise just use the |
| 393 // constructor name to avoid triggering possible side effects. |
| 394 // Similar for the message property. If the message property shadows the |
| 395 // internally created error object, use that message property. Otherwise |
| 396 // use empty string as message. |
| 397 if (internal_error_lookup.IsFound()) { |
| 398 if (!ShadowsInternalError(isolate, &name_lookup, &internal_error_lookup)) { |
| 399 Handle<JSObject> holder = internal_error_lookup.GetHolder<JSObject>(); |
| 400 name = Handle<String>(holder->constructor_name()); |
| 401 } |
| 402 if (!ShadowsInternalError(isolate, &message_lookup, |
| 403 &internal_error_lookup)) { |
| 404 message = isolate->factory()->empty_string(); |
| 405 } |
| 406 } |
| 407 if (name.is_null()) { |
| 408 ASSIGN_RETURN_ON_EXCEPTION( |
| 409 isolate, name, |
| 410 GetStringifiedProperty(isolate, &name_lookup, |
| 411 isolate->factory()->Error_string()), |
| 412 String); |
| 413 } |
| 414 if (message.is_null()) { |
| 415 ASSIGN_RETURN_ON_EXCEPTION( |
| 416 isolate, message, |
| 417 GetStringifiedProperty(isolate, &message_lookup, |
| 418 isolate->factory()->empty_string()), |
| 419 String); |
| 420 } |
| 421 |
| 422 if (name->length() == 0) return message; |
| 423 if (message->length() == 0) return name; |
| 424 IncrementalStringBuilder builder(isolate); |
| 425 builder.AppendString(name); |
| 426 builder.AppendCString(": "); |
| 427 builder.AppendString(message); |
| 428 return builder.Finish(); |
| 429 } |
| 430 |
| 431 |
| 432 bool ErrorToStringHelper::ShadowsInternalError( |
| 433 Isolate* isolate, LookupIterator* property_lookup, |
| 434 LookupIterator* internal_error_lookup) { |
| 435 Handle<JSObject> holder = property_lookup->GetHolder<JSObject>(); |
| 436 // It's fine if the property is defined on the error itself. |
| 437 if (holder.is_identical_to(property_lookup->GetReceiver())) return true; |
| 438 PrototypeIterator it(isolate, holder, PrototypeIterator::START_AT_RECEIVER); |
| 439 while (true) { |
| 440 if (it.IsAtEnd()) return false; |
| 441 if (it.IsAtEnd(internal_error_lookup->GetHolder<JSObject>())) return true; |
| 442 it.AdvanceIgnoringProxies(); |
| 443 } |
| 444 } |
| 445 |
| 446 |
| 447 MaybeHandle<String> ErrorToStringHelper::GetStringifiedProperty( |
| 448 Isolate* isolate, LookupIterator* property_lookup, |
| 449 Handle<String> default_value) { |
| 450 if (!property_lookup->IsFound()) return default_value; |
| 451 Handle<Object> obj; |
| 452 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Object::GetProperty(property_lookup), |
| 453 String); |
| 454 if (obj->IsUndefined()) return default_value; |
| 455 if (!obj->IsString()) { |
| 456 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Execution::ToString(isolate, obj), |
| 457 String); |
| 458 } |
| 459 return Handle<String>::cast(obj); |
| 460 } |
| 461 |
370 } // namespace internal | 462 } // namespace internal |
371 } // namespace v8 | 463 } // namespace v8 |
OLD | NEW |