| 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); |