| 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 { | 10 { |
| 11 const v8::Locker locker(v8::Isolate::GetCurrent()); |
| 11 const v8::HandleScope handleScope; | 12 const v8::HandleScope handleScope; |
| 12 const v8::Handle<v8::ObjectTemplate> global = | 13 const v8::Handle<v8::ObjectTemplate> global = |
| 13 AdblockPlus::GlobalJsObject::Create(errorCallback); | 14 AdblockPlus::GlobalJsObject::Create(errorCallback); |
| 14 return v8::Context::New(0, global); | 15 return v8::Context::New(0, global); |
| 15 } | 16 } |
| 16 | 17 |
| 17 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) | 18 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) |
| 18 { | 19 { |
| 19 const v8::Handle<v8::String> v8Source = v8::String::New(source); | 20 const v8::Handle<v8::String> v8Source = v8::String::New(source); |
| 20 if (filename && filename[0]) | 21 if (filename && filename[0]) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 63 } |
| 63 | 64 |
| 64 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, | 65 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, |
| 65 ErrorCallback* const errorCallback) | 66 ErrorCallback* const errorCallback) |
| 66 : fileReader(fileReader), context(CreateContext(*errorCallback)) | 67 : fileReader(fileReader), context(CreateContext(*errorCallback)) |
| 67 { | 68 { |
| 68 } | 69 } |
| 69 | 70 |
| 70 void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename) | 71 void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename) |
| 71 { | 72 { |
| 73 const v8::Locker locker(v8::Isolate::GetCurrent()); |
| 72 const v8::HandleScope handleScope; | 74 const v8::HandleScope handleScope; |
| 73 const v8::Context::Scope contextScope(context); | 75 const v8::Context::Scope contextScope(context); |
| 74 const v8::TryCatch tryCatch; | 76 const v8::TryCatch tryCatch; |
| 75 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 77 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
| 76 CheckTryCatch(tryCatch); | 78 CheckTryCatch(tryCatch); |
| 77 script->Run(); | 79 script->Run(); |
| 78 CheckTryCatch(tryCatch); | 80 CheckTryCatch(tryCatch); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void AdblockPlus::JsEngine::Evaluate(const std::string& source, | 83 void AdblockPlus::JsEngine::Evaluate(const std::string& source, |
| 82 const std::string& filename) | 84 const std::string& filename) |
| 83 { | 85 { |
| 84 Evaluate(source.c_str(), filename.c_str()); | 86 Evaluate(source.c_str(), filename.c_str()); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 89 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
| 88 { | 90 { |
| 89 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); | 91 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); |
| 90 if (!*file) | 92 if (!*file) |
| 91 throw std::runtime_error("Unable to load script " + scriptPath); | 93 throw std::runtime_error("Unable to load script " + scriptPath); |
| 92 Evaluate(Slurp(*file)); | 94 Evaluate(Slurp(*file)); |
| 93 } | 95 } |
| 94 | 96 |
| 95 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) | 97 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) |
| 96 { | 98 { |
| 99 const v8::Locker locker(v8::Isolate::GetCurrent()); |
| 97 const v8::HandleScope handleScope; | 100 const v8::HandleScope handleScope; |
| 98 const v8::Context::Scope contextScope(context); | 101 const v8::Context::Scope contextScope(context); |
| 99 const v8::Local<v8::Object> global = context->Global(); | 102 const v8::Local<v8::Object> global = context->Global(); |
| 100 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 103 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
| 101 global->Get(v8::String::New(functionName.c_str()))); | 104 global->Get(v8::String::New(functionName.c_str()))); |
| 102 const v8::TryCatch tryCatch; | 105 const v8::TryCatch tryCatch; |
| 103 const v8::Local<v8::Value> result = function->Call(function, 0, 0); | 106 const v8::Local<v8::Value> result = function->Call(function, 0, 0); |
| 104 CheckTryCatch(tryCatch); | 107 CheckTryCatch(tryCatch); |
| 105 const v8::String::AsciiValue ascii(result); | 108 const v8::String::AsciiValue ascii(result); |
| 106 return *ascii; | 109 return *ascii; |
| 107 } | 110 } |
| 108 | 111 |
| 109 void AdblockPlus::JsEngine::Gc() | 112 void AdblockPlus::JsEngine::Gc() |
| 110 { | 113 { |
| 111 while (!v8::V8::IdleNotification()); | 114 while (!v8::V8::IdleNotification()); |
| 112 } | 115 } |
| OLD | NEW |