| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 public: | 58 public: |
| 59 static void Init() | 59 static void Init() |
| 60 { | 60 { |
| 61 // it's threadsafe since C++11 and it will be instantiated only once and | 61 // it's threadsafe since C++11 and it will be instantiated only once and |
| 62 // destroyed at the application exit | 62 // destroyed at the application exit |
| 63 static V8Initializer initializer; | 63 static V8Initializer initializer; |
| 64 } | 64 } |
| 65 }; | 65 }; |
| 66 } | 66 } |
| 67 | 67 |
| 68 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | |
| 69 : isolate(v8::Isolate::New()) | |
| 70 { | |
| 71 } | |
| 72 | |
| 73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | |
| 74 { | |
| 75 isolate->Dispose(); | |
| 76 isolate = nullptr; | |
| 77 } | |
| 78 | |
| 79 AdblockPlus::JsEngine::JsEngine() | 68 AdblockPlus::JsEngine::JsEngine() |
| 69 : isolate(v8::Isolate::GetCurrent()) |
| 80 { | 70 { |
| 81 } | 71 } |
| 82 | 72 |
| 83 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 73 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) |
| 84 { | 74 { |
| 85 V8Initializer::Init(); | 75 V8Initializer::Init(); |
| 86 JsEnginePtr result(new JsEngine()); | 76 JsEnginePtr result(new JsEngine()); |
| 87 | 77 |
| 88 const v8::Locker locker(result->isolate); | 78 const v8::Locker locker(result->isolate); |
| 89 const v8::Isolate::Scope isolateScope(result->isolate); | 79 const v8::HandleScope handleScope; |
| 90 const v8::HandleScope handleScope(result->isolate); | |
| 91 | 80 |
| 92 result->context.reset(new v8::Persistent<v8::Context>(result->isolate, | 81 result->context.reset(new v8::Persistent<v8::Context>(result->isolate, |
| 93 v8::Context::New(result->isolate))); | 82 v8::Context::New(result->isolate))); |
| 94 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( | 83 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( |
| 95 result->isolate, *result->context)->Global(); | 84 result->isolate, *result->context)->Global(); |
| 96 AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 85 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); |
| 97 JsValuePtr(new JsValue(result, globalContext))); | 86 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); |
| 98 return result; | 87 return result; |
| 99 } | 88 } |
| 100 | 89 |
| 101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 90 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
| 102 const std::string& filename) | 91 const std::string& filename) |
| 103 { | 92 { |
| 104 const JsContext context(shared_from_this()); | 93 const JsContext context(shared_from_this()); |
| 105 const v8::TryCatch tryCatch; | 94 const v8::TryCatch tryCatch; |
| 106 const v8::Handle<v8::Script> script = CompileScript(isolate, source, | 95 const v8::Handle<v8::Script> script = CompileScript(isolate, source, |
| 107 filename); | 96 filename); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); | 149 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); |
| 161 } | 150 } |
| 162 | 151 |
| 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 152 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
| 164 v8::InvocationCallback callback) | 153 v8::InvocationCallback callback) |
| 165 { | 154 { |
| 166 const JsContext context(shared_from_this()); | 155 const JsContext context(shared_from_this()); |
| 167 | 156 |
| 168 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 157 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
| 169 // it's no longer used | 158 // it's no longer used |
| 170 std::tr1::weak_ptr<JsEngine>* data = | 159 std::weak_ptr<JsEngine>* data = |
| 171 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 160 new std::weak_ptr<JsEngine>(shared_from_this()); |
| 172 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 161 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |
| 173 v8::External::New(data)); | 162 v8::External::New(data)); |
| 174 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 163 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); |
| 175 } | 164 } |
| 176 | 165 |
| 177 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) | 166 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) |
| 178 { | 167 { |
| 179 const v8::Local<const v8::External> external = | 168 const v8::Local<const v8::External> external = |
| 180 v8::Local<const v8::External>::Cast(arguments.Data()); | 169 v8::Local<const v8::External>::Cast(arguments.Data()); |
| 181 std::tr1::weak_ptr<JsEngine>* data = | 170 std::weak_ptr<JsEngine>* data = |
| 182 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 171 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); |
| 183 JsEnginePtr result = data->lock(); | 172 JsEnginePtr result = data->lock(); |
| 184 if (!result) | 173 if (!result) |
| 185 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 174 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
| 186 return result; | 175 return result; |
| 187 } | 176 } |
| 188 | 177 |
| 189 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) | 178 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) |
| 190 { | 179 { |
| 191 const JsContext context(shared_from_this()); | 180 const JsContext context(shared_from_this()); |
| 192 JsValueList list; | 181 JsValueList list; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 return logSystem; | 221 return logSystem; |
| 233 } | 222 } |
| 234 | 223 |
| 235 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 224 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) |
| 236 { | 225 { |
| 237 if (!val) | 226 if (!val) |
| 238 throw std::runtime_error("LogSystem cannot be null"); | 227 throw std::runtime_error("LogSystem cannot be null"); |
| 239 | 228 |
| 240 logSystem = val; | 229 logSystem = val; |
| 241 } | 230 } |
| 231 |
| 232 |
| 233 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
| 234 AdblockPlus::JsValuePtr value) |
| 235 { |
| 236 if (!globalJsObject) |
| 237 throw std::runtime_error("Global object cannot be null"); |
| 238 |
| 239 globalJsObject->SetProperty(name, value); |
| 240 } |
| LEFT | RIGHT |