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

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/runtime.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::String> error_message = message->Get();
13542 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13543 CHECK(!stack_trace.IsEmpty());
13544 int frame_count = stack_trace->GetFrameCount();
13545 CHECK_EQ(3, frame_count);
13546 int line_number[] = {1, 2, 5};
13547 for (int i = 0; i < frame_count; i++) {
13548 CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber());
13549 }
13550 }
13551
13552
13553 // Test that we only return the stack trace at the site where the exception
13554 // is first thrown (not where it is rethrown).
13555 TEST(RethrowStackTrace) {
13556 v8::HandleScope scope;
13557 LocalContext env;
13558 // We make sure that
13559 // - the stack trace of the ReferenceError in g() is reported.
13560 // - the stack trace is not overwritten when e1 is rethrown by t().
13561 // - the stack trace of e2 does not overwrite that of e1.
13562 const char* source =
13563 "function g() { error; } \n"
13564 "function f() { g(); } \n"
13565 "function t(e) { throw e; } \n"
13566 "try { \n"
13567 " f(); \n"
13568 "} catch (e1) { \n"
13569 " try { \n"
13570 " error; \n"
13571 " } catch (e2) { \n"
13572 " t(e1); \n"
13573 " } \n"
13574 "} \n";
13575 v8::V8::AddMessageListener(RethrowStackTraceHandler);
13576 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13577 CompileRun(source);
13578 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13579 v8::V8::RemoveMessageListeners(RethrowStackTraceHandler);
13580 }
13581
13582
13583 static void RethrowPrimitiveStackTraceHandler(v8::Handle<v8::Message> message,
13584 v8::Handle<v8::Value> data) {
13585 v8::Handle<v8::String> error_message = message->Get();
13586 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13587 CHECK(!stack_trace.IsEmpty());
13588 int frame_count = stack_trace->GetFrameCount();
13589 CHECK_EQ(2, frame_count);
13590 int line_number[] = {3, 7};
13591 for (int i = 0; i < frame_count; i++) {
13592 CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber());
13593 }
13594 }
13595
13596
13597 // Test that we do not recognize identity for primitive exceptions.
13598 TEST(RethrowPrimitiveStackTrace) {
13599 v8::HandleScope scope;
13600 LocalContext env;
13601 // We do not capture stack trace for non Error objects on creation time.
13602 // Instead, we capture the stack trace on last throw.
13603 const char* source =
13604 "function g() { throw 404; } \n"
13605 "function f() { g(); } \n"
13606 "function t(e) { throw e; } \n"
13607 "try { \n"
13608 " f(); \n"
13609 "} catch (e1) { \n"
13610 " t(e1) \n"
13611 "} \n";
13612 v8::V8::AddMessageListener(RethrowPrimitiveStackTraceHandler);
13613 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13614 CompileRun(source);
13615 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13616 v8::V8::RemoveMessageListeners(RethrowPrimitiveStackTraceHandler);
13617 }
13618
13619
13620 static void RethrowExistingStackTraceHandler(v8::Handle<v8::Message> message,
13621 v8::Handle<v8::Value> data) {
13622 // Use the frame where JavaScript is called from.
13623 v8::Handle<v8::String> error_message = message->Get();
13624 v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace();
13625 CHECK(!stack_trace.IsEmpty());
13626 CHECK_EQ(1, stack_trace->GetFrameCount());
13627 CHECK_EQ(1, stack_trace->GetFrame(0)->GetLineNumber());
13628 }
13629
13630
13631 // Test that the stack trace is captured when the error object is created and
13632 // not where it is thrown.
13633 TEST(RethrowExistingStackTrace) {
13634 v8::HandleScope scope;
13635 LocalContext env;
13636 const char* source =
13637 "var e = new Error(); \n"
13638 "throw e; \n";
13639 v8::V8::AddMessageListener(RethrowExistingStackTraceHandler);
13640 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
13641 CompileRun(source);
13642 v8::V8::SetCaptureStackTraceForUncaughtExceptions(false);
13643 v8::V8::RemoveMessageListeners(RethrowExistingStackTraceHandler);
13644 }
13645
13646
13538 v8::Handle<Value> AnalyzeStackOfEvalWithSourceURL(const v8::Arguments& args) { 13647 v8::Handle<Value> AnalyzeStackOfEvalWithSourceURL(const v8::Arguments& args) {
13539 v8::HandleScope scope; 13648 v8::HandleScope scope;
13540 v8::Handle<v8::StackTrace> stackTrace = 13649 v8::Handle<v8::StackTrace> stackTrace =
13541 v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); 13650 v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed);
13542 CHECK_EQ(5, stackTrace->GetFrameCount()); 13651 CHECK_EQ(5, stackTrace->GetFrameCount());
13543 v8::Handle<v8::String> url = v8_str("eval_url"); 13652 v8::Handle<v8::String> url = v8_str("eval_url");
13544 for (int i = 0; i < 3; i++) { 13653 for (int i = 0; i < 3; i++) {
13545 v8::Handle<v8::String> name = 13654 v8::Handle<v8::String> name =
13546 stackTrace->GetFrame(i)->GetScriptNameOrSourceURL(); 13655 stackTrace->GetFrame(i)->GetScriptNameOrSourceURL();
13547 CHECK(!name.IsEmpty()); 13656 CHECK(!name.IsEmpty());
(...skipping 2397 matching lines...) Expand 10 before | Expand all | Expand 10 after
15945 CompileRun("throw 'exception';"); 16054 CompileRun("throw 'exception';");
15946 } 16055 }
15947 16056
15948 16057
15949 TEST(CallCompletedCallbackTwoExceptions) { 16058 TEST(CallCompletedCallbackTwoExceptions) {
15950 v8::HandleScope scope; 16059 v8::HandleScope scope;
15951 LocalContext env; 16060 LocalContext env;
15952 v8::V8::AddCallCompletedCallback(CallCompletedCallbackException); 16061 v8::V8::AddCallCompletedCallback(CallCompletedCallbackException);
15953 CompileRun("throw 'first exception';"); 16062 CompileRun("throw 'first exception';");
15954 } 16063 }
OLDNEW
« src/runtime.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698