Rev 969 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 969 | Rev 970 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | * Copyright (C) December 10, 2008 by Mattias Runge * |
2 | * Copyright (C) December 10, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * virtualmachine.cpp * |
4 | * virtualmachine.cpp * |
| 5 | * * |
5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
9 | * (at your option) any later version. * |
| 10 | * * |
10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
14 | * GNU General Public License for more details. * |
| 15 | * * |
15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #include <map> |
22 | #include <map> |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | #include "virtualmachine.h" |
25 | #include "virtualmachine.h" |
| 26 | 26 | ||
| 27 | VirtualMachine* VirtualMachine::myInstance = NULL; |
27 | VirtualMachine* VirtualMachine::myInstance = NULL; |
| 28 | 28 | ||
| 29 | VirtualMachine& VirtualMachine::getInstance() |
29 | VirtualMachine& VirtualMachine::getInstance() |
| 30 | { |
30 | { |
| 31 | if (myInstance == NULL) |
31 | if (myInstance == NULL) |
| 32 | { |
32 | { |
| 33 | myInstance = new VirtualMachine(); |
33 | myInstance = new VirtualMachine(); |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | return *myInstance; |
36 | return *myInstance; |
| 37 | } |
37 | } |
| 38 | 38 | ||
| 39 | void VirtualMachine::deleteInstance() |
39 | void VirtualMachine::deleteInstance() |
| 40 | { |
40 | { |
| 41 | if (myInstance != NULL) |
41 | if (myInstance != NULL) |
| 42 | { |
42 | { |
| 43 | delete myInstance; |
43 | delete myInstance; |
| 44 | myInstance = NULL; |
44 | myInstance = NULL; |
| 45 | } |
45 | } |
| 46 | } |
46 | } |
| 47 | 47 | ||
| 48 | VirtualMachine::VirtualMachine() |
48 | VirtualMachine::VirtualMachine() |
| 49 | { |
49 | { |
| 50 | 50 | ||
| 51 | } |
51 | } |
| 52 | 52 | ||
| 53 | VirtualMachine::~VirtualMachine() |
53 | VirtualMachine::~VirtualMachine() |
| 54 | { |
54 | { |
| 55 | 55 | ||
| 56 | } |
56 | } |
| 57 | 57 | ||
| 58 | void VirtualMachine::run() |
58 | void VirtualMachine::run() |
| 59 | { |
59 | { |
| 60 | SyslogStream &slog = SyslogStream::getInstance(); |
60 | SyslogStream &slog = SyslogStream::getInstance(); |
| 61 | 61 | ||
| 62 | string basePath = Settings::get("BasePath") + "/Services/System/"; |
62 | string basePath = Settings::get("BasePath") + "/Services/System/"; |
| 63 | string scriptName = "Base.js"; |
63 | string scriptName = "Base.js"; |
| 64 | string scriptFileName = basePath + scriptName; |
64 | string scriptFileName = basePath + scriptName; |
| 65 | 65 | ||
| 66 | if (!file_exists(scriptFileName)) |
66 | if (!file_exists(scriptFileName)) |
| 67 | { |
67 | { |
| 68 | slog << "Failed to load :" << scriptFileName << "\n"; |
68 | slog << "Failed to load :" << scriptFileName << "\n"; |
| 69 | } |
69 | } |
| 70 | 70 | ||
| 71 | string scriptSource = file_get_contents(scriptFileName); |
71 | string scriptSource = file_get_contents(scriptFileName); |
| 72 | 72 | ||
| 73 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
73 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 74 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
74 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 75 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
75 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 76 | 76 | ||
| 77 | // Create a template for the global object. |
77 | // Create a template for the global object. |
| 78 | myGlobal = ObjectTemplate::New(); |
78 | myGlobal = ObjectTemplate::New(); |
| 79 | 79 | ||
| 80 | //associates "print" on script to the Print function |
80 | //associates "print" on script to the Print function |
| 81 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
81 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
| 82 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
82 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
| 83 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
83 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
| 84 | myGlobal->Set(String::New("setTimeout"), FunctionTemplate::New(VirtualMachine_setTimeout)); |
84 | myGlobal->Set(String::New("setTimeout"), FunctionTemplate::New(VirtualMachine_setTimeout)); |
| 85 | myGlobal->Set(String::New("setInterval"), FunctionTemplate::New(VirtualMachine_setInterval)); |
85 | myGlobal->Set(String::New("setInterval"), FunctionTemplate::New(VirtualMachine_setInterval)); |
| 86 | myGlobal->Set(String::New("clearTimeout"), FunctionTemplate::New(VirtualMachine_clearInterval)); |
86 | myGlobal->Set(String::New("clearTimeout"), FunctionTemplate::New(VirtualMachine_clearInterval)); |
| 87 | myGlobal->Set(String::New("clearInterval"), FunctionTemplate::New(VirtualMachine_clearInterval)); |
87 | myGlobal->Set(String::New("clearInterval"), FunctionTemplate::New(VirtualMachine_clearInterval)); |
| 88 | 88 | ||
| 89 | //create context for the script |
89 | //create context for the script |
| 90 | myContext = Context::New(NULL, myGlobal); |
90 | myContext = Context::New(NULL, myGlobal); |
| 91 | 91 | ||
| 92 | 92 | ||
| 93 | //access global context within this scope |
93 | //access global context within this scope |
| 94 | Context::Scope context_scope(myContext); |
94 | Context::Scope context_scope(myContext); |
| 95 | //exception handler |
95 | //exception handler |
| 96 | TryCatch try_catch; |
96 | TryCatch try_catch; |
| 97 | //compile script to binary code - JIT |
97 | //compile script to binary code - JIT |
| 98 | Handle<Script> script = Script::Compile(source, name); |
98 | Handle<Script> script = Script::Compile(source, name); |
| 99 | 99 | ||
| 100 | //check if we got problems on compilation |
100 | //check if we got problems on compilation |
| 101 | if (script.IsEmpty()) |
101 | if (script.IsEmpty()) |
| 102 | { |
102 | { |
| 103 | 103 | ||
| 104 | // Print errors that happened during compilation. |
104 | // Print errors that happened during compilation. |
| 105 | String::AsciiValue error(try_catch.Exception()); |
105 | String::AsciiValue error(try_catch.Exception()); |
| 106 | slog << "Failed to compile script: " << *error << "\n"; |
106 | slog << "Failed to compile script: " << *error << "\n"; |
| 107 | } |
107 | } |
| 108 | else |
108 | else |
| 109 | { |
109 | { |
| 110 | //no errors , let's continue |
110 | //no errors , let's continue |
| 111 | Handle<Value> result = script->Run(); |
111 | Handle<Value> result = script->Run(); |
| 112 | 112 | ||
| 113 | //check if execution ended with errors |
113 | //check if execution ended with errors |
| 114 | if (result.IsEmpty()) |
114 | if (result.IsEmpty()) |
| 115 | { |
115 | { |
| 116 | // Print errors that happened during execution. |
116 | // Print errors that happened during execution. |
| 117 | String::AsciiValue error(try_catch.Exception()); |
117 | String::AsciiValue error(try_catch.Exception()); |
| 118 | slog << "Constructor: Failed to run script: " << *error << "\n"; |
118 | slog << "Constructor: Failed to run script: " << *error << "\n"; |
| 119 | } |
119 | } |
| 120 | else |
120 | else |
| 121 | { |
121 | { |
| 122 | loadScript("System/Service.js"); |
122 | loadScript("System/Service.js"); |
| 123 | loadScript("System/ServiceManager.js"); |
123 | loadScript("System/ServiceManager.js"); |
| 124 | loadScript("System/CanMessage.js"); |
124 | loadScript("System/CanMessage.js"); |
| 125 | loadScript("System/CanManager.js"); |
125 | loadScript("System/CanManager.js"); |
| 126 | loadScript("System/CanService.js"); |
126 | loadScript("System/CanService.js"); |
| 127 | loadScript("System/Startup.js"); |
127 | loadScript("System/Startup.js"); |
| - | 128 | ||
| - | 129 | if (file_exists(basePath + "Autostart.js")) |
|
| - | 130 | { |
|
| 128 | loadScript("Autostart.js"); |
131 | loadScript("Autostart.js"); |
| - | 132 | } |
|
| 129 | 133 | ||
| 130 | myFunctionHandleHeartbeat = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleHeartbeat"))); |
134 | myFunctionHandleHeartbeat = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleHeartbeat"))); |
| 131 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
135 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
| 132 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
136 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
| 133 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
137 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
| 134 | } |
138 | } |
| 135 | } |
139 | } |
| 136 | 140 | ||
| 137 | const int argc = 0; |
141 | const int argc = 0; |
| 138 | Handle<Value> argv[argc] = { }; |
142 | Handle<Value> argv[argc] = { }; |
| 139 | 143 | ||
| 140 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); // argc and argv are your standard arguments to a function |
144 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); // argc and argv are your standard arguments to a function |
| 141 | 145 | ||
| 142 | mySemaphore.lock(); |
146 | mySemaphore.lock(); |
| 143 | 147 | ||
| 144 | while (1) |
148 | while (1) |
| 145 | { |
149 | { |
| 146 | string expression; |
150 | string expression; |
| 147 | 151 | ||
| 148 | while (myExpressions.size() > 0) |
152 | while (myExpressions.size() > 0) |
| 149 | { |
153 | { |
| 150 | expression = myExpressions.pop(); |
154 | expression = myExpressions.pop(); |
| 151 | 155 | ||
| 152 | runExpression(expression); |
156 | runExpression(expression); |
| 153 | } |
157 | } |
| 154 | 158 | ||
| 155 | CanMessage canMessage; |
159 | CanMessage canMessage; |
| 156 | 160 | ||
| 157 | while (myCanMessages.size() > 0) |
161 | while (myCanMessages.size() > 0) |
| 158 | { |
162 | { |
| 159 | canMessage = myCanMessages.pop(); |
163 | canMessage = myCanMessages.pop(); |
| 160 | 164 | ||
| 161 | if (canMessage.isHeartbeat()) |
165 | if (canMessage.isHeartbeat()) |
| 162 | { |
166 | { |
| 163 | callHandleHeartbeat(stoi(canMessage.getData()["HardwareId"].getValue())); |
167 | callHandleHeartbeat(stoi(canMessage.getData()["HardwareId"].getValue())); |
| 164 | } |
168 | } |
| 165 | else |
169 | else |
| 166 | { |
170 | { |
| 167 | callHandleMessage(canMessage); |
171 | callHandleMessage(canMessage); |
| 168 | } |
172 | } |
| 169 | } |
173 | } |
| 170 | 174 | ||
| 171 | mySemaphore.wait(); |
175 | mySemaphore.wait(); |
| 172 | } |
176 | } |
| 173 | 177 | ||
| 174 | mySemaphore.unlock(); |
178 | mySemaphore.unlock(); |
| 175 | } |
179 | } |
| 176 | 180 | ||
| 177 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
181 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
| 178 | { |
182 | { |
| 179 | myCanMessages.push(canMessage); |
183 | myCanMessages.push(canMessage); |
| 180 | mySemaphore.broadcast(); |
184 | mySemaphore.broadcast(); |
| 181 | } |
185 | } |
| 182 | 186 | ||
| 183 | void VirtualMachine::queueExpression(string expression) |
187 | void VirtualMachine::queueExpression(string expression) |
| 184 | { |
188 | { |
| 185 | myExpressions.push(expression); |
189 | myExpressions.push(expression); |
| 186 | mySemaphore.broadcast(); |
190 | mySemaphore.broadcast(); |
| 187 | } |
191 | } |
| 188 | 192 | ||
| 189 | bool VirtualMachine::loadScript(string scriptName) |
193 | bool VirtualMachine::loadScript(string scriptName) |
| 190 | { |
194 | { |
| 191 | SyslogStream &slog = SyslogStream::getInstance(); |
195 | SyslogStream &slog = SyslogStream::getInstance(); |
| 192 | 196 | ||
| 193 | string basePath = Settings::get("BasePath") + "/Services/"; |
197 | string basePath = Settings::get("BasePath") + "/Services/"; |
| 194 | string scriptFileName = basePath + scriptName; |
198 | string scriptFileName = basePath + scriptName; |
| 195 | 199 | ||
| 196 | if (!file_exists(scriptFileName)) |
200 | if (!file_exists(scriptFileName)) |
| 197 | { |
201 | { |
| 198 | slog << "Failed to load :" << scriptFileName << "\n"; |
202 | slog << "Failed to load :" << scriptFileName << "\n"; |
| 199 | return false; |
203 | return false; |
| 200 | } |
204 | } |
| 201 | 205 | ||
| 202 | string scriptSource = file_get_contents(scriptFileName); |
206 | string scriptSource = file_get_contents(scriptFileName); |
| 203 | 207 | ||
| 204 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
208 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 205 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
209 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 206 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
210 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 207 | 211 | ||
| 208 | //access global context within this scope |
212 | //access global context within this scope |
| 209 | Context::Scope context_scope(myContext); |
213 | Context::Scope context_scope(myContext); |
| 210 | //exception handler |
214 | //exception handler |
| 211 | TryCatch try_catch; |
215 | TryCatch try_catch; |
| 212 | //compile script to binary code - JIT |
216 | //compile script to binary code - JIT |
| 213 | Handle<Script> script = Script::Compile(source, name); |
217 | Handle<Script> script = Script::Compile(source, name); |
| 214 | 218 | ||
| 215 | //check if we got problems on compilation |
219 | //check if we got problems on compilation |
| 216 | if (script.IsEmpty()) |
220 | if (script.IsEmpty()) |
| 217 | { |
221 | { |
| 218 | // Print errors that happened during compilation. |
222 | // Print errors that happened during compilation. |
| 219 | String::AsciiValue error(try_catch.Exception()); |
223 | String::AsciiValue error(try_catch.Exception()); |
| 220 | slog << "Failed to compile script: " << *error << "\n"; |
224 | slog << "Failed to compile script: " << *error << "\n"; |
| 221 | return false; |
225 | return false; |
| 222 | } |
226 | } |
| 223 | else |
227 | else |
| 224 | { |
228 | { |
| 225 | //no errors , let's continue |
229 | //no errors , let's continue |
| 226 | Handle<Value> result = script->Run(); |
230 | Handle<Value> result = script->Run(); |
| 227 | 231 | ||
| 228 | //check if execution ended with errors |
232 | //check if execution ended with errors |
| 229 | if (result.IsEmpty()) |
233 | if (result.IsEmpty()) |
| 230 | { |
234 | { |
| 231 | // Print errors that happened during execution. |
235 | // Print errors that happened during execution. |
| 232 | String::AsciiValue error(try_catch.Exception()); |
236 | String::AsciiValue error(try_catch.Exception()); |
| 233 | slog << "loadScript: Failed to run script: " << *error << "\n"; |
237 | slog << "loadScript: Failed to run script: " << *error << "\n"; |
| 234 | return false; |
238 | return false; |
| 235 | } |
239 | } |
| 236 | } |
240 | } |
| 237 | 241 | ||
| 238 | return true; |
242 | return true; |
| 239 | } |
243 | } |
| 240 | 244 | ||
| 241 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
245 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
| 242 | { |
246 | { |
| 243 | const int argc = 1; |
247 | const int argc = 1; |
| 244 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
248 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
| 245 | 249 | ||
| 246 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
250 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
| 247 | } |
251 | } |
| 248 | 252 | ||
| 249 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
253 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
| 250 | { |
254 | { |
| 251 | const int argc = 6; |
255 | const int argc = 6; |
| 252 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
256 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
| 253 | String::New(canMessage.getDirectionFlag().c_str()), |
257 | String::New(canMessage.getDirectionFlag().c_str()), |
| 254 | String::New(canMessage.getModuleName().c_str()), |
258 | String::New(canMessage.getModuleName().c_str()), |
| 255 | Integer::New(canMessage.getModuleId()), |
259 | Integer::New(canMessage.getModuleId()), |
| 256 | String::New(canMessage.getCommandName().c_str()), |
260 | String::New(canMessage.getCommandName().c_str()), |
| 257 | String::New(canMessage.getJSONData().c_str()) }; |
261 | String::New(canMessage.getJSONData().c_str()) }; |
| 258 | 262 | ||
| 259 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
263 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 260 | } |
264 | } |
| 261 | 265 | ||
| 262 | int VirtualMachine::setInterval(string expression, int interval, bool single) |
266 | int VirtualMachine::setInterval(string expression, int interval, bool single) |
| 263 | { |
267 | { |
| 264 | IntervalThread intervalThread(expression, interval, single); |
268 | IntervalThread intervalThread(expression, interval, single); |
| 265 | intervalThread.start(); |
269 | intervalThread.start(); |
| 266 | 270 | ||
| 267 | int timeoutId = myTimers.size(); |
271 | int timeoutId = myTimers.size(); |
| 268 | myTimers[timeoutId] = intervalThread; |
272 | myTimers[timeoutId] = intervalThread; |
| 269 | myTimers[timeoutId].start(); |
273 | myTimers[timeoutId].start(); |
| 270 | 274 | ||
| 271 | return timeoutId; |
275 | return timeoutId; |
| 272 | } |
276 | } |
| 273 | 277 | ||
| 274 | bool VirtualMachine::clearInterval(int timeoutId) |
278 | bool VirtualMachine::clearInterval(int timeoutId) |
| 275 | { |
279 | { |
| 276 | if (myTimers.find(timeoutId) != myTimers.end()) |
280 | if (myTimers.find(timeoutId) != myTimers.end()) |
| 277 | { |
281 | { |
| 278 | myTimers.erase(timeoutId); |
282 | myTimers.erase(timeoutId); |
| 279 | return true; |
283 | return true; |
| 280 | } |
284 | } |
| 281 | 285 | ||
| 282 | return false; |
286 | return false; |
| 283 | } |
287 | } |
| 284 | 288 | ||
| 285 | bool VirtualMachine::runExpression(string expression) |
289 | bool VirtualMachine::runExpression(string expression) |
| 286 | { |
290 | { |
| 287 | SyslogStream &slog = SyslogStream::getInstance(); |
291 | SyslogStream &slog = SyslogStream::getInstance(); |
| 288 | 292 | ||
| 289 | string scriptName = "runExpression"; |
293 | string scriptName = "runExpression"; |
| 290 | 294 | ||
| 291 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
295 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
| 292 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
296 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 293 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
297 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 294 | 298 | ||
| 295 | //access global context within this scope |
299 | //access global context within this scope |
| 296 | Context::Scope context_scope(myContext); |
300 | Context::Scope context_scope(myContext); |
| 297 | //exception handler |
301 | //exception handler |
| 298 | TryCatch try_catch; |
302 | TryCatch try_catch; |
| 299 | //compile script to binary code - JIT |
303 | //compile script to binary code - JIT |
| 300 | Handle<Script> script = Script::Compile(source, name); |
304 | Handle<Script> script = Script::Compile(source, name); |
| 301 | 305 | ||
| 302 | //check if we got problems on compilation |
306 | //check if we got problems on compilation |
| 303 | if (script.IsEmpty()) |
307 | if (script.IsEmpty()) |
| 304 | { |
308 | { |
| 305 | // Print errors that happened during compilation. |
309 | // Print errors that happened during compilation. |
| 306 | String::AsciiValue error(try_catch.Exception()); |
310 | String::AsciiValue error(try_catch.Exception()); |
| 307 | slog << "Failed to compile script: " << *error << "\n"; |
311 | slog << "Failed to compile script: " << *error << "\n"; |
| 308 | 312 | ||
| 309 | return false; |
313 | return false; |
| 310 | } |
314 | } |
| 311 | else |
315 | else |
| 312 | { |
316 | { |
| 313 | //no errors , let's continue |
317 | //no errors , let's continue |
| 314 | Handle<Value> result = script->Run(); |
318 | Handle<Value> result = script->Run(); |
| 315 | 319 | ||
| 316 | //check if execution ended with errors |
320 | //check if execution ended with errors |
| 317 | if (result.IsEmpty()) |
321 | if (result.IsEmpty()) |
| 318 | { |
322 | { |
| 319 | // Print errors that happened during execution. |
323 | // Print errors that happened during execution. |
| 320 | String::AsciiValue error(try_catch.Exception()); |
324 | String::AsciiValue error(try_catch.Exception()); |
| 321 | slog << "runExpression: Failed to run script: " << *error << "\n"; |
325 | slog << "runExpression: Failed to run script: " << *error << "\n"; |
| 322 | return false; |
326 | return false; |
| 323 | } |
327 | } |
| 324 | } |
328 | } |
| 325 | 329 | ||
| 326 | return true; |
330 | return true; |
| 327 | } |
331 | } |
| 328 | 332 | ||
| 329 | Handle<Value> VirtualMachine_log(const Arguments& args) |
333 | Handle<Value> VirtualMachine_log(const Arguments& args) |
| 330 | { |
334 | { |
| 331 | SyslogStream &slog = SyslogStream::getInstance(); |
335 | SyslogStream &slog = SyslogStream::getInstance(); |
| 332 | 336 | ||
| 333 | String::AsciiValue str(args[0]); |
337 | String::AsciiValue str(args[0]); |
| 334 | 338 | ||
| 335 | slog << *str; |
339 | slog << *str; |
| 336 | 340 | ||
| 337 | return Undefined(); |
341 | return Undefined(); |
| 338 | } |
342 | } |
| 339 | 343 | ||
| 340 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
344 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
| 341 | { |
345 | { |
| 342 | SyslogStream &slog = SyslogStream::getInstance(); |
346 | SyslogStream &slog = SyslogStream::getInstance(); |
| 343 | CanNetManager &canMan = CanNetManager::getInstance(); |
347 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 344 | 348 | ||
| 345 | CanMessage canMessage; |
349 | CanMessage canMessage; |
| 346 | 350 | ||
| 347 | String::AsciiValue className(args[0]); |
351 | String::AsciiValue className(args[0]); |
| 348 | canMessage.setClassName(*className); |
352 | canMessage.setClassName(*className); |
| 349 | String::AsciiValue directionFlag(args[1]); |
353 | String::AsciiValue directionFlag(args[1]); |
| 350 | canMessage.setDirectionFlag(*directionFlag); |
354 | canMessage.setDirectionFlag(*directionFlag); |
| 351 | String::AsciiValue moduleName(args[2]); |
355 | String::AsciiValue moduleName(args[2]); |
| 352 | canMessage.setModuleName(*moduleName); |
356 | canMessage.setModuleName(*moduleName); |
| 353 | canMessage.setModuleId(args[3]->Uint32Value()); |
357 | canMessage.setModuleId(args[3]->Uint32Value()); |
| 354 | String::AsciiValue commandName(args[4]); |
358 | String::AsciiValue commandName(args[4]); |
| 355 | canMessage.setCommandName(*commandName); |
359 | canMessage.setCommandName(*commandName); |
| 356 | 360 | ||
| 357 | String::AsciiValue dataString(args[5]); |
361 | String::AsciiValue dataString(args[5]); |
| 358 | 362 | ||
| 359 | vector<string> parts = explode(",", trim(*dataString, ',')); |
363 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 360 | map<string, CanVariable> data; |
364 | map<string, CanVariable> data; |
| 361 | 365 | ||
| 362 | for (int n = 0; n < parts.size(); n++) |
366 | for (int n = 0; n < parts.size(); n++) |
| 363 | { |
367 | { |
| 364 | vector<string> keyAndValue = explode(":", parts[n]); |
368 | vector<string> keyAndValue = explode(":", parts[n]); |
| 365 | 369 | ||
| 366 | //string key = trim(keyAndValue[0], ' '); |
370 | //string key = trim(keyAndValue[0], ' '); |
| 367 | //string value = trim(keyAndValue[1], ' '); |
371 | //string value = trim(keyAndValue[1], ' '); |
| 368 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
372 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 369 | } |
373 | } |
| 370 | 374 | ||
| 371 | canMessage.setData(data); |
375 | canMessage.setData(data); |
| 372 | 376 | ||
| 373 | canMan.sendMessage(canMessage); |
377 | canMan.sendMessage(canMessage); |
| 374 | 378 | ||
| 375 | return Undefined(); |
379 | return Undefined(); |
| 376 | } |
380 | } |
| 377 | 381 | ||
| 378 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
382 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
| 379 | { |
383 | { |
| 380 | VirtualMachine &vm = VirtualMachine::getInstance(); |
384 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 381 | 385 | ||
| 382 | String::AsciiValue str(args[0]); |
386 | String::AsciiValue str(args[0]); |
| 383 | 387 | ||
| 384 | return Boolean::New(vm.loadScript(*str)); |
388 | return Boolean::New(vm.loadScript(*str)); |
| 385 | } |
389 | } |
| 386 | 390 | ||
| 387 | Handle<Value> VirtualMachine_setInterval(const Arguments& args) |
391 | Handle<Value> VirtualMachine_setInterval(const Arguments& args) |
| 388 | { |
392 | { |
| 389 | VirtualMachine &vm = VirtualMachine::getInstance(); |
393 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 390 | 394 | ||
| 391 | String::AsciiValue expression(args[0]); |
395 | String::AsciiValue expression(args[0]); |
| 392 | unsigned int interval = args[1]->Uint32Value(); |
396 | unsigned int interval = args[1]->Uint32Value(); |
| 393 | 397 | ||
| 394 | return Integer::New(vm.setInterval(*expression, interval, false)); |
398 | return Integer::New(vm.setInterval(*expression, interval, false)); |
| 395 | } |
399 | } |
| 396 | 400 | ||
| 397 | Handle<Value> VirtualMachine_setTimeout(const Arguments& args) |
401 | Handle<Value> VirtualMachine_setTimeout(const Arguments& args) |
| 398 | { |
402 | { |
| 399 | VirtualMachine &vm = VirtualMachine::getInstance(); |
403 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 400 | 404 | ||
| 401 | String::AsciiValue expression(args[0]); |
405 | String::AsciiValue expression(args[0]); |
| 402 | unsigned int interval = args[1]->Uint32Value(); |
406 | unsigned int interval = args[1]->Uint32Value(); |
| 403 | 407 | ||
| 404 | return Integer::New(vm.setInterval(*expression, interval, true)); |
408 | return Integer::New(vm.setInterval(*expression, interval, true)); |
| 405 | } |
409 | } |
| 406 | 410 | ||
| 407 | Handle<Value> VirtualMachine_clearInterval(const Arguments& args) |
411 | Handle<Value> VirtualMachine_clearInterval(const Arguments& args) |
| 408 | { |
412 | { |
| 409 | VirtualMachine &vm = VirtualMachine::getInstance(); |
413 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 410 | 414 | ||
| 411 | return Boolean::New(vm.clearInterval(args[0]->Uint32Value())); |
415 | return Boolean::New(vm.clearInterval(args[0]->Uint32Value())); |
| 412 | } |
416 | } |
| 413 | 417 | ||