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

Side by Side Diff: src/JsEngine.cpp

Issue 10727002: Get rid of dependencies on v8.h in public header files (Closed)
Patch Set: Added helper class to make using v8 values via auto_ptr less awkward Created May 23, 2013, 4:08 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
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 <sstream>
20 19
21 #include "GlobalJsObject.h" 20 #include "GlobalJsObject.h"
21 #include "JsContext.h"
22 #include "JsError.h"
22 #include "Utils.h" 23 #include "Utils.h"
23 24
24 namespace 25 namespace
25 { 26 {
26 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 27 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
27 { 28 {
28 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 29 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
29 if (filename.length()) 30 if (filename.length())
30 { 31 {
31 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() ); 32 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
32 return v8::Script::Compile(v8Source, v8Filename); 33 return v8::Script::Compile(v8Source, v8Filename);
33 } 34 }
34 else 35 else
35 return v8::Script::Compile(v8Source); 36 return v8::Script::Compile(v8Source);
36 } 37 }
37 38
38 void CheckTryCatch(const v8::TryCatch& tryCatch) 39 void CheckTryCatch(const v8::TryCatch& tryCatch)
39 { 40 {
40 if (tryCatch.HasCaught()) 41 if (tryCatch.HasCaught())
41 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 42 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
42 } 43 }
43
44 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
45 const v8::Handle<v8::Message> message)
46 {
47 std::stringstream error;
48 error << *v8::String::Utf8Value(exception);
49 if (!message.IsEmpty())
50 {
51 error << " at ";
52 error << *v8::String::Utf8Value(message->GetScriptResourceName());
53 error << ":";
54 error << message->GetLineNumber();
55 }
56 return error.str();
57 }
58 }
59
60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
61 const v8::Handle<v8::Message> message)
62 : std::runtime_error(ExceptionToString(exception, message))
63 {
64 } 44 }
65 45
66 AdblockPlus::JsEngine::JsEngine() 46 AdblockPlus::JsEngine::JsEngine()
67 : isolate(v8::Isolate::GetCurrent()) 47 : isolate(v8::Isolate::GetCurrent())
68 { 48 {
69 } 49 }
70 50
71 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) 51 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo)
72 { 52 {
73 JsEnginePtr result(new JsEngine()); 53 JsEnginePtr result(new JsEngine());
74 54
75 const v8::Locker locker(result->isolate); 55 const v8::Locker locker(result->isolate);
76 const v8::HandleScope handleScope; 56 const v8::HandleScope handleScope;
77 57
78 result->context = v8::Context::New(); 58 result->context.reset(result->isolate, v8::Context::New());
79 AdblockPlus::GlobalJsObject::Setup(result, appInfo, 59 AdblockPlus::GlobalJsObject::Setup(result, appInfo,
80 JsValuePtr(new JsValue(result, result->context->Global()))); 60 JsValuePtr(new JsValue(result, result->context->Global())));
81 return result; 61 return result;
82 } 62 }
83 63
84 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
85 const std::string& filename) 65 const std::string& filename)
86 { 66 {
87 const Context context(shared_from_this()); 67 const JsContext context(shared_from_this());
88 const v8::TryCatch tryCatch; 68 const v8::TryCatch tryCatch;
89 const v8::Handle<v8::Script> script = CompileScript(source, filename); 69 const v8::Handle<v8::Script> script = CompileScript(source, filename);
90 CheckTryCatch(tryCatch); 70 CheckTryCatch(tryCatch);
91 v8::Local<v8::Value> result = script->Run(); 71 v8::Local<v8::Value> result = script->Run();
92 CheckTryCatch(tryCatch); 72 CheckTryCatch(tryCatch);
93 return JsValuePtr(new JsValue(shared_from_this(), result)); 73 return JsValuePtr(new JsValue(shared_from_this(), result));
94 } 74 }
95 75
96 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 76 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
97 AdblockPlus::JsEngine::EventCallback callback) 77 AdblockPlus::JsEngine::EventCallback callback)
(...skipping 13 matching lines...) Expand all
111 it->second(); 91 it->second();
112 } 92 }
113 93
114 void AdblockPlus::JsEngine::Gc() 94 void AdblockPlus::JsEngine::Gc()
115 { 95 {
116 while (!v8::V8::IdleNotification()); 96 while (!v8::V8::IdleNotification());
117 } 97 }
118 98
119 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 99 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
120 { 100 {
121 const Context context(shared_from_this()); 101 const JsContext context(shared_from_this());
122 return JsValuePtr(new JsValue(shared_from_this(), 102 return JsValuePtr(new JsValue(shared_from_this(),
123 v8::String::New(val.c_str(), val.length()))); 103 v8::String::New(val.c_str(), val.length())));
124 } 104 }
125 105
126 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) 106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
127 { 107 {
128 const Context context(shared_from_this()); 108 const JsContext context(shared_from_this());
129 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val))); 109 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val)));
130 } 110 }
131 111
132 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) 112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
133 { 113 {
134 const Context context(shared_from_this()); 114 const JsContext context(shared_from_this());
135 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); 115 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
136 } 116 }
137 117
138 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 118 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
139 { 119 {
140 const Context context(shared_from_this()); 120 const JsContext context(shared_from_this());
141 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); 121 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New()));
142 } 122 }
143 123
144 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( 124 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
145 v8::InvocationCallback callback) 125 v8::InvocationCallback callback)
146 { 126 {
147 const Context context(shared_from_this()); 127 const JsContext context(shared_from_this());
148 128
149 // Note: we are leaking this weak pointer, no obvious way to destroy it when 129 // Note: we are leaking this weak pointer, no obvious way to destroy it when
150 // it's no longer used 130 // it's no longer used
151 std::tr1::weak_ptr<JsEngine>* data = 131 std::tr1::weak_ptr<JsEngine>* data =
152 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); 132 new std::tr1::weak_ptr<JsEngine>(shared_from_this());
153 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, 133 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
154 v8::External::New(data)); 134 v8::External::New(data));
155 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); 135 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction()));
156 } 136 }
157 137
158 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments) 138 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments)
159 { 139 {
160 const v8::Local<const v8::External> external = 140 const v8::Local<const v8::External> external =
161 v8::Local<const v8::External>::Cast(arguments.Data()); 141 v8::Local<const v8::External>::Cast(arguments.Data());
162 std::tr1::weak_ptr<JsEngine>* data = 142 std::tr1::weak_ptr<JsEngine>* data =
163 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); 143 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value());
164 JsEnginePtr result = data->lock(); 144 JsEnginePtr result = data->lock();
165 if (!result) 145 if (!result)
166 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ; 146 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ;
167 return result; 147 return result;
168 } 148 }
169 149
170 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) 150 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
171 { 151 {
172 const Context context(shared_from_this()); 152 const JsContext context(shared_from_this());
173 JsValueList list; 153 JsValueList list;
174 for (int i = 0; i < arguments.Length(); i++) 154 for (int i = 0; i < arguments.Length(); i++)
175 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); 155 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i])));
176 return list; 156 return list;
177 } 157 }
178 158
179 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() 159 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem()
180 { 160 {
181 if (!fileSystem) 161 if (!fileSystem)
182 fileSystem.reset(new DefaultFileSystem()); 162 fileSystem.reset(new DefaultFileSystem());
(...skipping 30 matching lines...) Expand all
213 return logSystem; 193 return logSystem;
214 } 194 }
215 195
216 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) 196 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val)
217 { 197 {
218 if (!val) 198 if (!val)
219 throw std::runtime_error("LogSystem cannot be null"); 199 throw std::runtime_error("LogSystem cannot be null");
220 200
221 logSystem = val; 201 logSystem = val;
222 } 202 }
223
224 AdblockPlus::JsEngine::Context::Context(const JsEnginePtr jsEngine)
225 : locker(jsEngine->isolate), handleScope(),
226 contextScope(jsEngine->context)
227 {
228 }
OLDNEW
« include/AdblockPlus/V8ValueHolder.h ('K') | « src/JsContext.cpp ('k') | src/JsError.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld