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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/bootstrapper.h" | |
11 #include "src/execution.h" | 10 #include "src/execution.h" |
12 #include "src/isolate-inl.h" | 11 #include "src/isolate-inl.h" |
13 #include "src/keys.h" | 12 #include "src/keys.h" |
14 #include "src/string-builder.h" | 13 #include "src/string-builder.h" |
15 #include "src/wasm/wasm-module.h" | 14 #include "src/wasm/wasm-module.h" |
16 | 15 |
17 namespace v8 { | 16 namespace v8 { |
18 namespace internal { | 17 namespace internal { |
19 | 18 |
20 MessageLocation::MessageLocation(Handle<Script> script, int start_pos, | 19 MessageLocation::MessageLocation(Handle<Script> script, int start_pos, |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 // Return null in case of duplicates to avoid confusion. | 296 // Return null in case of duplicates to avoid confusion. |
298 if (!result.is_null()) return isolate_->factory()->null_value(); | 297 if (!result.is_null()) return isolate_->factory()->null_value(); |
299 result = inner_scope.CloseAndEscape(name_key); | 298 result = inner_scope.CloseAndEscape(name_key); |
300 } | 299 } |
301 } | 300 } |
302 | 301 |
303 if (!result.is_null()) return outer_scope.CloseAndEscape(result); | 302 if (!result.is_null()) return outer_scope.CloseAndEscape(result); |
304 return isolate_->factory()->null_value(); | 303 return isolate_->factory()->null_value(); |
305 } | 304 } |
306 | 305 |
306 Handle<Object> CallSite::GetTypeName() { | |
307 // TODO(jgruber): Check for strict/constructor here as in | |
308 // CallSitePrototypeGetThis. | |
309 | |
310 if (receiver_->IsNull(isolate_) || receiver_->IsUndefined(isolate_)) | |
311 return isolate_->factory()->null_value(); | |
312 | |
313 if (receiver_->IsJSProxy()) return isolate_->factory()->Proxy_string(); | |
314 | |
315 Handle<JSReceiver> receiver_object = | |
316 Object::ToObject(isolate_, receiver_).ToHandleChecked(); | |
317 return JSReceiver::GetConstructorName(receiver_object); | |
318 } | |
319 | |
320 namespace { | |
321 | |
322 Object* EvalFromFunctionName(Isolate* isolate, Handle<Script> script) { | |
323 if (script->eval_from_shared()->IsUndefined(isolate)) | |
324 return *isolate->factory()->undefined_value(); | |
325 | |
326 Handle<SharedFunctionInfo> shared( | |
327 SharedFunctionInfo::cast(script->eval_from_shared())); | |
328 // Find the name of the function calling eval. | |
329 if (shared->name()->BooleanValue()) { | |
330 return shared->name(); | |
331 } | |
332 | |
333 return shared->inferred_name(); | |
334 } | |
335 | |
336 Object* EvalFromScript(Isolate* isolate, Handle<Script> script) { | |
337 if (script->eval_from_shared()->IsUndefined(isolate)) | |
338 return *isolate->factory()->undefined_value(); | |
339 | |
340 Handle<SharedFunctionInfo> eval_from_shared( | |
341 SharedFunctionInfo::cast(script->eval_from_shared())); | |
342 return eval_from_shared->script()->IsScript() | |
343 ? eval_from_shared->script() | |
344 : *isolate->factory()->undefined_value(); | |
345 } | |
346 | |
347 MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) { | |
348 Handle<Object> sourceURL = Script::GetNameOrSourceURL(script); | |
349 if (!sourceURL->IsUndefined(isolate)) { | |
350 DCHECK(sourceURL->IsString()); | |
351 return Handle<String>::cast(sourceURL); | |
352 } | |
353 | |
354 IncrementalStringBuilder builder(isolate); | |
355 builder.AppendCString("eval at "); | |
356 | |
357 Handle<Object> eval_from_function_name = | |
358 handle(EvalFromFunctionName(isolate, script), isolate); | |
359 if (eval_from_function_name->BooleanValue()) { | |
360 Handle<String> str; | |
361 ASSIGN_RETURN_ON_EXCEPTION( | |
362 isolate, str, Object::ToString(isolate, eval_from_function_name), | |
363 String); | |
364 builder.AppendString(str); | |
365 } else { | |
366 builder.AppendCString("<anonymous>"); | |
367 } | |
368 | |
369 Handle<Object> eval_from_script_obj = | |
370 handle(EvalFromScript(isolate, script), isolate); | |
371 if (eval_from_script_obj->IsScript()) { | |
372 Handle<Script> eval_from_script = | |
373 Handle<Script>::cast(eval_from_script_obj); | |
374 builder.AppendCString(" ("); | |
375 if (eval_from_script->compilation_type() == Script::COMPILATION_TYPE_EVAL) { | |
376 // Eval script originated from another eval. | |
377 Handle<String> str; | |
378 ASSIGN_RETURN_ON_EXCEPTION( | |
379 isolate, str, FormatEvalOrigin(isolate, eval_from_script), String); | |
380 builder.AppendString(str); | |
381 } else { | |
382 DCHECK(eval_from_script->compilation_type() != | |
383 Script::COMPILATION_TYPE_EVAL); | |
384 // eval script originated from "real" source. | |
385 Handle<Object> name_obj = handle(eval_from_script->name(), isolate); | |
386 if (eval_from_script->name()->IsString()) { | |
387 builder.AppendString(Handle<String>::cast(name_obj)); | |
388 | |
389 Script::PositionInfo info; | |
390 if (eval_from_script->GetPositionInfo(script->GetEvalPosition(), &info, | |
391 Script::NO_OFFSET)) { | |
392 builder.AppendCString(":"); | |
393 | |
394 Handle<String> str = isolate->factory()->NumberToString( | |
395 handle(Smi::FromInt(info.line + 1), isolate)); | |
396 builder.AppendString(str); | |
397 | |
398 builder.AppendCString(":"); | |
399 | |
400 str = isolate->factory()->NumberToString( | |
401 handle(Smi::FromInt(info.column + 1), isolate)); | |
402 builder.AppendString(str); | |
403 } | |
404 } else { | |
405 DCHECK(!eval_from_script->name()->IsString()); | |
406 builder.AppendCString("unknown source"); | |
407 } | |
408 } | |
409 builder.AppendCString(")"); | |
410 } | |
411 | |
412 Handle<String> result; | |
413 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String); | |
414 return result; | |
415 } | |
416 | |
417 } // namespace | |
418 | |
419 Handle<Object> CallSite::GetEvalOrigin() { | |
420 if (IsWasm()) return isolate_->factory()->undefined_value(); | |
421 DCHECK(IsJavaScript()); | |
422 | |
423 Handle<Object> script = handle(fun_->shared()->script(), isolate_); | |
424 if (!script->IsScript()) return isolate_->factory()->undefined_value(); | |
425 | |
426 return FormatEvalOrigin(isolate_, Handle<Script>::cast(script)) | |
427 .ToHandleChecked(); | |
428 } | |
307 | 429 |
308 int CallSite::GetLineNumber() { | 430 int CallSite::GetLineNumber() { |
309 if (pos_ >= 0 && IsJavaScript()) { | 431 if (pos_ >= 0 && IsJavaScript()) { |
310 Handle<Object> script_obj(fun_->shared()->script(), isolate_); | 432 Handle<Object> script_obj(fun_->shared()->script(), isolate_); |
311 if (script_obj->IsScript()) { | 433 if (script_obj->IsScript()) { |
312 Handle<Script> script = Handle<Script>::cast(script_obj); | 434 Handle<Script> script = Handle<Script>::cast(script_obj); |
313 return Script::GetLineNumber(script, pos_) + 1; | 435 return Script::GetLineNumber(script, pos_) + 1; |
314 } | 436 } |
315 } | 437 } |
316 return -1; | 438 return -1; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 // receiver. | 480 // receiver. |
359 Object* ctor_symbol = isolate_->heap()->call_site_constructor_symbol(); | 481 Object* ctor_symbol = isolate_->heap()->call_site_constructor_symbol(); |
360 if (*receiver_ == ctor_symbol) return true; | 482 if (*receiver_ == ctor_symbol) return true; |
361 if (!IsJavaScript() || !receiver_->IsJSObject()) return false; | 483 if (!IsJavaScript() || !receiver_->IsJSObject()) return false; |
362 Handle<Object> constructor = | 484 Handle<Object> constructor = |
363 JSReceiver::GetDataProperty(Handle<JSObject>::cast(receiver_), | 485 JSReceiver::GetDataProperty(Handle<JSObject>::cast(receiver_), |
364 isolate_->factory()->constructor_string()); | 486 isolate_->factory()->constructor_string()); |
365 return constructor.is_identical_to(fun_); | 487 return constructor.is_identical_to(fun_); |
366 } | 488 } |
367 | 489 |
490 namespace { | |
491 | |
492 // Convert the raw frames as written by Isolate::CaptureSimpleStackTrace into | |
493 // a vector of JS CallSite objects. | |
494 MaybeHandle<FixedArray> GetStackFrames(Isolate* isolate, | |
495 Handle<Object> raw_stack) { | |
496 DCHECK(raw_stack->IsJSArray()); | |
497 Handle<JSArray> raw_stack_array = Handle<JSArray>::cast(raw_stack); | |
498 | |
499 DCHECK(raw_stack_array->elements()->IsFixedArray()); | |
500 Handle<FixedArray> raw_stack_elements = | |
501 handle(FixedArray::cast(raw_stack_array->elements()), isolate); | |
502 | |
503 const int raw_stack_len = raw_stack_elements->length(); | |
504 DCHECK(raw_stack_len % 4 == 1); // Multiples of 4 plus sloppy frames count. | |
505 const int frame_count = (raw_stack_len - 1) / 4; | |
506 | |
507 Handle<Object> sloppy_frames_obj = | |
508 FixedArray::get(*raw_stack_elements, 0, isolate); | |
509 int sloppy_frames = Handle<Smi>::cast(sloppy_frames_obj)->value(); | |
510 | |
511 Handle<JSFunction> callsite_ctor = | |
512 handle(isolate->native_context()->callsite_function(), isolate); | |
513 | |
514 int dst_ix = 0; | |
515 Handle<FixedArray> frames = isolate->factory()->NewFixedArray(frame_count); | |
516 for (int i = 1; i < raw_stack_len; i += 4) { | |
517 Handle<Object> recv = FixedArray::get(*raw_stack_elements, i, isolate); | |
518 Handle<Object> fun = FixedArray::get(*raw_stack_elements, i + 1, isolate); | |
519 Handle<AbstractCode> code = Handle<AbstractCode>::cast( | |
520 FixedArray::get(*raw_stack_elements, i + 2, isolate)); | |
521 Handle<Smi> pc = | |
522 Handle<Smi>::cast(FixedArray::get(*raw_stack_elements, i + 3, isolate)); | |
523 | |
524 Handle<Object> pos = | |
525 (fun->IsSmi() && pc->value() < 0) | |
526 ? handle(Smi::FromInt(-1 - pc->value()), isolate) | |
527 : handle(Smi::FromInt(code->SourcePosition(pc->value())), isolate); | |
528 | |
529 sloppy_frames--; | |
530 Handle<Object> strict = isolate->factory()->ToBoolean(sloppy_frames < 0); | |
531 | |
532 Handle<Object> callsite; | |
533 ASSIGN_RETURN_ON_EXCEPTION( | |
534 isolate, callsite, | |
535 CallSiteUtils::Construct(isolate, callsite_ctor, callsite_ctor, recv, | |
536 fun, pos, strict), | |
537 FixedArray); | |
538 | |
539 frames->set(dst_ix++, *callsite); | |
540 } | |
541 | |
542 DCHECK_EQ(frame_count, dst_ix); | |
543 return frames; | |
544 } | |
545 | |
546 MaybeHandle<Object> AppendErrorString(Isolate* isolate, Handle<Object> error, | |
547 IncrementalStringBuilder* builder) { | |
548 MaybeHandle<String> err_str = | |
549 ErrorUtils::ToString(isolate, Handle<Object>::cast(error)); | |
550 if (err_str.is_null()) { | |
551 // Error.toString threw. Try to return a string representation of the thrown | |
552 // exception instead. | |
553 | |
554 DCHECK(isolate->has_pending_exception()); | |
555 Handle<Object> pending_exception = | |
556 handle(isolate->pending_exception(), isolate); | |
557 isolate->clear_pending_exception(); | |
558 | |
559 err_str = ErrorUtils::ToString(isolate, pending_exception); | |
560 if (err_str.is_null()) { | |
561 // Formatting the thrown exception threw again, give up. | |
562 DCHECK(isolate->has_pending_exception()); | |
563 isolate->clear_pending_exception(); | |
564 | |
565 builder->AppendCString("<error>"); | |
566 } else { | |
567 // Formatted thrown exception successfully, append it. | |
568 builder->AppendCString("<error: "); | |
569 builder->AppendString(err_str.ToHandleChecked()); | |
570 builder->AppendCharacter('>'); | |
571 } | |
572 } else { | |
573 builder->AppendString(err_str.ToHandleChecked()); | |
574 } | |
575 | |
576 return error; | |
577 } | |
578 | |
579 class PrepareStackTraceScope { | |
580 public: | |
581 explicit PrepareStackTraceScope(Isolate* isolate) : isolate_(isolate) { | |
582 DCHECK(!isolate_->formatting_stack_trace()); | |
583 isolate_->set_formatting_stack_trace(true); | |
584 } | |
585 | |
586 ~PrepareStackTraceScope() { isolate_->set_formatting_stack_trace(false); } | |
587 | |
588 private: | |
589 Isolate* isolate_; | |
590 | |
591 DISALLOW_COPY_AND_ASSIGN(PrepareStackTraceScope); | |
592 }; | |
593 | |
594 } // namespace | |
595 | |
368 MaybeHandle<Object> FormatStackTrace(Isolate* isolate, Handle<JSObject> error, | 596 MaybeHandle<Object> FormatStackTrace(Isolate* isolate, Handle<JSObject> error, |
369 Handle<Object> stack_trace) { | 597 Handle<Object> raw_stack) { |
370 // TODO(jgruber): Port FormatStackTrace from JS. | 598 // Create JS CallSite objects from the raw stack frame array. |
371 Handle<JSFunction> fun = isolate->error_format_stack_trace(); | |
372 | 599 |
373 int argc = 2; | 600 Handle<FixedArray> frames; |
374 ScopedVector<Handle<Object>> argv(argc); | 601 ASSIGN_RETURN_ON_EXCEPTION(isolate, frames, |
375 argv[0] = error; | 602 GetStackFrames(isolate, raw_stack), Object); |
376 argv[1] = stack_trace; | |
377 | 603 |
378 Handle<Object> formatted_stack_trace; | 604 // If there's a user-specified "prepareStackFrames" function, call it on the |
605 // frames and use its result. | |
606 | |
607 Handle<JSFunction> global_error = isolate->error_function(); | |
608 Handle<Object> prepare_stack_trace; | |
379 ASSIGN_RETURN_ON_EXCEPTION( | 609 ASSIGN_RETURN_ON_EXCEPTION( |
380 isolate, formatted_stack_trace, | 610 isolate, prepare_stack_trace, |
381 Execution::Call(isolate, fun, error, argc, argv.start()), Object); | 611 JSFunction::GetProperty(isolate, global_error, "prepareStackTrace"), |
612 Object); | |
382 | 613 |
383 return formatted_stack_trace; | 614 const bool in_recursion = isolate->formatting_stack_trace(); |
615 if (prepare_stack_trace->IsJSFunction() && !in_recursion) { | |
616 PrepareStackTraceScope scope(isolate); | |
617 Handle<JSArray> array = isolate->factory()->NewJSArrayWithElements(frames); | |
618 | |
619 const int argc = 2; | |
620 ScopedVector<Handle<Object>> argv(argc); | |
621 argv[0] = error; | |
622 argv[1] = array; | |
623 | |
624 Handle<Object> result; | |
625 ASSIGN_RETURN_ON_EXCEPTION( | |
626 isolate, result, Execution::Call(isolate, prepare_stack_trace, | |
627 global_error, argc, argv.start()), | |
628 Object); | |
629 | |
630 return result; | |
631 } | |
632 | |
633 IncrementalStringBuilder builder(isolate); | |
634 | |
635 RETURN_ON_EXCEPTION(isolate, AppendErrorString(isolate, error, &builder), | |
636 Object); | |
637 | |
638 for (int i = 0; i < frames->length(); i++) { | |
639 builder.AppendCString("\n at "); | |
640 | |
641 Handle<Object> frame = FixedArray::get(*frames, i, isolate); | |
642 MaybeHandle<String> maybe_frame_string = | |
643 CallSiteUtils::ToString(isolate, frame); | |
644 if (maybe_frame_string.is_null()) { | |
645 // CallSite.toString threw. Try to return a string representation of the | |
646 // thrown exception instead. | |
647 | |
648 DCHECK(isolate->has_pending_exception()); | |
649 Handle<Object> pending_exception = | |
650 handle(isolate->pending_exception(), isolate); | |
651 isolate->clear_pending_exception(); | |
652 | |
653 maybe_frame_string = ErrorUtils::ToString(isolate, pending_exception); | |
654 if (maybe_frame_string.is_null()) { | |
655 // Formatting the thrown exception threw again, give up. | |
656 | |
657 builder.AppendCString("<error>"); | |
658 } else { | |
659 // Formatted thrown exception successfully, append it. | |
660 builder.AppendCString("<error: "); | |
661 builder.AppendString(maybe_frame_string.ToHandleChecked()); | |
662 builder.AppendCString("<error>"); | |
663 } | |
664 } else { | |
665 // CallSite.toString completed without throwing. | |
666 builder.AppendString(maybe_frame_string.ToHandleChecked()); | |
667 } | |
668 } | |
669 | |
670 RETURN_RESULT(isolate, builder.Finish(), Object); | |
384 } | 671 } |
385 | 672 |
386 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate, | 673 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate, |
387 int template_index, | 674 int template_index, |
388 Handle<Object> arg) { | 675 Handle<Object> arg) { |
389 Factory* factory = isolate->factory(); | 676 Factory* factory = isolate->factory(); |
390 Handle<String> result_string; | 677 Handle<String> result_string; |
391 if (arg->IsString()) { | 678 if (arg->IsString()) { |
392 result_string = Handle<String>::cast(arg); | 679 result_string = Handle<String>::cast(arg); |
393 } else { | 680 } else { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 builder.AppendString(arg); | 744 builder.AppendString(arg); |
458 } | 745 } |
459 } else { | 746 } else { |
460 builder.AppendCharacter(*c); | 747 builder.AppendCharacter(*c); |
461 } | 748 } |
462 } | 749 } |
463 | 750 |
464 return builder.Finish(); | 751 return builder.Finish(); |
465 } | 752 } |
466 | 753 |
467 MaybeHandle<Object> ConstructError(Isolate* isolate, Handle<JSFunction> target, | 754 MaybeHandle<Object> ErrorUtils::Construct( |
468 Handle<Object> new_target, | 755 Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target, |
469 Handle<Object> message, FrameSkipMode mode, | 756 Handle<Object> message, FrameSkipMode mode, bool suppress_detailed_trace) { |
470 bool suppress_detailed_trace) { | |
471 // 1. If NewTarget is undefined, let newTarget be the active function object, | 757 // 1. If NewTarget is undefined, let newTarget be the active function object, |
472 // else let newTarget be NewTarget. | 758 // else let newTarget be NewTarget. |
473 | 759 |
474 Handle<JSReceiver> new_target_recv = | 760 Handle<JSReceiver> new_target_recv = |
475 new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target) | 761 new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target) |
476 : Handle<JSReceiver>::cast(target); | 762 : Handle<JSReceiver>::cast(target); |
477 | 763 |
478 // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", | 764 // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", |
479 // « [[ErrorData]] »). | 765 // « [[ErrorData]] »). |
480 Handle<JSObject> err; | 766 Handle<JSObject> err; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 } | 800 } |
515 | 801 |
516 // Capture a simple stack trace for the stack property. | 802 // Capture a simple stack trace for the stack property. |
517 RETURN_ON_EXCEPTION(isolate, | 803 RETURN_ON_EXCEPTION(isolate, |
518 isolate->CaptureAndSetSimpleStackTrace(err, mode, caller), | 804 isolate->CaptureAndSetSimpleStackTrace(err, mode, caller), |
519 Object); | 805 Object); |
520 | 806 |
521 return err; | 807 return err; |
522 } | 808 } |
523 | 809 |
810 namespace { | |
811 | |
812 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, | |
813 Handle<JSReceiver> recv, | |
814 Handle<String> key, | |
815 Handle<String> default_str) { | |
816 Handle<Object> obj; | |
817 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, JSObject::GetProperty(recv, key), | |
818 String); | |
819 | |
820 Handle<String> str; | |
821 if (obj->IsUndefined(isolate)) { | |
822 str = default_str; | |
823 } else { | |
824 ASSIGN_RETURN_ON_EXCEPTION(isolate, str, Object::ToString(isolate, obj), | |
825 String); | |
826 } | |
827 | |
828 return str; | |
829 } | |
830 | |
831 } // namespace | |
832 | |
833 // ES6 section 19.5.3.4 Error.prototype.toString ( ) | |
834 MaybeHandle<String> ErrorUtils::ToString(Isolate* isolate, | |
835 Handle<Object> receiver) { | |
836 // 1. Let O be the this value. | |
837 // 2. If Type(O) is not Object, throw a TypeError exception. | |
838 if (!receiver->IsJSReceiver()) { | |
839 return isolate->Throw<String>(isolate->factory()->NewTypeError( | |
840 MessageTemplate::kIncompatibleMethodReceiver, | |
841 isolate->factory()->NewStringFromAsciiChecked( | |
842 "Error.prototype.toString"), | |
843 receiver)); | |
844 } | |
845 Handle<JSReceiver> recv = Handle<JSReceiver>::cast(receiver); | |
846 | |
847 // 3. Let name be ? Get(O, "name"). | |
848 // 4. If name is undefined, let name be "Error"; otherwise let name be | |
849 // ? ToString(name). | |
850 Handle<String> name_key = isolate->factory()->name_string(); | |
851 Handle<String> name_default = isolate->factory()->Error_string(); | |
852 Handle<String> name; | |
853 ASSIGN_RETURN_ON_EXCEPTION( | |
854 isolate, name, | |
855 GetStringPropertyOrDefault(isolate, recv, name_key, name_default), | |
856 String); | |
857 | |
858 // 5. Let msg be ? Get(O, "message"). | |
859 // 6. If msg is undefined, let msg be the empty String; otherwise let msg be | |
860 // ? ToString(msg). | |
861 Handle<String> msg_key = isolate->factory()->message_string(); | |
862 Handle<String> msg_default = isolate->factory()->empty_string(); | |
863 Handle<String> msg; | |
864 ASSIGN_RETURN_ON_EXCEPTION( | |
865 isolate, msg, | |
866 GetStringPropertyOrDefault(isolate, recv, msg_key, msg_default), String); | |
867 | |
868 // 7. If name is the empty String, return msg. | |
869 // 8. If msg is the empty String, return name. | |
870 if (name->length() == 0) return msg; | |
871 if (msg->length() == 0) return name; | |
872 | |
873 // 9. Return the result of concatenating name, the code unit 0x003A (COLON), | |
874 // the code unit 0x0020 (SPACE), and msg. | |
875 IncrementalStringBuilder builder(isolate); | |
876 builder.AppendString(name); | |
877 builder.AppendCString(": "); | |
878 builder.AppendString(msg); | |
879 | |
880 Handle<String> result; | |
881 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String); | |
882 return result; | |
883 } | |
884 | |
885 #define SET_CALLSITE_PROPERTY(target, key, value) \ | |
886 RETURN_ON_EXCEPTION( \ | |
887 isolate, JSObject::SetOwnPropertyIgnoreAttributes( \ | |
888 target, isolate->factory()->key(), value, DONT_ENUM), \ | |
889 Object) | |
890 | |
891 MaybeHandle<Object> CallSiteUtils::Construct( | |
892 Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target, | |
893 Handle<Object> receiver, Handle<Object> fun, Handle<Object> pos, | |
894 Handle<Object> strict_mode) { | |
895 // Create the JS object. | |
896 | |
897 Handle<JSReceiver> new_target_recv = | |
898 new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target) | |
899 : Handle<JSReceiver>::cast(target); | |
900 | |
901 Handle<JSObject> obj; | |
902 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, | |
903 JSObject::New(target, new_target_recv), Object); | |
904 | |
905 // For wasm frames, receiver is the wasm object and fun is the function index | |
906 // instead of an actual function. | |
907 const bool is_wasm_object = | |
908 receiver->IsJSObject() && wasm::IsWasmObject(JSObject::cast(*receiver)); | |
909 if (!fun->IsJSFunction() && !is_wasm_object) { | |
910 THROW_NEW_ERROR(isolate, | |
911 NewTypeError(MessageTemplate::kCallSiteExpectsFunction, | |
912 Object::TypeOf(isolate, receiver), | |
913 Object::TypeOf(isolate, fun)), | |
914 Object); | |
915 } | |
916 | |
917 if (is_wasm_object) { | |
918 DCHECK(fun->IsSmi()); | |
919 DCHECK(wasm::GetNumberOfFunctions(JSObject::cast(*receiver)) > | |
920 Smi::cast(*fun)->value()); | |
921 | |
922 SET_CALLSITE_PROPERTY(obj, call_site_wasm_obj_symbol, receiver); | |
923 SET_CALLSITE_PROPERTY(obj, call_site_wasm_func_index_symbol, fun); | |
924 } else { | |
925 DCHECK(fun->IsJSFunction()); | |
926 SET_CALLSITE_PROPERTY(obj, call_site_receiver_symbol, receiver); | |
927 SET_CALLSITE_PROPERTY(obj, call_site_function_symbol, fun); | |
928 } | |
929 | |
930 Handle<Object> pos_int32; | |
931 ASSIGN_RETURN_ON_EXCEPTION(isolate, pos_int32, Object::ToInt32(isolate, pos), | |
Yang
2016/08/01 12:31:20
Can we check this for being a Smi as well and omit
jgruber
2016/08/01 14:01:08
Done.
| |
932 Object); | |
933 SET_CALLSITE_PROPERTY(obj, call_site_position_symbol, pos_int32); | |
934 SET_CALLSITE_PROPERTY( | |
935 obj, call_site_strict_symbol, | |
936 isolate->factory()->ToBoolean(strict_mode->BooleanValue())); | |
937 | |
938 return obj; | |
939 } | |
940 | |
941 #undef SET_CALLSITE_PROPERTY | |
942 | |
943 namespace { | |
944 | |
945 bool IsNonEmptyString(Handle<Object> object) { | |
946 return (object->IsString() && String::cast(*object)->length() > 0); | |
947 } | |
948 | |
949 MaybeHandle<JSObject> AppendWasmToString(Isolate* isolate, | |
950 Handle<JSObject> recv, | |
951 CallSite* call_site, | |
952 IncrementalStringBuilder* builder) { | |
953 Handle<Object> name = call_site->GetFunctionName(); | |
954 if (name->IsNull(isolate)) { | |
955 builder->AppendCString("<WASM UNNAMED>"); | |
956 } else { | |
957 DCHECK(name->IsString()); | |
958 builder->AppendString(Handle<String>::cast(name)); | |
959 } | |
960 | |
961 builder->AppendCString(" (<WASM>["); | |
962 | |
963 Handle<String> ix = isolate->factory()->NumberToString( | |
964 handle(Smi::FromInt(call_site->wasm_func_index()), isolate)); | |
965 builder->AppendString(ix); | |
966 | |
967 builder->AppendCString("]+"); | |
968 | |
969 Handle<Object> pos; | |
970 ASSIGN_RETURN_ON_EXCEPTION( | |
971 isolate, pos, JSObject::GetProperty( | |
972 recv, isolate->factory()->call_site_position_symbol()), | |
973 JSObject); | |
974 DCHECK(pos->IsNumber()); | |
975 builder->AppendString(isolate->factory()->NumberToString(pos)); | |
976 builder->AppendCString(")"); | |
977 | |
978 return recv; | |
979 } | |
980 | |
981 MaybeHandle<JSObject> AppendFileLocation(Isolate* isolate, | |
982 Handle<JSObject> recv, | |
983 CallSite* call_site, | |
984 IncrementalStringBuilder* builder) { | |
985 if (call_site->IsNative()) { | |
986 builder->AppendCString("native"); | |
987 return recv; | |
988 } | |
989 | |
990 Handle<Object> file_name = call_site->GetScriptNameOrSourceUrl(); | |
991 if (!file_name->IsString() && call_site->IsEval()) { | |
992 Handle<Object> eval_origin = call_site->GetEvalOrigin(); | |
993 DCHECK(eval_origin->IsString()); | |
994 builder->AppendString(Handle<String>::cast(eval_origin)); | |
995 builder->AppendCString(", "); // Expecting source position to follow. | |
996 } | |
997 | |
998 if (IsNonEmptyString(file_name)) { | |
999 builder->AppendString(Handle<String>::cast(file_name)); | |
1000 } else { | |
1001 // Source code does not originate from a file and is not native, but we | |
1002 // can still get the source position inside the source string, e.g. in | |
1003 // an eval string. | |
1004 builder->AppendCString("<anonymous>"); | |
1005 } | |
1006 | |
1007 int line_number = call_site->GetLineNumber(); | |
1008 if (line_number != -1) { | |
1009 builder->AppendCharacter(':'); | |
1010 Handle<String> line_string = isolate->factory()->NumberToString( | |
1011 handle(Smi::FromInt(line_number), isolate), isolate); | |
1012 builder->AppendString(line_string); | |
1013 | |
1014 int column_number = call_site->GetColumnNumber(); | |
1015 if (column_number != -1) { | |
1016 builder->AppendCharacter(':'); | |
1017 Handle<String> column_string = isolate->factory()->NumberToString( | |
1018 handle(Smi::FromInt(column_number), isolate), isolate); | |
1019 builder->AppendString(column_string); | |
1020 } | |
1021 } | |
1022 | |
1023 return recv; | |
1024 } | |
1025 | |
1026 int StringIndexOf(Isolate* isolate, Handle<String> subject, | |
1027 Handle<String> pattern) { | |
1028 if (pattern->length() > subject->length()) return -1; | |
1029 return String::IndexOf(isolate, subject, pattern, 0); | |
1030 } | |
1031 | |
1032 // Returns true iff | |
1033 // 1. the subject ends with '.' + pattern, or | |
1034 // 2. subject == pattern. | |
1035 bool StringEndsWithMethodName(Isolate* isolate, Handle<String> subject, | |
1036 Handle<String> pattern) { | |
1037 if (String::Equals(subject, pattern)) return true; | |
1038 | |
1039 FlatStringReader subject_reader(isolate, String::Flatten(subject)); | |
1040 FlatStringReader pattern_reader(isolate, String::Flatten(pattern)); | |
1041 | |
1042 int pattern_index = pattern_reader.length() - 1; | |
1043 int subject_index = subject_reader.length() - 1; | |
1044 for (int i = 0; i <= pattern_reader.length(); i++) { // Iterate over len + 1. | |
1045 if (subject_index < 0) { | |
1046 return false; | |
1047 } | |
1048 | |
1049 const uc32 subject_char = subject_reader.Get(subject_index); | |
1050 if (i == pattern_reader.length()) { | |
1051 if (subject_char != '.') return false; | |
1052 } else if (subject_char != pattern_reader.Get(pattern_index)) { | |
1053 return false; | |
1054 } | |
1055 | |
1056 pattern_index--; | |
1057 subject_index--; | |
1058 } | |
1059 | |
1060 return true; | |
1061 } | |
1062 | |
1063 MaybeHandle<JSObject> AppendMethodCall(Isolate* isolate, Handle<JSObject> recv, | |
1064 CallSite* call_site, | |
1065 IncrementalStringBuilder* builder) { | |
1066 Handle<Object> type_name = call_site->GetTypeName(); | |
1067 Handle<Object> method_name = call_site->GetMethodName(); | |
1068 Handle<Object> function_name = call_site->GetFunctionName(); | |
1069 | |
1070 if (IsNonEmptyString(function_name)) { | |
1071 Handle<String> function_string = Handle<String>::cast(function_name); | |
1072 if (IsNonEmptyString(type_name)) { | |
1073 Handle<String> type_string = Handle<String>::cast(type_name); | |
1074 bool starts_with_type_name = | |
1075 (StringIndexOf(isolate, function_string, type_string) == 0); | |
1076 if (!starts_with_type_name) { | |
1077 builder->AppendString(type_string); | |
1078 builder->AppendCharacter('.'); | |
1079 } | |
1080 } | |
1081 builder->AppendString(function_string); | |
1082 | |
1083 if (IsNonEmptyString(method_name)) { | |
1084 Handle<String> method_string = Handle<String>::cast(method_name); | |
1085 if (!StringEndsWithMethodName(isolate, function_string, method_string)) { | |
1086 builder->AppendCString(" [as "); | |
1087 builder->AppendString(method_string); | |
1088 builder->AppendCharacter(']'); | |
1089 } | |
1090 } | |
1091 } else { | |
1092 builder->AppendString(Handle<String>::cast(type_name)); | |
1093 builder->AppendCharacter('.'); | |
1094 if (IsNonEmptyString(method_name)) { | |
1095 builder->AppendString(Handle<String>::cast(method_name)); | |
1096 } else { | |
1097 builder->AppendCString("<anonymous>"); | |
1098 } | |
1099 } | |
1100 | |
1101 return recv; | |
1102 } | |
1103 | |
1104 } // namespace | |
1105 | |
1106 MaybeHandle<String> CallSiteUtils::ToString(Isolate* isolate, | |
1107 Handle<Object> receiver) { | |
1108 if (!receiver->IsJSObject()) { | |
1109 THROW_NEW_ERROR( | |
1110 isolate, | |
1111 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, | |
1112 isolate->factory()->NewStringFromAsciiChecked("toString"), | |
1113 receiver), | |
1114 String); | |
1115 } | |
1116 Handle<JSObject> recv = Handle<JSObject>::cast(receiver); | |
1117 | |
1118 if (!JSReceiver::HasOwnProperty( | |
1119 recv, isolate->factory()->call_site_position_symbol()) | |
1120 .FromMaybe(false)) { | |
1121 THROW_NEW_ERROR( | |
1122 isolate, | |
1123 NewTypeError(MessageTemplate::kCallSiteMethod, | |
1124 isolate->factory()->NewStringFromAsciiChecked("toString")), | |
1125 String); | |
1126 } | |
1127 | |
1128 IncrementalStringBuilder builder(isolate); | |
1129 | |
1130 CallSite call_site(isolate, recv); | |
1131 if (call_site.IsWasm()) { | |
1132 RETURN_ON_EXCEPTION(isolate, | |
1133 AppendWasmToString(isolate, recv, &call_site, &builder), | |
1134 String); | |
1135 RETURN_RESULT(isolate, builder.Finish(), String); | |
1136 } | |
1137 | |
1138 DCHECK(!call_site.IsWasm()); | |
1139 Handle<Object> function_name = call_site.GetFunctionName(); | |
1140 | |
1141 const bool is_toplevel = call_site.IsToplevel(); | |
1142 const bool is_constructor = call_site.IsConstructor(); | |
1143 const bool is_method_call = !(is_toplevel || is_constructor); | |
1144 | |
1145 if (is_method_call) { | |
1146 RETURN_ON_EXCEPTION( | |
1147 isolate, AppendMethodCall(isolate, recv, &call_site, &builder), String); | |
1148 } else if (is_constructor) { | |
1149 builder.AppendCString("new "); | |
1150 if (IsNonEmptyString(function_name)) { | |
1151 builder.AppendString(Handle<String>::cast(function_name)); | |
1152 } else { | |
1153 builder.AppendCString("<anonymous>"); | |
1154 } | |
1155 } else if (IsNonEmptyString(function_name)) { | |
1156 builder.AppendString(Handle<String>::cast(function_name)); | |
1157 } else { | |
1158 RETURN_ON_EXCEPTION(isolate, | |
1159 AppendFileLocation(isolate, recv, &call_site, &builder), | |
1160 String); | |
1161 RETURN_RESULT(isolate, builder.Finish(), String); | |
1162 } | |
1163 | |
1164 builder.AppendCString(" ("); | |
1165 RETURN_ON_EXCEPTION( | |
1166 isolate, AppendFileLocation(isolate, recv, &call_site, &builder), String); | |
1167 builder.AppendCString(")"); | |
1168 | |
1169 RETURN_RESULT(isolate, builder.Finish(), String); | |
1170 } | |
1171 | |
524 } // namespace internal | 1172 } // namespace internal |
525 } // namespace v8 | 1173 } // namespace v8 |
OLD | NEW |