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: Created March 14, 2013, 10:02 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
« include/AdblockPlus/JsEngine.h ('K') | « 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,13 +1,15 @@
#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"),
@@ -33,25 +35,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)
+v8::Handle<v8::Script> AdblockPlus::JsEngine::CompileScript(const char* source, const char* filename)
+{
+ const v8::Handle<v8::String> v8Source = v8::String::New(source);
+ if (filename)
+ {
+ const v8::Handle<v8::String> v8Filename = v8::String::New(filename);
+ return v8::Script::Compile(v8Source, v8Filename);
+ }
+ else
+ return v8::Script::Compile(v8Source);
+}
+
+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::Load(const std::string& scriptPath)
{
const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
« include/AdblockPlus/JsEngine.h ('K') | « shell/src/Main.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld