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

Unified Diff: src/JsEngine.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Patch Set: Created April 15, 2013, 2:54 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
« no previous file with comments | « src/GlobalJsObject.cpp ('k') | src/JsValue.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/JsEngine.cpp
===================================================================
--- a/src/JsEngine.cpp
+++ b/src/JsEngine.cpp
@@ -2,23 +2,22 @@
#include <sstream>
#include "GlobalJsObject.h"
namespace
{
v8::Handle<v8::Context> CreateContext(
v8::Isolate* isolate,
- AdblockPlus::ErrorCallback& errorCallback,
- AdblockPlus::WebRequest& webRequest)
+ AdblockPlus::JsEngine& jsEngine)
{
const v8::Locker locker(isolate);
const v8::HandleScope handleScope;
const v8::Handle<v8::ObjectTemplate> global =
- AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest);
+ AdblockPlus::GlobalJsObject::Create(jsEngine);
return v8::Context::New(0, global);
}
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())
{
@@ -59,21 +58,22 @@ namespace
}
AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
const v8::Handle<v8::Message> message)
: std::runtime_error(ExceptionToString(exception, message))
{
}
-AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
+AdblockPlus::JsEngine::JsEngine(FileReader* const fileReader,
WebRequest* const webRequest,
ErrorCallback* const errorCallback)
- : fileReader(fileReader), isolate(v8::Isolate::GetCurrent()),
- context(CreateContext(isolate, *errorCallback, *webRequest))
+ : fileReader(*fileReader), webRequest(*webRequest),
+ errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()),
+ context(CreateContext(isolate, *this))
{
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& source,
const std::string& filename)
{
const Context context(*this);
const v8::TryCatch tryCatch;
@@ -81,17 +81,17 @@ AdblockPlus::JsValuePtr AdblockPlus::JsE
CheckTryCatch(tryCatch);
v8::Local<v8::Value> result = script->Run();
CheckTryCatch(tryCatch);
return JsValuePtr(new JsValue(*this, result));
}
void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
{
- const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
+ const std::auto_ptr<std::istream> file = fileReader.Read(scriptPath);
if (!*file)
throw std::runtime_error("Unable to load script " + scriptPath);
Evaluate(Slurp(*file));
}
void AdblockPlus::JsEngine::Gc()
{
while (!v8::V8::IdleNotification());
@@ -110,13 +110,35 @@ AdblockPlus::JsValuePtr AdblockPlus::JsE
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
{
const Context context(*this);
return JsValuePtr(new JsValue(*this, v8::Boolean::New(val)));
}
+AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
+{
+ const Context context(*this);
+ return JsValuePtr(new JsValue(*this, v8::Object::New()));
+}
+
+AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments& arguments)
+{
+ const v8::Local<const v8::External> external =
+ v8::Local<const v8::External>::Cast(arguments.Data());
+ return *static_cast<JsEngine* const>(external->Value());
+}
+
+AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Arguments& arguments)
+{
+ const Context context(*this);
+ JsValueList list;
+ for (int i = 0; i < arguments.Length(); i++)
+ list.push_back(JsValuePtr(new JsValue(*this, arguments[i])));
+ return list;
+}
+
AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
: locker(jsEngine.isolate), handleScope(),
contextScope(jsEngine.context)
{
}
« no previous file with comments | « src/GlobalJsObject.cpp ('k') | src/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld