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

Side by Side Diff: src/JsEngine.cpp

Issue 6233220328718336: Issue #3593, #1197- fix isolate management (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « src/JsContext.cpp ('k') | src/JsValue.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 28 matching lines...) Expand all
58 public: 57 public:
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::JsEngine::JsEngine() 67 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate()
69 : isolate(v8::Isolate::GetCurrent()) 68 {
69 V8Initializer::Init();
70 isolate = v8::Isolate::New();
71 }
72
73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate()
74 {
75 isolate->Dispose();
76 isolate = nullptr;
77 }
78
79 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate)
80 : isolate(isolate)
70 { 81 {
71 } 82 }
72 83
73 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) 84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate)
74 { 85 {
75 V8Initializer::Init(); 86 JsEnginePtr result(new JsEngine(isolate));
76 JsEnginePtr result(new JsEngine());
77 87
78 const v8::Locker locker(result->isolate); 88 const v8::Locker locker(result->GetIsolate());
79 const v8::HandleScope handleScope; 89 const v8::Isolate::Scope isolateScope(result->GetIsolate());
90 const v8::HandleScope handleScope(result->GetIsolate());
80 91
81 result->context.reset(new v8::Persistent<v8::Context>(result->isolate, 92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(),
82 v8::Context::New(result->isolate))); 93 v8::Context::New(result->GetIsolate())));
83 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( 94 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New(
84 result->isolate, *result->context)->Global(); 95 result->GetIsolate(), *result->context)->Global();
85 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); 96 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext));
86 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); 97 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject);
87 return result; 98 return result;
88 } 99 }
89 100
90 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
91 const std::string& filename) 102 const std::string& filename)
92 { 103 {
93 const JsContext context(shared_from_this()); 104 const JsContext context(shared_from_this());
94 const v8::TryCatch tryCatch; 105 const v8::TryCatch tryCatch;
95 const v8::Handle<v8::Script> script = CompileScript(isolate, source, 106 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source,
96 filename); 107 filename);
97 CheckTryCatch(tryCatch); 108 CheckTryCatch(tryCatch);
98 v8::Local<v8::Value> result = script->Run(); 109 v8::Local<v8::Value> result = script->Run();
99 CheckTryCatch(tryCatch); 110 CheckTryCatch(tryCatch);
100 return JsValuePtr(new JsValue(shared_from_this(), result)); 111 return JsValuePtr(new JsValue(shared_from_this(), result));
101 } 112 }
102 113
103 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 114 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
104 AdblockPlus::JsEngine::EventCallback callback) 115 AdblockPlus::JsEngine::EventCallback callback)
105 { 116 {
(...skipping 14 matching lines...) Expand all
120 131
121 void AdblockPlus::JsEngine::Gc() 132 void AdblockPlus::JsEngine::Gc()
122 { 133 {
123 while (!v8::V8::IdleNotification()); 134 while (!v8::V8::IdleNotification());
124 } 135 }
125 136
126 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 137 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
127 { 138 {
128 const JsContext context(shared_from_this()); 139 const JsContext context(shared_from_this());
129 return JsValuePtr(new JsValue(shared_from_this(), 140 return JsValuePtr(new JsValue(shared_from_this(),
130 Utils::ToV8String(isolate, val))); 141 Utils::ToV8String(GetIsolate(), val)));
131 } 142 }
132 143
133 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) 144 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
134 { 145 {
135 const JsContext context(shared_from_this()); 146 const JsContext context(shared_from_this());
136 return JsValuePtr(new JsValue(shared_from_this(), 147 return JsValuePtr(new JsValue(shared_from_this(),
137 v8::Number::New(isolate, val))); 148 v8::Number::New(GetIsolate(), val)));
138 } 149 }
139 150
140 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) 151 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
141 { 152 {
142 const JsContext context(shared_from_this()); 153 const JsContext context(shared_from_this());
143 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); 154 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
144 } 155 }
145 156
146 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
147 { 158 {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 242
232 243
233 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 244 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
234 AdblockPlus::JsValuePtr value) 245 AdblockPlus::JsValuePtr value)
235 { 246 {
236 if (!globalJsObject) 247 if (!globalJsObject)
237 throw std::runtime_error("Global object cannot be null"); 248 throw std::runtime_error("Global object cannot be null");
238 249
239 globalJsObject->SetProperty(name, value); 250 globalJsObject->SetProperty(name, value);
240 } 251 }
OLDNEW
« no previous file with comments | « src/JsContext.cpp ('k') | src/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld