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 v8::Isolate* isolate, |
11 AdblockPlus::FileSystem& fileSystem, | 11 AdblockPlus::JsEngine& jsEngine) |
12 AdblockPlus::WebRequest& webRequest, | |
13 AdblockPlus::ErrorCallback& errorCallback) | |
14 { | 12 { |
15 const v8::Locker locker(isolate); | 13 const v8::Locker locker(isolate); |
16 const v8::HandleScope handleScope; | 14 const v8::HandleScope handleScope; |
17 const v8::Handle<v8::ObjectTemplate> global = | 15 const v8::Handle<v8::ObjectTemplate> global = |
18 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest, | 16 AdblockPlus::GlobalJsObject::Create(jsEngine); |
19 errorCallback); | |
20 return v8::Context::New(0, global); | 17 return v8::Context::New(0, global); |
21 } | 18 } |
22 | 19 |
23 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) | 20 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
ing& filename) |
24 { | 21 { |
25 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); | 22 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |
26 if (filename.length()) | 23 if (filename.length()) |
27 { | 24 { |
28 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); | 25 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
); |
29 return v8::Script::Compile(v8Source, v8Filename); | 26 return v8::Script::Compile(v8Source, v8Filename); |
(...skipping 26 matching lines...) Expand all Loading... |
56 | 53 |
57 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, | 54 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, |
58 const v8::Handle<v8::Message> message) | 55 const v8::Handle<v8::Message> message) |
59 : std::runtime_error(ExceptionToString(exception, message)) | 56 : std::runtime_error(ExceptionToString(exception, message)) |
60 { | 57 { |
61 } | 58 } |
62 | 59 |
63 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, | 60 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, |
64 WebRequest* const webRequest, | 61 WebRequest* const webRequest, |
65 ErrorCallback* const errorCallback) | 62 ErrorCallback* const errorCallback) |
66 : fileSystem(fileSystem), isolate(v8::Isolate::GetCurrent()), | 63 : fileSystem(*fileSystem), webRequest(*webRequest), |
67 context(CreateContext(isolate, *fileSystem, *webRequest, *errorCallback)) | 64 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()), |
| 65 context(CreateContext(isolate, *this)) |
68 { | 66 { |
69 } | 67 } |
70 | 68 |
71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 69 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
72 const std::string& filename) | 70 const std::string& filename) |
73 { | 71 { |
74 const Context context(*this); | 72 const Context context(*this); |
75 const v8::TryCatch tryCatch; | 73 const v8::TryCatch tryCatch; |
76 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 74 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
77 CheckTryCatch(tryCatch); | 75 CheckTryCatch(tryCatch); |
78 v8::Local<v8::Value> result = script->Run(); | 76 v8::Local<v8::Value> result = script->Run(); |
79 CheckTryCatch(tryCatch); | 77 CheckTryCatch(tryCatch); |
80 return JsValuePtr(new JsValue(*this, result)); | 78 return JsValuePtr(new JsValue(*this, result)); |
81 } | 79 } |
82 | 80 |
83 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) | 81 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) |
84 { | 82 { |
85 const std::tr1::shared_ptr<std::istream> file = fileSystem->Read(scriptPath); | 83 const std::tr1::shared_ptr<std::istream> file = fileSystem.Read(scriptPath); |
86 if (!file || !*file) | 84 if (!file || !*file) |
87 throw std::runtime_error("Unable to load script " + scriptPath); | 85 throw std::runtime_error("Unable to load script " + scriptPath); |
88 Evaluate(Utils::Slurp(*file)); | 86 Evaluate(Utils::Slurp(*file)); |
89 } | 87 } |
90 | 88 |
91 void AdblockPlus::JsEngine::Gc() | 89 void AdblockPlus::JsEngine::Gc() |
92 { | 90 { |
93 while (!v8::V8::IdleNotification()); | 91 while (!v8::V8::IdleNotification()); |
94 } | 92 } |
95 | 93 |
96 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 94 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
97 { | 95 { |
98 const Context context(*this); | 96 const Context context(*this); |
99 return JsValuePtr(new JsValue(*this, v8::String::New(val.c_str(), val.length()
))); | 97 return JsValuePtr(new JsValue(*this, v8::String::New(val.c_str(), val.length()
))); |
100 } | 98 } |
101 | 99 |
102 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 100 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
103 { | 101 { |
104 const Context context(*this); | 102 const Context context(*this); |
105 return JsValuePtr(new JsValue(*this, v8::Integer::New(val))); | 103 return JsValuePtr(new JsValue(*this, v8::Integer::New(val))); |
106 } | 104 } |
107 | 105 |
108 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
109 { | 107 { |
110 const Context context(*this); | 108 const Context context(*this); |
111 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val))); | 109 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val))); |
112 } | 110 } |
113 | 111 |
| 112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
| 113 { |
| 114 const Context context(*this); |
| 115 return JsValuePtr(new JsValue(*this, v8::Object::New())); |
| 116 } |
| 117 |
| 118 AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments&
arguments) |
| 119 { |
| 120 const v8::Local<const v8::External> external = |
| 121 v8::Local<const v8::External>::Cast(arguments.Data()); |
| 122 return *static_cast<JsEngine* const>(external->Value()); |
| 123 } |
| 124 |
| 125 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) |
| 126 { |
| 127 const Context context(*this); |
| 128 JsValueList list; |
| 129 for (int i = 0; i < arguments.Length(); i++) |
| 130 list.push_back(JsValuePtr(new JsValue(*this, arguments[i]))); |
| 131 return list; |
| 132 } |
| 133 |
114 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) | 134 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) |
115 : locker(jsEngine.isolate), handleScope(), | 135 : locker(jsEngine.isolate), handleScope(), |
116 contextScope(jsEngine.context) | 136 contextScope(jsEngine.context) |
117 { | 137 { |
118 } | 138 } |
OLD | NEW |