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 namespace | 6 namespace |
7 { | 7 { |
8 v8::Handle<v8::Context> CreateContext( | 8 v8::Handle<v8::Context> CreateContext( |
9 AdblockPlus::ErrorCallback& errorCallback) | 9 AdblockPlus::ErrorCallback& errorCallback) |
10 { | 10 { |
11 const v8::HandleScope handleScope; | 11 const v8::HandleScope handleScope; |
12 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 12 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
13 global->Set(v8::String::New("console"), | 13 global->Set(v8::String::New("console"), |
14 AdblockPlus::ConsoleJsObject::Create(errorCallback)); | 14 AdblockPlus::ConsoleJsObject::Create(errorCallback)); |
15 return v8::Context::New(0, global); | 15 return v8::Context::New(0, global); |
16 } | 16 } |
17 | 17 |
| 18 void CheckTryCatch(const v8::TryCatch& tryCatch) |
| 19 { |
| 20 if (tryCatch.HasCaught()) |
| 21 throw AdblockPlus::JsError(tryCatch.Exception()); |
| 22 } |
| 23 |
18 std::string Slurp(std::istream& stream) | 24 std::string Slurp(std::istream& stream) |
19 { | 25 { |
20 std::stringstream content; | 26 std::stringstream content; |
21 content << stream.rdbuf(); | 27 content << stream.rdbuf(); |
22 return content.str(); | 28 return content.str(); |
23 } | 29 } |
24 } | 30 } |
25 | 31 |
26 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) | 32 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) |
27 : std::runtime_error(*v8::String::AsciiValue(exception)) | 33 : std::runtime_error(*v8::String::AsciiValue(exception)) |
28 { | 34 { |
29 } | 35 } |
30 | 36 |
31 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, | 37 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, |
32 ErrorCallback* const errorCallback) | 38 ErrorCallback* const errorCallback) |
33 : fileReader(fileReader), context(CreateContext(*errorCallback)) | 39 : fileReader(fileReader), context(CreateContext(*errorCallback)) |
34 { | 40 { |
35 } | 41 } |
36 | 42 |
37 void AdblockPlus::JsEngine::Evaluate(const std::string& source) | 43 void AdblockPlus::JsEngine::Evaluate(const std::string& source) |
38 { | 44 { |
39 const v8::HandleScope handleScope; | 45 const v8::HandleScope handleScope; |
40 const v8::Context::Scope contextScope(context); | 46 const v8::Context::Scope contextScope(context); |
41 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); | 47 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |
| 48 const v8::TryCatch tryCatch; |
42 const v8::Handle<v8::Script> script = v8::Script::Compile(v8Source); | 49 const v8::Handle<v8::Script> script = v8::Script::Compile(v8Source); |
43 const v8::TryCatch tryCatch; | 50 CheckTryCatch(tryCatch); |
44 const v8::Handle<const v8::Value> result = script->Run(); | 51 const v8::Handle<const v8::Value> result = script->Run(); |
45 if (result.IsEmpty()) | 52 CheckTryCatch(tryCatch); |
46 throw JsError(tryCatch.Exception()); | |
47 } | 53 } |
48 | 54 |
49 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 55 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
50 { | 56 { |
51 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); | 57 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); |
52 if (!*file) | 58 if (!*file) |
53 throw std::runtime_error("Unable to load script " + scriptPath); | 59 throw std::runtime_error("Unable to load script " + scriptPath); |
54 Evaluate(Slurp(*file)); | 60 Evaluate(Slurp(*file)); |
55 } | 61 } |
56 | 62 |
57 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) | 63 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) |
58 { | 64 { |
59 const v8::HandleScope handleScope; | 65 const v8::HandleScope handleScope; |
60 const v8::Context::Scope contextScope(context); | 66 const v8::Context::Scope contextScope(context); |
61 const v8::Local<v8::Object> global = context->Global(); | 67 const v8::Local<v8::Object> global = context->Global(); |
62 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 68 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
63 global->Get(v8::String::New(functionName.c_str()))); | 69 global->Get(v8::String::New(functionName.c_str()))); |
64 const v8::TryCatch tryCatch; | 70 const v8::TryCatch tryCatch; |
65 const v8::Local<v8::Value> result = function->Call(function, 0, 0); | 71 const v8::Local<v8::Value> result = function->Call(function, 0, 0); |
66 if (result.IsEmpty()) | 72 CheckTryCatch(tryCatch); |
67 throw JsError(tryCatch.Exception()); | |
68 const v8::String::AsciiValue ascii(result); | 73 const v8::String::AsciiValue ascii(result); |
69 return *ascii; | 74 return *ascii; |
70 } | 75 } |
OLD | NEW |