OLD | NEW |
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 #include <sstream> | 2 #include <sstream> |
3 | 3 |
4 #include "GlobalJsObject.h" | 4 #include "GlobalJsObject.h" |
5 | 5 |
6 namespace | 6 namespace |
7 { | 7 { |
8 v8::Handle<v8::Context> CreateContext( | 8 v8::Handle<v8::Context> CreateContext( |
9 AdblockPlus::ErrorCallback& errorCallback, | 9 AdblockPlus::ErrorCallback& errorCallback, |
10 AdblockPlus::WebRequest& webRequest) | 10 AdblockPlus::WebRequest& webRequest) |
11 { | 11 { |
12 const v8::Locker locker(v8::Isolate::GetCurrent()); | 12 const v8::Locker locker(v8::Isolate::GetCurrent()); |
13 const v8::HandleScope handleScope; | 13 const v8::HandleScope handleScope; |
14 const v8::Handle<v8::ObjectTemplate> global = | 14 const v8::Handle<v8::ObjectTemplate> global = |
15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); | 15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); |
16 return v8::Context::New(0, global); | 16 return v8::Context::New(0, global); |
17 } | 17 } |
18 | 18 |
19 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) | 19 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) |
20 { | 20 { |
21 const v8::Handle<v8::String> v8Source = v8::String::New(source); | 21 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |
22 if (filename && filename[0]) | 22 if (filename.length()) |
23 { | 23 { |
24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename); | 24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); |
25 return v8::Script::Compile(v8Source, v8Filename); | 25 return v8::Script::Compile(v8Source, v8Filename); |
26 } | 26 } |
27 else | 27 else |
28 return v8::Script::Compile(v8Source); | 28 return v8::Script::Compile(v8Source); |
29 } | 29 } |
30 | 30 |
31 void CheckTryCatch(const v8::TryCatch& tryCatch) | 31 void CheckTryCatch(const v8::TryCatch& tryCatch) |
32 { | 32 { |
33 if (tryCatch.HasCaught()) | 33 if (tryCatch.HasCaught()) |
34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |
(...skipping 28 matching lines...) Expand all Loading... |
63 { | 63 { |
64 } | 64 } |
65 | 65 |
66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, | 66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, |
67 WebRequest* const webRequest, | 67 WebRequest* const webRequest, |
68 ErrorCallback* const errorCallback) | 68 ErrorCallback* const errorCallback) |
69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) | 69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) |
70 { | 70 { |
71 } | 71 } |
72 | 72 |
73 std::string AdblockPlus::JsEngine::Evaluate(const char* source, const char* file
name) | 73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, |
| 74 const std::string& filename) |
74 { | 75 { |
75 const v8::Locker locker(v8::Isolate::GetCurrent()); | 76 const v8::Locker locker(v8::Isolate::GetCurrent()); |
76 const v8::HandleScope handleScope; | 77 const v8::HandleScope handleScope; |
77 const v8::Context::Scope contextScope(context); | 78 const v8::Context::Scope contextScope(context); |
78 const v8::TryCatch tryCatch; | 79 const v8::TryCatch tryCatch; |
79 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 80 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
80 CheckTryCatch(tryCatch); | 81 CheckTryCatch(tryCatch); |
81 v8::Local<v8::Value> result = script->Run(); | 82 v8::Local<v8::Value> result = script->Run(); |
82 CheckTryCatch(tryCatch); | 83 CheckTryCatch(tryCatch); |
83 v8::String::Utf8Value resultString(result); | 84 v8::String::Utf8Value resultString(result); |
84 return std::string(*resultString); | 85 return std::string(*resultString); |
85 } | 86 } |
86 | 87 |
87 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, | |
88 const std::string& filename) | |
89 { | |
90 return Evaluate(source.c_str(), filename.c_str()); | |
91 } | |
92 | |
93 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 88 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
94 { | 89 { |
95 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); | 90 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); |
96 if (!*file) | 91 if (!*file) |
97 throw std::runtime_error("Unable to load script " + scriptPath); | 92 throw std::runtime_error("Unable to load script " + scriptPath); |
98 Evaluate(Slurp(*file)); | 93 Evaluate(Slurp(*file)); |
99 } | 94 } |
100 | 95 |
101 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) | |
102 { | |
103 const v8::Locker locker(v8::Isolate::GetCurrent()); | |
104 const v8::HandleScope handleScope; | |
105 const v8::Context::Scope contextScope(context); | |
106 const v8::Local<v8::Object> global = context->Global(); | |
107 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
108 global->Get(v8::String::New(functionName.c_str()))); | |
109 const v8::TryCatch tryCatch; | |
110 const v8::Local<v8::Value> result = function->Call(function, 0, 0); | |
111 CheckTryCatch(tryCatch); | |
112 const v8::String::AsciiValue ascii(result); | |
113 return *ascii; | |
114 } | |
115 | |
116 std::string AdblockPlus::JsEngine::GetVariable(const std::string& name) | |
117 { | |
118 const v8::Locker locker(v8::Isolate::GetCurrent()); | |
119 const v8::HandleScope handleScope; | |
120 const v8::Context::Scope contextScope(context); | |
121 const v8::Local<v8::Object> global = context->Global(); | |
122 const v8::Local<v8::Value> value = global->Get(v8::String::New(name.c_str())); | |
123 if (value->IsUndefined()) | |
124 return ""; | |
125 const v8::String::AsciiValue ascii(value); | |
126 return *ascii; | |
127 } | |
128 | |
129 void AdblockPlus::JsEngine::Gc() | 96 void AdblockPlus::JsEngine::Gc() |
130 { | 97 { |
131 while (!v8::V8::IdleNotification()); | 98 while (!v8::V8::IdleNotification()); |
132 } | 99 } |
OLD | NEW |