| 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/messages.h" | 5 #include "src/messages.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/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
| 10 #include "src/string-builder.h" | 10 #include "src/string-builder.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 Isolate::ExceptionScope exception_scope(isolate); | 77 Isolate::ExceptionScope exception_scope(isolate); |
| 78 isolate->clear_pending_exception(); | 78 isolate->clear_pending_exception(); |
| 79 isolate->set_external_caught_exception(false); | 79 isolate->set_external_caught_exception(false); |
| 80 | 80 |
| 81 // Turn the exception on the message into a string if it is an object. | 81 // Turn the exception on the message into a string if it is an object. |
| 82 if (message->argument()->IsJSObject()) { | 82 if (message->argument()->IsJSObject()) { |
| 83 HandleScope scope(isolate); | 83 HandleScope scope(isolate); |
| 84 Handle<Object> argument(message->argument(), isolate); | 84 Handle<Object> argument(message->argument(), isolate); |
| 85 Handle<Object> args[] = {argument}; | 85 Handle<Object> args[] = {argument}; |
| 86 MaybeHandle<Object> maybe_stringified = Execution::TryCall( | 86 MaybeHandle<Object> maybe_stringified = Execution::TryCall( |
| 87 isolate, isolate->no_side_effects_to_string_fun(), | 87 isolate, isolate->to_detail_string_fun(), |
| 88 isolate->factory()->undefined_value(), arraysize(args), args); | 88 isolate->factory()->undefined_value(), arraysize(args), args); |
| 89 Handle<Object> stringified; | 89 Handle<Object> stringified; |
| 90 if (!maybe_stringified.ToHandle(&stringified)) { | 90 if (!maybe_stringified.ToHandle(&stringified)) { |
| 91 stringified = isolate->factory()->NewStringFromAsciiChecked("exception"); | 91 stringified = isolate->factory()->NewStringFromAsciiChecked("exception"); |
| 92 } | 92 } |
| 93 message->set_argument(*stringified); | 93 message->set_argument(*stringified); |
| 94 } | 94 } |
| 95 | 95 |
| 96 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); | 96 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 97 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception); | 97 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 310 |
| 311 | 311 |
| 312 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate, | 312 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate, |
| 313 int template_index, | 313 int template_index, |
| 314 Handle<Object> arg) { | 314 Handle<Object> arg) { |
| 315 Factory* factory = isolate->factory(); | 315 Factory* factory = isolate->factory(); |
| 316 Handle<String> result_string; | 316 Handle<String> result_string; |
| 317 if (arg->IsString()) { | 317 if (arg->IsString()) { |
| 318 result_string = Handle<String>::cast(arg); | 318 result_string = Handle<String>::cast(arg); |
| 319 } else { | 319 } else { |
| 320 Handle<JSFunction> fun = isolate->no_side_effects_to_string_fun(); | 320 Handle<JSFunction> fun = isolate->no_side_effect_to_string_fun(); |
| 321 | 321 |
| 322 MaybeHandle<Object> maybe_result = | 322 MaybeHandle<Object> maybe_result = |
| 323 Execution::TryCall(isolate, fun, factory->undefined_value(), 1, &arg); | 323 Execution::TryCall(isolate, fun, factory->undefined_value(), 1, &arg); |
| 324 Handle<Object> result; | 324 Handle<Object> result; |
| 325 if (!maybe_result.ToHandle(&result) || !result->IsString()) { | 325 if (!maybe_result.ToHandle(&result) || !result->IsString()) { |
| 326 return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>")); | 326 return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>")); |
| 327 } | 327 } |
| 328 result_string = Handle<String>::cast(result); | 328 result_string = Handle<String>::cast(result); |
| 329 } | 329 } |
| 330 MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage( | 330 MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 } | 384 } |
| 385 } else { | 385 } else { |
| 386 builder.AppendCharacter(*c); | 386 builder.AppendCharacter(*c); |
| 387 } | 387 } |
| 388 } | 388 } |
| 389 | 389 |
| 390 return builder.Finish(); | 390 return builder.Finish(); |
| 391 } | 391 } |
| 392 | 392 |
| 393 | 393 |
| 394 MaybeHandle<String> ErrorToStringHelper::Stringify(Isolate* isolate, |
| 395 Handle<JSObject> error) { |
| 396 VisitedScope scope(this, error); |
| 397 if (scope.has_visited()) return isolate->factory()->empty_string(); |
| 398 |
| 399 Handle<String> name; |
| 400 Handle<String> message; |
| 401 Handle<Name> internal_key = isolate->factory()->internal_error_symbol(); |
| 402 Handle<String> message_string = |
| 403 isolate->factory()->NewStringFromStaticChars("message"); |
| 404 Handle<String> name_string = isolate->factory()->name_string(); |
| 405 LookupIterator internal_error_lookup( |
| 406 error, internal_key, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 407 |
| 408 // Find out whether an internally created error object is on the prototype |
| 409 // chain. If the name property is found on a holder prior to the internally |
| 410 // created error object, use that name property. Otherwise just use the |
| 411 // constructor name to avoid triggering possible side effects. |
| 412 // Similar for the message property. If the message property shadows the |
| 413 // internally created error object, use that message property. Otherwise |
| 414 // use empty string as message. |
| 415 LookupIterator name_lookup(error, name_string, |
| 416 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 417 if (internal_error_lookup.IsFound() && |
| 418 !ShadowsInternalError(isolate, &name_lookup, &internal_error_lookup)) { |
| 419 Handle<JSObject> holder = internal_error_lookup.GetHolder<JSObject>(); |
| 420 name = JSReceiver::GetConstructorName(holder); |
| 421 } else { |
| 422 ASSIGN_RETURN_ON_EXCEPTION( |
| 423 isolate, name, |
| 424 GetStringifiedProperty(isolate, &name_lookup, |
| 425 isolate->factory()->Error_string()), |
| 426 String); |
| 427 } |
| 428 |
| 429 LookupIterator message_lookup( |
| 430 error, message_string, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 431 if (internal_error_lookup.IsFound() && |
| 432 !ShadowsInternalError(isolate, &message_lookup, &internal_error_lookup)) { |
| 433 message = isolate->factory()->empty_string(); |
| 434 } else { |
| 435 ASSIGN_RETURN_ON_EXCEPTION( |
| 436 isolate, message, |
| 437 GetStringifiedProperty(isolate, &message_lookup, |
| 438 isolate->factory()->empty_string()), |
| 439 String); |
| 440 } |
| 441 |
| 442 if (name->length() == 0) return message; |
| 443 if (message->length() == 0) return name; |
| 444 IncrementalStringBuilder builder(isolate); |
| 445 builder.AppendString(name); |
| 446 builder.AppendCString(": "); |
| 447 builder.AppendString(message); |
| 448 return builder.Finish(); |
| 449 } |
| 450 |
| 451 |
| 452 bool ErrorToStringHelper::ShadowsInternalError( |
| 453 Isolate* isolate, LookupIterator* property_lookup, |
| 454 LookupIterator* internal_error_lookup) { |
| 455 if (!property_lookup->IsFound()) return false; |
| 456 Handle<JSObject> holder = property_lookup->GetHolder<JSObject>(); |
| 457 // It's fine if the property is defined on the error itself. |
| 458 if (holder.is_identical_to(property_lookup->GetReceiver())) return true; |
| 459 PrototypeIterator it(isolate, holder, PrototypeIterator::START_AT_RECEIVER); |
| 460 while (true) { |
| 461 if (it.IsAtEnd()) return false; |
| 462 if (it.IsAtEnd(internal_error_lookup->GetHolder<JSObject>())) return true; |
| 463 it.AdvanceIgnoringProxies(); |
| 464 } |
| 465 } |
| 466 |
| 467 |
| 468 MaybeHandle<String> ErrorToStringHelper::GetStringifiedProperty( |
| 469 Isolate* isolate, LookupIterator* property_lookup, |
| 470 Handle<String> default_value) { |
| 471 if (!property_lookup->IsFound()) return default_value; |
| 472 Handle<Object> obj; |
| 473 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Object::GetProperty(property_lookup), |
| 474 String); |
| 475 if (obj->IsUndefined()) return default_value; |
| 476 if (!obj->IsString()) { |
| 477 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Object::ToString(isolate, obj), |
| 478 String); |
| 479 } |
| 480 return Handle<String>::cast(obj); |
| 481 } |
| 482 |
| 394 } // namespace internal | 483 } // namespace internal |
| 395 } // namespace v8 | 484 } // namespace v8 |
| OLD | NEW |