OLD | NEW |
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 #include <sstream> | 2 #include <sstream> |
3 | 3 |
4 #include "GlobalJsObject.h" | 4 #include "GlobalJsObject.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 AdblockPlus::WebRequest& webRequest) |
10 { | 11 { |
11 const v8::Locker locker(v8::Isolate::GetCurrent()); | 12 const v8::Locker locker(v8::Isolate::GetCurrent()); |
12 const v8::HandleScope handleScope; | 13 const v8::HandleScope handleScope; |
13 const v8::Handle<v8::ObjectTemplate> global = | 14 const v8::Handle<v8::ObjectTemplate> global = |
14 AdblockPlus::GlobalJsObject::Create(errorCallback); | 15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); |
15 return v8::Context::New(0, global); | 16 return v8::Context::New(0, global); |
16 } | 17 } |
17 | 18 |
18 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) | 19 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) |
19 { | 20 { |
20 const v8::Handle<v8::String> v8Source = v8::String::New(source); | 21 const v8::Handle<v8::String> v8Source = v8::String::New(source); |
21 if (filename && filename[0]) | 22 if (filename && filename[0]) |
22 { | 23 { |
23 const v8::Handle<v8::String> v8Filename = v8::String::New(filename); | 24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename); |
24 return v8::Script::Compile(v8Source, v8Filename); | 25 return v8::Script::Compile(v8Source, v8Filename); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 } | 57 } |
57 } | 58 } |
58 | 59 |
59 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, | 60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
60 const v8::Handle<v8::Message> message) | 61 const v8::Handle<v8::Message> message) |
61 : std::runtime_error(ExceptionToString(exception, message)) | 62 : std::runtime_error(ExceptionToString(exception, message)) |
62 { | 63 { |
63 } | 64 } |
64 | 65 |
65 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, | 66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, |
| 67 WebRequest* const webRequest, |
66 ErrorCallback* const errorCallback) | 68 ErrorCallback* const errorCallback) |
67 : fileReader(fileReader), context(CreateContext(*errorCallback)) | 69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) |
68 { | 70 { |
69 } | 71 } |
70 | 72 |
71 void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename) | 73 std::string AdblockPlus::JsEngine::Evaluate(const char* source, const char* file
name) |
72 { | 74 { |
73 const v8::Locker locker(v8::Isolate::GetCurrent()); | 75 const v8::Locker locker(v8::Isolate::GetCurrent()); |
74 const v8::HandleScope handleScope; | 76 const v8::HandleScope handleScope; |
75 const v8::Context::Scope contextScope(context); | 77 const v8::Context::Scope contextScope(context); |
76 const v8::TryCatch tryCatch; | 78 const v8::TryCatch tryCatch; |
77 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 79 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
78 CheckTryCatch(tryCatch); | 80 CheckTryCatch(tryCatch); |
79 script->Run(); | 81 v8::Local<v8::Value> result = script->Run(); |
80 CheckTryCatch(tryCatch); | 82 CheckTryCatch(tryCatch); |
| 83 v8::String::Utf8Value resultString(result); |
| 84 return std::string(*resultString); |
81 } | 85 } |
82 | 86 |
83 void AdblockPlus::JsEngine::Evaluate(const std::string& source, | 87 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, |
84 const std::string& filename) | 88 const std::string& filename) |
85 { | 89 { |
86 Evaluate(source.c_str(), filename.c_str()); | 90 return Evaluate(source.c_str(), filename.c_str()); |
87 } | 91 } |
88 | 92 |
89 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 93 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
90 { | 94 { |
91 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); | 95 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); |
92 if (!*file) | 96 if (!*file) |
93 throw std::runtime_error("Unable to load script " + scriptPath); | 97 throw std::runtime_error("Unable to load script " + scriptPath); |
94 Evaluate(Slurp(*file)); | 98 Evaluate(Slurp(*file)); |
95 } | 99 } |
96 | 100 |
(...skipping 22 matching lines...) Expand all Loading... |
119 if (value->IsUndefined()) | 123 if (value->IsUndefined()) |
120 return ""; | 124 return ""; |
121 const v8::String::AsciiValue ascii(value); | 125 const v8::String::AsciiValue ascii(value); |
122 return *ascii; | 126 return *ascii; |
123 } | 127 } |
124 | 128 |
125 void AdblockPlus::JsEngine::Gc() | 129 void AdblockPlus::JsEngine::Gc() |
126 { | 130 { |
127 while (!v8::V8::IdleNotification()); | 131 while (!v8::V8::IdleNotification()); |
128 } | 132 } |
OLD | NEW |