Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 6233220328718336: Issue #3593, #1197- fix isolate management (Closed)
Left Patch Set: Created June 11, 2015, 12:45 p.m.
Right Patch Set: rebase fix v8 initialization Created May 20, 2016, 3:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/JsContext.cpp ('k') | src/JsValue.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 *
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
20 #include "GlobalJsObject.h" 19 #include "GlobalJsObject.h"
21 #include "JsContext.h" 20 #include "JsContext.h"
22 #include "JsError.h" 21 #include "JsError.h"
23 #include "Utils.h" 22 #include "Utils.h"
24 23
25 namespace 24 namespace
26 { 25 {
27 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, 26 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate,
28 const std::string& source, const std::string& filename) 27 const std::string& source, const std::string& filename)
29 { 28 {
(...skipping 29 matching lines...) Expand all
59 static void Init() 58 static void Init()
60 { 59 {
61 // it's threadsafe since C++11 and it will be instantiated only once and 60 // it's threadsafe since C++11 and it will be instantiated only once and
62 // destroyed at the application exit 61 // destroyed at the application exit
63 static V8Initializer initializer; 62 static V8Initializer initializer;
64 } 63 }
65 }; 64 };
66 } 65 }
67 66
68 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() 67 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate()
69 : isolate(v8::Isolate::New()) 68 {
Eric 2015/08/05 22:29:16 This is changing the behavior non-trivially, since
sergei 2015/11/16 16:52:09 There is no any default isolate. It `v8::Isolate::
Eric 2015/11/17 21:27:43 Sure there is. See isolate.cc:340 Isolate* Iso
70 { 69 V8Initializer::Init();
70 isolate = v8::Isolate::New();
71 } 71 }
72 72
73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() 73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate()
74 { 74 {
75 isolate->Dispose(); 75 isolate->Dispose();
Eric 2015/08/05 22:29:16 If we initialize with 'New()' rather than 'GetCurr
sergei 2015/11/16 16:52:09 Do you mean we should create a typedef on `std::sh
Eric 2015/11/17 21:27:43 No.
76 isolate = nullptr; 76 isolate = nullptr;
77 } 77 }
78 78
79 AdblockPlus::JsEngine::JsEngine() 79 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate)
80 { 80 : isolate(isolate)
81 } 81 {
82 82 }
83 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) 83
84 { 84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate)
85 V8Initializer::Init(); 85 {
86 JsEnginePtr result(new JsEngine()); 86 JsEnginePtr result(new JsEngine(isolate));
87 87
88 const v8::Locker locker(result->isolate); 88 const v8::Locker locker(result->GetIsolate());
89 const v8::Isolate::Scope isolateScope(result->isolate); 89 const v8::Isolate::Scope isolateScope(result->GetIsolate());
90 const v8::HandleScope handleScope(result->isolate); 90 const v8::HandleScope handleScope(result->GetIsolate());
91 91
92 result->context.reset(result->isolate, v8::Context::New(result->isolate)); 92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(),
93 v8::Context::New(result->GetIsolate())));
93 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( 94 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New(
94 result->isolate, result->context)->Global(); 95 result->GetIsolate(), *result->context)->Global();
95 AdblockPlus::GlobalJsObject::Setup(result, appInfo, 96 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext));
96 JsValuePtr(new JsValue(result, globalContext))); 97 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject);
97 return result; 98 return result;
98 } 99 }
99 100
100 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
101 const std::string& filename) 102 const std::string& filename)
102 { 103 {
103 const JsContext context(shared_from_this()); 104 const JsContext context(shared_from_this());
104 const v8::TryCatch tryCatch; 105 const v8::TryCatch tryCatch;
105 const v8::Handle<v8::Script> script = CompileScript(isolate, source, 106 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source,
106 filename); 107 filename);
107 CheckTryCatch(tryCatch); 108 CheckTryCatch(tryCatch);
108 v8::Local<v8::Value> result = script->Run(); 109 v8::Local<v8::Value> result = script->Run();
109 CheckTryCatch(tryCatch); 110 CheckTryCatch(tryCatch);
110 return JsValuePtr(new JsValue(shared_from_this(), result)); 111 return JsValuePtr(new JsValue(shared_from_this(), result));
111 } 112 }
112 113
113 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 114 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
114 AdblockPlus::JsEngine::EventCallback callback) 115 AdblockPlus::JsEngine::EventCallback callback)
115 { 116 {
(...skipping 14 matching lines...) Expand all
130 131
131 void AdblockPlus::JsEngine::Gc() 132 void AdblockPlus::JsEngine::Gc()
132 { 133 {
133 while (!v8::V8::IdleNotification()); 134 while (!v8::V8::IdleNotification());
134 } 135 }
135 136
136 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 137 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
137 { 138 {
138 const JsContext context(shared_from_this()); 139 const JsContext context(shared_from_this());
139 return JsValuePtr(new JsValue(shared_from_this(), 140 return JsValuePtr(new JsValue(shared_from_this(),
140 Utils::ToV8String(isolate, val))); 141 Utils::ToV8String(GetIsolate(), val)));
141 } 142 }
142 143
143 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) 144 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
144 { 145 {
145 const JsContext context(shared_from_this()); 146 const JsContext context(shared_from_this());
146 return JsValuePtr(new JsValue(shared_from_this(), 147 return JsValuePtr(new JsValue(shared_from_this(),
147 v8::Number::New(isolate, val))); 148 v8::Number::New(GetIsolate(), val)));
148 } 149 }
149 150
150 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) 151 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
151 { 152 {
152 const JsContext context(shared_from_this()); 153 const JsContext context(shared_from_this());
153 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); 154 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
154 } 155 }
155 156
156 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
157 { 158 {
158 const JsContext context(shared_from_this()); 159 const JsContext context(shared_from_this());
159 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); 160 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New()));
160 } 161 }
161 162
162 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
163 v8::InvocationCallback callback) 164 v8::InvocationCallback callback)
164 { 165 {
165 const JsContext context(shared_from_this()); 166 const JsContext context(shared_from_this());
166 167
167 // Note: we are leaking this weak pointer, no obvious way to destroy it when 168 // Note: we are leaking this weak pointer, no obvious way to destroy it when
168 // it's no longer used 169 // it's no longer used
169 std::tr1::weak_ptr<JsEngine>* data = 170 std::weak_ptr<JsEngine>* data =
170 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); 171 new std::weak_ptr<JsEngine>(shared_from_this());
171 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, 172 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
172 v8::External::New(data)); 173 v8::External::New(data));
173 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); 174 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction()));
174 } 175 }
175 176
176 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments) 177 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments)
177 { 178 {
178 const v8::Local<const v8::External> external = 179 const v8::Local<const v8::External> external =
179 v8::Local<const v8::External>::Cast(arguments.Data()); 180 v8::Local<const v8::External>::Cast(arguments.Data());
180 std::tr1::weak_ptr<JsEngine>* data = 181 std::weak_ptr<JsEngine>* data =
181 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); 182 static_cast<std::weak_ptr<JsEngine>*>(external->Value());
182 JsEnginePtr result = data->lock(); 183 JsEnginePtr result = data->lock();
183 if (!result) 184 if (!result)
184 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ; 185 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ;
185 return result; 186 return result;
186 } 187 }
187 188
188 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) 189 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
189 { 190 {
190 const JsContext context(shared_from_this()); 191 const JsContext context(shared_from_this());
191 JsValueList list; 192 JsValueList list;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 return logSystem; 232 return logSystem;
232 } 233 }
233 234
234 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) 235 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val)
235 { 236 {
236 if (!val) 237 if (!val)
237 throw std::runtime_error("LogSystem cannot be null"); 238 throw std::runtime_error("LogSystem cannot be null");
238 239
239 logSystem = val; 240 logSystem = val;
240 } 241 }
242
243
244 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
245 AdblockPlus::JsValuePtr value)
246 {
247 if (!globalJsObject)
248 throw std::runtime_error("Global object cannot be null");
249
250 globalJsObject->SetProperty(name, value);
251 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld