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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 5598762307158016: Issue 1550 - Get rid of V8ValueHolder.h (Closed)
Left Patch Set: Created Nov. 10, 2014, 9:18 a.m.
Right Patch Set: rebase Created May 20, 2016, 3:20 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 <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-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 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(new v8::Persistent<v8::Context>(result->isolate, 81 result->context.reset(new v8::Persistent<v8::Context>(result->isolate,
59 v8::Context::New(result->isolate))); 82 v8::Context::New(result->isolate)));
60 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( 83 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New(
61 result->isolate, *result->context)->Global(); 84 result->isolate, *result->context)->Global();
62 AdblockPlus::GlobalJsObject::Setup(result, appInfo, 85 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext));
63 JsValuePtr(new JsValue(result, globalContext))); 86 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject);
64 return result; 87 return result;
65 } 88 }
66 89
67 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 90 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
68 const std::string& filename) 91 const std::string& filename)
69 { 92 {
70 const JsContext context(shared_from_this()); 93 const JsContext context(shared_from_this());
71 const v8::TryCatch tryCatch; 94 const v8::TryCatch tryCatch;
72 const v8::Handle<v8::Script> script = CompileScript(source, filename); 95 const v8::Handle<v8::Script> script = CompileScript(isolate, source,
96 filename);
73 CheckTryCatch(tryCatch); 97 CheckTryCatch(tryCatch);
74 v8::Local<v8::Value> result = script->Run(); 98 v8::Local<v8::Value> result = script->Run();
75 CheckTryCatch(tryCatch); 99 CheckTryCatch(tryCatch);
76 return JsValuePtr(new JsValue(shared_from_this(), result)); 100 return JsValuePtr(new JsValue(shared_from_this(), result));
77 } 101 }
78 102
79 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 103 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
80 AdblockPlus::JsEngine::EventCallback callback) 104 AdblockPlus::JsEngine::EventCallback callback)
81 { 105 {
82 eventCallbacks[eventName] = callback; 106 eventCallbacks[eventName] = callback;
(...skipping 13 matching lines...) Expand all
96 120
97 void AdblockPlus::JsEngine::Gc() 121 void AdblockPlus::JsEngine::Gc()
98 { 122 {
99 while (!v8::V8::IdleNotification()); 123 while (!v8::V8::IdleNotification());
100 } 124 }
101 125
102 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 126 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
103 { 127 {
104 const JsContext context(shared_from_this()); 128 const JsContext context(shared_from_this());
105 return JsValuePtr(new JsValue(shared_from_this(), 129 return JsValuePtr(new JsValue(shared_from_this(),
106 v8::String::New(val.c_str(), val.length()))); 130 Utils::ToV8String(isolate, val)));
107 } 131 }
108 132
109 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) 133 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
110 { 134 {
111 const JsContext context(shared_from_this()); 135 const JsContext context(shared_from_this());
112 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val))); 136 return JsValuePtr(new JsValue(shared_from_this(),
137 v8::Number::New(isolate, val)));
113 } 138 }
114 139
115 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) 140 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
116 { 141 {
117 const JsContext context(shared_from_this()); 142 const JsContext context(shared_from_this());
118 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); 143 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
119 } 144 }
120 145
121 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 146 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
122 { 147 {
123 const JsContext context(shared_from_this()); 148 const JsContext context(shared_from_this());
124 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); 149 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New()));
125 } 150 }
126 151
127 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( 152 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
128 v8::InvocationCallback callback) 153 v8::InvocationCallback callback)
129 { 154 {
130 const JsContext context(shared_from_this()); 155 const JsContext context(shared_from_this());
131 156
132 // 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
133 // it's no longer used 158 // it's no longer used
134 std::tr1::weak_ptr<JsEngine>* data = 159 std::weak_ptr<JsEngine>* data =
135 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); 160 new std::weak_ptr<JsEngine>(shared_from_this());
136 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, 161 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
137 v8::External::New(data)); 162 v8::External::New(data));
138 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); 163 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction()));
139 } 164 }
140 165
141 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments) 166 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments)
142 { 167 {
143 const v8::Local<const v8::External> external = 168 const v8::Local<const v8::External> external =
144 v8::Local<const v8::External>::Cast(arguments.Data()); 169 v8::Local<const v8::External>::Cast(arguments.Data());
145 std::tr1::weak_ptr<JsEngine>* data = 170 std::weak_ptr<JsEngine>* data =
146 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); 171 static_cast<std::weak_ptr<JsEngine>*>(external->Value());
147 JsEnginePtr result = data->lock(); 172 JsEnginePtr result = data->lock();
148 if (!result) 173 if (!result)
149 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?") ;
150 return result; 175 return result;
151 } 176 }
152 177
153 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) 178 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
154 { 179 {
155 const JsContext context(shared_from_this()); 180 const JsContext context(shared_from_this());
156 JsValueList list; 181 JsValueList list;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 return logSystem; 221 return logSystem;
197 } 222 }
198 223
199 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) 224 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val)
200 { 225 {
201 if (!val) 226 if (!val)
202 throw std::runtime_error("LogSystem cannot be null"); 227 throw std::runtime_error("LogSystem cannot be null");
203 228
204 logSystem = val; 229 logSystem = val;
205 } 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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld