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