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

Unified Diff: src/JsEngine.cpp

Issue 10259001: XMLHttpRequest API (Closed)
Patch Set: Created April 10, 2013, 3:19 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
@@ -1,22 +1,23 @@
#include <AdblockPlus.h>
#include <sstream>
#include "GlobalJsObject.h"
namespace
{
v8::Handle<v8::Context> CreateContext(
- AdblockPlus::ErrorCallback& errorCallback)
+ AdblockPlus::ErrorCallback& errorCallback,
+ AdblockPlus::WebRequest& webRequest)
{
const v8::Locker locker(v8::Isolate::GetCurrent());
const v8::HandleScope handleScope;
const v8::Handle<v8::ObjectTemplate> global =
- AdblockPlus::GlobalJsObject::Create(errorCallback);
+ AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest);
return v8::Context::New(0, global);
}
v8::Handle<v8::Script> CompileScript(const char* source, const char* filename)
{
const v8::Handle<v8::String> v8Source = v8::String::New(source);
if (filename && filename[0])
{
@@ -58,37 +59,40 @@ 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,
+ WebRequest* const webRequest,
ErrorCallback* const errorCallback)
- : fileReader(fileReader), context(CreateContext(*errorCallback))
+ : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest))
{
}
-void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename)
+std::string AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename)
{
const v8::Locker locker(v8::Isolate::GetCurrent());
const v8::HandleScope handleScope;
const v8::Context::Scope contextScope(context);
const v8::TryCatch tryCatch;
const v8::Handle<v8::Script> script = CompileScript(source, filename);
CheckTryCatch(tryCatch);
- script->Run();
+ v8::Local<v8::Value> result = script->Run();
CheckTryCatch(tryCatch);
+ v8::String::Utf8Value resultString(result);
+ return std::string(*resultString);
}
-void AdblockPlus::JsEngine::Evaluate(const std::string& source,
+std::string AdblockPlus::JsEngine::Evaluate(const std::string& source,
const std::string& filename)
{
- Evaluate(source.c_str(), filename.c_str());
+ return Evaluate(source.c_str(), filename.c_str());
}
void AdblockPlus::JsEngine::Load(const std::string& 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));

Powered by Google App Engine
This is Rietveld