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