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

Side by Side Diff: src/messages.cc

Issue 1507273002: Make Error.prototype.toString spec compliant; and fix various side-effect-free error printing metho… (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 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
« no previous file with comments | « src/messages.h ('k') | src/objects.h » ('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 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 Handle<Object> exception(exception_object, isolate); 75 Handle<Object> exception(exception_object, isolate);
76 76
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
86 MaybeHandle<Object> maybe_stringified = Execution::TryCall( 86 MaybeHandle<Object> maybe_stringified;
87 isolate, isolate->to_detail_string_fun(),
88 isolate->factory()->undefined_value(), arraysize(args), args);
89 Handle<Object> stringified; 87 Handle<Object> stringified;
88 // Make sure we don't leak uncaught internally generated Error objects.
89 if (Object::IsErrorObject(isolate, argument)) {
Yang 2015/12/09 15:08:10 this also happens if the user creates an Error obj
90 Handle<Object> args[] = {argument};
91 maybe_stringified = Execution::TryCall(
92 isolate, isolate->no_side_effects_to_string_fun(),
93 isolate->factory()->undefined_value(), arraysize(args), args);
94 } else {
95 v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
96 catcher.SetVerbose(false);
97 catcher.SetCaptureMessage(false);
98
99 maybe_stringified = Object::ToString(isolate, argument);
100 }
101
90 if (!maybe_stringified.ToHandle(&stringified)) { 102 if (!maybe_stringified.ToHandle(&stringified)) {
91 stringified = isolate->factory()->NewStringFromAsciiChecked("exception"); 103 stringified = isolate->factory()->NewStringFromAsciiChecked("exception");
92 } 104 }
93 message->set_argument(*stringified); 105 message->set_argument(*stringified);
94 } 106 }
95 107
96 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); 108 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
97 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception); 109 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception);
98 110
99 v8::NeanderArray global_listeners(isolate->factory()->message_listeners()); 111 v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 322
311 323
312 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate, 324 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate,
313 int template_index, 325 int template_index,
314 Handle<Object> arg) { 326 Handle<Object> arg) {
315 Factory* factory = isolate->factory(); 327 Factory* factory = isolate->factory();
316 Handle<String> result_string; 328 Handle<String> result_string;
317 if (arg->IsString()) { 329 if (arg->IsString()) {
318 result_string = Handle<String>::cast(arg); 330 result_string = Handle<String>::cast(arg);
319 } else { 331 } else {
320 Handle<JSFunction> fun = isolate->no_side_effect_to_string_fun(); 332 Handle<JSFunction> fun = isolate->no_side_effects_to_string_fun();
321 333
322 MaybeHandle<Object> maybe_result = 334 MaybeHandle<Object> maybe_result =
323 Execution::TryCall(isolate, fun, factory->undefined_value(), 1, &arg); 335 Execution::TryCall(isolate, fun, factory->undefined_value(), 1, &arg);
324 Handle<Object> result; 336 Handle<Object> result;
325 if (!maybe_result.ToHandle(&result) || !result->IsString()) { 337 if (!maybe_result.ToHandle(&result) || !result->IsString()) {
326 return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>")); 338 return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>"));
327 } 339 }
328 result_string = Handle<String>::cast(result); 340 result_string = Handle<String>::cast(result);
329 } 341 }
330 MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage( 342 MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 } 396 }
385 } else { 397 } else {
386 builder.AppendCharacter(*c); 398 builder.AppendCharacter(*c);
387 } 399 }
388 } 400 }
389 401
390 return builder.Finish(); 402 return builder.Finish();
391 } 403 }
392 404
393 405
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
483 } // namespace internal 406 } // namespace internal
484 } // namespace v8 407 } // namespace v8
OLDNEW
« no previous file with comments | « src/messages.h ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698