|
| 1 | +const char* ToCString(const v8::String::Utf8Value& value) { |
| 2 | + return *value ? *value : "<string conversion failed>"; |
| 3 | +} |
| 4 | +void ReportException(v8::TryCatch* try_catch) { |
| 5 | + v8::HandleScope handle_scope; |
| 6 | + v8::String::Utf8Value exception(try_catch->Exception()); |
| 7 | + const char* exception_string = ToCString(exception); |
| 8 | + v8::Handle<v8::Message> message = try_catch->Message(); |
| 9 | + if (message.IsEmpty()) { |
| 10 | + // V8 didn't provide any extra information about this error; just |
| 11 | + // print the exception. |
| 12 | + printf("%s\n", exception_string); |
| 13 | + } else { |
| 14 | + // Print (filename):(line number): (message). |
| 15 | + v8::String::Utf8Value filename(message->GetScriptResourceName()); |
| 16 | + const char* filename_string = ToCString(filename); |
| 17 | + int linenum = message->GetLineNumber(); |
| 18 | + printf("%s:%i: %s\n", filename_string, linenum, exception_string); |
| 19 | + // Print line of source code. |
| 20 | + v8::String::Utf8Value sourceline(message->GetSourceLine()); |
| 21 | + const char* sourceline_string = ToCString(sourceline); |
| 22 | + printf("%s\n", sourceline_string); |
| 23 | + // Print wavy underline (GetUnderline is deprecated). |
| 24 | + int start = message->GetStartColumn(); |
| 25 | + for (int i = 0; i < start; i++) { |
| 26 | + printf(" "); |
| 27 | + } |
| 28 | + int end = message->GetEndColumn(); |
| 29 | + for (int i = start; i < end; i++) { |
| 30 | + printf("^"); |
| 31 | + } |
| 32 | + printf("\n"); |
| 33 | + v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 34 | + if (stack_trace.length() > 0) { |
| 35 | + const char* stack_trace_string = ToCString(stack_trace); |
| 36 | + printf("%s\n", stack_trace_string); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +// Reads a file into a v8 string. |
| 41 | +v8::Handle<v8::String> ReadFile(const char* name) { |
| 42 | + FILE* file = fopen(name, "rb"); |
| 43 | + if (file == NULL) return v8::Handle<v8::String>(); |
| 44 | + |
| 45 | + fseek(file, 0, SEEK_END); |
| 46 | + int size = ftell(file); |
| 47 | + rewind(file); |
| 48 | + |
| 49 | + char* chars = new char[size + 1]; |
| 50 | + chars[size] = '\0'; |
| 51 | + for (int i = 0; i < size;) { |
| 52 | + int read = fread(&chars[i], 1, size - i, file); |
| 53 | + i += read; |
| 54 | + } |
| 55 | + fclose(file); |
| 56 | + v8::Handle<v8::String> result = v8::String::New(chars, size); |
| 57 | + delete[] chars; |
| 58 | + return result; |
| 59 | +} |
| 60 | +// Executes a string within the current v8 context. |
| 61 | +bool ExecuteString(v8::Handle<v8::String> source, |
| 62 | + v8::Handle<v8::Value> name, |
| 63 | + bool print_result, |
| 64 | + bool report_exceptions) { |
| 65 | + v8::HandleScope handle_scope; |
| 66 | + v8::TryCatch try_catch; |
| 67 | + v8::Handle<v8::Script> script = v8::Script::Compile(source, name); |
| 68 | + if (script.IsEmpty()) { |
| 69 | + // Print errors that happened during compilation. |
| 70 | + if (report_exceptions) |
| 71 | + ReportException(&try_catch); |
| 72 | + return false; |
| 73 | + } else { |
| 74 | + v8::Handle<v8::Value> result = script->Run(); |
| 75 | + if (result.IsEmpty()) { |
| 76 | + assert(try_catch.HasCaught()); |
| 77 | + // Print errors that happened during execution. |
| 78 | + if (report_exceptions) |
| 79 | + ReportException(&try_catch); |
| 80 | + return false; |
| 81 | + } else { |
| 82 | + assert(!try_catch.HasCaught()); |
| 83 | + if (print_result && !result->IsUndefined()) { |
| 84 | + // If all went well and the result wasn't undefined then print |
| 85 | + // the returned value. |
| 86 | + v8::String::Utf8Value str(result); |
| 87 | + const char* cstr = ToCString(str); |
| 88 | + printf("%s\n", cstr); |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +v8::Handle<v8::Value> jsRequire(const v8::Arguments& args) { |
| 96 | + for (int i = 0; i < args.Length(); i++) { |
| 97 | + v8::HandleScope handle_scope; |
| 98 | + v8::String::Utf8Value file(args[i]); |
| 99 | + if (*file == NULL) { |
| 100 | + return v8::ThrowException(v8::String::New("Error loading file")); |
| 101 | + } |
| 102 | + v8::Handle<v8::String> source = ReadFile(*file); |
| 103 | + if (source.IsEmpty()) { |
| 104 | + return v8::ThrowException(v8::String::New("Error loading file")); |
| 105 | + } |
| 106 | + if (!ExecuteString(source, v8::String::New(*file), false, false)) { |
| 107 | + return v8::ThrowException(v8::String::New("Error executing file")); |
| 108 | + } |
| 109 | + } |
| 110 | + return v8::Undefined(); |
| 111 | +} |
| 112 | + |
0 commit comments