LEFT | RIGHT |
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 #include "Utils.h" |
5 | 6 |
6 namespace | 7 namespace |
7 { | 8 { |
8 v8::Handle<v8::Context> CreateContext( | 9 v8::Handle<v8::Context> CreateContext( |
9 AdblockPlus::ErrorCallback& errorCallback, | 10 v8::Isolate* isolate, |
10 AdblockPlus::WebRequest& webRequest) | 11 AdblockPlus::FileSystem& fileSystem, |
| 12 AdblockPlus::WebRequest& webRequest, |
| 13 AdblockPlus::ErrorCallback& errorCallback) |
11 { | 14 { |
12 const v8::Locker locker(v8::Isolate::GetCurrent()); | 15 const v8::Locker locker(isolate); |
13 const v8::HandleScope handleScope; | 16 const v8::HandleScope handleScope; |
14 const v8::Handle<v8::ObjectTemplate> global = | 17 const v8::Handle<v8::ObjectTemplate> global = |
15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); | 18 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest, |
| 19 errorCallback); |
16 return v8::Context::New(0, global); | 20 return v8::Context::New(0, global); |
17 } | 21 } |
18 | 22 |
19 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) | 23 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) |
20 { | 24 { |
21 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); | 25 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |
22 if (filename.length()) | 26 if (filename.length()) |
23 { | 27 { |
24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); | 28 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); |
25 return v8::Script::Compile(v8Source, v8Filename); | 29 return v8::Script::Compile(v8Source, v8Filename); |
26 } | 30 } |
27 else | 31 else |
28 return v8::Script::Compile(v8Source); | 32 return v8::Script::Compile(v8Source); |
29 } | 33 } |
30 | 34 |
31 void CheckTryCatch(const v8::TryCatch& tryCatch) | 35 void CheckTryCatch(const v8::TryCatch& tryCatch) |
32 { | 36 { |
33 if (tryCatch.HasCaught()) | 37 if (tryCatch.HasCaught()) |
34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 38 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |
35 } | |
36 | |
37 std::string Slurp(std::istream& stream) | |
38 { | |
39 std::stringstream content; | |
40 content << stream.rdbuf(); | |
41 return content.str(); | |
42 } | 39 } |
43 | 40 |
44 std::string ExceptionToString(const v8::Handle<v8::Value> exception, | 41 std::string ExceptionToString(const v8::Handle<v8::Value> exception, |
45 const v8::Handle<v8::Message> message) | 42 const v8::Handle<v8::Message> message) |
46 { | 43 { |
47 std::stringstream error; | 44 std::stringstream error; |
48 error << *v8::String::Utf8Value(exception); | 45 error << *v8::String::Utf8Value(exception); |
49 if (!message.IsEmpty()) | 46 if (!message.IsEmpty()) |
50 { | 47 { |
51 error << " at "; | 48 error << " at "; |
52 error << *v8::String::Utf8Value(message->GetScriptResourceName()); | 49 error << *v8::String::Utf8Value(message->GetScriptResourceName()); |
53 error << ":"; | 50 error << ":"; |
54 error << message->GetLineNumber(); | 51 error << message->GetLineNumber(); |
55 } | 52 } |
56 return error.str(); | 53 return error.str(); |
57 } | 54 } |
58 } | 55 } |
59 | 56 |
60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, | 57 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
61 const v8::Handle<v8::Message> message) | 58 const v8::Handle<v8::Message> message) |
62 : std::runtime_error(ExceptionToString(exception, message)) | 59 : std::runtime_error(ExceptionToString(exception, message)) |
63 { | 60 { |
64 } | 61 } |
65 | 62 |
66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, | 63 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, |
67 WebRequest* const webRequest, | 64 WebRequest* const webRequest, |
68 ErrorCallback* const errorCallback) | 65 ErrorCallback* const errorCallback) |
69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) | 66 : fileSystem(fileSystem), isolate(v8::Isolate::GetCurrent()), |
| 67 context(CreateContext(isolate, *fileSystem, *webRequest, *errorCallback)) |
70 { | 68 { |
71 } | 69 } |
72 | 70 |
73 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
74 const std::string& filename) | 72 const std::string& filename) |
75 { | 73 { |
76 const v8::Locker locker(v8::Isolate::GetCurrent()); | 74 const Context context(*this); |
77 const v8::HandleScope handleScope; | |
78 const v8::Context::Scope contextScope(context); | |
79 const v8::TryCatch tryCatch; | 75 const v8::TryCatch tryCatch; |
80 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 76 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
81 CheckTryCatch(tryCatch); | 77 CheckTryCatch(tryCatch); |
82 v8::Local<v8::Value> result = script->Run(); | 78 v8::Local<v8::Value> result = script->Run(); |
83 CheckTryCatch(tryCatch); | 79 CheckTryCatch(tryCatch); |
84 return JsValuePtr(new JsValue(v8::Isolate::GetCurrent(), context, result)); | 80 return JsValuePtr(new JsValue(*this, result)); |
85 } | 81 } |
86 | 82 |
87 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 83 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
88 { | 84 { |
89 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); | 85 const std::tr1::shared_ptr<std::istream> file = fileSystem->Read(scriptPath); |
90 if (!*file) | 86 if (!file || !*file) |
91 throw std::runtime_error("Unable to load script " + scriptPath); | 87 throw std::runtime_error("Unable to load script " + scriptPath); |
92 Evaluate(Slurp(*file)); | 88 Evaluate(Utils::Slurp(*file)); |
93 } | 89 } |
94 | 90 |
95 void AdblockPlus::JsEngine::Gc() | 91 void AdblockPlus::JsEngine::Gc() |
96 { | 92 { |
97 while (!v8::V8::IdleNotification()); | 93 while (!v8::V8::IdleNotification()); |
98 } | 94 } |
| 95 |
| 96 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
| 97 { |
| 98 const Context context(*this); |
| 99 return JsValuePtr(new JsValue(*this, v8::String::New(val.c_str(), val.length()
))); |
| 100 } |
| 101 |
| 102 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
| 103 { |
| 104 const Context context(*this); |
| 105 return JsValuePtr(new JsValue(*this, v8::Integer::New(val))); |
| 106 } |
| 107 |
| 108 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
| 109 { |
| 110 const Context context(*this); |
| 111 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val))); |
| 112 } |
| 113 |
| 114 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) |
| 115 : locker(jsEngine.isolate), handleScope(), |
| 116 contextScope(jsEngine.context) |
| 117 { |
| 118 } |
LEFT | RIGHT |