Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsEngine.cpp

Issue 9884006: Add file name and line number when reporting exceptions (Closed)
Patch Set: Now with free functions Created March 15, 2013, 3:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('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 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <sstream> 2 #include <sstream>
3 3
4 #include "ConsoleJsObject.h" 4 #include "ConsoleJsObject.h"
5 5
6 extern const char* jsSources[]; 6 extern const char* jsSources[];
7 7
8 namespace 8 namespace
9 { 9 {
10 v8::Handle<v8::Context> CreateContext( 10 v8::Handle<v8::Context> CreateContext(
11 AdblockPlus::ErrorCallback& errorCallback) 11 AdblockPlus::ErrorCallback& errorCallback)
12 { 12 {
13 const v8::HandleScope handleScope; 13 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 14 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
15 global->Set(v8::String::New("console"), 15 global->Set(v8::String::New("console"),
16 AdblockPlus::ConsoleJsObject::Create(errorCallback)); 16 AdblockPlus::ConsoleJsObject::Create(errorCallback));
17 return v8::Context::New(0, global); 17 return v8::Context::New(0, global);
18 } 18 }
19 19
20 void CheckTryCatch(const v8::TryCatch& tryCatch) 20 void CheckTryCatch(const v8::TryCatch& tryCatch)
21 { 21 {
22 if (tryCatch.HasCaught()) 22 if (tryCatch.HasCaught())
23 throw AdblockPlus::JsError(tryCatch.Exception()); 23 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
24 } 24 }
25 25
26 std::string Slurp(std::istream& stream) 26 std::string Slurp(std::istream& stream)
27 { 27 {
28 std::stringstream content; 28 std::stringstream content;
29 content << stream.rdbuf(); 29 content << stream.rdbuf();
30 return content.str(); 30 return content.str();
31 } 31 }
32
33 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
34 const v8::Handle<v8::Message> message)
35 {
36 std::stringstream error;
37 error << *v8::String::Utf8Value(exception);
38 if (!message.IsEmpty())
39 {
40 error << " at ";
41 error << *v8::String::Utf8Value(message->GetScriptResourceName());
42 error << ":";
43 error << message->GetLineNumber();
44 }
45 return error.str();
46 }
32 } 47 }
33 48
34 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) 49 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
35 : std::runtime_error(*v8::String::AsciiValue(exception)) 50 const v8::Handle<v8::Message> message)
51 : std::runtime_error(ExceptionToString(exception, message))
36 { 52 {
37 } 53 }
38 54
39 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 55 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
40 ErrorCallback* const errorCallback) 56 ErrorCallback* const errorCallback)
41 : fileReader(fileReader), context(CreateContext(*errorCallback)) 57 : fileReader(fileReader), context(CreateContext(*errorCallback))
42 { 58 {
43 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 59 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
44 Evaluate(jsSources[i + 1], jsSources[i]); 60 Evaluate(jsSources[i + 1], jsSources[i]);
45 } 61 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 const v8::Local<v8::Value> result = function->Call(function, 0, 0); 102 const v8::Local<v8::Value> result = function->Call(function, 0, 0);
87 CheckTryCatch(tryCatch); 103 CheckTryCatch(tryCatch);
88 const v8::String::AsciiValue ascii(result); 104 const v8::String::AsciiValue ascii(result);
89 return *ascii; 105 return *ascii;
90 } 106 }
91 107
92 void AdblockPlus::JsEngine::Gc() 108 void AdblockPlus::JsEngine::Gc()
93 { 109 {
94 while (!v8::V8::IdleNotification()); 110 while (!v8::V8::IdleNotification());
95 } 111 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld