| Left: | ||
| Right: |
| 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 #include "Utils.h" | 5 #include "Utils.h" |
| 6 | 6 |
| 7 namespace | 7 namespace |
| 8 { | 8 { |
| 9 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) | 9 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) |
| 10 { | 10 { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 return error.str(); | 39 return error.str(); |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, | 43 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
| 44 const v8::Handle<v8::Message> message) | 44 const v8::Handle<v8::Message> message) |
| 45 : std::runtime_error(ExceptionToString(exception, message)) | 45 : std::runtime_error(ExceptionToString(exception, message)) |
| 46 { | 46 { |
| 47 } | 47 } |
| 48 | 48 |
| 49 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo, | 49 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo) |
| 50 FileSystem* const fileSystem, | 50 : isolate(v8::Isolate::GetCurrent()) |
| 51 WebRequest* const webRequest, | |
| 52 ErrorCallback* const errorCallback) | |
| 53 : fileSystem(*fileSystem), webRequest(*webRequest), | |
| 54 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()) | |
| 55 { | 51 { |
| 56 const v8::Locker locker(isolate); | 52 const v8::Locker locker(isolate); |
| 57 const v8::HandleScope handleScope; | 53 const v8::HandleScope handleScope; |
| 58 | 54 |
| 59 context = v8::Context::New(); | 55 context = v8::Context::New(); |
| 60 AdblockPlus::GlobalJsObject::Setup(*this, appInfo, | 56 AdblockPlus::GlobalJsObject::Setup(*this, appInfo, |
| 61 JsValuePtr(new JsValue(*this, context->Global()))); | 57 JsValuePtr(new JsValue(*this, context->Global()))); |
| 62 } | 58 } |
| 63 | 59 |
| 64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, | 60 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, |
| 65 const std::string& filename) | 61 const std::string& filename) |
| 66 { | 62 { |
| 67 const Context context(*this); | 63 const Context context(*this); |
| 68 const v8::TryCatch tryCatch; | 64 const v8::TryCatch tryCatch; |
| 69 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 65 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
| 70 CheckTryCatch(tryCatch); | 66 CheckTryCatch(tryCatch); |
| 71 v8::Local<v8::Value> result = script->Run(); | 67 v8::Local<v8::Value> result = script->Run(); |
| 72 CheckTryCatch(tryCatch); | 68 CheckTryCatch(tryCatch); |
| 73 return JsValuePtr(new JsValue(*this, result)); | 69 return JsValuePtr(new JsValue(*this, result)); |
| 74 } | 70 } |
| 75 | 71 |
| 76 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 72 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
| 77 { | 73 { |
| 78 const std::tr1::shared_ptr<std::istream> file = fileSystem.Read(scriptPath); | 74 const std::tr1::shared_ptr<std::istream> file = GetFileSystem()->Read(scriptPa th); |
| 79 if (!file || !*file) | 75 if (!file || !*file) |
| 80 throw std::runtime_error("Unable to load script " + scriptPath); | 76 throw std::runtime_error("Unable to load script " + scriptPath); |
| 81 Evaluate(Utils::Slurp(*file)); | 77 Evaluate(Utils::Slurp(*file)); |
| 82 } | 78 } |
| 83 | 79 |
| 84 void AdblockPlus::JsEngine::Gc() | 80 void AdblockPlus::JsEngine::Gc() |
| 85 { | 81 { |
| 86 while (!v8::V8::IdleNotification()); | 82 while (!v8::V8::IdleNotification()); |
| 87 } | 83 } |
| 88 | 84 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 | 125 |
| 130 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) | 126 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) |
| 131 { | 127 { |
| 132 const Context context(*this); | 128 const Context context(*this); |
| 133 JsValueList list; | 129 JsValueList list; |
| 134 for (int i = 0; i < arguments.Length(); i++) | 130 for (int i = 0; i < arguments.Length(); i++) |
| 135 list.push_back(JsValuePtr(new JsValue(*this, arguments[i]))); | 131 list.push_back(JsValuePtr(new JsValue(*this, arguments[i]))); |
| 136 return list; | 132 return list; |
| 137 } | 133 } |
| 138 | 134 |
| 135 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | |
| 136 { | |
| 137 if (!fileSystem) | |
| 138 fileSystem.reset(new DefaultFileSystem()); | |
|
Felix Dahlke
2013/04/18 13:30:12
I'd rather have this in the constructor and make t
Wladimir Palant
2013/04/18 13:59:55
The reason for not doing it in the constructor was
Felix Dahlke
2013/04/18 14:04:12
Oh right, was still thinking we pass them in to th
| |
| 139 return fileSystem; | |
| 140 } | |
| 141 | |
| 142 void AdblockPlus::JsEngine::SetFileSystem(AdblockPlus::FileSystemPtr val) | |
| 143 { | |
| 144 fileSystem = val; | |
| 145 } | |
| 146 | |
| 147 AdblockPlus::WebRequestPtr AdblockPlus::JsEngine::GetWebRequest() | |
| 148 { | |
| 149 if (!webRequest) | |
| 150 webRequest.reset(new DefaultWebRequest()); | |
| 151 return webRequest; | |
| 152 } | |
| 153 | |
| 154 void AdblockPlus::JsEngine::SetWebRequest(AdblockPlus::WebRequestPtr val) | |
| 155 { | |
| 156 webRequest = val; | |
| 157 } | |
| 158 | |
| 159 AdblockPlus::ErrorCallbackPtr AdblockPlus::JsEngine::GetErrorCallback() | |
| 160 { | |
| 161 if (!errorCallback) | |
| 162 errorCallback.reset(new DefaultErrorCallback()); | |
| 163 return errorCallback; | |
| 164 } | |
| 165 | |
| 166 void AdblockPlus::JsEngine::SetErrorCallback(AdblockPlus::ErrorCallbackPtr val) | |
| 167 { | |
| 168 errorCallback = val; | |
| 169 } | |
| 170 | |
| 139 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) | 171 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) |
| 140 : locker(jsEngine.isolate), handleScope(), | 172 : locker(jsEngine.isolate), handleScope(), |
| 141 contextScope(jsEngine.context) | 173 contextScope(jsEngine.context) |
| 142 { | 174 { |
| 143 } | 175 } |
| OLD | NEW |