Rev 1203 | Rev 1402 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1203 | Rev 1229 | ||
|---|---|---|---|
| 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 "virtualmachine.h" |
22 | #include "virtualmachine.h" |
| 23 | 23 | ||
| 24 | VirtualMachine* VirtualMachine::myInstance = NULL; |
24 | VirtualMachine* VirtualMachine::myInstance = NULL; |
| 25 | 25 | ||
| 26 | VirtualMachine& VirtualMachine::getInstance() |
26 | VirtualMachine& VirtualMachine::getInstance() |
| 27 | { |
27 | { |
| 28 | if (myInstance == NULL) |
28 | if (myInstance == NULL) |
| 29 | { |
29 | { |
| 30 | myInstance = new VirtualMachine(); |
30 | myInstance = new VirtualMachine(); |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | return *myInstance; |
33 | return *myInstance; |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | void VirtualMachine::deleteInstance() |
36 | void VirtualMachine::deleteInstance() |
| 37 | { |
37 | { |
| 38 | if (myInstance != NULL) |
38 | if (myInstance != NULL) |
| 39 | { |
39 | { |
| 40 | delete myInstance; |
40 | delete myInstance; |
| 41 | myInstance = NULL; |
41 | myInstance = NULL; |
| 42 | } |
42 | } |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | VirtualMachine::VirtualMachine() |
45 | VirtualMachine::VirtualMachine() |
| 46 | { |
46 | { |
| 47 | } |
47 | } |
| 48 | 48 | ||
| 49 | VirtualMachine::~VirtualMachine() |
49 | VirtualMachine::~VirtualMachine() |
| 50 | { |
50 | { |
| 51 | stop(); |
51 | stop(); |
| 52 | 52 | ||
| 53 | myCommandThread.stop(); |
53 | myCommandThread.stop(); |
| 54 | 54 | ||
| 55 | ///FIXME: Verify that this works and does not cause segfaults |
55 | ///FIXME: Verify that this works and does not cause segfaults |
| 56 | for (map<unsigned long int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++) |
56 | for (map<unsigned long int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++) |
| 57 | { |
57 | { |
| 58 | iter->second->stop(); |
58 | iter->second->stop(); |
| 59 | delete iter->second; |
59 | delete iter->second; |
| 60 | //mySocketThreads.erase(iter); |
60 | //mySocketThreads.erase(iter); |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | mySocketThreads.clear(); |
63 | mySocketThreads.clear(); |
| 64 | 64 | ||
| 65 | for (map<int, IntervalThread*>::iterator iter = myIntervalThreads.begin(); iter != myIntervalThreads.end(); iter++) |
65 | for (map<int, IntervalThread*>::iterator iter = myIntervalThreads.begin(); iter != myIntervalThreads.end(); iter++) |
| 66 | { |
66 | { |
| 67 | iter->second->stop(); |
67 | iter->second->stop(); |
| 68 | delete iter->second; |
68 | delete iter->second; |
| 69 | //myIntervalThreads.erase(iter); |
69 | //myIntervalThreads.erase(iter); |
| 70 | } |
70 | } |
| 71 | 71 | ||
| 72 | myIntervalThreads.clear(); |
72 | myIntervalThreads.clear(); |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | void VirtualMachine::run() |
75 | void VirtualMachine::run() |
| 76 | { |
76 | { |
| 77 | myGlobal = ObjectTemplate::New(); |
77 | myGlobal = ObjectTemplate::New(); |
| 78 | myGlobal->Set(String::New("_quit"), FunctionTemplate::New(VirtualMachine::_quit)); |
78 | myGlobal->Set(String::New("_quit"), FunctionTemplate::New(VirtualMachine::_quit)); |
| 79 | myGlobal->Set(String::New("_print"), FunctionTemplate::New(VirtualMachine::_print)); |
79 | myGlobal->Set(String::New("_print"), FunctionTemplate::New(VirtualMachine::_print)); |
| 80 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine::_log)); |
80 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine::_log)); |
| 81 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine::_sendCanMessage)); |
81 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine::_sendCanMessage)); |
| 82 | myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine::_sendCanNMTMessage)); |
82 | myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine::_sendCanNMTMessage)); |
| 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("startIntervalThread"), FunctionTemplate::New(VirtualMachine::_startIntervalThread)); |
84 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine::_startIntervalThread)); |
| 85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine::_stopIntervalThread)); |
85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine::_stopIntervalThread)); |
| 86 | myGlobal->Set(String::New("setIntervalThreadTimeout"), FunctionTemplate::New(VirtualMachine::_setIntervalThreadTimeout)); |
86 | myGlobal->Set(String::New("setIntervalThreadTimeout"), FunctionTemplate::New(VirtualMachine::_setIntervalThreadTimeout)); |
| 87 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread)); |
87 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread)); |
| 88 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread)); |
88 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread)); |
| 89 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread)); |
89 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread)); |
| 90 | myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore)); |
90 | myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore)); |
| 91 | myGlobal->Set(String::New("getFileContents"), FunctionTemplate::New(VirtualMachine::_getFileContents)); |
91 | myGlobal->Set(String::New("getFileContents"), FunctionTemplate::New(VirtualMachine::_getFileContents)); |
| 92 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex)); |
92 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex)); |
| 93 | myGlobal->Set(String::New("hex2bin"), FunctionTemplate::New(VirtualMachine::_hex2bin)); |
93 | myGlobal->Set(String::New("hex2bin"), FunctionTemplate::New(VirtualMachine::_hex2bin)); |
| 94 | myGlobal->Set(String::New("bin2hex"), FunctionTemplate::New(VirtualMachine::_bin2hex)); |
94 | myGlobal->Set(String::New("bin2hex"), FunctionTemplate::New(VirtualMachine::_bin2hex)); |
| 95 | myGlobal->Set(String::New("bin2float"), FunctionTemplate::New(VirtualMachine::_bin2float)); |
95 | myGlobal->Set(String::New("bin2float"), FunctionTemplate::New(VirtualMachine::_bin2float)); |
| 96 | myGlobal->Set(String::New("float2bin"), FunctionTemplate::New(VirtualMachine::_float2bin)); |
96 | myGlobal->Set(String::New("float2bin"), FunctionTemplate::New(VirtualMachine::_float2bin)); |
| 97 | myGlobal->Set(String::New("bin2uint"), FunctionTemplate::New(VirtualMachine::_bin2uint)); |
97 | myGlobal->Set(String::New("bin2uint"), FunctionTemplate::New(VirtualMachine::_bin2uint)); |
| 98 | myGlobal->Set(String::New("uint2bin"), FunctionTemplate::New(VirtualMachine::_uint2bin)); |
98 | myGlobal->Set(String::New("uint2bin"), FunctionTemplate::New(VirtualMachine::_uint2bin)); |
| 99 | myGlobal->Set(String::New("hex2uint"), FunctionTemplate::New(VirtualMachine::_hex2uint)); |
99 | myGlobal->Set(String::New("hex2uint"), FunctionTemplate::New(VirtualMachine::_hex2uint)); |
| 100 | myGlobal->Set(String::New("sleep"), FunctionTemplate::New(VirtualMachine::_sleep)); |
100 | myGlobal->Set(String::New("sleep"), FunctionTemplate::New(VirtualMachine::_sleep)); |
| 101 | myContext = Context::New(NULL, myGlobal); |
101 | myContext = Context::New(NULL, myGlobal); |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | if (loadScript("System/Base.js")) |
104 | if (loadScript("System/Base.js")) |
| 105 | { |
105 | { |
| 106 | loadScript("System/Startup.js"); |
106 | loadScript("System/Startup.js"); |
| 107 | 107 | ||
| 108 | loadScript("Autostart.js"); |
108 | loadScript("Autostart.js"); |
| 109 | 109 | ||
| 110 | Context::Scope contextScope(myContext); |
110 | Context::Scope contextScope(myContext); |
| 111 | 111 | ||
| 112 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
112 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
| 113 | } |
113 | } |
| 114 | else |
114 | else |
| 115 | { |
115 | { |
| 116 | ///FIXME: We should probably exit the application here |
116 | ///FIXME: We should probably exit the application here |
| 117 | return; |
117 | return; |
| 118 | } |
118 | } |
| 119 | 119 | ||
| 120 | const int argc = 0; |
120 | const int argc = 0; |
| 121 | Handle<Value> argv[argc] = { }; |
121 | Handle<Value> argv[argc] = { }; |
| 122 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); |
122 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); |
| 123 | 123 | ||
| 124 | 124 | ||
| 125 | string commandPort = Settings::get("CommandPort"); |
125 | string commandPort = Settings::get("CommandPort"); |
| 126 | if (commandPort != "") |
126 | if (commandPort != "") |
| 127 | { |
127 | { |
| 128 | myCommandThread.setSettings(stoi(commandPort), "Command console"); |
128 | myCommandThread.setSettings(stoi(commandPort), "Command console"); |
| 129 | myCommandThread.start(); |
129 | myCommandThread.start(); |
| 130 | } |
130 | } |
| 131 | else |
131 | else |
| 132 | { |
132 | { |
| 133 | Logger &log = Logger::getInstance(); |
133 | Logger &log = Logger::getInstance(); |
| 134 | log.add("CommandPort is not defined, can not start Command console socket\n"); |
134 | log.add("CommandPort is not defined, can not start Command console socket\n"); |
| 135 | } |
135 | } |
| 136 | 136 | ||
| 137 | 137 | ||
| 138 | mySemaphore.lock(); |
138 | mySemaphore.lock(); |
| 139 | 139 | ||
| 140 | while (true) |
140 | while (true) |
| 141 | { |
141 | { |
| 142 | while (myExpressions.size() > 0) |
142 | while (myExpressions.size() > 0) |
| 143 | { |
143 | { |
| 144 | runExpression(myExpressions.pop()); |
144 | runExpression(myExpressions.pop()); |
| 145 | } |
145 | } |
| 146 | 146 | ||
| 147 | mySemaphore.wait(); |
147 | mySemaphore.wait(); |
| 148 | } |
148 | } |
| 149 | 149 | ||
| 150 | mySemaphore.unlock(); |
150 | mySemaphore.unlock(); |
| 151 | } |
151 | } |
| 152 | 152 | ||
| 153 | void VirtualMachine::queueExpression(Expression expression) |
153 | void VirtualMachine::queueExpression(Expression expression) |
| 154 | { |
154 | { |
| 155 | myExpressions.push(expression); |
155 | myExpressions.push(expression); |
| 156 | mySemaphore.broadcast(); |
156 | mySemaphore.broadcast(); |
| 157 | } |
157 | } |
| 158 | 158 | ||
| 159 | string VirtualMachine::formatException(TryCatch* tryCatch) |
159 | string VirtualMachine::formatException(TryCatch* tryCatch) |
| 160 | { |
160 | { |
| 161 | string result; |
161 | string result; |
| 162 | 162 | ||
| 163 | HandleScope handleScope; |
163 | HandleScope handleScope; |
| 164 | String::Utf8Value exception(tryCatch->Exception()); |
164 | String::Utf8Value exception(tryCatch->Exception()); |
| 165 | Handle<v8::Message> message = tryCatch->Message(); |
165 | Handle<v8::Message> message = tryCatch->Message(); |
| 166 | 166 | ||
| 167 | string strException = *exception; |
167 | string strException = *exception; |
| 168 | 168 | ||
| 169 | if (message.IsEmpty()) |
169 | if (message.IsEmpty()) |
| 170 | { |
170 | { |
| 171 | result += strException + "\n"; |
171 | result += strException + "\n"; |
| 172 | } |
172 | } |
| 173 | else |
173 | else |
| 174 | { |
174 | { |
| 175 | String::Utf8Value filename(message->GetScriptResourceName()); |
175 | String::Utf8Value filename(message->GetScriptResourceName()); |
| 176 | string strFilename = *filename; |
176 | string strFilename = *filename; |
| 177 | 177 | ||
| 178 | result += strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n"; |
178 | result += strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n"; |
| 179 | 179 | ||
| 180 | String::Utf8Value sourceline(message->GetSourceLine()); |
180 | String::Utf8Value sourceline(message->GetSourceLine()); |
| 181 | string strSourceline = *sourceline; |
181 | string strSourceline = *sourceline; |
| 182 | 182 | ||
| 183 | result += strSourceline + "\n"; |
183 | result += strSourceline + "\n"; |
| 184 | 184 | ||
| 185 | string underline = rpad("", message->GetStartColumn(), ' '); |
185 | string underline = rpad("", message->GetStartColumn(), ' '); |
| 186 | underline = rpad(underline, message->GetEndColumn(), '^'); |
186 | underline = rpad(underline, message->GetEndColumn(), '^'); |
| 187 | 187 | ||
| 188 | result += underline + "\n"; |
188 | result += underline + "\n"; |
| 189 | } |
189 | } |
| 190 | 190 | ||
| 191 | return result; |
191 | return result; |
| 192 | } |
192 | } |
| 193 | 193 | ||
| 194 | bool VirtualMachine::loadDataStore(string storeName) |
194 | bool VirtualMachine::loadDataStore(string storeName) |
| 195 | { |
195 | { |
| 196 | Logger &log = Logger::getInstance(); |
196 | Logger &log = Logger::getInstance(); |
| 197 | 197 | ||
| 198 | string basePath = Settings::get("BasePath") + "Services/DataStores/"; |
198 | string basePath = Settings::get("BasePath") + "Services/DataStores/"; |
| 199 | string scriptFileName = basePath + storeName + ".js"; |
199 | string scriptFileName = basePath + storeName + ".js"; |
| 200 | 200 | ||
| 201 | if (!file_exists(scriptFileName)) |
201 | if (!file_exists(scriptFileName)) |
| 202 | { |
202 | { |
| 203 | log.add("Failed to load datastore " + scriptFileName + ", file does not exist.\n"); |
203 | log.add("Failed to load datastore " + scriptFileName + ", file does not exist.\n"); |
| 204 | return false; |
204 | return false; |
| 205 | } |
205 | } |
| 206 | 206 | ||
| 207 | string scriptSource = file_get_contents(scriptFileName); |
207 | string scriptSource = file_get_contents(scriptFileName); |
| 208 | 208 | ||
| 209 | scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");"; |
209 | scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");"; |
| 210 | 210 | ||
| 211 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
211 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 212 | Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length()); |
212 | Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length()); |
| 213 | 213 | ||
| 214 | Context::Scope contextScope(myContext); |
214 | Context::Scope contextScope(myContext); |
| 215 | 215 | ||
| 216 | TryCatch tryCatch; |
216 | TryCatch tryCatch; |
| 217 | 217 | ||
| 218 | Handle<Script> script = Script::Compile(source, name); |
218 | Handle<Script> script = Script::Compile(source, name); |
| 219 | 219 | ||
| 220 | if (script.IsEmpty()) |
220 | if (script.IsEmpty()) |
| 221 | { |
221 | { |
| 222 | log.add(formatException(&tryCatch)); |
222 | log.add(formatException(&tryCatch)); |
| 223 | return false; |
223 | return false; |
| 224 | } |
224 | } |
| 225 | else |
225 | else |
| 226 | { |
226 | { |
| 227 | Handle<Value> result = script->Run(); |
227 | Handle<Value> result = script->Run(); |
| 228 | 228 | ||
| 229 | if (result.IsEmpty()) |
229 | if (result.IsEmpty()) |
| 230 | { |
230 | { |
| 231 | log.add(formatException(&tryCatch)); |
231 | log.add(formatException(&tryCatch)); |
| 232 | return false; |
232 | return false; |
| 233 | } |
233 | } |
| 234 | 234 | ||
| 235 | log.add("Loaded datastore " + storeName + "\n"); |
235 | log.add("Loaded datastore " + storeName + "\n"); |
| 236 | } |
236 | } |
| 237 | 237 | ||
| 238 | return true; |
238 | return true; |
| 239 | } |
239 | } |
| 240 | 240 | ||
| 241 | bool VirtualMachine::loadScript(string scriptName) |
241 | bool VirtualMachine::loadScript(string scriptName) |
| 242 | { |
242 | { |
| 243 | Logger &log = Logger::getInstance(); |
243 | Logger &log = Logger::getInstance(); |
| 244 | 244 | ||
| 245 | string basePath = Settings::get("BasePath") + "Services/"; |
245 | string basePath = Settings::get("BasePath") + "Services/"; |
| 246 | string scriptFileName = basePath + scriptName; |
246 | string scriptFileName = basePath + scriptName; |
| 247 | 247 | ||
| 248 | if (!file_exists(scriptFileName)) |
248 | if (!file_exists(scriptFileName)) |
| 249 | { |
249 | { |
| 250 | log.add("Failed to load " + scriptFileName + ", file does not exist.\n"); |
250 | log.add("Failed to load " + scriptFileName + ", file does not exist.\n"); |
| 251 | return false; |
251 | return false; |
| 252 | } |
252 | } |
| 253 | 253 | ||
| 254 | string scriptSource = file_get_contents(scriptFileName); |
254 | string scriptSource = file_get_contents(scriptFileName); |
| 255 | 255 | ||
| 256 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
256 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 257 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
257 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 258 | 258 | ||
| 259 | Context::Scope contextScope(myContext); |
259 | Context::Scope contextScope(myContext); |
| 260 | 260 | ||
| 261 | TryCatch tryCatch; |
261 | TryCatch tryCatch; |
| 262 | 262 | ||
| 263 | Handle<Script> script = Script::Compile(source, name); |
263 | Handle<Script> script = Script::Compile(source, name); |
| 264 | 264 | ||
| 265 | if (script.IsEmpty()) |
265 | if (script.IsEmpty()) |
| 266 | { |
266 | { |
| 267 | log.addToSyslog(formatException(&tryCatch)); |
267 | log.addToSyslog(formatException(&tryCatch)); |
| 268 | return false; |
268 | return false; |
| 269 | } |
269 | } |
| 270 | else |
270 | else |
| 271 | { |
271 | { |
| 272 | Handle<Value> result = script->Run(); |
272 | Handle<Value> result = script->Run(); |
| 273 | 273 | ||
| 274 | if (result.IsEmpty()) |
274 | if (result.IsEmpty()) |
| 275 | { |
275 | { |
| 276 | log.addToSyslog(formatException(&tryCatch)); |
276 | log.addToSyslog(formatException(&tryCatch)); |
| 277 | return false; |
277 | return false; |
| 278 | } |
278 | } |
| 279 | 279 | ||
| 280 | log.add("Loaded " + scriptName + "\n"); |
280 | log.add("Loaded " + scriptName + "\n"); |
| 281 | } |
281 | } |
| 282 | 282 | ||
| 283 | return true; |
283 | return true; |
| 284 | } |
284 | } |
| 285 | 285 | ||
| 286 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
286 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
| 287 | { |
287 | { |
| 288 | IntervalThread *intervalThread = new IntervalThread(timeout); |
288 | IntervalThread *intervalThread = new IntervalThread(timeout); |
| 289 | 289 | ||
| 290 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
290 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
| 291 | myIntervalThreads[intervalThread->getId()]->start(); |
291 | myIntervalThreads[intervalThread->getId()]->start(); |
| 292 | 292 | ||
| 293 | return intervalThread->getId(); |
293 | return intervalThread->getId(); |
| 294 | } |
294 | } |
| 295 | 295 | ||
| 296 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
296 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
| 297 | { |
297 | { |
| 298 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
298 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 299 | { |
299 | { |
| 300 | myIntervalThreads[id]->stop(); |
300 | myIntervalThreads[id]->stop(); |
| 301 | delete myIntervalThreads[id]; |
301 | delete myIntervalThreads[id]; |
| 302 | myIntervalThreads.erase(id); |
302 | myIntervalThreads.erase(id); |
| 303 | 303 | ||
| 304 | return true; |
304 | return true; |
| 305 | } |
305 | } |
| 306 | 306 | ||
| 307 | return false; |
307 | return false; |
| 308 | } |
308 | } |
| 309 | 309 | ||
| 310 | bool VirtualMachine::setIntervalThreadTimeout(unsigned int id, unsigned int timeout) |
310 | bool VirtualMachine::setIntervalThreadTimeout(unsigned int id, unsigned int timeout) |
| 311 | { |
311 | { |
| 312 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
312 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 313 | { |
313 | { |
| 314 | myIntervalThreads[id]->setTimeout(timeout); |
314 | myIntervalThreads[id]->setTimeout(timeout); |
| 315 | return true; |
315 | return true; |
| 316 | } |
316 | } |
| 317 | 317 | ||
| 318 | return false; |
318 | return false; |
| 319 | } |
319 | } |
| 320 | 320 | ||
| 321 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
321 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
| 322 | { |
322 | { |
| 323 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
323 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
| 324 | mySocketThreads[socketThread->getId()] = socketThread; |
324 | mySocketThreads[socketThread->getId()] = socketThread; |
| 325 | mySocketThreads[socketThread->getId()]->start(); |
325 | mySocketThreads[socketThread->getId()]->start(); |
| 326 | mySocketThreads[socketThread->getId()]->startSocket(); |
326 | mySocketThreads[socketThread->getId()]->startSocket(); |
| 327 | 327 | ||
| 328 | return socketThread->getId(); |
328 | return socketThread->getId(); |
| 329 | } |
329 | } |
| 330 | 330 | ||
| 331 | bool VirtualMachine::stopSocketThread(unsigned int id) |
331 | bool VirtualMachine::stopSocketThread(unsigned int id) |
| 332 | { |
332 | { |
| 333 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
333 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 334 | { |
334 | { |
| 335 | delete mySocketThreads[id]; |
335 | delete mySocketThreads[id]; |
| 336 | mySocketThreads.erase(id); |
336 | mySocketThreads.erase(id); |
| 337 | return true; |
337 | return true; |
| 338 | } |
338 | } |
| 339 | 339 | ||
| 340 | return false; |
340 | return false; |
| 341 | } |
341 | } |
| 342 | 342 | ||
| 343 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
343 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
| 344 | { |
344 | { |
| 345 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
345 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 346 | { |
346 | { |
| 347 | mySocketThreads[id]->send(data); |
347 | mySocketThreads[id]->send(data); |
| 348 | } |
348 | } |
| 349 | } |
349 | } |
| 350 | 350 | ||
| 351 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
351 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
| 352 | { |
352 | { |
| 353 | myCommandThread.disconnectClient(id); |
353 | myCommandThread.disconnectClient(id); |
| 354 | } |
354 | } |
| 355 | 355 | ||
| 356 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
356 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
| 357 | { |
357 | { |
| 358 | myCommandThread.sendTo(id, data); |
358 | myCommandThread.sendTo(id, data); |
| 359 | } |
359 | } |
| 360 | 360 | ||
| 361 | void VirtualMachine::print(unsigned int id, string data) |
361 | void VirtualMachine::print(unsigned int id, string data) |
| 362 | { |
362 | { |
| 363 | if (id == 0) |
363 | if (id == 0) |
| 364 | { |
364 | { |
| 365 | Logger &log = Logger::getInstance(); |
365 | Logger &log = Logger::getInstance(); |
| 366 | log.add(data); |
366 | log.add(data); |
| 367 | } |
367 | } |
| 368 | else |
368 | else |
| 369 | { |
369 | { |
| 370 | VirtualMachine &vm = VirtualMachine::getInstance(); |
370 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 371 | vm.sendToCommandClient(id, data); |
371 | vm.sendToCommandClient(id, data); |
| 372 | } |
372 | } |
| 373 | } |
373 | } |
| 374 | 374 | ||
| 375 | bool VirtualMachine::runExpression(Expression expression) |
375 | bool VirtualMachine::runExpression(Expression expression) |
| 376 | { |
376 | { |
| 377 | string scriptName = "runExpression"; |
377 | string scriptName = "runExpression"; |
| 378 | 378 | ||
| 379 | string scriptData = expression.getScript(); |
379 | string scriptData = expression.getScript(); |
| 380 | 380 | ||
| 381 | scriptData = "setClientId(" + itos(expression.getTargetId()) + ");\n" + scriptData + "\n"; |
381 | scriptData = "setClientId(" + itos(expression.getTargetId()) + ");\n" + scriptData + "\n"; |
| 382 | 382 | ||
| 383 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
383 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
| 384 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
384 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 385 | 385 | ||
| 386 | Context::Scope contextScope(myContext); |
386 | Context::Scope contextScope(myContext); |
| 387 | 387 | ||
| 388 | TryCatch tryCatch; |
388 | TryCatch tryCatch; |
| 389 | 389 | ||
| 390 | Handle<Script> script = Script::Compile(source, name); |
390 | Handle<Script> script = Script::Compile(source, name); |
| 391 | 391 | ||
| 392 | if (script.IsEmpty()) |
392 | if (script.IsEmpty()) |
| 393 | { |
393 | { |
| 394 | print(expression.getTargetId(), formatException(&tryCatch)); |
394 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 395 | return false; |
395 | return false; |
| 396 | } |
396 | } |
| 397 | else |
397 | else |
| 398 | { |
398 | { |
| 399 | Handle<Value> result = script->Run(); |
399 | Handle<Value> result = script->Run(); |
| 400 | 400 | ||
| 401 | if (result.IsEmpty()) |
401 | if (result.IsEmpty()) |
| 402 | { |
402 | { |
| 403 | print(expression.getTargetId(), formatException(&tryCatch)); |
403 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 404 | return false; |
404 | return false; |
| 405 | } |
405 | } |
| 406 | else if (expression.getTargetId() != 0) |
406 | else if (expression.getTargetId() != 0) |
| 407 | { |
407 | { |
| 408 | String::AsciiValue ascii(result); |
408 | String::AsciiValue ascii(result); |
| 409 | string resultString = *ascii; |
409 | string resultString = *ascii; |
| 410 | 410 | ||
| 411 | if (resultString != "undefined") |
411 | if (resultString != "undefined") |
| 412 | { |
412 | { |
| 413 | print(expression.getTargetId(), resultString + "\n"); |
413 | print(expression.getTargetId(), resultString + "\n"); |
| 414 | } |
414 | } |
| 415 | } |
415 | } |
| 416 | } |
416 | } |
| 417 | 417 | ||
| 418 | return true; |
418 | return true; |
| 419 | } |
419 | } |
| 420 | 420 | ||
| 421 | 421 | ||
| 422 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
422 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
| 423 | { |
423 | { |
| 424 | VirtualMachine &vm = VirtualMachine::getInstance(); |
424 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 425 | 425 | ||
| 426 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
426 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
| 427 | 427 | ||
| 428 | return Undefined(); |
428 | return Undefined(); |
| 429 | } |
429 | } |
| 430 | 430 | ||
| 431 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
431 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
| 432 | { |
432 | { |
| 433 | Logger &log = Logger::getInstance(); |
433 | Logger &log = Logger::getInstance(); |
| 434 | 434 | ||
| 435 | String::AsciiValue str(args[0]); |
435 | String::AsciiValue str(args[0]); |
| 436 | 436 | ||
| 437 | log.add(*str); |
437 | log.add(*str); |
| 438 | 438 | ||
| 439 | return Undefined(); |
439 | return Undefined(); |
| 440 | } |
440 | } |
| 441 | 441 | ||
| 442 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
442 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
| 443 | { |
443 | { |
| 444 | VirtualMachine &vm = VirtualMachine::getInstance(); |
444 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 445 | 445 | ||
| 446 | String::AsciiValue str(args[1]); |
446 | String::AsciiValue str(args[1]); |
| 447 | 447 | ||
| 448 | vm.print(args[0]->Uint32Value(), *str); |
448 | vm.print(args[0]->Uint32Value(), *str); |
| 449 | 449 | ||
| 450 | return Undefined(); |
450 | return Undefined(); |
| 451 | } |
451 | } |
| 452 | 452 | ||
| 453 | Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args) |
453 | Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args) |
| 454 | { |
454 | { |
| 455 | CanNetManager &canMan = CanNetManager::getInstance(); |
455 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 456 | 456 | ||
| 457 | CanMessage canMessage; |
457 | CanMessage canMessage; |
| 458 | 458 | ||
| 459 | String::AsciiValue className(args[0]); |
459 | String::AsciiValue className(args[0]); |
| 460 | canMessage.setClassName(*className); |
460 | canMessage.setClassName(*className); |
| 461 | String::AsciiValue directionFlag(args[1]); |
461 | String::AsciiValue directionFlag(args[1]); |
| 462 | canMessage.setDirectionFlag(*directionFlag); |
462 | canMessage.setDirectionFlag(*directionFlag); |
| 463 | String::AsciiValue moduleName(args[2]); |
463 | String::AsciiValue moduleName(args[2]); |
| 464 | canMessage.setModuleName(*moduleName); |
464 | canMessage.setModuleName(*moduleName); |
| 465 | canMessage.setModuleId(args[3]->Uint32Value()); |
465 | canMessage.setModuleId(args[3]->Uint32Value()); |
| 466 | String::AsciiValue commandName(args[4]); |
466 | String::AsciiValue commandName(args[4]); |
| 467 | canMessage.setCommandName(*commandName); |
467 | canMessage.setCommandName(*commandName); |
| 468 | 468 | ||
| 469 | String::AsciiValue dataString(args[5]); |
469 | String::AsciiValue dataString(args[5]); |
| 470 | 470 | ||
| 471 | vector<string> parts = explode(",", trim(*dataString, ',')); |
471 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 472 | map<string, CanVariable> data; |
472 | map<string, CanVariable> data; |
| 473 | 473 | ||
| 474 | for (int n = 0; n < parts.size(); n++) |
474 | for (int n = 0; n < parts.size(); n++) |
| 475 | { |
475 | { |
| 476 | vector<string> keyAndValue = explode(":", parts[n]); |
476 | vector<string> keyAndValue = explode(":", parts[n]); |
| - | 477 | ||
| - | 478 | /* decode base64 on the data part */ |
|
| - | 479 | keyAndValue[1] = base64_decode(keyAndValue[1]); |
|
| 477 | 480 | ||
| 478 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
481 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 479 | } |
482 | } |
| 480 | 483 | ||
| 481 | canMessage.setData(data); |
484 | canMessage.setData(data); |
| 482 | 485 | ||
| 483 | canMan.sendMessage(canMessage); |
486 | canMan.sendMessage(canMessage); |
| 484 | 487 | ||
| 485 | return Undefined(); |
488 | return Undefined(); |
| 486 | } |
489 | } |
| 487 | 490 | ||
| 488 | Handle<Value> VirtualMachine::_sendCanNMTMessage(const Arguments& args) |
491 | Handle<Value> VirtualMachine::_sendCanNMTMessage(const Arguments& args) |
| 489 | { |
492 | { |
| 490 | CanNetManager &canMan = CanNetManager::getInstance(); |
493 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 491 | 494 | ||
| 492 | CanMessage canMessage; |
495 | CanMessage canMessage; |
| 493 | 496 | ||
| 494 | String::AsciiValue className(args[0]); |
497 | String::AsciiValue className(args[0]); |
| 495 | canMessage.setClassName(*className); |
498 | canMessage.setClassName(*className); |
| 496 | String::AsciiValue commandName(args[1]); |
499 | String::AsciiValue commandName(args[1]); |
| 497 | canMessage.setCommandName(*commandName); |
500 | canMessage.setCommandName(*commandName); |
| 498 | 501 | ||
| 499 | String::AsciiValue dataString(args[2]); |
502 | String::AsciiValue dataString(args[2]); |
| 500 | 503 | ||
| 501 | vector<string> parts = explode(",", trim(*dataString, ',')); |
504 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 502 | map<string, CanVariable> data; |
505 | map<string, CanVariable> data; |
| 503 | 506 | ||
| 504 | for (int n = 0; n < parts.size(); n++) |
507 | for (int n = 0; n < parts.size(); n++) |
| 505 | { |
508 | { |
| 506 | vector<string> keyAndValue = explode(":", parts[n]); |
509 | vector<string> keyAndValue = explode(":", parts[n]); |
| 507 | 510 | ||
| 508 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
511 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 509 | } |
512 | } |
| 510 | 513 | ||
| 511 | canMessage.setData(data); |
514 | canMessage.setData(data); |
| 512 | 515 | ||
| 513 | canMan.sendMessage(canMessage); |
516 | canMan.sendMessage(canMessage); |
| 514 | 517 | ||
| 515 | return Undefined(); |
518 | return Undefined(); |
| 516 | } |
519 | } |
| 517 | 520 | ||
| 518 | Handle<Value> VirtualMachine::_loadScript(const Arguments& args) |
521 | Handle<Value> VirtualMachine::_loadScript(const Arguments& args) |
| 519 | { |
522 | { |
| 520 | VirtualMachine &vm = VirtualMachine::getInstance(); |
523 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 521 | 524 | ||
| 522 | String::AsciiValue str(args[0]); |
525 | String::AsciiValue str(args[0]); |
| 523 | 526 | ||
| 524 | return Boolean::New(vm.loadScript(*str)); |
527 | return Boolean::New(vm.loadScript(*str)); |
| 525 | } |
528 | } |
| 526 | 529 | ||
| 527 | Handle<Value> VirtualMachine::_loadDataStore(const Arguments& args) |
530 | Handle<Value> VirtualMachine::_loadDataStore(const Arguments& args) |
| 528 | { |
531 | { |
| 529 | VirtualMachine &vm = VirtualMachine::getInstance(); |
532 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 530 | 533 | ||
| 531 | String::AsciiValue str(args[0]); |
534 | String::AsciiValue str(args[0]); |
| 532 | 535 | ||
| 533 | return Boolean::New(vm.loadDataStore(*str)); |
536 | return Boolean::New(vm.loadDataStore(*str)); |
| 534 | } |
537 | } |
| 535 | 538 | ||
| 536 | Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args) |
539 | Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args) |
| 537 | { |
540 | { |
| 538 | VirtualMachine &vm = VirtualMachine::getInstance(); |
541 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 539 | return Integer::New(vm.startIntervalThread(args[0]->Uint32Value())); |
542 | return Integer::New(vm.startIntervalThread(args[0]->Uint32Value())); |
| 540 | } |
543 | } |
| 541 | 544 | ||
| 542 | Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args) |
545 | Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args) |
| 543 | { |
546 | { |
| 544 | VirtualMachine &vm = VirtualMachine::getInstance(); |
547 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 545 | return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value())); |
548 | return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value())); |
| 546 | } |
549 | } |
| 547 | 550 | ||
| 548 | Handle<Value> VirtualMachine::_setIntervalThreadTimeout(const Arguments& args) |
551 | Handle<Value> VirtualMachine::_setIntervalThreadTimeout(const Arguments& args) |
| 549 | { |
552 | { |
| 550 | VirtualMachine &vm = VirtualMachine::getInstance(); |
553 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 551 | return Integer::New(vm.setIntervalThreadTimeout(args[0]->Uint32Value(), args[1]->Uint32Value())); |
554 | return Integer::New(vm.setIntervalThreadTimeout(args[0]->Uint32Value(), args[1]->Uint32Value())); |
| 552 | } |
555 | } |
| 553 | 556 | ||
| 554 | Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args) |
557 | Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args) |
| 555 | { |
558 | { |
| 556 | VirtualMachine &vm = VirtualMachine::getInstance(); |
559 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 557 | 560 | ||
| 558 | String::AsciiValue address(args[0]); |
561 | String::AsciiValue address(args[0]); |
| 559 | 562 | ||
| 560 | return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value())); |
563 | return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value())); |
| 561 | } |
564 | } |
| 562 | 565 | ||
| 563 | Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args) |
566 | Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args) |
| 564 | { |
567 | { |
| 565 | VirtualMachine &vm = VirtualMachine::getInstance(); |
568 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 566 | 569 | ||
| 567 | return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value())); |
570 | return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value())); |
| 568 | } |
571 | } |
| 569 | 572 | ||
| 570 | Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args) |
573 | Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args) |
| 571 | { |
574 | { |
| 572 | VirtualMachine &vm = VirtualMachine::getInstance(); |
575 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 573 | 576 | ||
| 574 | String::AsciiValue data(args[1]); |
577 | String::AsciiValue data(args[1]); |
| 575 | 578 | ||
| 576 | vm.sendToSocketThread(args[0]->Uint32Value(), *data); |
579 | vm.sendToSocketThread(args[0]->Uint32Value(), *data); |
| 577 | 580 | ||
| 578 | return Undefined(); |
581 | return Undefined(); |
| 579 | } |
582 | } |
| 580 | 583 | ||
| 581 | Handle<Value> VirtualMachine::_getFileContents(const Arguments& args) |
584 | Handle<Value> VirtualMachine::_getFileContents(const Arguments& args) |
| 582 | { |
585 | { |
| 583 | String::AsciiValue filename(args[0]); |
586 | String::AsciiValue filename(args[0]); |
| 584 | 587 | ||
| 585 | if (!file_exists(*filename)) |
588 | if (!file_exists(*filename)) |
| 586 | { |
589 | { |
| 587 | return Undefined(); |
590 | return Undefined(); |
| 588 | } |
591 | } |
| 589 | 592 | ||
| 590 | string content = file_get_contents(*filename); |
593 | string content = file_get_contents(*filename); |
| 591 | 594 | ||
| 592 | return String::New(content.c_str()); |
595 | return String::New(content.c_str()); |
| 593 | } |
596 | } |
| 594 | 597 | ||
| 595 | Handle<Value> VirtualMachine::_uint2hex(const Arguments& args) |
598 | Handle<Value> VirtualMachine::_uint2hex(const Arguments& args) |
| 596 | { |
599 | { |
| 597 | string hex = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value()); |
600 | string hex = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value()); |
| 598 | return String::New(hex.c_str()); |
601 | return String::New(hex.c_str()); |
| 599 | } |
602 | } |
| 600 | 603 | ||
| 601 | Handle<Value> VirtualMachine::_hex2bin(const Arguments& args) |
604 | Handle<Value> VirtualMachine::_hex2bin(const Arguments& args) |
| 602 | { |
605 | { |
| 603 | String::AsciiValue hex(args[0]); |
606 | String::AsciiValue hex(args[0]); |
| 604 | string bin = hex2bin(*hex); |
607 | string bin = hex2bin(*hex); |
| 605 | return String::New(bin.c_str()); |
608 | return String::New(bin.c_str()); |
| 606 | } |
609 | } |
| 607 | 610 | ||
| 608 | Handle<Value> VirtualMachine::_bin2hex(const Arguments& args) |
611 | Handle<Value> VirtualMachine::_bin2hex(const Arguments& args) |
| 609 | { |
612 | { |
| 610 | String::AsciiValue bin(args[0]); |
613 | String::AsciiValue bin(args[0]); |
| 611 | string hex = bin2hex(*bin); |
614 | string hex = bin2hex(*bin); |
| 612 | return String::New(hex.c_str()); |
615 | return String::New(hex.c_str()); |
| 613 | } |
616 | } |
| 614 | 617 | ||
| 615 | Handle<Value> VirtualMachine::_bin2float(const Arguments& args) |
618 | Handle<Value> VirtualMachine::_bin2float(const Arguments& args) |
| 616 | { |
619 | { |
| 617 | String::AsciiValue bin(args[0]); |
620 | String::AsciiValue bin(args[0]); |
| 618 | float f = bin2float(*bin); |
621 | float f = bin2float(*bin); |
| 619 | return Number::New(f); |
622 | return Number::New(f); |
| 620 | } |
623 | } |
| 621 | 624 | ||
| 622 | Handle<Value> VirtualMachine::_float2bin(const Arguments& args) |
625 | Handle<Value> VirtualMachine::_float2bin(const Arguments& args) |
| 623 | { |
626 | { |
| 624 | string bin = float2bin(args[0]->NumberValue(), args[1]->Uint32Value()); |
627 | string bin = float2bin(args[0]->NumberValue(), args[1]->Uint32Value()); |
| 625 | return String::New(bin.c_str()); |
628 | return String::New(bin.c_str()); |
| 626 | } |
629 | } |
| 627 | 630 | ||
| 628 | Handle<Value> VirtualMachine::_bin2uint(const Arguments& args) |
631 | Handle<Value> VirtualMachine::_bin2uint(const Arguments& args) |
| 629 | { |
632 | { |
| 630 | String::AsciiValue bin(args[0]); |
633 | String::AsciiValue bin(args[0]); |
| 631 | unsigned int num = bin2uint(*bin); |
634 | unsigned int num = bin2uint(*bin); |
| 632 | return Number::New(num); |
635 | return Number::New(num); |
| 633 | } |
636 | } |
| 634 | 637 | ||
| 635 | Handle<Value> VirtualMachine::_uint2bin(const Arguments& args) |
638 | Handle<Value> VirtualMachine::_uint2bin(const Arguments& args) |
| 636 | { |
639 | { |
| 637 | string bin = uint2bin(args[0]->Uint32Value(), args[1]->Uint32Value()); |
640 | string bin = uint2bin(args[0]->Uint32Value(), args[1]->Uint32Value()); |
| 638 | return String::New(bin.c_str()); |
641 | return String::New(bin.c_str()); |
| 639 | } |
642 | } |
| 640 | 643 | ||
| 641 | Handle<Value> VirtualMachine::_hex2uint(const Arguments& args) |
644 | Handle<Value> VirtualMachine::_hex2uint(const Arguments& args) |
| 642 | { |
645 | { |
| 643 | String::AsciiValue hex(args[0]); |
646 | String::AsciiValue hex(args[0]); |
| 644 | unsigned int num = hex2uint(*hex); |
647 | unsigned int num = hex2uint(*hex); |
| 645 | return Number::New(num); |
648 | return Number::New(num); |
| 646 | } |
649 | } |
| 647 | 650 | ||
| 648 | Handle<Value> VirtualMachine::_sleep(const Arguments& args) |
651 | Handle<Value> VirtualMachine::_sleep(const Arguments& args) |
| 649 | { |
652 | { |
| 650 | usleep(args[0]->Uint32Value()); |
653 | usleep(args[0]->Uint32Value()); |
| 651 | return Undefined(); |
654 | return Undefined(); |
| 652 | } |
655 | } |
| 653 | 656 | ||