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

Side by Side Diff: test/cctest/test-api.cc

Issue 9310122: When rethrowing an exception, print the stack trace of its original site instead of rethrow site. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: . Created 8 years, 10 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 | Annotate | Revision Log
« src/isolate.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 13517 matching lines...) Expand 10 before | Expand all | Expand 10 after
13528 " 'isConstructor'];\n" 13528 " 'isConstructor'];\n"
13529 "for (var i = 0; i < setters.length; i++) {\n" 13529 "for (var i = 0; i < setters.length; i++) {\n"
13530 " var prop = setters[i];\n" 13530 " var prop = setters[i];\n"
13531 " Object.prototype.__defineSetter__(prop, function() { throw prop; });\n" 13531 " Object.prototype.__defineSetter__(prop, function() { throw prop; });\n"
13532 "}\n"); 13532 "}\n");
13533 CompileRun("throw 'exception';"); 13533 CompileRun("throw 'exception';");
13534 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); 13534 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13535 } 13535 }
13536 13536
13537 13537
13538 static void RethrowStackTraceHandler(v8::Handle<v8::Message> message,
13539 v8::Handle<v8::Value> data) {
13540 // Use the frame where JavaScript is called from.
13541 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13542 CHECK(!stack_trace.IsEmpty());
13543 int frame_count = stack_trace->GetFrameCount();
13544 CHECK_EQ(3, frame_count);
13545 int line_number[] = {1, 2, 5};
13546 for (int i = 0; i < frame_count; i++) {
13547 CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber());
13548 }
13549 }
13550
13551
13552 // Test that we only return the stack trace at the site where the exception
13553 // is first thrown (not where it is rethrown).
13554 TEST(RethrowStackTrace) {
13555 v8::HandleScope scope;
13556 LocalContext env;
13557 // We make sure that
13558 // - the stack trace of the ReferenceError in g() is reported.
13559 // - the stack trace is not overwritten when e1 is rethrown by t().
13560 // - the stack trace of e2 does not overwrite that of e1.
13561 const char* source =
13562 "function g() { error; } \n"
13563 "function f() { g(); } \n"
13564 "function t(e) { throw e; } \n"
13565 "try { \n"
13566 " f(); \n"
13567 "} catch (e1) { \n"
13568 " try { \n"
13569 " error; \n"
13570 " } catch (e2) { \n"
13571 " t(e1); \n"
13572 " } \n"
13573 "} \n";
13574 v8::V8::AddMessageListener(RethrowStackTraceHandler);
13575 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13576 CompileRun(source);
13577 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13578 v8::V8::RemoveMessageListeners(RethrowStackTraceHandler);
13579 }
13580
13581
13582 static void RethrowPrimitiveStackTraceHandler(v8::Handle<v8::Message> message,
13583 v8::Handle<v8::Value> data) {
13584 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13585 CHECK(!stack_trace.IsEmpty());
13586 int frame_count = stack_trace->GetFrameCount();
13587 CHECK_EQ(2, frame_count);
13588 int line_number[] = {3, 7};
13589 for (int i = 0; i < frame_count; i++) {
13590 CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber());
13591 }
13592 }
13593
13594
13595 // Test that we do not recognize identity for primitive exceptions.
13596 TEST(RethrowPrimitiveStackTrace) {
13597 v8::HandleScope scope;
13598 LocalContext env;
13599 // We do not capture stack trace for non Error objects on creation time.
13600 // Instead, we capture the stack trace on last throw.
13601 const char* source =
13602 "function g() { throw 404; } \n"
13603 "function f() { g(); } \n"
13604 "function t(e) { throw e; } \n"
13605 "try { \n"
13606 " f(); \n"
13607 "} catch (e1) { \n"
13608 " t(e1) \n"
13609 "} \n";
13610 v8::V8::AddMessageListener(RethrowPrimitiveStackTraceHandler);
13611 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13612 CompileRun(source);
13613 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13614 v8::V8::RemoveMessageListeners(RethrowPrimitiveStackTraceHandler);
13615 }
13616
13617
13618 static void RethrowExistingStackTraceHandler(v8::Handle<v8::Message> message,
13619 v8::Handle<v8::Value> data) {
13620 // Use the frame where JavaScript is called from.
13621 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13622 CHECK(!stack_trace.IsEmpty());
13623 CHECK_EQ(1, stack_trace->GetFrameCount());
13624 CHECK_EQ(1, stack_trace->GetFrame(0)->GetLineNumber());
13625 }
13626
13627
13628 // Test that the stack trace is captured when the error object is created and
13629 // not where it is thrown.
13630 TEST(RethrowExistingStackTrace) {
13631 v8::HandleScope scope;
13632 LocalContext env;
13633 const char* source =
13634 "var e = new Error(); \n"
13635 "throw e; \n";
13636 v8::V8::AddMessageListener(RethrowExistingStackTraceHandler);
13637 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13638 CompileRun(source);
13639 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13640 v8::V8::RemoveMessageListeners(RethrowExistingStackTraceHandler);
13641 }
13642
13643
13644 static void RethrowBogusErrorStackTraceHandler(v8::Handle<v8::Message> message,
13645 v8::Handle<v8::Value> data) {
13646 // Use the frame where JavaScript is called from.
13647 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13648 CHECK(!stack_trace.IsEmpty());
13649 CHECK_EQ(1, stack_trace->GetFrameCount());
13650 CHECK_EQ(2, stack_trace->GetFrame(0)->GetLineNumber());
13651 }
13652
13653
13654 // Test that the stack trace is captured when the error object is created and
13655 // not where it is thrown.
Vyacheslav Egorov (Chromium) 2012/02/07 09:17:37 Comment is incorrect. In this test stack trace is
13656 TEST(RethrowBogusErrorStackTrace) {
13657 v8::HandleScope scope;
13658 LocalContext env;
13659 const char* source =
13660 "var e = {__proto__: new Error()} \n"
13661 "throw e; \n";
13662 v8::V8::AddMessageListener(RethrowBogusErrorStackTraceHandler);
13663 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13664 CompileRun(source);
13665 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13666 v8::V8::RemoveMessageListeners(RethrowBogusErrorStackTraceHandler);
13667 }
13668
13669
13538 v8::Handle<Value> AnalyzeStackOfEvalWithSourceURL(const v8::Arguments& args) { 13670 v8::Handle<Value> AnalyzeStackOfEvalWithSourceURL(const v8::Arguments& args) {
13539 v8::HandleScope scope; 13671 v8::HandleScope scope;
13540 v8::Handle<v8::StackTrace> stackTrace = 13672 v8::Handle<v8::StackTrace> stackTrace =
13541 v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); 13673 v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed);
13542 CHECK_EQ(5, stackTrace->GetFrameCount()); 13674 CHECK_EQ(5, stackTrace->GetFrameCount());
13543 v8::Handle<v8::String> url = v8_str("eval_url"); 13675 v8::Handle<v8::String> url = v8_str("eval_url");
13544 for (int i = 0; i < 3; i++) { 13676 for (int i = 0; i < 3; i++) {
13545 v8::Handle<v8::String> name = 13677 v8::Handle<v8::String> name =
13546 stackTrace->GetFrame(i)->GetScriptNameOrSourceURL(); 13678 stackTrace->GetFrame(i)->GetScriptNameOrSourceURL();
13547 CHECK(!name.IsEmpty()); 13679 CHECK(!name.IsEmpty());
(...skipping 2397 matching lines...) Expand 10 before | Expand all | Expand 10 after
15945 CompileRun("throw 'exception';"); 16077 CompileRun("throw 'exception';");
15946 } 16078 }
15947 16079
15948 16080
15949 TEST(CallCompletedCallbackTwoExceptions) { 16081 TEST(CallCompletedCallbackTwoExceptions) {
15950 v8::HandleScope scope; 16082 v8::HandleScope scope;
15951 LocalContext env; 16083 LocalContext env;
15952 v8::V8::AddCallCompletedCallback(CallCompletedCallbackException); 16084 v8::V8::AddCallCompletedCallback(CallCompletedCallbackException);
15953 CompileRun("throw 'first exception';"); 16085 CompileRun("throw 'first exception';");
15954 } 16086 }
OLDNEW
« src/isolate.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698