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 #include "Utils.h" | 5 #include "Utils.h" |
6 | 6 |
7 namespace | 7 namespace |
8 { | 8 { |
9 v8::Handle<v8::Context> CreateContext( | 9 v8::Handle<v8::Context> CreateContext( |
| 10 v8::Isolate* isolate, |
10 AdblockPlus::FileSystem& fileSystem, | 11 AdblockPlus::FileSystem& fileSystem, |
11 AdblockPlus::WebRequest& webRequest, | 12 AdblockPlus::WebRequest& webRequest, |
12 AdblockPlus::ErrorCallback& errorCallback) | 13 AdblockPlus::ErrorCallback& errorCallback) |
13 { | 14 { |
14 const v8::Locker locker(v8::Isolate::GetCurrent()); | 15 const v8::Locker locker(isolate); |
15 const v8::HandleScope handleScope; | 16 const v8::HandleScope handleScope; |
16 const v8::Handle<v8::ObjectTemplate> global = | 17 const v8::Handle<v8::ObjectTemplate> global = |
17 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest, | 18 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest, |
18 errorCallback); | 19 errorCallback); |
19 return v8::Context::New(0, global); | 20 return v8::Context::New(0, global); |
20 } | 21 } |
21 | 22 |
22 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) |
23 { | 24 { |
24 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()); |
(...skipping 30 matching lines...) Expand all Loading... |
55 | 56 |
56 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, | 57 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
57 const v8::Handle<v8::Message> message) | 58 const v8::Handle<v8::Message> message) |
58 : std::runtime_error(ExceptionToString(exception, message)) | 59 : std::runtime_error(ExceptionToString(exception, message)) |
59 { | 60 { |
60 } | 61 } |
61 | 62 |
62 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, | 63 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, |
63 WebRequest* const webRequest, | 64 WebRequest* const webRequest, |
64 ErrorCallback* const errorCallback) | 65 ErrorCallback* const errorCallback) |
65 : fileSystem(fileSystem), | 66 : fileSystem(fileSystem), isolate(v8::Isolate::GetCurrent()), |
66 context(CreateContext(*fileSystem, *webRequest, *errorCallback)) | 67 context(CreateContext(isolate, *fileSystem, *webRequest, *errorCallback)) |
67 { | 68 { |
68 } | 69 } |
69 | 70 |
70 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, | 71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
71 const std::string& filename) | 72 const std::string& filename) |
72 { | 73 { |
73 const v8::Locker locker(v8::Isolate::GetCurrent()); | 74 const Context context(*this); |
74 const v8::HandleScope handleScope; | |
75 const v8::Context::Scope contextScope(context); | |
76 const v8::TryCatch tryCatch; | 75 const v8::TryCatch tryCatch; |
77 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 76 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
78 CheckTryCatch(tryCatch); | 77 CheckTryCatch(tryCatch); |
79 v8::Local<v8::Value> result = script->Run(); | 78 v8::Local<v8::Value> result = script->Run(); |
80 CheckTryCatch(tryCatch); | 79 CheckTryCatch(tryCatch); |
81 v8::String::Utf8Value resultString(result); | 80 return JsValuePtr(new JsValue(*this, result)); |
82 return std::string(*resultString); | |
83 } | 81 } |
84 | 82 |
85 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 83 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
86 { | 84 { |
87 const std::tr1::shared_ptr<std::istream> file = fileSystem->Read(scriptPath); | 85 const std::tr1::shared_ptr<std::istream> file = fileSystem->Read(scriptPath); |
88 if (!file || !*file) | 86 if (!file || !*file) |
89 throw std::runtime_error("Unable to load script " + scriptPath); | 87 throw std::runtime_error("Unable to load script " + scriptPath); |
90 Evaluate(Utils::Slurp(*file)); | 88 Evaluate(Utils::Slurp(*file)); |
91 } | 89 } |
92 | 90 |
93 void AdblockPlus::JsEngine::Gc() | 91 void AdblockPlus::JsEngine::Gc() |
94 { | 92 { |
95 while (!v8::V8::IdleNotification()); | 93 while (!v8::V8::IdleNotification()); |
96 } | 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 } |
OLD | NEW |