OLD | NEW |
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-2016 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 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include <AdblockPlus.h> | 18 #include <AdblockPlus.h> |
19 #include "GlobalJsObject.h" | 19 #include "GlobalJsObject.h" |
20 #include "JsContext.h" | 20 #include "JsContext.h" |
21 #include "JsError.h" | 21 #include "JsError.h" |
22 #include "Utils.h" | 22 #include "Utils.h" |
| 23 #include <libplatform/libplatform.h> |
23 | 24 |
24 namespace | 25 namespace |
25 { | 26 { |
26 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 27 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, |
27 const std::string& source, const std::string& filename) | 28 const std::string& source, const std::string& filename) |
28 { | 29 { |
29 using AdblockPlus::Utils::ToV8String; | 30 using AdblockPlus::Utils::ToV8String; |
30 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); | 31 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); |
31 if (filename.length()) | 32 if (filename.length()) |
32 { | 33 { |
33 const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); | 34 const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); |
34 return v8::Script::Compile(v8Source, v8Filename); | 35 return v8::Script::Compile(v8Source, v8Filename); |
35 } | 36 } |
36 else | 37 else |
37 return v8::Script::Compile(v8Source); | 38 return v8::Script::Compile(v8Source); |
38 } | 39 } |
39 | 40 |
40 void CheckTryCatch(const v8::TryCatch& tryCatch) | 41 void CheckTryCatch(const v8::TryCatch& tryCatch) |
41 { | 42 { |
42 if (tryCatch.HasCaught()) | 43 if (tryCatch.HasCaught()) |
43 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 44 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |
44 } | 45 } |
45 | 46 |
46 class V8Initializer | 47 class V8Initializer |
47 { | 48 { |
48 V8Initializer() | 49 V8Initializer() |
| 50 : platform(v8::platform::CreateDefaultPlatform()) |
49 { | 51 { |
| 52 v8::V8::InitializePlatform(platform); |
50 v8::V8::Initialize(); | 53 v8::V8::Initialize(); |
51 } | 54 } |
52 | 55 |
53 ~V8Initializer() | 56 ~V8Initializer() |
54 { | 57 { |
55 v8::V8::Dispose(); | 58 v8::V8::Dispose(); |
| 59 v8::V8::ShutdownPlatform(); |
| 60 delete platform; |
56 } | 61 } |
| 62 v8::Platform* platform; |
57 public: | 63 public: |
58 static void Init() | 64 static void Init() |
59 { | 65 { |
60 // it's threadsafe since C++11 and it will be instantiated only once and | 66 // it's threadsafe since C++11 and it will be instantiated only once and |
61 // destroyed at the application exit | 67 // destroyed at the application exit |
62 static V8Initializer initializer; | 68 static V8Initializer initializer; |
63 } | 69 } |
64 }; | 70 }; |
65 } | 71 } |
66 | 72 |
67 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | 73 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() |
68 : isolate(v8::Isolate::New()) | |
69 { | 74 { |
| 75 V8Initializer::Init(); |
| 76 isolate = v8::Isolate::New(); |
70 } | 77 } |
71 | 78 |
72 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 79 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() |
73 { | 80 { |
74 isolate->Dispose(); | 81 isolate->Dispose(); |
75 isolate = nullptr; | 82 isolate = nullptr; |
76 } | 83 } |
77 | 84 |
78 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) | 85 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) |
79 : isolate(isolate) | 86 : isolate(isolate) |
80 { | 87 { |
81 } | 88 } |
82 | 89 |
83 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
t ScopedV8IsolatePtr& isolate) | 90 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
t ScopedV8IsolatePtr& isolate) |
84 { | 91 { |
85 V8Initializer::Init(); | |
86 JsEnginePtr result(new JsEngine(isolate)); | 92 JsEnginePtr result(new JsEngine(isolate)); |
87 | 93 |
88 const v8::Locker locker(result->GetIsolate()); | 94 const v8::Locker locker(result->GetIsolate()); |
89 const v8::Isolate::Scope isolateScope(result->GetIsolate()); | 95 const v8::Isolate::Scope isolateScope(result->GetIsolate()); |
90 const v8::HandleScope handleScope(result->GetIsolate()); | 96 const v8::HandleScope handleScope(result->GetIsolate()); |
91 | 97 |
92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), | 98 result->context.reset(new v8::UniquePersistent<v8::Context>(result->GetIsolate
(), |
93 v8::Context::New(result->GetIsolate()))); | 99 v8::Context::New(result->GetIsolate()))); |
94 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( | 100 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( |
95 result->GetIsolate(), *result->context)->Global(); | 101 result->GetIsolate(), *result->context)->Global(); |
96 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); | 102 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); |
97 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); | 103 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); |
98 return result; | 104 return result; |
99 } | 105 } |
100 | 106 |
101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
102 const std::string& filename) | 108 const std::string& filename) |
(...skipping 21 matching lines...) Expand all Loading... |
124 | 130 |
125 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) | 131 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) |
126 { | 132 { |
127 EventMap::iterator it = eventCallbacks.find(eventName); | 133 EventMap::iterator it = eventCallbacks.find(eventName); |
128 if (it != eventCallbacks.end()) | 134 if (it != eventCallbacks.end()) |
129 it->second(params); | 135 it->second(params); |
130 } | 136 } |
131 | 137 |
132 void AdblockPlus::JsEngine::Gc() | 138 void AdblockPlus::JsEngine::Gc() |
133 { | 139 { |
134 while (!v8::V8::IdleNotification()); | 140 while (!GetIsolate()->IdleNotification(1000)); |
135 } | 141 } |
136 | 142 |
137 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 143 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
138 { | 144 { |
139 const JsContext context(shared_from_this()); | 145 const JsContext context(shared_from_this()); |
140 return JsValuePtr(new JsValue(shared_from_this(), | 146 return JsValuePtr(new JsValue(shared_from_this(), |
141 Utils::ToV8String(GetIsolate(), val))); | 147 Utils::ToV8String(GetIsolate(), val))); |
142 } | 148 } |
143 | 149 |
144 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 150 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
145 { | 151 { |
146 const JsContext context(shared_from_this()); | 152 const JsContext context(shared_from_this()); |
147 return JsValuePtr(new JsValue(shared_from_this(), | 153 return JsValuePtr(new JsValue(shared_from_this(), |
148 v8::Number::New(GetIsolate(), val))); | 154 v8::Number::New(GetIsolate(), val))); |
149 } | 155 } |
150 | 156 |
151 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
152 { | 158 { |
153 const JsContext context(shared_from_this()); | 159 const JsContext context(shared_from_this()); |
154 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); | 160 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(GetIsolate(
), val))); |
155 } | 161 } |
156 | 162 |
157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
158 { | 164 { |
159 const JsContext context(shared_from_this()); | 165 const JsContext context(shared_from_this()); |
160 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); | 166 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(GetIsolate()
))); |
161 } | 167 } |
162 | 168 |
163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 169 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
164 v8::InvocationCallback callback) | 170 v8::FunctionCallback callback) |
165 { | 171 { |
166 const JsContext context(shared_from_this()); | 172 const JsContext context(shared_from_this()); |
167 | 173 |
168 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 174 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
169 // it's no longer used | 175 // it's no longer used |
170 std::weak_ptr<JsEngine>* data = | 176 std::weak_ptr<JsEngine>* data = |
171 new std::weak_ptr<JsEngine>(shared_from_this()); | 177 new std::weak_ptr<JsEngine>(shared_from_this()); |
172 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 178 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(GetIsolate()
, callback, |
173 v8::External::New(data)); | 179 v8::External::New(GetIsolate(), data)); |
174 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 180 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); |
175 } | 181 } |
176 | 182 |
177 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) | 183 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
CallbackInfo<v8::Value>& arguments) |
178 { | 184 { |
179 const v8::Local<const v8::External> external = | 185 const v8::Local<const v8::External> external = |
180 v8::Local<const v8::External>::Cast(arguments.Data()); | 186 v8::Local<const v8::External>::Cast(arguments.Data()); |
181 std::weak_ptr<JsEngine>* data = | 187 std::weak_ptr<JsEngine>* data = |
182 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); | 188 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); |
183 JsEnginePtr result = data->lock(); | 189 JsEnginePtr result = data->lock(); |
184 if (!result) | 190 if (!result) |
185 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 191 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
186 return result; | 192 return result; |
187 } | 193 } |
188 | 194 |
189 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) | 195 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct
ionCallbackInfo<v8::Value>& arguments) |
190 { | 196 { |
191 const JsContext context(shared_from_this()); | 197 const JsContext context(shared_from_this()); |
192 JsValueList list; | 198 JsValueList list; |
193 for (int i = 0; i < arguments.Length(); i++) | 199 for (int i = 0; i < arguments.Length(); i++) |
194 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); | 200 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); |
195 return list; | 201 return list; |
196 } | 202 } |
197 | 203 |
198 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | 204 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() |
199 { | 205 { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 248 |
243 | 249 |
244 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 250 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
245 AdblockPlus::JsValuePtr value) | 251 AdblockPlus::JsValuePtr value) |
246 { | 252 { |
247 if (!globalJsObject) | 253 if (!globalJsObject) |
248 throw std::runtime_error("Global object cannot be null"); | 254 throw std::runtime_error("Global object cannot be null"); |
249 | 255 |
250 globalJsObject->SetProperty(name, value); | 256 globalJsObject->SetProperty(name, value); |
251 } | 257 } |
OLD | NEW |