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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/JsEngine.cpp
===================================================================
--- a/src/JsEngine.cpp
+++ b/src/JsEngine.cpp
@@ -11,19 +11,20 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#include <AdblockPlus.h>
-#include <sstream>
#include "GlobalJsObject.h"
+#include "JsContext.h"
+#include "JsError.h"
#include "Utils.h"
namespace
{
v8::Handle<v8::Script> CompileScript(const std::string& source, const std::string& filename)
{
const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
if (filename.length())
@@ -35,61 +36,40 @@ namespace
return v8::Script::Compile(v8Source);
}
void CheckTryCatch(const v8::TryCatch& tryCatch)
{
if (tryCatch.HasCaught())
throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
}
-
- std::string ExceptionToString(const v8::Handle<v8::Value> exception,
- const v8::Handle<v8::Message> message)
- {
- std::stringstream error;
- error << *v8::String::Utf8Value(exception);
- if (!message.IsEmpty())
- {
- error << " at ";
- error << *v8::String::Utf8Value(message->GetScriptResourceName());
- error << ":";
- error << message->GetLineNumber();
- }
- return error.str();
- }
-}
-
-AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
- const v8::Handle<v8::Message> message)
- : std::runtime_error(ExceptionToString(exception, message))
-{
}
AdblockPlus::JsEngine::JsEngine()
: isolate(v8::Isolate::GetCurrent())
{
}
AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo)
{
JsEnginePtr result(new JsEngine());
const v8::Locker locker(result->isolate);
const v8::HandleScope handleScope;
- result->context = v8::Context::New();
+ result->context.reset(result->isolate, v8::Context::New());
AdblockPlus::GlobalJsObject::Setup(result, appInfo,
JsValuePtr(new JsValue(result, result->context->Global())));
return result;
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& source,
const std::string& filename)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
const v8::TryCatch tryCatch;
const v8::Handle<v8::Script> script = CompileScript(source, filename);
CheckTryCatch(tryCatch);
v8::Local<v8::Value> result = script->Run();
CheckTryCatch(tryCatch);
return JsValuePtr(new JsValue(shared_from_this(), result));
}
@@ -113,43 +93,43 @@ void AdblockPlus::JsEngine::TriggerEvent
void AdblockPlus::JsEngine::Gc()
{
while (!v8::V8::IdleNotification());
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
return JsValuePtr(new JsValue(shared_from_this(),
v8::String::New(val.c_str(), val.length())));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val)));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New()));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
v8::InvocationCallback callback)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
// Note: we are leaking this weak pointer, no obvious way to destroy it when
// it's no longer used
std::tr1::weak_ptr<JsEngine>* data =
new std::tr1::weak_ptr<JsEngine>(shared_from_this());
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
v8::External::New(data));
return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction()));
@@ -164,17 +144,17 @@ AdblockPlus::JsEnginePtr AdblockPlus::Js
JsEnginePtr result = data->lock();
if (!result)
throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?");
return result;
}
AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Arguments& arguments)
{
- const Context context(shared_from_this());
+ const JsContext context(shared_from_this());
JsValueList list;
for (int i = 0; i < arguments.Length(); i++)
list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i])));
return list;
}
AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem()
{
@@ -215,14 +195,8 @@ AdblockPlus::LogSystemPtr AdblockPlus::J
void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val)
{
if (!val)
throw std::runtime_error("LogSystem cannot be null");
logSystem = val;
}
-
-AdblockPlus::JsEngine::Context::Context(const JsEnginePtr jsEngine)
- : locker(jsEngine->isolate), handleScope(),
- contextScope(jsEngine->context)
-{
-}
« 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