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

Side by Side Diff: src/messages.cc

Issue 1281833002: Rewrite Error.prototype.toString in C++. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: address comments Created 5 years, 4 months 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/messages.js » ('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/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/heap/spaces-inl.h" 9 #include "src/heap/spaces-inl.h"
10 #include "src/messages.h" 10 #include "src/messages.h"
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 Handle<String> arg = args[i++]; 361 Handle<String> arg = args[i++];
362 builder.AppendString(arg); 362 builder.AppendString(arg);
363 } 363 }
364 } else { 364 } else {
365 builder.AppendCharacter(*c); 365 builder.AppendCharacter(*c);
366 } 366 }
367 } 367 }
368 368
369 return builder.Finish(); 369 return builder.Finish();
370 } 370 }
371
372
373 MaybeHandle<String> ErrorToStringHelper::Stringify(Isolate* isolate,
374 Handle<JSObject> error) {
375 VisitedScope scope(this, error);
376 if (scope.has_visited()) return isolate->factory()->empty_string();
377
378 Handle<String> name;
379 Handle<String> message;
380 Handle<Name> internal_key = isolate->factory()->internal_error_symbol();
381 Handle<String> message_string =
382 isolate->factory()->NewStringFromStaticChars("message");
383 Handle<String> name_string = isolate->factory()->name_string();
384 LookupIterator internal_error_lookup(
385 error, internal_key, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
386 LookupIterator message_lookup(
387 error, message_string, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
388 LookupIterator name_lookup(error, name_string,
389 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
390
391 // Find out whether an internally created error object is on the prototype
392 // chain. If the name property is found on a holder prior to the internally
393 // created error object, use that name property. Otherwise just use the
394 // constructor name to avoid triggering possible side effects.
395 // Similar for the message property. If the message property shadows the
396 // internally created error object, use that messingage property. Otherwise
Jakob Kummerow 2015/08/10 11:07:28 nit: typo
397 // use empty string as message.
398 if (internal_error_lookup.IsFound()) {
399 if (!ShadowsInternalError(isolate, &name_lookup, &internal_error_lookup)) {
400 Handle<JSObject> holder = internal_error_lookup.GetHolder<JSObject>();
401 name = Handle<String>(holder->constructor_name());
402 }
403 if (!ShadowsInternalError(isolate, &message_lookup,
404 &internal_error_lookup)) {
405 message = isolate->factory()->empty_string();
406 }
407 }
408 if (name.is_null()) {
409 ASSIGN_RETURN_ON_EXCEPTION(
410 isolate, name,
411 GetStringifiedProperty(isolate, &name_lookup,
412 isolate->factory()->Error_string()),
413 String);
414 }
415 if (message.is_null()) {
416 ASSIGN_RETURN_ON_EXCEPTION(
417 isolate, message,
418 GetStringifiedProperty(isolate, &message_lookup,
419 isolate->factory()->empty_string()),
420 String);
421 }
422
423 if (name->length() == 0) return message;
424 if (message->length() == 0) return name;
425 IncrementalStringBuilder builder(isolate);
426 builder.AppendString(name);
427 builder.AppendCString(": ");
428 builder.AppendString(message);
429 return builder.Finish();
430 }
431
432
433 bool ErrorToStringHelper::ShadowsInternalError(
434 Isolate* isolate, LookupIterator* property_lookup,
435 LookupIterator* internal_error_lookup) {
436 Handle<JSObject> holder = property_lookup->GetHolder<JSObject>();
437 // It's fine if the property is defined on the error itself.
438 if (holder.is_identical_to(property_lookup->GetReceiver())) return true;
439 PrototypeIterator it(isolate, holder, PrototypeIterator::START_AT_RECEIVER);
440 while (true) {
441 if (it.IsAtEnd()) return false;
442 if (it.IsAtEnd(internal_error_lookup->GetHolder<JSObject>())) return true;
443 it.AdvanceIgnoringProxies();
444 }
445 }
446
447
448 MaybeHandle<String> ErrorToStringHelper::GetStringifiedProperty(
449 Isolate* isolate, LookupIterator* property_lookup,
450 Handle<String> default_value) {
451 if (!property_lookup->IsFound()) return default_value;
452 Handle<Object> obj;
453 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Object::GetProperty(property_lookup),
454 String);
455 if (obj->IsUndefined()) return default_value;
456 if (!obj->IsString()) {
457 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Execution::ToString(isolate, obj),
458 String);
459 }
460 return Handle<String>::cast(obj);
461 }
462
371 } // namespace internal 463 } // namespace internal
372 } // namespace v8 464 } // namespace v8
OLDNEW
« no previous file with comments | « src/messages.h ('k') | src/messages.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698