Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Nicer exception reporting (filename, linenumber, position).

  • Loading branch information...
commit 14e7a342fd4e7f19434e18c0e9a4e4e28c83afbd 1 parent d4e425e
@cscott cscott authored
Showing with 48 additions and 5 deletions.
  1. +46 −5 v8-gl.cpp
  2. +2 −0  v8-gl.h
View
51 v8-gl.cpp
@@ -4,6 +4,7 @@
*/
#include "v8-gl.h"
+#include <assert.h>
#include <stdio.h>
#include <string>
#include <string.h>
@@ -46,10 +47,10 @@ bool exec(string file) {
TryCatch try_catch;
// Compile the script and check for errors.
- Handle<Script> compiled_script = Script::Compile(source);
+ Handle<Script> compiled_script = Script::Compile(source,
+ String::New(file.c_str()));
if (compiled_script.IsEmpty()) {
- String::Utf8Value error(try_catch.Exception());
- printf("%s \n\n", *error);
+ V8GL::ReportException(&try_catch);
// The script failed to compile; bail out.
return false;
}
@@ -58,14 +59,54 @@ bool exec(string file) {
Handle<Value> result = compiled_script->Run();
if (result.IsEmpty()) {
// The TryCatch above is still in effect and will have caught the error.
- String::Utf8Value error(try_catch.Exception());
- printf("%s \n\n", *error);
+ assert(try_catch.HasCaught());
+ V8GL::ReportException(&try_catch);
// Running the script failed; bail out.
return false;
}
return true;
}
+// nicer exception handling (borrowed from d8)
+const char* V8GL::ToCString(const v8::String::Utf8Value& value) {
+ return *value ? *value : "<string conversion failed>";
+}
+void V8GL::ReportException(v8::TryCatch* try_catch) {
+ HandleScope handle_scope;
+ v8::String::Utf8Value exception(try_catch->Exception());
+ const char* exception_string = ToCString(exception);
+ Handle<Message> 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_string);
+ } else {
+ // Print (filename):(line number): (message).
+ v8::String::Utf8Value filename(message->GetScriptResourceName());
+ const char* filename_string = ToCString(filename);
+ int linenum = message->GetLineNumber();
+ printf("%s:%i: %s\n", filename_string, linenum, exception_string);
+ // Print line of source code.
+ v8::String::Utf8Value sourceline(message->GetSourceLine());
+ const char* sourceline_string = ToCString(sourceline);
+ printf("%s\n", sourceline_string);
+ // 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");
+ v8::String::Utf8Value stack_trace(try_catch->StackTrace());
+ if (stack_trace.length() > 0) {
+ const char* stack_trace_string = ToCString(stack_trace);
+ printf("%s\n", stack_trace_string);
+ }
+ }
+}
//FUNCTIONS IN GLOBAL SCOPE
View
2  v8-gl.h
@@ -30,6 +30,8 @@ class V8GL {
public:
bool initialize(int* pargc, char** argv, string scriptname);
bool executeScript(string scriptname);
+ static const char* ToCString(const v8::String::Utf8Value& value);
+ static void ReportException(TryCatch* try_catch);
//keep a reference to the global context.
static Persistent<Context> context;
Please sign in to comment.
Something went wrong with that request. Please try again.