Rev 1740 | Rev 2107 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1740 | Rev 1741 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | #include <fstream> |
23 | #include <fstream> |
| 24 | 24 | ||
| 25 | #include <boost/lexical_cast.hpp> |
25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/bind.hpp> |
26 | #include <boost/bind.hpp> |
| 27 | 27 | ||
| 28 | namespace atom { |
28 | namespace atom { |
| 29 | namespace vm { |
29 | namespace vm { |
| 30 | 30 | ||
| 31 | Manager::Pointer Manager::instance_; |
31 | Manager::Pointer Manager::instance_; |
| 32 | 32 | ||
| 33 | Manager::Manager() : LOG("vm::Manager") |
33 | Manager::Manager() : LOG("vm::Manager") |
| 34 | { |
34 | { |
| 35 | } |
35 | } |
| 36 | 36 | ||
| 37 | Manager::~Manager() |
37 | Manager::~Manager() |
| 38 | { |
38 | { |
| 39 | this->io_service_.stop(); |
39 | this->io_service_.stop(); |
| 40 | 40 | ||
| 41 | this->plugins_.clear(); |
41 | this->plugins_.clear(); |
| 42 | this->context_.Dispose(); |
42 | this->context_.Dispose(); |
| 43 | 43 | ||
| 44 | for (ImportFunctionList::iterator it = this->functions_.begin(); it != this->functions_.end(); it++) |
44 | for (ImportFunctionList::iterator it = this->functions_.begin(); it != this->functions_.end(); it++) |
| 45 | { |
45 | { |
| 46 | it->second.Dispose(); |
46 | it->second.Dispose(); |
| 47 | } |
47 | } |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | void Manager::Create() |
50 | void Manager::Create() |
| 51 | { |
51 | { |
| 52 | Manager::instance_ = Manager::Pointer(new Manager()); |
52 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 53 | } |
53 | } |
| 54 | 54 | ||
| 55 | Manager::Pointer Manager::Instance() |
55 | Manager::Pointer Manager::Instance() |
| 56 | { |
56 | { |
| 57 | return Manager::instance_; |
57 | return Manager::instance_; |
| 58 | } |
58 | } |
| 59 | 59 | ||
| 60 | void Manager::Delete() |
60 | void Manager::Delete() |
| 61 | { |
61 | { |
| 62 | Manager::instance_.reset(); |
62 | Manager::instance_.reset(); |
| 63 | } |
63 | } |
| 64 | 64 | ||
| 65 | v8::Persistent<v8::Context>& Manager::GetContext() |
65 | v8::Persistent<v8::Context>& Manager::GetContext() |
| 66 | { |
66 | { |
| 67 | return this->context_; |
67 | return this->context_; |
| 68 | } |
68 | } |
| 69 | 69 | ||
| 70 | void Manager::AddPlugin(Plugin::Pointer plugin) |
70 | void Manager::AddPlugin(Plugin::Pointer plugin) |
| 71 | { |
71 | { |
| 72 | this->plugins_.push_back(plugin); |
72 | this->plugins_.push_back(plugin); |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | void Manager::Start(std::string script_path, std::string user_script_path) |
75 | void Manager::Start(std::string script_path, std::string user_script_path) |
| 76 | { |
76 | { |
| 77 | this->script_path_ = script_path; |
77 | this->script_path_ = script_path; |
| 78 | this->user_script_path_ = user_script_path; |
78 | this->user_script_path_ = user_script_path; |
| 79 | this->io_service_.post(boost::bind(&Manager::StartHandler, this)); |
79 | this->io_service_.post(boost::bind(&Manager::StartHandler, this)); |
| 80 | } |
80 | } |
| 81 | 81 | ||
| 82 | void Manager::CallOutput(std::string output) |
82 | void Manager::CallOutput(std::string output) |
| 83 | { |
83 | { |
| 84 | if (this->current_plugin_name_ != "") |
84 | if (this->current_plugin_name_ != "") |
| 85 | { |
85 | { |
| 86 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
86 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
| 87 | { |
87 | { |
| 88 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
88 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
| 89 | { |
89 | { |
| 90 | this->plugins_[n]->CallOutput(this->current_request_id_, output); |
90 | this->plugins_[n]->CallOutput(this->current_request_id_, output); |
| 91 | return; |
91 | return; |
| 92 | } |
92 | } |
| 93 | } |
93 | } |
| 94 | } |
94 | } |
| 95 | else |
95 | else |
| 96 | { |
96 | { |
| 97 | LOG.Info(output); |
97 | LOG.Info(output); |
| 98 | } |
98 | } |
| 99 | } |
99 | } |
| 100 | 100 | ||
| 101 | Value Manager::Export_Log(const v8::Arguments& args) |
101 | Value Manager::Export_Log(const v8::Arguments& args) |
| 102 | { |
102 | { |
| 103 | v8::Locker lock; |
103 | v8::Locker lock; |
| 104 | v8::Context::Scope context_scope(Manager::Instance()->GetContext()); |
104 | v8::Context::Scope context_scope(Manager::Instance()->GetContext()); |
| - | 105 | v8::HandleScope handle_scope; |
|
| 105 | 106 | ||
| 106 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
107 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 107 | 108 | ||
| 108 | if (args.Length() < 1) |
109 | if (args.Length() < 1) |
| 109 | { |
110 | { |
| 110 | Manager::instance_->LOG.Error(std::string(__FUNCTION__) + ": To few arguments to log."); |
111 | Manager::instance_->LOG.Error(std::string(__FUNCTION__) + ": To few arguments to log."); |
| 111 | return v8::Undefined(); |
112 | return v8::Undefined(); |
| 112 | } |
113 | } |
| 113 | 114 | ||
| 114 | v8::String::AsciiValue str(args[0]); |
115 | v8::String::AsciiValue str(args[0]); |
| 115 | 116 | ||
| 116 | Manager::instance_->CallOutput(*str); |
117 | Manager::instance_->CallOutput(*str); |
| 117 | 118 | ||
| 118 | return v8: |
119 | return handle_scope.Close(v8::Undefined()); |
| 119 | } |
120 | } |
| 120 | 121 | ||
| 121 | void Manager::Call(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
122 | void Manager::Call(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 122 | { |
123 | { |
| 123 | this->io_service_.post(boost::bind(&Manager::CallHandler, this, plugin_name, request_id, name, arguments)); |
124 | this->io_service_.post(boost::bind(&Manager::CallHandler, this, plugin_name, request_id, name, arguments)); |
| 124 | } |
125 | } |
| 125 | 126 | ||
| 126 | void Manager::Execute(std::string plugin_name, unsigned int request_id, std::string code) |
127 | void Manager::Execute(std::string plugin_name, unsigned int request_id, std::string code) |
| 127 | { |
128 | { |
| 128 | this->io_service_.post(boost::bind(&Manager::ExecuteHandler, this, plugin_name, request_id, code)); |
129 | this->io_service_.post(boost::bind(&Manager::ExecuteHandler, this, plugin_name, request_id, code)); |
| 129 | } |
130 | } |
| 130 | 131 | ||
| 131 | bool Manager::CallHandler(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
132 | bool Manager::CallHandler(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 132 | { |
133 | { |
| 133 | v8::Locker lock; |
134 | v8::Locker lock; |
| 134 | v8::Context::Scope context_scope(this->context_); |
135 | v8::Context::Scope context_scope(this->context_); |
| 135 | v8::TryCatch try_catch; |
136 | v8::TryCatch try_catch; |
| 136 | 137 | ||
| 137 | this->current_plugin_name_ = plugin_name; |
138 | this->current_plugin_name_ = plugin_name; |
| 138 | this->current_request_id_ = request_id; |
139 | this->current_request_id_ = request_id; |
| 139 | 140 | ||
| 140 | if (this->functions_.find(name) == this->functions_.end()) |
141 | if (this->functions_.find(name) == this->functions_.end()) |
| 141 | { |
142 | { |
| 142 | this->CallOutput("No such function found, " + name); |
143 | this->CallOutput("No such function found, " + name); |
| 143 | 144 | ||
| 144 | this->current_plugin_name_ = ""; |
145 | this->current_plugin_name_ = ""; |
| 145 | this->current_request_id_ = 0; |
146 | this->current_request_id_ = 0; |
| 146 | return false; |
147 | return false; |
| 147 | } |
148 | } |
| 148 | 149 | ||
| 149 | //LOG.Debug("Call: " + name); |
150 | //LOG.Debug("Call: " + name); |
| 150 | 151 | ||
| 151 | Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data()); |
152 | Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data()); |
| 152 | 153 | ||
| 153 | if (result.IsEmpty()) |
154 | if (result.IsEmpty()) |
| 154 | { |
155 | { |
| 155 | this->CallOutput(this->FormatException(try_catch)); |
156 | this->CallOutput(this->FormatException(try_catch)); |
| 156 | 157 | ||
| 157 | this->current_plugin_name_ = ""; |
158 | this->current_plugin_name_ = ""; |
| 158 | this->current_request_id_ = 0; |
159 | this->current_request_id_ = 0; |
| 159 | return false; |
160 | return false; |
| 160 | } |
161 | } |
| 161 | 162 | ||
| 162 | //v8::String::AsciiValue response(result->ToString()); |
163 | //v8::String::AsciiValue response(result->ToString()); |
| 163 | //this->CallOutput(plugin_name, request_id, std::string(*response)); |
164 | //this->CallOutput(plugin_name, request_id, std::string(*response)); |
| 164 | 165 | ||
| 165 | this->current_plugin_name_ = ""; |
166 | this->current_plugin_name_ = ""; |
| 166 | this->current_request_id_ = 0; |
167 | this->current_request_id_ = 0; |
| 167 | return true; |
168 | return true; |
| 168 | } |
169 | } |
| 169 | 170 | ||
| 170 | void Manager::StartHandler() |
171 | void Manager::StartHandler() |
| 171 | { |
172 | { |
| - | 173 | v8::Locker lock; |
|
| - | 174 | v8::HandleScope handle_scope; |
|
| 172 | v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New(); |
175 | v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New(); |
| 173 | 176 | ||
| 174 | raw_template->Set(v8::String::New("Log"), v8::FunctionTemplate::New(Manager::Export_Log)); |
177 | raw_template->Set(v8::String::New("Log"), v8::FunctionTemplate::New(Manager::Export_Log)); |
| 175 | 178 | ||
| 176 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
179 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 177 | { |
180 | { |
| 178 | ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions(); |
181 | ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions(); |
| 179 | 182 | ||
| 180 | for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++) |
183 | for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++) |
| 181 | { |
184 | { |
| 182 | raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second)); |
185 | raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second)); |
| 183 | } |
186 | } |
| 184 | } |
187 | } |
| 185 | 188 | ||
| 186 | this->context_ = v8::Context::New(NULL, raw_template); |
189 | this->context_ = v8::Context::New(NULL, raw_template); |
| 187 | 190 | ||
| 188 |
|
191 | |
| 189 | v8::Context::Scope context_scope(this->context_); |
192 | v8::Context::Scope context_scope(this->context_); |
| 190 | 193 | ||
| 191 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
194 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 192 | { |
195 | { |
| 193 | this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js"); |
196 | this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js"); |
| 194 | } |
197 | } |
| 195 | 198 | ||
| 196 | this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template); |
199 | this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template); |
| 197 | 200 | ||
| 198 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
201 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 199 | { |
202 | { |
| 200 | this->plugins_[c]->InitializeDone(); |
203 | this->plugins_[c]->InitializeDone(); |
| 201 | } |
204 | } |
| 202 | } |
205 | } |
| 203 | 206 | ||
| 204 | bool Manager::ImportFunction(std::string functionname) |
207 | bool Manager::ImportFunction(std::string functionname) |
| 205 | { |
208 | { |
| 206 | v8::Locker lock; |
209 | v8::Locker lock; |
| 207 | v8::Context::Scope context_scope(this->context_); |
210 | v8::Context::Scope context_scope(this->context_); |
| 208 | 211 | ||
| 209 | v8::TryCatch try_catch; |
212 | v8::TryCatch try_catch; |
| 210 | 213 | ||
| 211 | v8::Handle<v8::String> process_name = v8::String::New(functionname.data()); |
214 | v8::Handle<v8::String> process_name = v8::String::New(functionname.data()); |
| 212 | v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name); |
215 | v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name); |
| 213 | 216 | ||
| 214 | if (!process_val->IsFunction()) |
217 | if (!process_val->IsFunction()) |
| 215 | { |
218 | { |
| 216 | LOG.Error(functionname + " was not found to be a function!"); |
219 | LOG.Error(functionname + " was not found to be a function!"); |
| 217 | return false; |
220 | return false; |
| 218 | } |
221 | } |
| 219 | 222 | ||
| 220 | v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val); |
223 | v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val); |
| 221 | 224 | ||
| 222 | this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun); |
225 | this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun); |
| 223 | 226 | ||
| 224 | return true; |
227 | return true; |
| 225 | } |
228 | } |
| 226 | 229 | ||
| 227 | bool Manager::LoadScript(std::string scriptname) |
230 | bool Manager::LoadScript(std::string scriptname) |
| 228 | { |
231 | { |
| 229 | for (unsigned int n = 0; n < this->loaded_scripts_.size(); n++) |
232 | for (unsigned int n = 0; n < this->loaded_scripts_.size(); n++) |
| 230 | { |
233 | { |
| 231 | if (this->loaded_scripts_[n] == scriptname) |
234 | if (this->loaded_scripts_[n] == scriptname) |
| 232 | { |
235 | { |
| 233 | LOG.Debug(scriptname + " is already loaded."); |
236 | LOG.Debug(scriptname + " is already loaded."); |
| 234 | return true; |
237 | return true; |
| 235 | } |
238 | } |
| 236 | } |
239 | } |
| 237 | 240 | ||
| 238 | std::string path = this->user_script_path_ + scriptname; |
241 | std::string path = this->user_script_path_ + scriptname; |
| 239 | std::ifstream file(path.data()); |
242 | std::ifstream file(path.data()); |
| 240 | 243 | ||
| 241 | if (!file.is_open()) |
244 | if (!file.is_open()) |
| 242 | { |
245 | { |
| 243 | path = this->script_path_ + scriptname; |
246 | path = this->script_path_ + scriptname; |
| 244 | file.open(path.data()); |
247 | file.open(path.data()); |
| 245 | 248 | ||
| 246 | if (!file.is_open()) |
249 | if (!file.is_open()) |
| 247 | { |
250 | { |
| 248 | file.close(); |
251 | file.close(); |
| 249 | LOG.Warning("Could not load " + scriptname + "."); |
252 | LOG.Warning("Could not load " + scriptname + "."); |
| 250 | return false; |
253 | return false; |
| 251 | } |
254 | } |
| 252 | } |
255 | } |
| 253 | 256 | ||
| 254 | std::string code = ""; |
257 | std::string code = ""; |
| 255 | std::string line; |
258 | std::string line; |
| 256 | 259 | ||
| 257 | while (!file.eof()) |
260 | while (!file.eof()) |
| 258 | { |
261 | { |
| 259 | std::getline(file, line); |
262 | std::getline(file, line); |
| 260 | 263 | ||
| 261 | if (file.eof()) |
264 | if (file.eof()) |
| 262 | { |
265 | { |
| 263 | break; |
266 | break; |
| 264 | } |
267 | } |
| 265 | 268 | ||
| 266 | code += line + "\n"; |
269 | code += line + "\n"; |
| 267 | } |
270 | } |
| 268 | 271 | ||
| 269 | code += "\n"; |
272 | code += "\n"; |
| 270 | 273 | ||
| 271 | file.close(); |
274 | file.close(); |
| 272 | 275 | ||
| 273 | v8::Locker lock; |
276 | v8::Locker lock; |
| 274 | v8::Context::Scope context_scope(this->context_); |
277 | v8::Context::Scope context_scope(this->context_); |
| 275 | 278 | ||
| 276 | v8::TryCatch try_catch; |
279 | v8::TryCatch try_catch; |
| 277 | 280 | ||
| 278 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
281 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
| 279 | v8::String::New(scriptname.data(), scriptname.length())); |
282 | v8::String::New(scriptname.data(), scriptname.length())); |
| 280 | 283 | ||
| 281 | if (script.IsEmpty()) |
284 | if (script.IsEmpty()) |
| 282 | { |
285 | { |
| 283 | LOG.Error(this->FormatException(try_catch)); |
286 | LOG.Error(this->FormatException(try_catch)); |
| 284 | return false; |
287 | return false; |
| 285 | } |
288 | } |
| 286 | else |
289 | else |
| 287 | { |
290 | { |
| 288 | Value result = script->Run(); |
291 | Value result = script->Run(); |
| 289 | 292 | ||
| 290 | if (result.IsEmpty()) |
293 | if (result.IsEmpty()) |
| 291 | { |
294 | { |
| 292 | LOG.Error(this->FormatException(try_catch)); |
295 | LOG.Error(this->FormatException(try_catch)); |
| 293 | return false; |
296 | return false; |
| 294 | } |
297 | } |
| 295 | } |
298 | } |
| 296 | 299 | ||
| 297 | this->loaded_scripts_.push_back(scriptname); |
300 | this->loaded_scripts_.push_back(scriptname); |
| 298 | 301 | ||
| 299 | LOG.Info("\033[32mLoaded " + scriptname + " successfully.\033[0m"); |
302 | LOG.Info("\033[32mLoaded " + scriptname + " successfully.\033[0m"); |
| 300 | return true; |
303 | return true; |
| 301 | } |
304 | } |
| 302 | 305 | ||
| 303 | void Manager::ExecuteHandler(std::string plugin_name, unsigned int request_id, std::string code) |
306 | void Manager::ExecuteHandler(std::string plugin_name, unsigned int request_id, std::string code) |
| 304 | { |
307 | { |
| 305 | std::string scriptname = "execute_" + plugin_name + ".js"; |
308 | std::string scriptname = "execute_" + plugin_name + ".js"; |
| 306 | 309 | ||
| 307 | v8::Locker lock; |
310 | v8::Locker lock; |
| 308 | v8::Context::Scope context_scope(this->context_); |
311 | v8::Context::Scope context_scope(this->context_); |
| 309 | 312 | ||
| 310 | v8::TryCatch try_catch; |
313 | v8::TryCatch try_catch; |
| 311 | 314 | ||
| 312 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
315 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
| 313 | v8::String::New(scriptname.data(), scriptname.length())); |
316 | v8::String::New(scriptname.data(), scriptname.length())); |
| 314 | 317 | ||
| 315 | this->current_plugin_name_ = plugin_name; |
318 | this->current_plugin_name_ = plugin_name; |
| 316 | this->current_request_id_ = request_id; |
319 | this->current_request_id_ = request_id; |
| 317 | 320 | ||
| 318 | if (script.IsEmpty()) |
321 | if (script.IsEmpty()) |
| 319 | { |
322 | { |
| 320 | this->CallOutput(this->FormatException(try_catch)); |
323 | this->CallOutput(this->FormatException(try_catch)); |
| 321 | } |
324 | } |
| 322 | else |
325 | else |
| 323 | { |
326 | { |
| 324 | Value result = script->Run(); |
327 | Value result = script->Run(); |
| 325 | 328 | ||
| 326 | if (result.IsEmpty()) |
329 | if (result.IsEmpty()) |
| 327 | { |
330 | { |
| 328 | this->CallOutput(this->FormatException(try_catch)); |
331 | this->CallOutput(this->FormatException(try_catch)); |
| 329 | } |
332 | } |
| 330 | else |
333 | else |
| 331 | { |
334 | { |
| 332 | //v8::String::AsciiValue response(result->ToString()); |
335 | //v8::String::AsciiValue response(result->ToString()); |
| 333 | //this->CallOutput(std::string(*response)); |
336 | //this->CallOutput(std::string(*response)); |
| 334 | } |
337 | } |
| 335 | } |
338 | } |
| 336 | 339 | ||
| 337 | this->current_plugin_name_ = ""; |
340 | this->current_plugin_name_ = ""; |
| 338 | this->current_request_id_ = 0; |
341 | this->current_request_id_ = 0; |
| 339 | } |
342 | } |
| 340 | 343 | ||
| 341 | std::string Manager::FormatException(v8::TryCatch& try_catch) |
344 | std::string Manager::FormatException(v8::TryCatch& try_catch) |
| 342 | { |
345 | { |
| 343 | v8::Locker lock; |
346 | v8::Locker lock; |
| 344 | v8::Context::Scope context_scope(this->context_); |
347 | v8::Context::Scope context_scope(this->context_); |
| 345 | 348 | ||
| 346 | std::string result; |
349 | std::string result; |
| 347 | 350 | ||
| 348 | v8::HandleScope handle_scope; |
351 | v8::HandleScope handle_scope; |
| 349 | v8::String::Utf8Value exception(try_catch.Exception()); |
352 | v8::String::Utf8Value exception(try_catch.Exception()); |
| 350 | v8::Handle<v8::Message> message = try_catch.Message(); |
353 | v8::Handle<v8::Message> message = try_catch.Message(); |
| 351 | 354 | ||
| 352 | if (message.IsEmpty()) |
355 | if (message.IsEmpty()) |
| 353 | { |
356 | { |
| 354 | result = *exception; |
357 | result = *exception; |
| 355 | } |
358 | } |
| 356 | else |
359 | else |
| 357 | { |
360 | { |
| 358 | v8::String::Utf8Value filename(message->GetScriptResourceName()); |
361 | v8::String::Utf8Value filename(message->GetScriptResourceName()); |
| 359 | 362 | ||
| 360 | result = std::string(*filename) + ":" + boost::lexical_cast<std::string>(message->GetLineNumber()) + ": " + std::string(*exception); |
363 | result = std::string(*filename) + ":" + boost::lexical_cast<std::string>(message->GetLineNumber()) + ": " + std::string(*exception); |
| 361 | 364 | ||
| 362 | /*v8::String::Utf8Value sourceline(message->GetSourceLine()); |
365 | /*v8::String::Utf8Value sourceline(message->GetSourceLine()); |
| 363 | string strSourceline = *sourceline; |
366 | string strSourceline = *sourceline; |
| 364 | |
367 | |
| 365 | result += strSourceline + "\n"; |
368 | result += strSourceline + "\n"; |
| 366 | |
369 | |
| 367 | string underline = rpad("", message->GetStartColumn(), ' '); |
370 | string underline = rpad("", message->GetStartColumn(), ' '); |
| 368 | underline = rpad(underline, message->GetEndColumn(), '^'); |
371 | underline = rpad(underline, message->GetEndColumn(), '^'); |
| 369 | |
372 | |
| 370 | result += underline + "\n";*/ |
373 | result += underline + "\n";*/ |
| 371 | } |
374 | } |
| 372 | 375 | ||
| 373 | return result; |
376 | return result; |
| 374 | } |
377 | } |
| 375 | 378 | ||
| 376 | }; // namespace vm |
379 | }; // namespace vm |
| 377 | }; // namespace atom |
380 | }; // namespace atom |
| 378 | 381 | ||