LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 | 19 |
20 #include "GlobalJsObject.h" | 20 #include "GlobalJsObject.h" |
21 #include "JsContext.h" | 21 #include "JsContext.h" |
22 #include "JsError.h" | 22 #include "JsError.h" |
23 #include "Utils.h" | 23 #include "Utils.h" |
24 | 24 |
25 namespace | 25 namespace |
26 { | 26 { |
27 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) | 27 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, |
| 28 const std::string& source, const std::string& filename) |
28 { | 29 { |
29 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); | 30 using AdblockPlus::Utils::ToV8String; |
| 31 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); |
30 if (filename.length()) | 32 if (filename.length()) |
31 { | 33 { |
32 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); | 34 const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); |
33 return v8::Script::Compile(v8Source, v8Filename); | 35 return v8::Script::Compile(v8Source, v8Filename); |
34 } | 36 } |
35 else | 37 else |
36 return v8::Script::Compile(v8Source); | 38 return v8::Script::Compile(v8Source); |
37 } | 39 } |
38 | 40 |
39 void CheckTryCatch(const v8::TryCatch& tryCatch) | 41 void CheckTryCatch(const v8::TryCatch& tryCatch) |
40 { | 42 { |
41 if (tryCatch.HasCaught()) | 43 if (tryCatch.HasCaught()) |
42 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 44 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |
43 } | 45 } |
| 46 |
| 47 class V8Initializer |
| 48 { |
| 49 V8Initializer() |
| 50 { |
| 51 v8::V8::Initialize(); |
| 52 } |
| 53 |
| 54 ~V8Initializer() |
| 55 { |
| 56 v8::V8::Dispose(); |
| 57 } |
| 58 public: |
| 59 static void Init() |
| 60 { |
| 61 // it's threadsafe since C++11 and it will be instantiated only once and |
| 62 // destroyed at the application exit |
| 63 static V8Initializer initializer; |
| 64 } |
| 65 }; |
44 } | 66 } |
45 | 67 |
46 AdblockPlus::JsEngine::JsEngine() | 68 AdblockPlus::JsEngine::JsEngine() |
47 : isolate(v8::Isolate::GetCurrent()) | 69 : isolate(v8::Isolate::GetCurrent()) |
48 { | 70 { |
49 } | 71 } |
50 | 72 |
51 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 73 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) |
52 { | 74 { |
| 75 V8Initializer::Init(); |
53 JsEnginePtr result(new JsEngine()); | 76 JsEnginePtr result(new JsEngine()); |
54 | 77 |
55 const v8::Locker locker(result->isolate); | 78 const v8::Locker locker(result->isolate); |
56 const v8::HandleScope handleScope; | 79 const v8::HandleScope handleScope; |
57 | 80 |
58 result->context.reset(result->isolate, v8::Context::New()); | 81 result->context.reset(result->isolate, v8::Context::New(result->isolate)); |
59 AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 82 |
60 JsValuePtr(new JsValue(result, result->context->Global()))); | 83 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( |
| 84 result->isolate, result->context)->Global(); |
| 85 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); |
| 86 |
| 87 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); |
61 return result; | 88 return result; |
62 } | 89 } |
63 | 90 |
64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 91 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
65 const std::string& filename) | 92 const std::string& filename) |
66 { | 93 { |
67 const JsContext context(shared_from_this()); | 94 const JsContext context(shared_from_this()); |
68 const v8::TryCatch tryCatch; | 95 const v8::TryCatch tryCatch; |
69 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 96 const v8::Handle<v8::Script> script = CompileScript(isolate, source, |
| 97 filename); |
70 CheckTryCatch(tryCatch); | 98 CheckTryCatch(tryCatch); |
71 v8::Local<v8::Value> result = script->Run(); | 99 v8::Local<v8::Value> result = script->Run(); |
72 CheckTryCatch(tryCatch); | 100 CheckTryCatch(tryCatch); |
73 return JsValuePtr(new JsValue(shared_from_this(), result)); | 101 return JsValuePtr(new JsValue(shared_from_this(), result)); |
74 } | 102 } |
75 | 103 |
76 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 104 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
77 AdblockPlus::JsEngine::EventCallback callback) | 105 AdblockPlus::JsEngine::EventCallback callback) |
78 { | 106 { |
79 eventCallbacks[eventName] = callback; | 107 eventCallbacks[eventName] = callback; |
(...skipping 13 matching lines...) Expand all Loading... |
93 | 121 |
94 void AdblockPlus::JsEngine::Gc() | 122 void AdblockPlus::JsEngine::Gc() |
95 { | 123 { |
96 while (!v8::V8::IdleNotification()); | 124 while (!v8::V8::IdleNotification()); |
97 } | 125 } |
98 | 126 |
99 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 127 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
100 { | 128 { |
101 const JsContext context(shared_from_this()); | 129 const JsContext context(shared_from_this()); |
102 return JsValuePtr(new JsValue(shared_from_this(), | 130 return JsValuePtr(new JsValue(shared_from_this(), |
103 v8::String::New(val.c_str(), val.length()))); | 131 Utils::ToV8String(isolate, val))); |
104 } | 132 } |
105 | 133 |
106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 134 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
107 { | 135 { |
108 const JsContext context(shared_from_this()); | 136 const JsContext context(shared_from_this()); |
109 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val))); | 137 return JsValuePtr(new JsValue(shared_from_this(), |
| 138 v8::Number::New(isolate, val))); |
110 } | 139 } |
111 | 140 |
112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 141 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
113 { | 142 { |
114 const JsContext context(shared_from_this()); | 143 const JsContext context(shared_from_this()); |
115 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); | 144 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); |
116 } | 145 } |
117 | 146 |
118 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 147 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
119 { | 148 { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 return logSystem; | 222 return logSystem; |
194 } | 223 } |
195 | 224 |
196 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 225 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) |
197 { | 226 { |
198 if (!val) | 227 if (!val) |
199 throw std::runtime_error("LogSystem cannot be null"); | 228 throw std::runtime_error("LogSystem cannot be null"); |
200 | 229 |
201 logSystem = val; | 230 logSystem = val; |
202 } | 231 } |
| 232 |
| 233 |
| 234 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
| 235 AdblockPlus::JsValuePtr value) |
| 236 { |
| 237 if (!globalJsObject) |
| 238 throw std::runtime_error("Global object cannot be null"); |
| 239 |
| 240 globalJsObject->SetProperty(name, value); |
| 241 } |
LEFT | RIGHT |