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

Unified Diff: src/JsEngine.cpp

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Patch Set: Review comments addressed Created March 15, 2013, 3:59 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 | « shell/src/Main.cpp ('k') | no next file » | 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
@@ -1,25 +1,40 @@
#include <AdblockPlus.h>
#include <sstream>
#include "ConsoleJsObject.h"
+extern const char* jsSources[];
+
namespace
{
v8::Handle<v8::Context> CreateContext(
AdblockPlus::ErrorCallback& errorCallback)
{
const v8::HandleScope handleScope;
const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
global->Set(v8::String::New("console"),
AdblockPlus::ConsoleJsObject::Create(errorCallback));
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])
+ {
+ const v8::Handle<v8::String> v8Filename = v8::String::New(filename);
+ return v8::Script::Compile(v8Source, v8Filename);
+ }
+ else
+ return v8::Script::Compile(v8Source);
+ }
+
+
void CheckTryCatch(const v8::TryCatch& tryCatch)
{
if (tryCatch.HasCaught())
throw AdblockPlus::JsError(tryCatch.Exception());
}
std::string Slurp(std::istream& stream)
{
@@ -33,30 +48,38 @@ AdblockPlus::JsError::JsError(const v8::
: std::runtime_error(*v8::String::AsciiValue(exception))
{
}
AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
ErrorCallback* const errorCallback)
: fileReader(fileReader), context(CreateContext(*errorCallback))
{
+ for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
+ Evaluate(jsSources[i + 1], jsSources[i]);
}
-void AdblockPlus::JsEngine::Evaluate(const std::string& source)
+void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename)
{
const v8::HandleScope handleScope;
const v8::Context::Scope contextScope(context);
- const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
const v8::TryCatch tryCatch;
- const v8::Handle<v8::Script> script = v8::Script::Compile(v8Source);
+ const v8::Handle<v8::Script> script = CompileScript(source, filename);
CheckTryCatch(tryCatch);
script->Run();
CheckTryCatch(tryCatch);
}
+void AdblockPlus::JsEngine::Evaluate(const std::string& source,
+ const std::string& filename)
+{
+ 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));
}
« no previous file with comments | « shell/src/Main.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld