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

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: add test case 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
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.HasVisited()) 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 =
384 isolate->factory()->NewStringFromStaticChars("name");
Jakob Kummerow 2015/08/08 21:56:09 we already have isolate->factory()->name_string()
385 LookupIterator internal_error_lookup(
386 error, internal_key, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
387 LookupIterator message_lookup(
388 error, message_string, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
389 LookupIterator name_lookup(error, name_string,
390 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
391
392 // Find out whether an internally created error object is on the prototype
393 // chain. If the name property is found on a holder prior to the internally
394 // created error object, use that name property. Otherwise just use the
395 // constructor name to avoid triggering possible side effects.
396 // Similar for the message property. If the message property shadows the
397 // internally created error object, use that message property. Otherwise
398 // use empty string as message.
399 if (internal_error_lookup.IsFound()) {
400 if (!ShadowsInternalError(isolate, &name_lookup, &internal_error_lookup)) {
401 Handle<JSObject> holder = internal_error_lookup.GetHolder<JSObject>();
402 name = Handle<String>(holder->constructor_name());
403 }
404 if (!ShadowsInternalError(isolate, &message_lookup,
405 &internal_error_lookup)) {
406 message = isolate->factory()->empty_string();
407 }
408 }
409 if (name.is_null()) {
410 ASSIGN_RETURN_ON_EXCEPTION(
411 isolate, name,
412 GetStringifiedProperty(isolate, &name_lookup,
413 isolate->factory()->Error_string()),
414 String);
415 }
416 if (message.is_null()) {
417 ASSIGN_RETURN_ON_EXCEPTION(
418 isolate, message,
419 GetStringifiedProperty(isolate, &message_lookup,
420 isolate->factory()->empty_string()),
421 String);
422 }
423
424 if (name->length() == 0) return message;
425 if (message->length() == 0) return name;
426 IncrementalStringBuilder builder(isolate);
427 builder.AppendString(name);
428 builder.AppendCString(": ");
429 builder.AppendString(message);
430 return builder.Finish();
431 }
432
433
434 bool ErrorToStringHelper::ShadowsInternalError(
435 Isolate* isolate, LookupIterator* property_lookup,
436 LookupIterator* internal_error_lookup) {
437 Handle<JSObject> holder = property_lookup->GetHolder<JSObject>();
438 // It's fine if the property is defined on the error itself.
439 if (holder.is_identical_to(property_lookup->GetReceiver())) return true;
440 PrototypeIterator it(isolate, holder, PrototypeIterator::START_AT_RECEIVER);
441 while (true) {
442 if (it.IsAtEnd()) return false;
443 if (it.IsAtEnd(internal_error_lookup->GetHolder<JSObject>())) return true;
444 it.AdvanceIgnoringProxies();
445 }
446 }
447
448
449 MaybeHandle<String> ErrorToStringHelper::GetStringifiedProperty(
450 Isolate* isolate, LookupIterator* property_lookup,
451 Handle<String> default_value) {
452 if (!property_lookup->IsFound()) return default_value;
453 Handle<Object> obj;
454 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Object::GetProperty(property_lookup),
455 String);
456 if (obj->IsUndefined()) return default_value;
457 if (!obj->IsString()) {
458 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Execution::ToString(isolate, obj),
459 String);
460 }
461 return Handle<String>::cast(obj);
462 }
463
371 } // namespace internal 464 } // namespace internal
372 } // namespace v8 465 } // namespace v8
OLDNEW
« src/messages.h ('K') | « src/messages.h ('k') | src/messages.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698