OLD | NEW |
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 } | 32 } |
33 | 33 |
34 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) | 34 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
35 : std::runtime_error(*v8::String::AsciiValue(exception)) | 35 const v8::Handle<v8::Message> message) |
| 36 : std::runtime_error(ExceptionToString(exception, message)) |
36 { | 37 { |
37 } | 38 } |
38 | 39 |
| 40 std::string AdblockPlus::JsError::ExceptionToString(const v8::Handle<v8::Value>
exception, |
| 41 const v8::Handle<v8::Message> message) |
| 42 { |
| 43 std::stringstream error; |
| 44 error << *v8::String::Utf8Value(exception); |
| 45 if (!message.IsEmpty()) |
| 46 { |
| 47 error << " at "; |
| 48 error << *v8::String::Utf8Value(message->GetScriptResourceName()); |
| 49 error << ":"; |
| 50 error << message->GetLineNumber(); |
| 51 } |
| 52 return error.str(); |
| 53 } |
| 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 } |
46 | 62 |
47 v8::Handle<v8::Script> AdblockPlus::JsEngine::CompileScript(const char* source,
const char* filename) | 63 v8::Handle<v8::Script> AdblockPlus::JsEngine::CompileScript(const char* source,
const char* filename) |
48 { | 64 { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
OLD | NEW |