From 28133bc76146d66c683cad2e6ee3d05cdfa7dc0a Mon Sep 17 00:00:00 2001 From: "christian.plesner.hansen@gmail.com" Date: Tue, 9 Sep 2008 19:08:40 +0000 Subject: [PATCH] Changed the shell to print source position with error messages. Added debug flags to gcc when compiling samples in debug mode. git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@237 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- SConstruct | 3 +++ samples/shell.cc | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/SConstruct b/SConstruct index 8df4dab03..bdd0ab44d 100644 --- a/SConstruct +++ b/SConstruct @@ -190,6 +190,9 @@ SAMPLE_FLAGS = { 'CCFLAGS': ['-m32'], 'LINKFLAGS': ['-m32'] }, + 'mode:debug': { + 'CCFLAGS': ['-g', '-O0'] + } }, 'msvc': { 'all': { diff --git a/samples/shell.cc b/samples/shell.cc index 17adbdf52..643eeb46b 100644 --- a/samples/shell.cc +++ b/samples/shell.cc @@ -40,6 +40,7 @@ v8::Handle Load(const v8::Arguments& args); v8::Handle Quit(const v8::Arguments& args); v8::Handle Version(const v8::Arguments& args); v8::Handle ReadFile(const char* name); +void ReportException(v8::TryCatch* handler); int main(int argc, char* argv[]) { @@ -174,7 +175,7 @@ void RunShell(v8::Handle context) { char* str = fgets(buffer, kBufferSize, stdin); if (str == NULL) break; v8::HandleScope handle_scope; - ExecuteString(v8::String::New(str), v8::Undefined(), true); + ExecuteString(v8::String::New(str), v8::String::New("(shell)"), true); } printf("\n"); } @@ -189,15 +190,13 @@ bool ExecuteString(v8::Handle source, v8::Handle script = v8::Script::Compile(source, name); if (script.IsEmpty()) { // Print errors that happened during compilation. - v8::String::AsciiValue error(try_catch.Exception()); - printf("%s\n", *error); + ReportException(&try_catch); return false; } else { v8::Handle result = script->Run(); if (result.IsEmpty()) { // Print errors that happened during execution. - v8::String::AsciiValue error(try_catch.Exception()); - printf("%s\n", *error); + ReportException(&try_catch); return false; } else { if (print_result && !result->IsUndefined()) { @@ -210,3 +209,33 @@ bool ExecuteString(v8::Handle source, } } } + + +void ReportException(v8::TryCatch* try_catch) { + v8::HandleScope handle_scope; + v8::String::AsciiValue exception(try_catch->Exception()); + v8::Handle message = try_catch->Message(); + if (message.IsEmpty()) { + // V8 didn't provide any extra information about this error; just + // print the exception. + printf("%s\n", *exception); + } else { + // Print (filename):(line number): (message). + v8::String::AsciiValue filename(message->GetScriptResourceName()); + int linenum = message->GetLineNumber(); + printf("%s:%i: %s\n", *filename, linenum, *exception); + // Print line of source code. + v8::String::AsciiValue sourceline(message->GetSourceLine()); + printf("%s\n", *sourceline); + // Print wavy underline (GetUnderline is deprecated). + int start = message->GetStartColumn(); + for (int i = 0; i < start; i++) { + printf(" "); + } + int end = message->GetEndColumn(); + for (int i = start; i < end; i++) { + printf("^"); + } + printf("\n"); + } +} -- 2.34.1