1 | #include "InterpreterV8.h"
|
---|
2 |
|
---|
3 | #ifdef HAVE_V8
|
---|
4 |
|
---|
5 | #include <fstream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <iomanip>
|
---|
8 |
|
---|
9 | #include <sys/stat.h>
|
---|
10 |
|
---|
11 | #include <boost/tokenizer.hpp>
|
---|
12 | #include <boost/algorithm/string/join.hpp>
|
---|
13 |
|
---|
14 | #ifdef HAVE_NOVA
|
---|
15 | #include "externals/nova.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #ifdef HAVE_SQL
|
---|
19 | #include "Database.h"
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include <v8.h>
|
---|
23 |
|
---|
24 | #include "dim.h"
|
---|
25 | #include "tools.h"
|
---|
26 | #include "Readline.h"
|
---|
27 | #include "externals/izstream.h"
|
---|
28 |
|
---|
29 | #include "WindowLog.h"
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 | using namespace v8;
|
---|
33 |
|
---|
34 | v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateLocal;
|
---|
35 | v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateSky;
|
---|
36 | v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateEvent;
|
---|
37 | v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDescription;
|
---|
38 | //v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase;
|
---|
39 |
|
---|
40 |
|
---|
41 | // ==========================================================================
|
---|
42 | // Some documentation
|
---|
43 | // ==========================================================================
|
---|
44 | //
|
---|
45 | // Threads:
|
---|
46 | // --------
|
---|
47 | // In most cases Js* and other calls to native C++ code could be wrapped
|
---|
48 | // with an Unlocker to allow possible other JavaScipt 'threads' to run
|
---|
49 | // during that time. However, all of these calls should take much less than
|
---|
50 | // the preemption time of 10ms, so it would just be a waste of tim.
|
---|
51 | //
|
---|
52 | // Termination:
|
---|
53 | // ------------
|
---|
54 | // Each thread running V8 code needs to be signalled individually for
|
---|
55 | // termination. Therefor a list of V8 thread ids is created.
|
---|
56 | //
|
---|
57 | // If termination has already be signalled, no thread should start running
|
---|
58 | // anymore (thy could, e.g., wait for their locking). So after locking
|
---|
59 | // it has to be checked if the thread was terminated already. Note
|
---|
60 | // that all calls to Terminate() must be locked to ensure that fThreadId
|
---|
61 | // is correct when it is checked.
|
---|
62 | //
|
---|
63 | // The current thread id must be added to fThreadIds _before_ any
|
---|
64 | // function is called after Locking and before execution is given
|
---|
65 | // back to JavaScript, e.g. in script->Run(). So until the thread
|
---|
66 | // is added to the list Terminate will not be executed. If Terminate
|
---|
67 | // is then executed, it is ensured that the current thread is
|
---|
68 | // already in the list. If terminate has been called before
|
---|
69 | // the Locking, the check for the validiy of fThreadId ensures that
|
---|
70 | // nothing is executed.
|
---|
71 | //
|
---|
72 | // Empty handles:
|
---|
73 | // --------------
|
---|
74 | // If exceution is terminated, V8 calls might return with empty handles,
|
---|
75 | // e.g. Date::New(). Therefore, the returned handles of these calls have to
|
---|
76 | // be checked in all placed to avoid that V8 will core dump.
|
---|
77 | //
|
---|
78 | // HandleScope:
|
---|
79 | // ------------
|
---|
80 | // A handle scope is a garbage collector and collects all handles created
|
---|
81 | // until it goes out of scope. Handles which are not needed anymore are
|
---|
82 | // then deleted. To return a handle from a HandleScope you need to use
|
---|
83 | // Close(). E.g., String::AsciiValue does not create a new handle and
|
---|
84 | // hence does not need a HandleScope. Any ::New will need a handle scope.
|
---|
85 | // Forgetting the HandleScope could in principle fill your memory,
|
---|
86 | // but everything is properly deleted by the global HandleScope at
|
---|
87 | // script termination.
|
---|
88 | //
|
---|
89 | // Here is another good reference for v8, also containing some
|
---|
90 | // good explanations for the meaning of handles, persistent handles
|
---|
91 | // and weak handles: http://create.tpsitulsa.com/wiki/V8_Cookbook
|
---|
92 | //
|
---|
93 | // ==========================================================================
|
---|
94 | // Simple interface
|
---|
95 | // ==========================================================================
|
---|
96 |
|
---|
97 | Handle<Value> InterpreterV8::FuncExit(const Arguments &)
|
---|
98 | {
|
---|
99 | V8::TerminateExecution(fThreadId);
|
---|
100 |
|
---|
101 | // we have to throw an excption to make sure that the
|
---|
102 | // calling thread does not go on executing until it
|
---|
103 | // has realized that it should terminate
|
---|
104 | return ThrowException(Null());
|
---|
105 | }
|
---|
106 |
|
---|
107 | Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
|
---|
108 | {
|
---|
109 | if (args.Length()==0)
|
---|
110 | {
|
---|
111 | // Theoretically, the CPU usage can be reduced by maybe a factor
|
---|
112 | // of four using a larger value, but this also means that the
|
---|
113 | // JavaScript is locked for a longer time.
|
---|
114 | const Unlocker unlock;
|
---|
115 | usleep(1000);
|
---|
116 | return Undefined();
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (args.Length()!=1)
|
---|
120 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
---|
121 |
|
---|
122 | if (!args[0]->IsUint32())
|
---|
123 | return ThrowException(String::New("Argument 1 must be an uint32."));
|
---|
124 |
|
---|
125 | // Using a Javascript function has the advantage that it is fully
|
---|
126 | // interruptable without the need of C++ code
|
---|
127 | const string code =
|
---|
128 | "(function(){"
|
---|
129 | "var t=new Date();"
|
---|
130 | "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") v8.sleep();"
|
---|
131 | "})();";
|
---|
132 |
|
---|
133 | return ExecuteInternal(code);
|
---|
134 | }
|
---|
135 |
|
---|
136 | Handle<Value> InterpreterV8::FuncTimeout(const Arguments &args)
|
---|
137 | {
|
---|
138 | if (args.Length()<2)
|
---|
139 | return ThrowException(String::New("Number of arguments must be at least two."));
|
---|
140 |
|
---|
141 | if (!args[0]->IsNull() && !args[0]->IsInt32())
|
---|
142 | return ThrowException(String::New("Argument 0 not null and not an int32."));
|
---|
143 |
|
---|
144 | if (!args[1]->IsFunction())
|
---|
145 | return ThrowException(String::New("Argument 1 not a function."));
|
---|
146 |
|
---|
147 | if (args.Length()>2 && !args[2]->IsObject())
|
---|
148 | return ThrowException(String::New("Argument 2 not an object."));
|
---|
149 |
|
---|
150 | const int32_t timeout = args[0]->IsNull() ? 0 : args[0]->Int32Value();
|
---|
151 | const bool null = args[0]->IsNull();
|
---|
152 |
|
---|
153 | HandleScope handle_scope;
|
---|
154 |
|
---|
155 | Handle<Function> func = Handle<Function>::Cast(args[1]);
|
---|
156 |
|
---|
157 | const int nn = args.Length()==2 ? 0 : args.Length()-3;
|
---|
158 |
|
---|
159 | Handle<Value> argv[nn];
|
---|
160 | for (int i=0; i<nn; i++)
|
---|
161 | argv[i] = args[i+3];
|
---|
162 |
|
---|
163 | Time t;
|
---|
164 | while (1)
|
---|
165 | {
|
---|
166 | const Handle<Value> rc = args.Length()<3 ? func->Call(func, nn, argv) : func->Call(args[2]->ToObject(), nn, argv);
|
---|
167 | if (rc.IsEmpty())
|
---|
168 | return Undefined();
|
---|
169 |
|
---|
170 | if (!rc->IsUndefined())
|
---|
171 | return handle_scope.Close(rc);
|
---|
172 |
|
---|
173 | if (!null && Time()-t>=boost::posix_time::milliseconds(abs(timeout)))
|
---|
174 | break;
|
---|
175 |
|
---|
176 | // Theoretically, the CPU usage can be reduced by maybe a factor
|
---|
177 | // of four using a larger value, but this also means that the
|
---|
178 | // JavaScript is locked for a longer time.
|
---|
179 | const Unlocker unlock;
|
---|
180 | usleep(1000);
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (timeout<0)
|
---|
184 | return Undefined();
|
---|
185 |
|
---|
186 | const string str = "Waiting for func to return a defined value timed out.";
|
---|
187 | return ThrowException(String::New(str.c_str()));
|
---|
188 | }
|
---|
189 |
|
---|
190 | void InterpreterV8::Thread(int &id, Persistent<Object> _this, Persistent<Function> func, uint32_t ms)
|
---|
191 | {
|
---|
192 | const Locker lock;
|
---|
193 |
|
---|
194 | if (fThreadId<0)
|
---|
195 | {
|
---|
196 | id = -1;
|
---|
197 | return;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // Warning: As soon as id is set, the parent of this thread might terminate
|
---|
201 | // and hance the reference to id does not exist anymore. So, id
|
---|
202 | // is just a kind of return value and must not be used at all
|
---|
203 | // otherwise.
|
---|
204 |
|
---|
205 | const int id_local = V8::GetCurrentThreadId();
|
---|
206 | id = id_local;
|
---|
207 | fThreadIds.insert(id_local);
|
---|
208 |
|
---|
209 | const HandleScope handle_scope;
|
---|
210 |
|
---|
211 | func->CreationContext()->Enter();
|
---|
212 |
|
---|
213 | TryCatch exception;
|
---|
214 |
|
---|
215 | const bool rc = ms==0 || !ExecuteInternal("v8.sleep("+to_string(ms)+");").IsEmpty();
|
---|
216 | if (rc)
|
---|
217 | {
|
---|
218 | if (_this.IsEmpty())
|
---|
219 | func->Call(func, 0, NULL);
|
---|
220 | else
|
---|
221 | func->Call(_this, 0, NULL);
|
---|
222 | }
|
---|
223 |
|
---|
224 | func.Dispose();
|
---|
225 | _this.Dispose();
|
---|
226 |
|
---|
227 | fThreadIds.erase(id_local);
|
---|
228 |
|
---|
229 | if (!HandleException(exception, "thread"))
|
---|
230 | V8::TerminateExecution(fThreadId);
|
---|
231 |
|
---|
232 | func->CreationContext()->Exit();
|
---|
233 | }
|
---|
234 |
|
---|
235 | Handle<Value> InterpreterV8::FuncThread(const Arguments& args)
|
---|
236 | {
|
---|
237 | if (!args.IsConstructCall())
|
---|
238 | return ThrowException(String::New("Thread must be called as constructor."));
|
---|
239 |
|
---|
240 | if (args.Length()!=2 && args.Length()!=3)
|
---|
241 | return ThrowException(String::New("Number of arguments must be two or three."));
|
---|
242 |
|
---|
243 | if (!args[0]->IsUint32())
|
---|
244 | return ThrowException(String::New("Argument 0 not an uint32."));
|
---|
245 |
|
---|
246 | if (!args[1]->IsFunction())
|
---|
247 | return ThrowException(String::New("Argument 1 not a function."));
|
---|
248 |
|
---|
249 | if (args.Length()==3 && !args[2]->IsObject())
|
---|
250 | return ThrowException(String::New("Argument 2 not an object."));
|
---|
251 |
|
---|
252 | //if (!args.IsConstructCall())
|
---|
253 | // return Constructor(args);
|
---|
254 |
|
---|
255 | const HandleScope handle_scope;
|
---|
256 |
|
---|
257 | Handle<Function> handle = Handle<Function>::Cast(args[1]);
|
---|
258 |
|
---|
259 | Persistent<Function> func = Persistent<Function>::New(handle);
|
---|
260 | Persistent<Object> _this;
|
---|
261 | if (args.Length()==3)
|
---|
262 | _this = Persistent<Object>::New(args[2]->ToObject());
|
---|
263 |
|
---|
264 | const uint32_t ms = args[0]->Uint32Value();
|
---|
265 |
|
---|
266 | int id=-2;
|
---|
267 | fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), _this, func, ms)));
|
---|
268 | {
|
---|
269 | // Allow the thread to lock, so we can get the thread id.
|
---|
270 | const Unlocker unlock;
|
---|
271 | while (id==-2)
|
---|
272 | usleep(1);
|
---|
273 | }
|
---|
274 |
|
---|
275 | Handle<Object> self = args.This();
|
---|
276 |
|
---|
277 | self->Set(String::New("id"), Integer::NewFromUnsigned(id), ReadOnly);
|
---|
278 | self->Set(String::New("kill"), FunctionTemplate::New(WrapKill)->GetFunction(), ReadOnly);
|
---|
279 |
|
---|
280 | return Undefined();
|
---|
281 | }
|
---|
282 |
|
---|
283 | Handle<Value> InterpreterV8::FuncKill(const Arguments& args)
|
---|
284 | {
|
---|
285 | const uint32_t id = args.This()->Get(String::New("id"))->Uint32Value();
|
---|
286 |
|
---|
287 | V8::TerminateExecution(id);
|
---|
288 |
|
---|
289 | return Boolean::New(fThreadIds.erase(id));
|
---|
290 | }
|
---|
291 |
|
---|
292 | Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
|
---|
293 | {
|
---|
294 | if (args.Length()==0)
|
---|
295 | return ThrowException(String::New("Number of arguments must be at least 1."));
|
---|
296 |
|
---|
297 | if (!args[0]->IsString())
|
---|
298 | return ThrowException(String::New("Argument 1 must be a string."));
|
---|
299 |
|
---|
300 | const String::AsciiValue str(args[0]);
|
---|
301 |
|
---|
302 | string command = *str;
|
---|
303 |
|
---|
304 | if (command.length()==0)
|
---|
305 | return ThrowException(String::New("Server name empty."));
|
---|
306 |
|
---|
307 | if (args.Length()==0)
|
---|
308 | {
|
---|
309 | if (command.find_first_of('/')==string::npos)
|
---|
310 | command += "/";
|
---|
311 | }
|
---|
312 |
|
---|
313 | // Escape all string arguments. All others can be kept as they are.
|
---|
314 | for (int i=1; i<args.Length(); i++)
|
---|
315 | {
|
---|
316 | string arg = *String::AsciiValue(args[i]);
|
---|
317 |
|
---|
318 | // Escape string
|
---|
319 | if (args[i]->IsString())
|
---|
320 | {
|
---|
321 | boost::replace_all(arg, "\\", "\\\\");
|
---|
322 | boost::replace_all(arg, "'", "\\'");
|
---|
323 | boost::replace_all(arg, "\"", "\\\"");
|
---|
324 | }
|
---|
325 |
|
---|
326 | command += " "+arg;
|
---|
327 | }
|
---|
328 |
|
---|
329 | try
|
---|
330 | {
|
---|
331 | return Boolean::New(JsSend(command));
|
---|
332 | }
|
---|
333 | catch (const runtime_error &e)
|
---|
334 | {
|
---|
335 | return ThrowException(String::New(e.what()));
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | // ==========================================================================
|
---|
340 | // State control
|
---|
341 | // ==========================================================================
|
---|
342 |
|
---|
343 | Handle<Value> InterpreterV8::FuncWait(const Arguments& args)
|
---|
344 | {
|
---|
345 | if (args.Length()!=2 && args.Length()!=3)
|
---|
346 | return ThrowException(String::New("Number of arguments must be 2 or 3."));
|
---|
347 |
|
---|
348 | if (!args[0]->IsString())
|
---|
349 | return ThrowException(String::New("Argument 1 not a string."));
|
---|
350 |
|
---|
351 | if (!args[1]->IsInt32() && !args[1]->IsString())
|
---|
352 | return ThrowException(String::New("Argument 2 not an int32 and not a string."));
|
---|
353 |
|
---|
354 | if (args.Length()==3 && !args[2]->IsInt32() && !args[2]->IsUndefined())
|
---|
355 | return ThrowException(String::New("Argument 3 not an int32 and not undefined."));
|
---|
356 |
|
---|
357 | // Using a Javascript function has the advantage that it is fully
|
---|
358 | // interruptable without the need of C++ code
|
---|
359 |
|
---|
360 | const string index = args[1]->IsInt32() ? "s.index" : "s.name";
|
---|
361 | const bool timeout = args.Length()==3 && !args[2]->IsUndefined();
|
---|
362 | const string arg0 = *String::AsciiValue(args[0]);
|
---|
363 | const string state = args[1]->IsString() ? *String::AsciiValue(args[1]) : "";
|
---|
364 | const string arg1 = args[1]->IsString() ? ("\""+state+"\"") : to_string(args[1]->Int32Value());
|
---|
365 | const bool isNot = arg0[0]=='!';
|
---|
366 | const string name = isNot ? arg0.substr(1) : arg0;
|
---|
367 |
|
---|
368 | if (arg0.find_first_of("\"'")!=string::npos)
|
---|
369 | return ThrowException(String::New("Server name must not contain quotation marks."));
|
---|
370 |
|
---|
371 | if (args[1]->IsString())
|
---|
372 | if (state.find_first_of("\"'")!=string::npos)
|
---|
373 | return ThrowException(String::New("State name must not contain quotation marks."));
|
---|
374 |
|
---|
375 | string code = "(function(name,state,ms)"
|
---|
376 | "{";
|
---|
377 | if (timeout)
|
---|
378 | code += "var t = new Date();";
|
---|
379 | code += "var s;"
|
---|
380 | "while (1)"
|
---|
381 | "{"
|
---|
382 | "s = dim.state(name);"
|
---|
383 | "if(!s)throw new Error('Waiting for state "+arg1+" of server "+arg0+" failed.');";
|
---|
384 | if (isNot)
|
---|
385 | code +=
|
---|
386 | "if(state!="+index+")return true;";
|
---|
387 | else
|
---|
388 | code +=
|
---|
389 | "if(state=="+index+")return true;";
|
---|
390 | if (timeout)
|
---|
391 | code += "if((new Date()-t)>Math.abs(ms))break;";
|
---|
392 |
|
---|
393 | code += "v8.sleep();"
|
---|
394 | "}";
|
---|
395 | if (timeout)
|
---|
396 | code += "if(ms>0)throw new Error('Waiting for state "+arg1+" of server "+arg0+" ['+"+index+"+'] timed out.');";
|
---|
397 | code += "return false;"
|
---|
398 | "})('"+name+"',"+arg1;
|
---|
399 | if (timeout)
|
---|
400 | code += "," + (args[2]->IsUndefined()?"undefined":to_string(args[2]->Int32Value()));
|
---|
401 | code += ");";
|
---|
402 |
|
---|
403 | return ExecuteInternal(code);
|
---|
404 | }
|
---|
405 |
|
---|
406 | Handle<Value> InterpreterV8::FuncState(const Arguments& args)
|
---|
407 | {
|
---|
408 | if (args.Length()!=1)
|
---|
409 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
---|
410 |
|
---|
411 | if (!args[0]->IsString())
|
---|
412 | return ThrowException(String::New("Argument 1 must be a string."));
|
---|
413 |
|
---|
414 | // Return state.name/state.index
|
---|
415 |
|
---|
416 | const String::AsciiValue str(args[0]);
|
---|
417 |
|
---|
418 | const State rc = JsState(*str);
|
---|
419 | if (rc.index<=-256)
|
---|
420 | return Undefined();
|
---|
421 |
|
---|
422 | HandleScope handle_scope;
|
---|
423 |
|
---|
424 | Handle<Object> obj = Object::New();
|
---|
425 |
|
---|
426 | obj->Set(String::New("server"), String::New(*str), ReadOnly);
|
---|
427 | obj->Set(String::New("index"), Integer::New(rc.index), ReadOnly);
|
---|
428 | obj->Set(String::New("name"), String::New(rc.name.c_str()), ReadOnly);
|
---|
429 |
|
---|
430 | const Local<Value> date = Date::New(rc.time.JavaDate());
|
---|
431 | if (rc.index>-256 && !date.IsEmpty())
|
---|
432 | obj->Set(String::New("time"), date);
|
---|
433 |
|
---|
434 | return handle_scope.Close(obj);
|
---|
435 | }
|
---|
436 |
|
---|
437 | Handle<Value> InterpreterV8::FuncNewState(const Arguments& args)
|
---|
438 | {
|
---|
439 | if (args.Length()<1 || args.Length()>3)
|
---|
440 | return ThrowException(String::New("Number of arguments must be 1, 2 or 3."));
|
---|
441 |
|
---|
442 | if (!args[0]->IsUint32())
|
---|
443 | return ThrowException(String::New("Argument 1 must be an uint32."));
|
---|
444 | if (args.Length()>1 && !args[1]->IsString())
|
---|
445 | return ThrowException(String::New("Argument 2 must be a string."));
|
---|
446 | if (args.Length()>2 && !args[2]->IsString())
|
---|
447 | return ThrowException(String::New("Argument 3 must be a string."));
|
---|
448 |
|
---|
449 | const uint32_t index = args[0]->Int32Value();
|
---|
450 | const string name = *String::AsciiValue(args[1]);
|
---|
451 | const string comment = *String::AsciiValue(args[2]);
|
---|
452 |
|
---|
453 | if (index<10 || index>255)
|
---|
454 | return ThrowException(String::New("State must be in the range [10, 255]."));
|
---|
455 |
|
---|
456 | if (name.empty())
|
---|
457 | return ThrowException(String::New("State name must not be empty."));
|
---|
458 |
|
---|
459 | if (name.find_first_of(':')!=string::npos || name.find_first_of('=')!=string::npos)
|
---|
460 | return ThrowException(String::New("State name must not contain : or =."));
|
---|
461 |
|
---|
462 | struct Find : State
|
---|
463 | {
|
---|
464 | Find(int idx, const string &n) : State(idx, n) { }
|
---|
465 | bool operator()(const pair<int, string> &p) { return index==p.first || name==p.second; }
|
---|
466 | };
|
---|
467 |
|
---|
468 | if (find_if(fStates.begin(), fStates.end(), Find(index, name))!=fStates.end())
|
---|
469 | {
|
---|
470 | const string what =
|
---|
471 | "State index ["+to_string(index)+"] or name ["+name+"] already defined.";
|
---|
472 |
|
---|
473 | return ThrowException(String::New(what.c_str()));
|
---|
474 | }
|
---|
475 |
|
---|
476 | return Boolean::New(JsNewState(index, name, comment));
|
---|
477 | }
|
---|
478 |
|
---|
479 | Handle<Value> InterpreterV8::FuncSetState(const Arguments& args)
|
---|
480 | {
|
---|
481 | if (args.Length()!=1)
|
---|
482 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
---|
483 |
|
---|
484 | if (!args[0]->IsUint32() && !args[0]->IsString())
|
---|
485 | return ThrowException(String::New("Argument must be an uint32 or a string."));
|
---|
486 |
|
---|
487 | int index = -2;
|
---|
488 | if (args[0]->IsUint32())
|
---|
489 | {
|
---|
490 | index = args[0]->Int32Value();
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | const string name = *String::AsciiValue(args[0]);
|
---|
495 | index = JsGetState(name);
|
---|
496 | if (index==-2)
|
---|
497 | return ThrowException(String::New(("State '"+name+"' not found.").c_str()));
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (index<10 || index>255)
|
---|
501 | return ThrowException(String::New("State must be in the range [10, 255]."));
|
---|
502 |
|
---|
503 | return Boolean::New(JsSetState(index));
|
---|
504 | }
|
---|
505 |
|
---|
506 | Handle<Value> InterpreterV8::FuncGetState(const Arguments& args)
|
---|
507 | {
|
---|
508 | if (args.Length()>0)
|
---|
509 | return ThrowException(String::New("getState must not take arguments."));
|
---|
510 |
|
---|
511 | const State state = JsGetCurrentState();
|
---|
512 |
|
---|
513 | HandleScope handle_scope;
|
---|
514 |
|
---|
515 | Handle<Object> rc = Object::New();
|
---|
516 | if (rc.IsEmpty())
|
---|
517 | return Undefined();
|
---|
518 |
|
---|
519 | rc->Set(String::New("index"), Integer::New(state.index), ReadOnly);
|
---|
520 | rc->Set(String::New("name"), String::New(state.name.c_str()), ReadOnly);
|
---|
521 | rc->Set(String::New("description"), String::New(state.comment.c_str()), ReadOnly);
|
---|
522 |
|
---|
523 | return handle_scope.Close(rc);
|
---|
524 | }
|
---|
525 |
|
---|
526 | Handle<Value> InterpreterV8::FuncGetStates(const Arguments& args)
|
---|
527 | {
|
---|
528 | if (args.Length()>1)
|
---|
529 | return ThrowException(String::New("getStates must not take more than one arguments."));
|
---|
530 |
|
---|
531 | if (args.Length()==1 && !args[0]->IsString())
|
---|
532 | return ThrowException(String::New("Argument must be a string."));
|
---|
533 |
|
---|
534 | const string server = args.Length()==1 ? *String::AsciiValue(args[0]) : "DIM_CONTROL";
|
---|
535 |
|
---|
536 | const vector<State> states = JsGetStates(server);
|
---|
537 |
|
---|
538 | HandleScope handle_scope;
|
---|
539 |
|
---|
540 | Handle<Object> list = Object::New();
|
---|
541 | if (list.IsEmpty())
|
---|
542 | return Undefined();
|
---|
543 |
|
---|
544 | for (auto it=states.begin(); it!=states.end(); it++)
|
---|
545 | {
|
---|
546 | Handle<Value> entry = StringObject::New(String::New(it->name.c_str()));
|
---|
547 | if (entry.IsEmpty())
|
---|
548 | return Undefined();
|
---|
549 |
|
---|
550 | StringObject::Cast(*entry)->Set(String::New("description"), String::New(it->comment.c_str()), ReadOnly);
|
---|
551 | list->Set(Integer::New(it->index), entry, ReadOnly);
|
---|
552 | }
|
---|
553 |
|
---|
554 | return handle_scope.Close(list);
|
---|
555 | }
|
---|
556 |
|
---|
557 | Handle<Value> InterpreterV8::FuncGetDescription(const Arguments& args)
|
---|
558 | {
|
---|
559 | if (args.Length()!=1)
|
---|
560 | return ThrowException(String::New("getDescription must take exactly one argument."));
|
---|
561 |
|
---|
562 | if (args.Length()==1 && !args[0]->IsString())
|
---|
563 | return ThrowException(String::New("Argument must be a string."));
|
---|
564 |
|
---|
565 | const string service = *String::AsciiValue(args[0]);
|
---|
566 |
|
---|
567 | const vector<Description> descriptions = JsGetDescription(service);
|
---|
568 | const set<Service> services = JsGetServices();
|
---|
569 |
|
---|
570 | auto is=services.begin();
|
---|
571 | for (; is!=services.end(); is++)
|
---|
572 | if (is->name==service)
|
---|
573 | break;
|
---|
574 |
|
---|
575 | if (is==services.end())
|
---|
576 | return Undefined();
|
---|
577 |
|
---|
578 | HandleScope handle_scope;
|
---|
579 |
|
---|
580 | Handle<Object> arr = fTemplateDescription->GetFunction()->NewInstance();//Object::New();
|
---|
581 | if (arr.IsEmpty())
|
---|
582 | return Undefined();
|
---|
583 |
|
---|
584 | auto it=descriptions.begin();
|
---|
585 | arr->Set(String::New("name"), String::New(it->name.c_str()), ReadOnly);
|
---|
586 | if (!it->comment.empty())
|
---|
587 | arr->Set(String::New("description"), String::New(it->comment.c_str()), ReadOnly);
|
---|
588 | if (is!=services.end())
|
---|
589 | {
|
---|
590 | arr->Set(String::New("server"), String::New(is->server.c_str()), ReadOnly);
|
---|
591 | arr->Set(String::New("service"), String::New(is->service.c_str()), ReadOnly);
|
---|
592 | arr->Set(String::New("isCommand"), Boolean::New(is->iscmd), ReadOnly);
|
---|
593 | if (!is->format.empty())
|
---|
594 | arr->Set(String::New("format"), String::New(is->format.c_str()), ReadOnly);
|
---|
595 | }
|
---|
596 |
|
---|
597 | uint32_t i=0;
|
---|
598 | for (it++; it!=descriptions.end(); it++)
|
---|
599 | {
|
---|
600 | Handle<Object> obj = Object::New();
|
---|
601 | if (obj.IsEmpty())
|
---|
602 | return Undefined();
|
---|
603 |
|
---|
604 | if (!it->name.empty())
|
---|
605 | obj->Set(String::New("name"), String::New(it->name.c_str()), ReadOnly);
|
---|
606 | if (!it->comment.empty())
|
---|
607 | obj->Set(String::New("description"), String::New(it->comment.c_str()), ReadOnly);
|
---|
608 | if (!it->unit.empty())
|
---|
609 | obj->Set(String::New("unit"), String::New(it->unit.c_str()), ReadOnly);
|
---|
610 |
|
---|
611 | arr->Set(i++, obj);
|
---|
612 | }
|
---|
613 |
|
---|
614 | return handle_scope.Close(arr);
|
---|
615 | }
|
---|
616 |
|
---|
617 | Handle<Value> InterpreterV8::FuncGetServices(const Arguments& args)
|
---|
618 | {
|
---|
619 | if (args.Length()>2)
|
---|
620 | return ThrowException(String::New("getServices must not take more than two argument."));
|
---|
621 |
|
---|
622 | if (args.Length()>=1 && !args[0]->IsString())
|
---|
623 | return ThrowException(String::New("First argument must be a string."));
|
---|
624 |
|
---|
625 | if (args.Length()==2 && !args[1]->IsBoolean())
|
---|
626 | return ThrowException(String::New("Second argument must be a boolean."));
|
---|
627 |
|
---|
628 | string arg0 = args.Length() ? *String::AsciiValue(args[0]) : "";
|
---|
629 | if (arg0=="*")
|
---|
630 | arg0=="";
|
---|
631 |
|
---|
632 | const set<Service> services = JsGetServices();
|
---|
633 |
|
---|
634 | HandleScope handle_scope;
|
---|
635 |
|
---|
636 | Handle<Array> arr = Array::New();
|
---|
637 | if (arr.IsEmpty())
|
---|
638 | return Undefined();
|
---|
639 |
|
---|
640 | uint32_t i=0;
|
---|
641 | for (auto is=services.begin(); is!=services.end(); is++)
|
---|
642 | {
|
---|
643 | if (!arg0.empty() && is->name.find(arg0)!=0)
|
---|
644 | continue;
|
---|
645 |
|
---|
646 | if (args.Length()==2 && args[1]->BooleanValue()!=is->iscmd)
|
---|
647 | continue;
|
---|
648 |
|
---|
649 | Handle<Object> obj = Object::New();
|
---|
650 | if (obj.IsEmpty())
|
---|
651 | return Undefined();
|
---|
652 |
|
---|
653 | obj->Set(String::New("name"), String::New(is->name.c_str()), ReadOnly);
|
---|
654 | obj->Set(String::New("server"), String::New(is->server.c_str()), ReadOnly);
|
---|
655 | obj->Set(String::New("service"), String::New(is->service.c_str()), ReadOnly);
|
---|
656 | obj->Set(String::New("isCommand"), Boolean::New(is->iscmd), ReadOnly);
|
---|
657 | if (!is->format.empty())
|
---|
658 | obj->Set(String::New("format"), String::New(is->format.c_str()), ReadOnly);
|
---|
659 |
|
---|
660 | arr->Set(i++, obj);
|
---|
661 | }
|
---|
662 |
|
---|
663 | return handle_scope.Close(arr);
|
---|
664 | }
|
---|
665 |
|
---|
666 | // ==========================================================================
|
---|
667 | // Internal functions
|
---|
668 | // ==========================================================================
|
---|
669 |
|
---|
670 |
|
---|
671 | // The callback that is invoked by v8 whenever the JavaScript 'print'
|
---|
672 | // function is called. Prints its arguments on stdout separated by
|
---|
673 | // spaces and ending with a newline.
|
---|
674 | Handle<Value> InterpreterV8::FuncLog(const Arguments& args)
|
---|
675 | {
|
---|
676 | for (int i=0; i<args.Length(); i++)
|
---|
677 | {
|
---|
678 | const String::AsciiValue str(args[i]);
|
---|
679 | if (*str)
|
---|
680 | JsPrint(*str);
|
---|
681 | }
|
---|
682 |
|
---|
683 | if (args.Length()==0)
|
---|
684 | JsPrint();
|
---|
685 |
|
---|
686 | return Undefined();
|
---|
687 | }
|
---|
688 |
|
---|
689 | Handle<Value> InterpreterV8::FuncAlarm(const Arguments& args)
|
---|
690 | {
|
---|
691 | for (int i=0; i<args.Length(); i++)
|
---|
692 | {
|
---|
693 | const String::AsciiValue str(args[i]);
|
---|
694 | if (*str)
|
---|
695 | JsAlarm(*str);
|
---|
696 | }
|
---|
697 |
|
---|
698 | if (args.Length()==0)
|
---|
699 | JsAlarm();
|
---|
700 |
|
---|
701 | return Undefined();
|
---|
702 | }
|
---|
703 |
|
---|
704 | Handle<Value> InterpreterV8::FuncOut(const Arguments& args)
|
---|
705 | {
|
---|
706 | for (int i=0; i<args.Length(); i++)
|
---|
707 | {
|
---|
708 | const String::AsciiValue str(args[i]);
|
---|
709 | if (*str)
|
---|
710 | JsOut(*str);
|
---|
711 | }
|
---|
712 | return Undefined();
|
---|
713 | }
|
---|
714 |
|
---|
715 | Handle<Value> InterpreterV8::FuncWarn(const Arguments& args)
|
---|
716 | {
|
---|
717 | for (int i=0; i<args.Length(); i++)
|
---|
718 | {
|
---|
719 | const String::AsciiValue str(args[i]);
|
---|
720 | if (*str)
|
---|
721 | JsWarn(*str);
|
---|
722 | }
|
---|
723 | return Undefined();
|
---|
724 | }
|
---|
725 |
|
---|
726 | // The callback that is invoked by v8 whenever the JavaScript 'load'
|
---|
727 | // function is called. Loads, compiles and executes its argument
|
---|
728 | // JavaScript file.
|
---|
729 | Handle<Value> InterpreterV8::FuncInclude(const Arguments& args)
|
---|
730 | {
|
---|
731 | if (args.Length()!=1)
|
---|
732 | return ThrowException(String::New("Number of arguments must be one."));
|
---|
733 |
|
---|
734 | if (!args[0]->IsString())
|
---|
735 | return ThrowException(String::New("Argument must be a string."));
|
---|
736 |
|
---|
737 | const String::AsciiValue file(args[0]);
|
---|
738 | if (*file == NULL)
|
---|
739 | return ThrowException(String::New("File name missing."));
|
---|
740 |
|
---|
741 | if (strlen(*file)==0)
|
---|
742 | return ThrowException(String::New("File name empty."));
|
---|
743 |
|
---|
744 | izstream fin(*file);
|
---|
745 | if (!fin)
|
---|
746 | return ThrowException(String::New(errno!=0?strerror(errno):"Insufficient memory for decompression"));
|
---|
747 |
|
---|
748 | string buffer;
|
---|
749 | getline(fin, buffer, '\0');
|
---|
750 |
|
---|
751 | if ((fin.fail() && !fin.eof()) || fin.bad())
|
---|
752 | return ThrowException(String::New(strerror(errno)));
|
---|
753 |
|
---|
754 | if (buffer.length()>1 && buffer[0]=='#' && buffer[1]=='!')
|
---|
755 | buffer.insert(0, "//");
|
---|
756 |
|
---|
757 | return ExecuteCode(buffer, *file);
|
---|
758 | }
|
---|
759 |
|
---|
760 | Handle<Value> InterpreterV8::FuncFile(const Arguments& args)
|
---|
761 | {
|
---|
762 | if (args.Length()!=1 && args.Length()!=2)
|
---|
763 | return ThrowException(String::New("Number of arguments must be one or two."));
|
---|
764 |
|
---|
765 | const String::AsciiValue file(args[0]);
|
---|
766 | if (*file == NULL)
|
---|
767 | return ThrowException(String::New("File name missing"));
|
---|
768 |
|
---|
769 | if (args.Length()==2 && !args[1]->IsString())
|
---|
770 | return ThrowException(String::New("Second argument must be a string."));
|
---|
771 |
|
---|
772 | const string delim = args.Length()==2 ? *String::AsciiValue(args[1]) : "";
|
---|
773 |
|
---|
774 | if (args.Length()==2 && delim.size()!=1)
|
---|
775 | return ThrowException(String::New("Second argument must be a string of length 1."));
|
---|
776 |
|
---|
777 | HandleScope handle_scope;
|
---|
778 |
|
---|
779 | izstream fin(*file);
|
---|
780 | if (!fin)
|
---|
781 | return ThrowException(String::New(errno!=0?strerror(errno):"Insufficient memory for decompression"));
|
---|
782 |
|
---|
783 | if (args.Length()==1)
|
---|
784 | {
|
---|
785 | string buffer;
|
---|
786 | getline(fin, buffer, '\0');
|
---|
787 | if ((fin.fail() && !fin.eof()) || fin.bad())
|
---|
788 | return ThrowException(String::New(strerror(errno)));
|
---|
789 |
|
---|
790 | Handle<Value> str = StringObject::New(String::New(buffer.c_str()));
|
---|
791 | StringObject::Cast(*str)->Set(String::New("name"), String::New(*file));
|
---|
792 | return handle_scope.Close(str);
|
---|
793 | }
|
---|
794 |
|
---|
795 | Handle<Array> arr = Array::New();
|
---|
796 | if (arr.IsEmpty())
|
---|
797 | return Undefined();
|
---|
798 |
|
---|
799 | int i=0;
|
---|
800 | string buffer;
|
---|
801 | while (getline(fin, buffer, delim[0]))
|
---|
802 | arr->Set(i++, String::New(buffer.c_str()));
|
---|
803 |
|
---|
804 | if ((fin.fail() && !fin.eof()) || fin.bad())
|
---|
805 | return ThrowException(String::New(strerror(errno)));
|
---|
806 |
|
---|
807 | arr->Set(String::New("name"), String::New(*file));
|
---|
808 | arr->Set(String::New("delim"), String::New(delim.c_str(), 1));
|
---|
809 |
|
---|
810 | return handle_scope.Close(arr);
|
---|
811 | }
|
---|
812 |
|
---|
813 | // ==========================================================================
|
---|
814 | // Mail
|
---|
815 | // ==========================================================================
|
---|
816 |
|
---|
817 | Handle<Value> InterpreterV8::ConstructorMail(const Arguments &args)
|
---|
818 | {
|
---|
819 | if (!args.IsConstructCall())
|
---|
820 | return ThrowException(String::New("Mail must be called as constructor"));
|
---|
821 |
|
---|
822 | if (args.Length()!=1 || !args[0]->IsString())
|
---|
823 | return ThrowException(String::New("Constructor must be called with a single string as argument"));
|
---|
824 |
|
---|
825 | HandleScope handle_scope;
|
---|
826 |
|
---|
827 | Handle<Array> rec = Array::New();
|
---|
828 | Handle<Array> att = Array::New();
|
---|
829 | Handle<Array> bcc = Array::New();
|
---|
830 | Handle<Array> cc = Array::New();
|
---|
831 | Handle<Array> txt = Array::New();
|
---|
832 | if (rec.IsEmpty() || att.IsEmpty() || bcc.IsEmpty() || cc.IsEmpty() || txt.IsEmpty())
|
---|
833 | return Undefined();
|
---|
834 |
|
---|
835 | Handle<Object> self = args.This();
|
---|
836 |
|
---|
837 | self->Set(String::New("subject"), args[0]->ToString(), ReadOnly);
|
---|
838 | self->Set(String::New("recipients"), rec, ReadOnly);
|
---|
839 | self->Set(String::New("attachments"), att, ReadOnly);
|
---|
840 | self->Set(String::New("bcc"), bcc, ReadOnly);
|
---|
841 | self->Set(String::New("cc"), cc, ReadOnly);
|
---|
842 | self->Set(String::New("text"), txt, ReadOnly);
|
---|
843 |
|
---|
844 | self->Set(String::New("send"), FunctionTemplate::New(WrapSendMail)->GetFunction(), ReadOnly);
|
---|
845 |
|
---|
846 | return handle_scope.Close(self);
|
---|
847 | }
|
---|
848 |
|
---|
849 | vector<string> InterpreterV8::ValueToArray(const Handle<Value> &val, bool only)
|
---|
850 | {
|
---|
851 | vector<string> rc;
|
---|
852 |
|
---|
853 | Handle<Array> arr = Handle<Array>::Cast(val);
|
---|
854 | for (uint32_t i=0; i<arr->Length(); i++)
|
---|
855 | {
|
---|
856 | Handle<Value> obj = arr->Get(i);
|
---|
857 | if (obj.IsEmpty())
|
---|
858 | continue;
|
---|
859 |
|
---|
860 | if (obj->IsNull() || obj->IsUndefined())
|
---|
861 | continue;
|
---|
862 |
|
---|
863 | if (only && !obj->IsString())
|
---|
864 | continue;
|
---|
865 |
|
---|
866 | rc.push_back(*String::AsciiValue(obj->ToString()));
|
---|
867 | }
|
---|
868 |
|
---|
869 | return rc;
|
---|
870 | }
|
---|
871 |
|
---|
872 | Handle<Value> InterpreterV8::FuncSendMail(const Arguments& args)
|
---|
873 | {
|
---|
874 | HandleScope handle_scope;
|
---|
875 |
|
---|
876 | if (args.Length()>1)
|
---|
877 | return ThrowException(String::New("Only one argument allowed."));
|
---|
878 |
|
---|
879 | if (args.Length()==1 && !args[0]->IsBoolean())
|
---|
880 | return ThrowException(String::New("Argument must be a boolean."));
|
---|
881 |
|
---|
882 | const bool block = args.Length()==0 || args[0]->BooleanValue();
|
---|
883 |
|
---|
884 | const Handle<Value> sub = args.This()->Get(String::New("subject"));
|
---|
885 | const Handle<Value> rec = args.This()->Get(String::New("recipients"));
|
---|
886 | const Handle<Value> txt = args.This()->Get(String::New("text"));
|
---|
887 | const Handle<Value> att = args.This()->Get(String::New("attachments"));
|
---|
888 | const Handle<Value> bcc = args.This()->Get(String::New("bcc"));
|
---|
889 | const Handle<Value> cc = args.This()->Get(String::New("cc"));
|
---|
890 |
|
---|
891 | const vector<string> vrec = ValueToArray(rec);
|
---|
892 | const vector<string> vtxt = ValueToArray(txt, false);
|
---|
893 | const vector<string> vatt = ValueToArray(att);
|
---|
894 | const vector<string> vbcc = ValueToArray(bcc);
|
---|
895 | const vector<string> vcc = ValueToArray(cc);
|
---|
896 |
|
---|
897 | if (vrec.size()==0)
|
---|
898 | return ThrowException(String::New("At least one valid string is required in 'recipients'."));
|
---|
899 | if (vtxt.size()==0)
|
---|
900 | return ThrowException(String::New("At least one valid string is required in 'text'."));
|
---|
901 |
|
---|
902 | const string subject = *String::AsciiValue(sub->ToString());
|
---|
903 |
|
---|
904 | FILE *pipe = popen(("from=no-reply@fact-project.org mailx -~ "+vrec[0]).c_str(), "w");
|
---|
905 | if (!pipe)
|
---|
906 | return ThrowException(String::New(strerror(errno)));
|
---|
907 |
|
---|
908 | fprintf(pipe, "%s", ("~s"+subject+"\n").c_str());
|
---|
909 | for (auto it=vrec.begin()+1; it<vrec.end(); it++)
|
---|
910 | fprintf(pipe, "%s", ("~t"+*it+"\n").c_str());
|
---|
911 | for (auto it=vbcc.begin(); it<vbcc.end(); it++)
|
---|
912 | fprintf(pipe, "%s", ("~b"+*it+"\n").c_str());
|
---|
913 | for (auto it=vcc.begin(); it<vcc.end(); it++)
|
---|
914 | fprintf(pipe, "%s", ("~c"+*it+"\n").c_str());
|
---|
915 | for (auto it=vatt.begin(); it<vatt.end(); it++)
|
---|
916 | fprintf(pipe, "%s", ("~@"+*it+"\n").c_str()); // Must not contain white spaces
|
---|
917 |
|
---|
918 | for (auto it=vtxt.begin(); it<vtxt.end(); it++)
|
---|
919 | fwrite((*it+"\n").c_str(), it->length()+1, 1, pipe);
|
---|
920 |
|
---|
921 | fprintf(pipe, "\n---\nsent by dimctrl");
|
---|
922 |
|
---|
923 | if (!block)
|
---|
924 | return Undefined();
|
---|
925 |
|
---|
926 | const int rc = pclose(pipe);
|
---|
927 |
|
---|
928 | const Locker lock;
|
---|
929 | return handle_scope.Close(Integer::New(WEXITSTATUS(rc)));
|
---|
930 | }
|
---|
931 |
|
---|
932 | // ==========================================================================
|
---|
933 | // Curl
|
---|
934 | // ==========================================================================
|
---|
935 |
|
---|
936 | Handle<Value> InterpreterV8::ConstructorCurl(const Arguments &args)
|
---|
937 | {
|
---|
938 | if (!args.IsConstructCall())
|
---|
939 | return ThrowException(String::New("Curl must be called as constructor"));
|
---|
940 |
|
---|
941 | if (args.Length()!=1 || !args[0]->IsString())
|
---|
942 | return ThrowException(String::New("Constructor must be called with a single string as argument"));
|
---|
943 |
|
---|
944 | HandleScope handle_scope;
|
---|
945 |
|
---|
946 | Handle<Array> data = Array::New();
|
---|
947 | if (data.IsEmpty())
|
---|
948 | return Undefined();
|
---|
949 |
|
---|
950 | Handle<Object> self = args.This();
|
---|
951 |
|
---|
952 | self->Set(String::New("url"), args[0]->ToString(), ReadOnly);
|
---|
953 | self->Set(String::New("data"), data, ReadOnly);
|
---|
954 |
|
---|
955 | self->Set(String::New("send"), FunctionTemplate::New(WrapSendCurl)->GetFunction(), ReadOnly);
|
---|
956 |
|
---|
957 | return handle_scope.Close(self);
|
---|
958 | }
|
---|
959 |
|
---|
960 | Handle<Value> InterpreterV8::FuncSendCurl(const Arguments& args)
|
---|
961 | {
|
---|
962 | HandleScope handle_scope;
|
---|
963 |
|
---|
964 | if (args.Length()>1)
|
---|
965 | return ThrowException(String::New("Only one argument allowed."));
|
---|
966 |
|
---|
967 | if (args.Length()==1 && !args[0]->IsBoolean())
|
---|
968 | return ThrowException(String::New("Argument must be a boolean."));
|
---|
969 |
|
---|
970 | const bool block = args.Length()==0 || args[0]->BooleanValue();
|
---|
971 |
|
---|
972 | const Handle<Value> url = args.This()->Get(String::New("url"));
|
---|
973 | const Handle<Value> data = args.This()->Get(String::New("data"));
|
---|
974 |
|
---|
975 | const vector<string> vdata = ValueToArray(data);
|
---|
976 | const string sdata = boost::algorithm::join(vdata, "&");
|
---|
977 |
|
---|
978 | const string surl = *String::AsciiValue(url->ToString());
|
---|
979 |
|
---|
980 | string cmd = "curl -sSf ";
|
---|
981 | if (!sdata.empty())
|
---|
982 | cmd += "--data '"+sdata+"' ";
|
---|
983 | cmd += "'http://"+surl+"' 2>&1 ";
|
---|
984 |
|
---|
985 | FILE *pipe = popen(cmd.c_str(), "r");
|
---|
986 | if (!pipe)
|
---|
987 | return ThrowException(String::New(strerror(errno)));
|
---|
988 |
|
---|
989 | if (!block)
|
---|
990 | return Undefined();
|
---|
991 |
|
---|
992 | string txt;
|
---|
993 |
|
---|
994 | while (!feof(pipe))
|
---|
995 | {
|
---|
996 | char buf[1025];
|
---|
997 | if (fgets(buf, 1024, pipe)==NULL)
|
---|
998 | break;
|
---|
999 | txt += buf;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | const int rc = pclose(pipe);
|
---|
1003 |
|
---|
1004 | Handle<Object> obj = Object::New();
|
---|
1005 |
|
---|
1006 | obj->Set(String::New("cmd"), String::New(cmd.c_str()));
|
---|
1007 | obj->Set(String::New("data"), String::New(txt.c_str()));
|
---|
1008 | obj->Set(String::New("rc"), Integer::NewFromUnsigned(WEXITSTATUS(rc)));
|
---|
1009 |
|
---|
1010 | const Locker lock;
|
---|
1011 | return handle_scope.Close(obj);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | // ==========================================================================
|
---|
1015 | // Database
|
---|
1016 | // ==========================================================================
|
---|
1017 |
|
---|
1018 | Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
|
---|
1019 | {
|
---|
1020 | void *ptr = External::Unwrap(args.This()->GetInternalField(0));
|
---|
1021 | if (!ptr)
|
---|
1022 | return Boolean::New(false);
|
---|
1023 |
|
---|
1024 | #ifdef HAVE_SQL
|
---|
1025 | Database *db = reinterpret_cast<Database*>(ptr);
|
---|
1026 | auto it = find(fDatabases.begin(), fDatabases.end(), db);
|
---|
1027 | fDatabases.erase(it);
|
---|
1028 | delete db;
|
---|
1029 | #endif
|
---|
1030 |
|
---|
1031 | HandleScope handle_scope;
|
---|
1032 |
|
---|
1033 | args.This()->SetInternalField(0, External::New(0));
|
---|
1034 |
|
---|
1035 | return handle_scope.Close(Boolean::New(true));
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
|
---|
1039 | {
|
---|
1040 | if (args.Length()==0)
|
---|
1041 | return ThrowException(String::New("Arguments expected."));
|
---|
1042 |
|
---|
1043 | void *ptr = External::Unwrap(args.This()->GetInternalField(0));
|
---|
1044 | if (!ptr)
|
---|
1045 | return Undefined();
|
---|
1046 |
|
---|
1047 | string query;
|
---|
1048 | for (int i=0; i<args.Length(); i++)
|
---|
1049 | query += string(" ") + *String::AsciiValue(args[i]);
|
---|
1050 | query.erase(0, 1);
|
---|
1051 |
|
---|
1052 | #ifdef HAVE_SQL
|
---|
1053 | try
|
---|
1054 | {
|
---|
1055 | HandleScope handle_scope;
|
---|
1056 |
|
---|
1057 | Database *db = reinterpret_cast<Database*>(ptr);
|
---|
1058 |
|
---|
1059 | const mysqlpp::StoreQueryResult res = db->query(query).store();
|
---|
1060 |
|
---|
1061 | Handle<Array> ret = Array::New();
|
---|
1062 | if (ret.IsEmpty())
|
---|
1063 | return Undefined();
|
---|
1064 |
|
---|
1065 | ret->Set(String::New("table"), String::New(res.table()), ReadOnly);
|
---|
1066 | ret->Set(String::New("query"), String::New(query.c_str()), ReadOnly);
|
---|
1067 |
|
---|
1068 | Handle<Array> cols = Array::New();
|
---|
1069 | if (cols.IsEmpty())
|
---|
1070 | return Undefined();
|
---|
1071 |
|
---|
1072 | int irow=0;
|
---|
1073 | for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++)
|
---|
1074 | {
|
---|
1075 | Handle<Object> row = Object::New();
|
---|
1076 | if (row.IsEmpty())
|
---|
1077 | return Undefined();
|
---|
1078 |
|
---|
1079 | const mysqlpp::FieldNames *list = it->field_list().list;
|
---|
1080 |
|
---|
1081 | for (size_t i=0; i<it->size(); i++)
|
---|
1082 | {
|
---|
1083 | const Handle<Value> name = String::New((*list)[i].c_str());
|
---|
1084 | if (irow==0)
|
---|
1085 | cols->Set(i, name);
|
---|
1086 |
|
---|
1087 | if ((*it)[i].is_null())
|
---|
1088 | {
|
---|
1089 | row->Set(name, Undefined(), ReadOnly);
|
---|
1090 | continue;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | const string sql_type = (*it)[i].type().sql_name();
|
---|
1094 |
|
---|
1095 | const bool uns = sql_type.find("UNSIGNED")==string::npos;
|
---|
1096 |
|
---|
1097 | if (sql_type.find("BIGINT")!=string::npos)
|
---|
1098 | {
|
---|
1099 | if (uns)
|
---|
1100 | {
|
---|
1101 | const uint64_t val = (uint64_t)(*it)[i];
|
---|
1102 | if (val>UINT32_MAX)
|
---|
1103 | row->Set(name, Number::New(val), ReadOnly);
|
---|
1104 | else
|
---|
1105 | row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
|
---|
1106 | }
|
---|
1107 | else
|
---|
1108 | {
|
---|
1109 | const int64_t val = (int64_t)(*it)[i];
|
---|
1110 | if (val<INT32_MIN || val>INT32_MAX)
|
---|
1111 | row->Set(name, Number::New(val), ReadOnly);
|
---|
1112 | else
|
---|
1113 | row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
|
---|
1114 | }
|
---|
1115 | continue;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | // 32 bit
|
---|
1119 | if (sql_type.find("INT")!=string::npos)
|
---|
1120 | {
|
---|
1121 | if (uns)
|
---|
1122 | row->Set(name, Integer::NewFromUnsigned((uint32_t)(*it)[i]), ReadOnly);
|
---|
1123 | else
|
---|
1124 | row->Set(name, Integer::New((int32_t)(*it)[i]), ReadOnly);
|
---|
1125 | continue;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | if (sql_type.find("BOOL")!=string::npos )
|
---|
1129 | {
|
---|
1130 | row->Set(name, Boolean::New((bool)(*it)[i]), ReadOnly);
|
---|
1131 | continue;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | if (sql_type.find("FLOAT")!=string::npos)
|
---|
1135 | {
|
---|
1136 | ostringstream val;
|
---|
1137 | val << setprecision(7) << (float)(*it)[i];
|
---|
1138 | row->Set(name, Number::New(stod(val.str())), ReadOnly);
|
---|
1139 | continue;
|
---|
1140 |
|
---|
1141 | }
|
---|
1142 | if (sql_type.find("DOUBLE")!=string::npos)
|
---|
1143 | {
|
---|
1144 | row->Set(name, Number::New((double)(*it)[i]), ReadOnly);
|
---|
1145 | continue;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | if (sql_type.find("CHAR")!=string::npos ||
|
---|
1149 | sql_type.find("TEXT")!=string::npos)
|
---|
1150 | {
|
---|
1151 | row->Set(name, String::New((const char*)(*it)[i]), ReadOnly);
|
---|
1152 | continue;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | time_t date = 0;
|
---|
1156 | if (sql_type.find("TIMESTAMP")!=string::npos)
|
---|
1157 | date = mysqlpp::Time((*it)[i]);
|
---|
1158 |
|
---|
1159 | if (sql_type.find("DATETIME")!=string::npos)
|
---|
1160 | date = mysqlpp::DateTime((*it)[i]);
|
---|
1161 |
|
---|
1162 | if (sql_type.find(" DATE ")!=string::npos)
|
---|
1163 | date = mysqlpp::Date((*it)[i]);
|
---|
1164 |
|
---|
1165 | if (date>0)
|
---|
1166 | {
|
---|
1167 | // It is important to catch the exception thrown
|
---|
1168 | // by Date::New in case of thread termination!
|
---|
1169 | const Local<Value> val = Date::New(date*1000);
|
---|
1170 | if (val.IsEmpty())
|
---|
1171 | return Undefined();
|
---|
1172 |
|
---|
1173 | row->Set(name, val, ReadOnly);
|
---|
1174 | }
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | ret->Set(irow++, row);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | if (irow>0)
|
---|
1181 | ret->Set(String::New("cols"), cols, ReadOnly);
|
---|
1182 |
|
---|
1183 | return handle_scope.Close(ret);
|
---|
1184 | }
|
---|
1185 | catch (const exception &e)
|
---|
1186 | {
|
---|
1187 | return ThrowException(String::New(e.what()));
|
---|
1188 | }
|
---|
1189 | #endif
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
|
---|
1193 | {
|
---|
1194 | if (!args.IsConstructCall())
|
---|
1195 | return ThrowException(String::New("Database must be called as constructor."));
|
---|
1196 |
|
---|
1197 | if (args.Length()!=1)
|
---|
1198 | return ThrowException(String::New("Number of arguments must be 1."));
|
---|
1199 |
|
---|
1200 | if (!args[0]->IsString())
|
---|
1201 | return ThrowException(String::New("Argument 1 not a string."));
|
---|
1202 |
|
---|
1203 | #ifdef HAVE_SQL
|
---|
1204 | try
|
---|
1205 | {
|
---|
1206 | HandleScope handle_scope;
|
---|
1207 |
|
---|
1208 | //if (!args.IsConstructCall())
|
---|
1209 | // return Constructor(fTemplateDatabase, args);
|
---|
1210 |
|
---|
1211 | Database *db = new Database(*String::AsciiValue(args[0]));
|
---|
1212 | fDatabases.push_back(db);
|
---|
1213 |
|
---|
1214 | Handle<Object> self = args.This();
|
---|
1215 | self->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly);
|
---|
1216 | self->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly);
|
---|
1217 | self->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly);
|
---|
1218 | self->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly);
|
---|
1219 | self->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery)->GetFunction(), ReadOnly);
|
---|
1220 | self->Set(String::New("close"), FunctionTemplate::New(WrapDbClose)->GetFunction(), ReadOnly);
|
---|
1221 | self->SetInternalField(0, External::New(db));
|
---|
1222 |
|
---|
1223 | return handle_scope.Close(self);
|
---|
1224 | }
|
---|
1225 | catch (const exception &e)
|
---|
1226 | {
|
---|
1227 | return ThrowException(String::New(e.what()));
|
---|
1228 | }
|
---|
1229 | #endif
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | // ==========================================================================
|
---|
1233 | // Services
|
---|
1234 | // ==========================================================================
|
---|
1235 |
|
---|
1236 | Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
|
---|
1237 | {
|
---|
1238 | // Dim values are always unsigned per (FACT++) definition
|
---|
1239 | switch (type)
|
---|
1240 | {
|
---|
1241 | case 'F':
|
---|
1242 | {
|
---|
1243 | // Remove the "imprecision" effect coming from casting a float to
|
---|
1244 | // a double and then showing it with double precision
|
---|
1245 | ostringstream val;
|
---|
1246 | val << setprecision(7) << *reinterpret_cast<const float*>(ptr);
|
---|
1247 | ptr += 4;
|
---|
1248 | return Number::New(stod(val.str()));
|
---|
1249 | }
|
---|
1250 | case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; }
|
---|
1251 | case 'I':
|
---|
1252 | case 'L': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; }
|
---|
1253 | case 'X':
|
---|
1254 | {
|
---|
1255 | const int64_t val = *reinterpret_cast<const int64_t*>(ptr);
|
---|
1256 | ptr += 8;
|
---|
1257 | if (val>=0 && val<=UINT32_MAX)
|
---|
1258 | return Integer::NewFromUnsigned(val);
|
---|
1259 | if (val>=INT32_MIN && val<0)
|
---|
1260 | return Integer::New(val);
|
---|
1261 | return Number::New(val);
|
---|
1262 | }
|
---|
1263 | case 'S': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
|
---|
1264 | case 'C': { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; }
|
---|
1265 | }
|
---|
1266 | return Undefined();
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
|
---|
1270 | {
|
---|
1271 | HandleScope handle_scope;
|
---|
1272 |
|
---|
1273 | //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value();
|
---|
1274 |
|
---|
1275 | const String::AsciiValue str(args.This()->Get(String::New("name")));
|
---|
1276 |
|
---|
1277 | const auto it = fReverseMap.find(*str);
|
---|
1278 | if (it!=fReverseMap.end())
|
---|
1279 | {
|
---|
1280 | it->second.Dispose();
|
---|
1281 | fReverseMap.erase(it);
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | args.This()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
|
---|
1285 |
|
---|
1286 | return handle_scope.Close(Boolean::New(JsUnsubscribe(*str)));
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | Handle<Value> InterpreterV8::ConvertEvent(const EventImp *evt, uint64_t counter, const char *str)
|
---|
1290 | {
|
---|
1291 | const vector<Description> vec = JsDescription(str);
|
---|
1292 |
|
---|
1293 | Handle<Object> ret = fTemplateEvent->GetFunction()->NewInstance();//Object::New();
|
---|
1294 | if (ret.IsEmpty())
|
---|
1295 | return Undefined();
|
---|
1296 |
|
---|
1297 | const Local<Value> date = Date::New(evt->GetJavaDate());
|
---|
1298 | if (date.IsEmpty())
|
---|
1299 | return Undefined();
|
---|
1300 |
|
---|
1301 | ret->Set(String::New("name"), String::New(str), ReadOnly);
|
---|
1302 | ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly);
|
---|
1303 | ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly);
|
---|
1304 | ret->Set(String::New("size"), Integer::New(evt->GetSize()), ReadOnly);
|
---|
1305 | ret->Set(String::New("counter"), Integer::New(counter), ReadOnly);
|
---|
1306 | if (evt->GetJavaDate()>0)
|
---|
1307 | ret->Set(String::New("time"), date, ReadOnly);
|
---|
1308 |
|
---|
1309 | // If names are available data will also be provided as an
|
---|
1310 | // object. If an empty event was received, but names are available,
|
---|
1311 | // the object will be empty. Otherwise 'obj' will be undefined.
|
---|
1312 | // obj===undefined: no data received
|
---|
1313 | // obj!==undefined, length==0: names for event available
|
---|
1314 | // obj!==undefined, obj.length>0: names available, data received
|
---|
1315 | Handle<Object> named = Object::New();
|
---|
1316 | if (vec.size()>0)
|
---|
1317 | ret->Set(String::New("obj"), named, ReadOnly);
|
---|
1318 |
|
---|
1319 | // If no event was received (usually a disconnection event in
|
---|
1320 | // the context of FACT++), no data is returned
|
---|
1321 | if (evt->IsEmpty())
|
---|
1322 | return ret;
|
---|
1323 |
|
---|
1324 | // If valid data was received, but the size was zero, then
|
---|
1325 | // null is returned as data
|
---|
1326 | // data===undefined: no data received
|
---|
1327 | // data===null: event received, but no data
|
---|
1328 | // data.length>0: event received, contains data
|
---|
1329 | if (evt->GetSize()==0 || evt->GetFormat().empty())
|
---|
1330 | {
|
---|
1331 | ret->Set(String::New("data"), Null(), ReadOnly);
|
---|
1332 | return ret;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | // It seems a copy is required either in the boost which comes with
|
---|
1336 | // Ubuntu 16.04 or in gcc5 ?!
|
---|
1337 | const string fmt = evt->GetFormat();
|
---|
1338 |
|
---|
1339 | typedef boost::char_separator<char> separator;
|
---|
1340 | const boost::tokenizer<separator> tokenizer(fmt, separator(";:"));
|
---|
1341 |
|
---|
1342 | const vector<string> tok(tokenizer.begin(), tokenizer.end());
|
---|
1343 |
|
---|
1344 | Handle<Object> arr = tok.size()>1 ? Array::New() : ret;
|
---|
1345 | if (arr.IsEmpty())
|
---|
1346 | return Undefined();
|
---|
1347 |
|
---|
1348 | const char *ptr = evt->GetText();
|
---|
1349 | const char *end = evt->GetText()+evt->GetSize();
|
---|
1350 |
|
---|
1351 | try
|
---|
1352 | {
|
---|
1353 | size_t pos = 1;
|
---|
1354 | for (auto it=tok.begin(); it<tok.end() && ptr<end; it++, pos++)
|
---|
1355 | {
|
---|
1356 | char type = (*it)[0];
|
---|
1357 | it++;
|
---|
1358 |
|
---|
1359 | string name = pos<vec.size() ? vec[pos].name : "";
|
---|
1360 | if (tok.size()==1)
|
---|
1361 | name = "data";
|
---|
1362 |
|
---|
1363 | // Get element size
|
---|
1364 | uint32_t sz = 1;
|
---|
1365 | switch (type)
|
---|
1366 | {
|
---|
1367 | case 'X':
|
---|
1368 | case 'D': sz = 8; break;
|
---|
1369 | case 'F':
|
---|
1370 | case 'I':
|
---|
1371 | case 'L': sz = 4; break;
|
---|
1372 | case 'S': sz = 2; break;
|
---|
1373 | case 'C': sz = 1; break;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | // Check if no number is attached if the size of the
|
---|
1377 | // received data is consistent with the format string
|
---|
1378 | if (it==tok.end() && (end-ptr)%sz>0)
|
---|
1379 | return Exception::Error(String::New(("Number of received bytes ["+to_string(evt->GetSize())+"] does not match format ["+evt->GetFormat()+"]").c_str()));
|
---|
1380 |
|
---|
1381 | // Check if format has a number attached.
|
---|
1382 | // If no number is attached calculate number of elements
|
---|
1383 | const uint32_t cnt = it==tok.end() ? (end-ptr)/sz : stoi(it->c_str());
|
---|
1384 |
|
---|
1385 | // is_str: Array of type C but unknown size (String)
|
---|
1386 | // is_one: Array of known size, but size is 1 (I:1)
|
---|
1387 | const bool is_str = type=='C' && it==tok.end();
|
---|
1388 | const bool is_one = cnt==1 && it!=tok.end();
|
---|
1389 |
|
---|
1390 | Handle<Value> v;
|
---|
1391 |
|
---|
1392 | if (is_str)
|
---|
1393 | v = String::New(ptr);
|
---|
1394 | if (is_one)
|
---|
1395 | v = Convert(type, ptr);
|
---|
1396 |
|
---|
1397 | // Array of known (I:5) or unknown size (I), but no string
|
---|
1398 | if (!is_str && !is_one)
|
---|
1399 | {
|
---|
1400 | Handle<Object> a = Array::New(cnt);
|
---|
1401 | if (a.IsEmpty())
|
---|
1402 | return Undefined();
|
---|
1403 |
|
---|
1404 | for (uint32_t i=0; i<cnt; i++)
|
---|
1405 | a->Set(i, Convert(type, ptr));
|
---|
1406 |
|
---|
1407 | v = a;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | if (tok.size()>1)
|
---|
1411 | arr->Set(pos-1, v);
|
---|
1412 | else
|
---|
1413 | ret->Set(String::New("data"), v, ReadOnly);
|
---|
1414 |
|
---|
1415 | if (!name.empty())
|
---|
1416 | {
|
---|
1417 | const Handle<String> n = String::New(name.c_str());
|
---|
1418 | named->Set(n, v);
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | if (tok.size()>1)
|
---|
1423 | ret->Set(String::New("data"), arr, ReadOnly);
|
---|
1424 |
|
---|
1425 | return ret;
|
---|
1426 | }
|
---|
1427 | catch (...)
|
---|
1428 | {
|
---|
1429 | return Exception::Error(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str()));
|
---|
1430 | }
|
---|
1431 | }
|
---|
1432 | /*
|
---|
1433 | Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
|
---|
1434 | {
|
---|
1435 | HandleScope handle_scope;
|
---|
1436 |
|
---|
1437 | const String::AsciiValue str(args.Holder()->Get(String::New("name")));
|
---|
1438 |
|
---|
1439 | const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
|
---|
1440 |
|
---|
1441 | const EventImp *evt = p.second;
|
---|
1442 | if (!evt)
|
---|
1443 | return Undefined();
|
---|
1444 |
|
---|
1445 | //if (counter==cnt)
|
---|
1446 | // return info.Holder();//Holder()->Get(String::New("data"));
|
---|
1447 |
|
---|
1448 | Handle<Value> ret = ConvertEvent(evt, p.first, *str);
|
---|
1449 | return ret->IsNativeError() ? ThrowException(ret) : handle_scope.Close(ret);
|
---|
1450 | }
|
---|
1451 | */
|
---|
1452 | Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
|
---|
1453 | {
|
---|
1454 | if (args.Length()>2)
|
---|
1455 | return ThrowException(String::New("Number of arguments must not be greater than 2."));
|
---|
1456 |
|
---|
1457 | if (args.Length()>=1 && !args[0]->IsInt32() && !args[0]->IsNull())
|
---|
1458 | return ThrowException(String::New("Argument 1 not an uint32."));
|
---|
1459 |
|
---|
1460 | if (args.Length()==2 && !args[1]->IsBoolean())
|
---|
1461 | return ThrowException(String::New("Argument 2 not a boolean."));
|
---|
1462 |
|
---|
1463 | // Using a Javascript function has the advantage that it is fully
|
---|
1464 | // interruptable without the need of C++ code
|
---|
1465 | const bool null = args.Length()>=1 && args[0]->IsNull();
|
---|
1466 | const int32_t timeout = args.Length()>=1 ? args[0]->Int32Value() : 0;
|
---|
1467 | const bool named = args.Length()<2 || args[1]->BooleanValue();
|
---|
1468 |
|
---|
1469 | HandleScope handle_scope;
|
---|
1470 |
|
---|
1471 | const Handle<String> data = String::New("data");
|
---|
1472 | const Handle<String> object = String::New("obj");
|
---|
1473 |
|
---|
1474 | const String::AsciiValue name(args.Holder()->Get(String::New("name")));
|
---|
1475 |
|
---|
1476 | TryCatch exception;
|
---|
1477 |
|
---|
1478 | Time t;
|
---|
1479 | while (!exception.HasCaught())
|
---|
1480 | {
|
---|
1481 | const pair<uint64_t, EventImp *> p = JsGetEvent(*name);
|
---|
1482 |
|
---|
1483 | const EventImp *evt = p.second;
|
---|
1484 | if (evt)
|
---|
1485 | {
|
---|
1486 | const Handle<Value> val = ConvertEvent(evt, p.first, *name);
|
---|
1487 | if (val->IsNativeError())
|
---|
1488 | return ThrowException(val);
|
---|
1489 |
|
---|
1490 | // Protect against the return of an exception
|
---|
1491 | if (val->IsObject())
|
---|
1492 | {
|
---|
1493 | const Handle<Object> event = val->ToObject();
|
---|
1494 | const Handle<Value> obj = event->Get(named?object:data);
|
---|
1495 | if (!obj.IsEmpty())
|
---|
1496 | {
|
---|
1497 | if (!named)
|
---|
1498 | {
|
---|
1499 | // No names (no 'obj'), but 'data'
|
---|
1500 | if (!obj->IsUndefined())
|
---|
1501 | return handle_scope.Close(val);
|
---|
1502 | }
|
---|
1503 | else
|
---|
1504 | {
|
---|
1505 | // Has names and data was received?
|
---|
1506 | if (obj->IsObject() && obj->ToObject()->GetOwnPropertyNames()->Length()>0)
|
---|
1507 | return handle_scope.Close(val);
|
---|
1508 | }
|
---|
1509 | }
|
---|
1510 | }
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | if (args.Length()==0)
|
---|
1514 | break;
|
---|
1515 |
|
---|
1516 | if (!null && Time()-t>=boost::posix_time::milliseconds(abs(timeout)))
|
---|
1517 | break;
|
---|
1518 |
|
---|
1519 | // Theoretically, the CPU usage can be reduced by maybe a factor
|
---|
1520 | // of four using a larger value, but this also means that the
|
---|
1521 | // JavaScript is locked for a longer time.
|
---|
1522 | const Unlocker unlock;
|
---|
1523 | usleep(1000);
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | // This hides the location of the exception, which is wanted.
|
---|
1527 | if (exception.HasCaught())
|
---|
1528 | return exception.ReThrow();
|
---|
1529 |
|
---|
1530 | if (timeout<0)
|
---|
1531 | return Undefined();
|
---|
1532 |
|
---|
1533 | const string str = "Waiting for a valid event of "+string(*name)+" timed out.";
|
---|
1534 | return ThrowException(String::New(str.c_str()));
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 |
|
---|
1538 | // This is a callback from the RemoteControl piping event handling
|
---|
1539 | // to the java script ---> in test phase!
|
---|
1540 | void InterpreterV8::JsHandleEvent(const EventImp &evt, uint64_t cnt, const string &service)
|
---|
1541 | {
|
---|
1542 | // FIXME: This blocks service updates, we have to run this
|
---|
1543 | // in a dedicated thread.
|
---|
1544 | const Locker locker;
|
---|
1545 |
|
---|
1546 | if (fThreadId<0)
|
---|
1547 | return;
|
---|
1548 |
|
---|
1549 | const auto it = fReverseMap.find(service);
|
---|
1550 | if (it==fReverseMap.end())
|
---|
1551 | return;
|
---|
1552 |
|
---|
1553 | const HandleScope handle_scope;
|
---|
1554 |
|
---|
1555 | Handle<Object> obj = it->second;
|
---|
1556 |
|
---|
1557 | const Handle<String> onchange = String::New("onchange");
|
---|
1558 | if (!obj->Has(onchange))
|
---|
1559 | return;
|
---|
1560 |
|
---|
1561 | const Handle<Value> val = obj->Get(onchange);
|
---|
1562 | if (!val->IsFunction())
|
---|
1563 | return;
|
---|
1564 |
|
---|
1565 | obj->CreationContext()->Enter();
|
---|
1566 |
|
---|
1567 | // -------------------------------------------------------------------
|
---|
1568 |
|
---|
1569 | TryCatch exception;
|
---|
1570 |
|
---|
1571 | const int id = V8::GetCurrentThreadId();
|
---|
1572 | fThreadIds.insert(id);
|
---|
1573 |
|
---|
1574 | Handle<Value> ret = ConvertEvent(&evt, cnt, service.c_str());
|
---|
1575 | if (ret->IsObject())
|
---|
1576 | Handle<Function>::Cast(val)->Call(obj, 1, &ret);
|
---|
1577 |
|
---|
1578 | fThreadIds.erase(id);
|
---|
1579 |
|
---|
1580 | if (!HandleException(exception, "Service.onchange"))
|
---|
1581 | V8::TerminateExecution(fThreadId);
|
---|
1582 |
|
---|
1583 | if (ret->IsNativeError())
|
---|
1584 | {
|
---|
1585 | JsException(service+".onchange callback - "+*String::AsciiValue(ret));
|
---|
1586 | V8::TerminateExecution(fThreadId);
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | obj->CreationContext()->Exit();
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local<Value> value, const AccessorInfo &)
|
---|
1593 | {
|
---|
1594 | // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
|
---|
1595 | const string server = *String::AsciiValue(prop);
|
---|
1596 | auto it = fStateCallbacks.find(server);
|
---|
1597 |
|
---|
1598 | if (it!=fStateCallbacks.end())
|
---|
1599 | {
|
---|
1600 | it->second.Dispose();
|
---|
1601 | fStateCallbacks.erase(it);
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | if (value->IsFunction())
|
---|
1605 | fStateCallbacks[server] = Persistent<Object>::New(value->ToObject());
|
---|
1606 |
|
---|
1607 | return Handle<Value>();
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 | void InterpreterV8::JsHandleState(const std::string &server, const State &state)
|
---|
1611 | {
|
---|
1612 | // FIXME: This blocks service updates, we have to run this
|
---|
1613 | // in a dedicated thread.
|
---|
1614 | const Locker locker;
|
---|
1615 |
|
---|
1616 | if (fThreadId<0)
|
---|
1617 | return;
|
---|
1618 |
|
---|
1619 | auto it = fStateCallbacks.find(server);
|
---|
1620 | if (it==fStateCallbacks.end())
|
---|
1621 | {
|
---|
1622 | it = fStateCallbacks.find("*");
|
---|
1623 | if (it==fStateCallbacks.end())
|
---|
1624 | return;
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 | const HandleScope handle_scope;
|
---|
1628 |
|
---|
1629 | it->second->CreationContext()->Enter();
|
---|
1630 |
|
---|
1631 | // -------------------------------------------------------------------
|
---|
1632 |
|
---|
1633 | Handle<ObjectTemplate> obj = ObjectTemplate::New();
|
---|
1634 | obj->Set(String::New("server"), String::New(server.c_str()), ReadOnly);
|
---|
1635 |
|
---|
1636 | if (state.index>-256)
|
---|
1637 | {
|
---|
1638 | obj->Set(String::New("index"), Integer::New(state.index), ReadOnly);
|
---|
1639 | obj->Set(String::New("name"), String::New(state.name.c_str()), ReadOnly);
|
---|
1640 | obj->Set(String::New("comment"), String::New(state.comment.c_str()), ReadOnly);
|
---|
1641 | const Local<Value> date = Date::New(state.time.JavaDate());
|
---|
1642 | if (!date.IsEmpty())
|
---|
1643 | obj->Set(String::New("time"), date);
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | // -------------------------------------------------------------------
|
---|
1647 |
|
---|
1648 | TryCatch exception;
|
---|
1649 |
|
---|
1650 | const int id = V8::GetCurrentThreadId();
|
---|
1651 | fThreadIds.insert(id);
|
---|
1652 |
|
---|
1653 | Handle<Value> args[] = { obj->NewInstance() };
|
---|
1654 | Handle<Function> fun = Handle<Function>(Function::Cast(*it->second));
|
---|
1655 | fun->Call(fun, 1, args);
|
---|
1656 |
|
---|
1657 | fThreadIds.erase(id);
|
---|
1658 |
|
---|
1659 | if (!HandleException(exception, "dim.onchange"))
|
---|
1660 | V8::TerminateExecution(fThreadId);
|
---|
1661 |
|
---|
1662 | it->second->CreationContext()->Exit();
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | // ==========================================================================
|
---|
1666 | // Interrupt handling
|
---|
1667 | // ==========================================================================
|
---|
1668 |
|
---|
1669 | Handle<Value> InterpreterV8::FuncSetInterrupt(const Arguments &args)
|
---|
1670 | {
|
---|
1671 | if (args.Length()!=1)
|
---|
1672 | return ThrowException(String::New("Number of arguments must be 1."));
|
---|
1673 |
|
---|
1674 | if (!args[0]->IsNull() && !args[0]->IsUndefined() && !args[0]->IsFunction())
|
---|
1675 | return ThrowException(String::New("Argument not a function, null or undefined."));
|
---|
1676 |
|
---|
1677 | if (args[0]->IsNull() || args[0]->IsUndefined())
|
---|
1678 | {
|
---|
1679 | fInterruptCallback.Dispose();
|
---|
1680 | fInterruptCallback.Clear();
|
---|
1681 | return Undefined();
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
|
---|
1685 | fInterruptCallback = Persistent<Object>::New(args[0]->ToObject());
|
---|
1686 | return Undefined();
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | Handle<Value> InterpreterV8::HandleInterruptImp(string str, uint64_t time)
|
---|
1690 | {
|
---|
1691 | if (fInterruptCallback.IsEmpty())
|
---|
1692 | return Handle<Value>();
|
---|
1693 |
|
---|
1694 | const size_t p = str.find_last_of('\n');
|
---|
1695 |
|
---|
1696 | const string usr = p==string::npos?"":str.substr(p+1);
|
---|
1697 |
|
---|
1698 | string irq = p==string::npos?str:str.substr(0, p);
|
---|
1699 |
|
---|
1700 | map<string,string> data;
|
---|
1701 | try
|
---|
1702 | {
|
---|
1703 | data = Tools::Split(irq, true);
|
---|
1704 | }
|
---|
1705 | catch (const exception &e)
|
---|
1706 | {
|
---|
1707 | irq = "ERROR";
|
---|
1708 | data["0"] = e.what();
|
---|
1709 | JsWarn("Couldn't parse interrupt: "+irq+" ["+string(e.what())+"]");
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | Local<Value> irq_str = String::New(irq.c_str());
|
---|
1713 | Local<Value> usr_str = String::New(usr.c_str());
|
---|
1714 | Local<Value> date = Date::New(time);
|
---|
1715 | Handle<Object> arr = Array::New(data.size());
|
---|
1716 |
|
---|
1717 | if (date.IsEmpty() || arr.IsEmpty())
|
---|
1718 | return Handle<Value>();
|
---|
1719 |
|
---|
1720 | for (auto it=data.begin(); it!=data.end(); it++)
|
---|
1721 | arr->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
|
---|
1722 |
|
---|
1723 | Handle<Value> args[] = { irq_str, arr, date, usr_str };
|
---|
1724 | Handle<Function> fun = Handle<Function>(Function::Cast(*fInterruptCallback));
|
---|
1725 |
|
---|
1726 | return fun->Call(fun, 4, args);
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 | int InterpreterV8::JsHandleInterrupt(const EventImp &evt)
|
---|
1730 | {
|
---|
1731 | // FIXME: This blocks service updates, we have to run this
|
---|
1732 | // in a dedicated thread.
|
---|
1733 | const Locker locker;
|
---|
1734 |
|
---|
1735 | if (fThreadId<0 || fInterruptCallback.IsEmpty())
|
---|
1736 | return -42;
|
---|
1737 |
|
---|
1738 | const HandleScope handle_scope;
|
---|
1739 |
|
---|
1740 | fInterruptCallback->CreationContext()->Enter();
|
---|
1741 |
|
---|
1742 | // -------------------------------------------------------------------
|
---|
1743 |
|
---|
1744 | TryCatch exception;
|
---|
1745 |
|
---|
1746 | const int id = V8::GetCurrentThreadId();
|
---|
1747 | fThreadIds.insert(id);
|
---|
1748 |
|
---|
1749 | const Handle<Value> val = HandleInterruptImp(evt.GetString(), evt.GetJavaDate());
|
---|
1750 |
|
---|
1751 | fThreadIds.erase(id);
|
---|
1752 |
|
---|
1753 | const int rc = !val.IsEmpty() && val->IsInt32() ? val->Int32Value() : 0;
|
---|
1754 |
|
---|
1755 | if (!HandleException(exception, "interrupt"))
|
---|
1756 | V8::TerminateExecution(fThreadId);
|
---|
1757 |
|
---|
1758 | fInterruptCallback->CreationContext()->Exit();
|
---|
1759 |
|
---|
1760 | return rc<10 || rc>255 ? -42 : rc;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | Handle<Value> InterpreterV8::FuncTriggerInterrupt(const Arguments &args)
|
---|
1764 | {
|
---|
1765 | string data;
|
---|
1766 | for (int i=0; i<args.Length(); i++)
|
---|
1767 | {
|
---|
1768 | const String::AsciiValue str(args[i]);
|
---|
1769 |
|
---|
1770 | if (string(*str).find_first_of('\n')!=string::npos)
|
---|
1771 | return ThrowException(String::New("No argument must contain line breaks."));
|
---|
1772 |
|
---|
1773 | if (!*str)
|
---|
1774 | continue;
|
---|
1775 |
|
---|
1776 | data += *str;
|
---|
1777 | data += ' ';
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 | HandleScope handle_scope;
|
---|
1781 |
|
---|
1782 | const Handle<Value> rc = HandleInterruptImp(Tools::Trim(data), Time().JavaDate());
|
---|
1783 | return handle_scope.Close(rc);
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | // ==========================================================================
|
---|
1787 | // Class 'Subscription'
|
---|
1788 | // ==========================================================================
|
---|
1789 |
|
---|
1790 | Handle<Value> InterpreterV8::FuncSubscription(const Arguments &args)
|
---|
1791 | {
|
---|
1792 | if (args.Length()!=1 && args.Length()!=2)
|
---|
1793 | return ThrowException(String::New("Number of arguments must be one or two."));
|
---|
1794 |
|
---|
1795 | if (!args[0]->IsString())
|
---|
1796 | return ThrowException(String::New("Argument 1 must be a string."));
|
---|
1797 |
|
---|
1798 | if (args.Length()==2 && !args[1]->IsFunction())
|
---|
1799 | return ThrowException(String::New("Argument 2 must be a function."));
|
---|
1800 |
|
---|
1801 | const String::AsciiValue str(args[0]);
|
---|
1802 |
|
---|
1803 | if (!args.IsConstructCall())
|
---|
1804 | {
|
---|
1805 | const auto it = fReverseMap.find(*str);
|
---|
1806 | if (it!=fReverseMap.end())
|
---|
1807 | return it->second;
|
---|
1808 |
|
---|
1809 | return Undefined();
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | const HandleScope handle_scope;
|
---|
1813 |
|
---|
1814 | Handle<Object> self = args.This();
|
---|
1815 | self->Set(String::New("get"), FunctionTemplate::New(WrapGetData)->GetFunction(), ReadOnly);
|
---|
1816 | self->Set(String::New("close"), FunctionTemplate::New(WrapClose)->GetFunction(), ReadOnly);
|
---|
1817 | self->Set(String::New("name"), String::New(*str), ReadOnly);
|
---|
1818 | self->Set(String::New("isOpen"), Boolean::New(true));
|
---|
1819 |
|
---|
1820 | if (args.Length()==2)
|
---|
1821 | self->Set(String::New("onchange"), args[1]);
|
---|
1822 |
|
---|
1823 | fReverseMap[*str] = Persistent<Object>::New(self);
|
---|
1824 |
|
---|
1825 | void *ptr = JsSubscribe(*str);
|
---|
1826 | if (ptr==0)
|
---|
1827 | return ThrowException(String::New(("Subscription to '"+string(*str)+"' already exists.").c_str()));
|
---|
1828 |
|
---|
1829 | self->SetInternalField(0, External::New(ptr));
|
---|
1830 |
|
---|
1831 | return Undefined();
|
---|
1832 |
|
---|
1833 | // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance());
|
---|
1834 | // obj.MakeWeak((void*)1, Cleanup);
|
---|
1835 | // return obj;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | // ==========================================================================
|
---|
1839 | // Astrometry
|
---|
1840 | // ==========================================================================
|
---|
1841 | #ifdef HAVE_NOVA
|
---|
1842 |
|
---|
1843 | double InterpreterV8::GetDataMember(const Arguments &args, const char *name)
|
---|
1844 | {
|
---|
1845 | return args.This()->Get(String::New(name))->NumberValue();
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | Handle<Value> InterpreterV8::CalcDist(const Arguments &args, const bool local)
|
---|
1849 | {
|
---|
1850 | if (args.Length()!=2)
|
---|
1851 | return ThrowException(String::New("dist must be called with exactly two arguments."));
|
---|
1852 |
|
---|
1853 | if (!args[0]->IsObject() || !args[1]->IsObject())
|
---|
1854 | return ThrowException(String::New("At least one argument not an object."));
|
---|
1855 |
|
---|
1856 | // FiXME: Add a check for the argument type
|
---|
1857 |
|
---|
1858 | HandleScope handle_scope;
|
---|
1859 |
|
---|
1860 | Handle<Object> obj[2] =
|
---|
1861 | {
|
---|
1862 | Handle<Object>::Cast(args[0]),
|
---|
1863 | Handle<Object>::Cast(args[1])
|
---|
1864 | };
|
---|
1865 |
|
---|
1866 | const Handle<String> s_theta = String::New(local?"zd":"dec"); // was: zd
|
---|
1867 | const Handle<String> s_phi = String::New(local?"az":"ra"); // was: az
|
---|
1868 |
|
---|
1869 | const double conv_t = M_PI/180;
|
---|
1870 | const double conv_p = local ? -M_PI/180 : M_PI/12;
|
---|
1871 | const double offset = local ? 0 : M_PI;
|
---|
1872 |
|
---|
1873 | const double theta0 = offset - obj[0]->Get(s_theta)->NumberValue() * conv_t;
|
---|
1874 | const double phi0 = obj[0]->Get(s_phi )->NumberValue() * conv_p;
|
---|
1875 | const double theta1 = offset - obj[1]->Get(s_theta)->NumberValue() * conv_t;
|
---|
1876 | const double phi1 = obj[1]->Get(s_phi )->NumberValue() * conv_p;
|
---|
1877 |
|
---|
1878 | if (!finite(theta0) || !finite(theta1) || !finite(phi0) || !finite(phi1))
|
---|
1879 | return ThrowException(String::New("some values not valid or not finite."));
|
---|
1880 |
|
---|
1881 | /*
|
---|
1882 | const double x0 = sin(zd0) * cos(az0); // az0 -= az0
|
---|
1883 | const double y0 = sin(zd0) * sin(az0); // az0 -= az0
|
---|
1884 | const double z0 = cos(zd0);
|
---|
1885 |
|
---|
1886 | const double x1 = sin(zd1) * cos(az1); // az1 -= az0
|
---|
1887 | const double y1 = sin(zd1) * sin(az1); // az1 -= az0
|
---|
1888 | const double z1 = cos(zd1);
|
---|
1889 |
|
---|
1890 | const double res = acos(x0*x1 + y0*y1 + z0*z1) * 180/M_PI;
|
---|
1891 | */
|
---|
1892 |
|
---|
1893 | // cos(az1-az0) = cos(az1)*cos(az0) + sin(az1)*sin(az0)
|
---|
1894 |
|
---|
1895 | const double x = sin(theta0) * sin(theta1) * cos(phi1-phi0);
|
---|
1896 | const double y = cos(theta0) * cos(theta1);
|
---|
1897 |
|
---|
1898 | const double res = acos(x + y) * 180/M_PI;
|
---|
1899 |
|
---|
1900 | return handle_scope.Close(Number::New(res));
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | Handle<Value> InterpreterV8::LocalDist(const Arguments &args)
|
---|
1904 | {
|
---|
1905 | return CalcDist(args, true);
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | Handle<Value> InterpreterV8::SkyDist(const Arguments &args)
|
---|
1909 | {
|
---|
1910 | return CalcDist(args, false);
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | Handle<Value> InterpreterV8::MoonDisk(const Arguments &args)
|
---|
1914 | {
|
---|
1915 | if (args.Length()>1)
|
---|
1916 | return ThrowException(String::New("disk must not be called with more than one argument."));
|
---|
1917 |
|
---|
1918 | const uint64_t v = uint64_t(args[0]->NumberValue());
|
---|
1919 | const Time utc = args.Length()==0 ? Time() : Time(v/1000, v%1000);
|
---|
1920 |
|
---|
1921 | return Number::New(Nova::GetLunarDisk(utc.JD()));
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | struct AstroArgs
|
---|
1925 | {
|
---|
1926 | string obs;
|
---|
1927 | Nova::LnLatPosn posn;
|
---|
1928 | double jd;
|
---|
1929 | uint64_t jsdate;
|
---|
1930 |
|
---|
1931 | AstroArgs() : jsdate(0) { }
|
---|
1932 | };
|
---|
1933 |
|
---|
1934 | AstroArgs EvalAstroArgs(int offset, const Arguments &args, int8_t type=2)
|
---|
1935 | {
|
---|
1936 | const uint8_t max = abs(type);
|
---|
1937 |
|
---|
1938 | if (args.Length()>offset+max)
|
---|
1939 | throw runtime_error("Number of arguments must not exceed "+to_string(offset+max)+".");
|
---|
1940 |
|
---|
1941 | if (type==1 && args.Length()==offset+1 && !args[offset]->IsString())
|
---|
1942 | throw runtime_error("Argument "+to_string(offset+1)+" must be a string.");
|
---|
1943 | if (type==-1 && args.Length()==offset+1 && !args[offset]->IsDate())
|
---|
1944 | throw runtime_error("Argument "+to_string(offset+1)+" must be a date.");
|
---|
1945 |
|
---|
1946 | if (args.Length()==offset+1 && !(args[offset]->IsDate() || args[offset]->IsString()))
|
---|
1947 | throw runtime_error("Argument "+to_string(offset+1)+" must be a string or Date.");
|
---|
1948 |
|
---|
1949 | if (args.Length()==offset+2 &&
|
---|
1950 | !(args[offset+0]->IsDate() && args[offset+1]->IsString()) &&
|
---|
1951 | !(args[offset+1]->IsDate() && args[offset+0]->IsString()))
|
---|
1952 | throw runtime_error("Arguments "+to_string(offset+1)+" and "+to_string(offset+2)+" must be a string/Date or Date/string.");
|
---|
1953 |
|
---|
1954 | HandleScope handle_scope;
|
---|
1955 |
|
---|
1956 | Local<Value> obs = args.This()->Get(String::New("observatory"));
|
---|
1957 | if (args.Length()>offset && args[offset]->IsString())
|
---|
1958 | obs = args[offset];
|
---|
1959 | if (args.Length()>offset+1 && args[offset+1]->IsString())
|
---|
1960 | obs = args[offset+1];
|
---|
1961 |
|
---|
1962 | AstroArgs rc;
|
---|
1963 |
|
---|
1964 | // For constructors, observatory can stay empty if not explicitly given
|
---|
1965 | if (offset<2)
|
---|
1966 | rc.obs = "ORM";
|
---|
1967 |
|
---|
1968 | if (!obs.IsEmpty() && !obs->IsUndefined())
|
---|
1969 | rc.obs = *String::AsciiValue(obs);
|
---|
1970 |
|
---|
1971 | rc.posn = rc.obs;
|
---|
1972 |
|
---|
1973 | if ((!rc.obs.empty() || offset==0) && !rc.posn.isValid())
|
---|
1974 | throw runtime_error("Observatory "+rc.obs+" unknown.");
|
---|
1975 |
|
---|
1976 | Local<Value> date = args.This()->Get(String::New("time"));
|
---|
1977 | if (args.Length()>offset && args[offset]->IsDate())
|
---|
1978 | date = args[offset];
|
---|
1979 | if (args.Length()>offset+1 && args[offset+1]->IsDate())
|
---|
1980 | date = args[offset+1];
|
---|
1981 |
|
---|
1982 | // For constructors, time can stay empty if not explicitly given
|
---|
1983 | if (offset<2)
|
---|
1984 | rc.jsdate = Time().JavaDate();
|
---|
1985 |
|
---|
1986 | if (!date.IsEmpty() && !date->IsUndefined())
|
---|
1987 | rc.jsdate = uint64_t(date->NumberValue());
|
---|
1988 |
|
---|
1989 | rc.jd = Time(rc.jsdate/1000, rc.jsdate%1000).JD();
|
---|
1990 |
|
---|
1991 | return rc;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | Handle<Value> InterpreterV8::LocalToSky(const Arguments &args)
|
---|
1995 | {
|
---|
1996 | AstroArgs local;
|
---|
1997 | try
|
---|
1998 | {
|
---|
1999 | local = EvalAstroArgs(0, args, 2);
|
---|
2000 | }
|
---|
2001 | catch (const exception &e)
|
---|
2002 | {
|
---|
2003 | return ThrowException(String::New(e.what()));
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | Nova::ZdAzPosn hrz;
|
---|
2007 | hrz.zd = GetDataMember(args, "zd");
|
---|
2008 | hrz.az = GetDataMember(args, "az");
|
---|
2009 |
|
---|
2010 | if (!finite(hrz.zd) || !finite(hrz.az))
|
---|
2011 | return ThrowException(String::New("Zd and az must be finite."));
|
---|
2012 |
|
---|
2013 | const Nova::EquPosn equ = Nova::GetEquFromHrz(hrz, local.posn, local.jd);
|
---|
2014 |
|
---|
2015 | HandleScope handle_scope;
|
---|
2016 |
|
---|
2017 | Handle<Value> arg_loc[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) };
|
---|
2018 | Handle<Object> loc = fTemplateLocal->GetFunction()->NewInstance(4, arg_loc);
|
---|
2019 |
|
---|
2020 | Handle<Value> arg_sky[] = { Number::New(equ.ra/15), Number::New(equ.dec), loc };
|
---|
2021 | return handle_scope.Close(fTemplateSky->GetFunction()->NewInstance(3, arg_sky));
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | Handle<Value> InterpreterV8::SkyToLocal(const Arguments &args)
|
---|
2025 | {
|
---|
2026 | AstroArgs local;
|
---|
2027 | try
|
---|
2028 | {
|
---|
2029 | local = EvalAstroArgs(0, args, 2);
|
---|
2030 | }
|
---|
2031 | catch (const exception &e)
|
---|
2032 | {
|
---|
2033 | return ThrowException(String::New(e.what()));
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | Nova::EquPosn equ;
|
---|
2037 | equ.ra = GetDataMember(args, "ra")*15;
|
---|
2038 | equ.dec = GetDataMember(args, "dec");
|
---|
2039 |
|
---|
2040 | if (!finite(equ.ra) || !finite(equ.dec))
|
---|
2041 | return ThrowException(String::New("Ra and dec must be finite."));
|
---|
2042 |
|
---|
2043 | HandleScope handle_scope;
|
---|
2044 |
|
---|
2045 | const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, local.posn, local.jd);
|
---|
2046 |
|
---|
2047 | Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) };
|
---|
2048 | return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(4, arg));
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | Handle<Value> InterpreterV8::MoonToLocal(const Arguments &args)
|
---|
2052 | {
|
---|
2053 | AstroArgs local;
|
---|
2054 | try
|
---|
2055 | {
|
---|
2056 | local = EvalAstroArgs(0, args, 1);
|
---|
2057 | }
|
---|
2058 | catch (const exception &e)
|
---|
2059 | {
|
---|
2060 | return ThrowException(String::New(e.what()));
|
---|
2061 | }
|
---|
2062 |
|
---|
2063 | Nova::EquPosn equ;
|
---|
2064 | equ.ra = GetDataMember(args, "ra")*15;
|
---|
2065 | equ.dec = GetDataMember(args, "dec");
|
---|
2066 |
|
---|
2067 | if (!finite(equ.ra) || !finite(equ.dec))
|
---|
2068 | return ThrowException(String::New("Ra and dec must be finite."));
|
---|
2069 |
|
---|
2070 | HandleScope handle_scope;
|
---|
2071 |
|
---|
2072 | const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, local.posn, local.jd);
|
---|
2073 |
|
---|
2074 | Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) };
|
---|
2075 | return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(4, arg));
|
---|
2076 | }
|
---|
2077 |
|
---|
2078 | Handle<Value> InterpreterV8::ConstructorMoon(const Arguments &args)
|
---|
2079 | {
|
---|
2080 | AstroArgs local;
|
---|
2081 | try
|
---|
2082 | {
|
---|
2083 | local = EvalAstroArgs(0, args, -1);
|
---|
2084 | }
|
---|
2085 | catch (const exception &e)
|
---|
2086 | {
|
---|
2087 | return ThrowException(String::New(e.what()));
|
---|
2088 | }
|
---|
2089 |
|
---|
2090 | const Nova::EquPosn equ = Nova::GetLunarEquCoords(local.jd, 0.01);
|
---|
2091 |
|
---|
2092 | HandleScope handle_scope;
|
---|
2093 |
|
---|
2094 | // ----------------------------
|
---|
2095 |
|
---|
2096 | if (!args.IsConstructCall())
|
---|
2097 | return handle_scope.Close(Constructor(args));
|
---|
2098 |
|
---|
2099 | Handle<Function> function =
|
---|
2100 | FunctionTemplate::New(MoonToLocal)->GetFunction();
|
---|
2101 | if (function.IsEmpty())
|
---|
2102 | return Undefined();
|
---|
2103 |
|
---|
2104 | Handle<Object> self = args.This();
|
---|
2105 | self->Set(String::New("ra"), Number::New(equ.ra/15), ReadOnly);
|
---|
2106 | self->Set(String::New("dec"), Number::New(equ.dec), ReadOnly);
|
---|
2107 | self->Set(String::New("toLocal"), function, ReadOnly);
|
---|
2108 | self->Set(String::New("time"), Date::New(local.jsdate), ReadOnly);
|
---|
2109 |
|
---|
2110 | return handle_scope.Close(self);
|
---|
2111 | }
|
---|
2112 |
|
---|
2113 | Handle<Value> InterpreterV8::ConstructorSky(const Arguments &args)
|
---|
2114 | {
|
---|
2115 | if (args.Length()<2 || args.Length()>3)
|
---|
2116 | return ThrowException(String::New("Sky constructor takes two or three arguments."));
|
---|
2117 |
|
---|
2118 | if (args.Length()>2 && !args[2]->IsObject())
|
---|
2119 | {
|
---|
2120 | const string n = *String::AsciiValue(args[2]->ToObject()->GetConstructorName());
|
---|
2121 | if (n!="Local")
|
---|
2122 | return ThrowException(String::New("Third argument must be of type Local."));
|
---|
2123 | }
|
---|
2124 |
|
---|
2125 | const double ra = args[0]->NumberValue();
|
---|
2126 | const double dec = args[1]->NumberValue();
|
---|
2127 |
|
---|
2128 | if (!finite(ra) || !finite(dec))
|
---|
2129 | return ThrowException(String::New("The first two arguments to Sky must be valid numbers."));
|
---|
2130 |
|
---|
2131 | // ----------------------------
|
---|
2132 |
|
---|
2133 | HandleScope handle_scope;
|
---|
2134 |
|
---|
2135 | if (!args.IsConstructCall())
|
---|
2136 | return handle_scope.Close(Constructor(args));
|
---|
2137 |
|
---|
2138 | Handle<Function> function =
|
---|
2139 | FunctionTemplate::New(SkyToLocal)->GetFunction();
|
---|
2140 | if (function.IsEmpty())
|
---|
2141 | return Undefined();
|
---|
2142 |
|
---|
2143 | Handle<Object> self = args.This();
|
---|
2144 | self->Set(String::New("ra"), Number::New(ra), ReadOnly);
|
---|
2145 | self->Set(String::New("dec"), Number::New(dec), ReadOnly);
|
---|
2146 | self->Set(String::New("toLocal"), function, ReadOnly);
|
---|
2147 | if (args.Length()==3)
|
---|
2148 | self->Set(String::New("local"), args[2], ReadOnly);
|
---|
2149 |
|
---|
2150 | return handle_scope.Close(self);
|
---|
2151 | }
|
---|
2152 |
|
---|
2153 | Handle<Value> InterpreterV8::ConstructorLocal(const Arguments &args)
|
---|
2154 | {
|
---|
2155 | AstroArgs local;
|
---|
2156 | try
|
---|
2157 | {
|
---|
2158 | local = EvalAstroArgs(2, args, 2);
|
---|
2159 | }
|
---|
2160 | catch (const exception &e)
|
---|
2161 | {
|
---|
2162 | return ThrowException(String::New(e.what()));
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | const double zd = args[0]->NumberValue();
|
---|
2166 | const double az = args[1]->NumberValue();
|
---|
2167 |
|
---|
2168 | if (!finite(zd) || !finite(az))
|
---|
2169 | return ThrowException(String::New("The first two arguments to Local must be valid numbers."));
|
---|
2170 |
|
---|
2171 | // --------------------
|
---|
2172 |
|
---|
2173 | HandleScope handle_scope;
|
---|
2174 |
|
---|
2175 | if (!args.IsConstructCall())
|
---|
2176 | return handle_scope.Close(Constructor(args));
|
---|
2177 |
|
---|
2178 | Handle<Function> function =
|
---|
2179 | FunctionTemplate::New(LocalToSky)->GetFunction();
|
---|
2180 | if (function.IsEmpty())
|
---|
2181 | return Undefined();
|
---|
2182 |
|
---|
2183 | Handle<Object> self = args.This();
|
---|
2184 | self->Set(String::New("zd"), Number::New(zd), ReadOnly);
|
---|
2185 | self->Set(String::New("az"), Number::New(az), ReadOnly);
|
---|
2186 | self->Set(String::New("toSky"), function, ReadOnly);
|
---|
2187 | if (!local.obs.empty())
|
---|
2188 | self->Set(String::New("observatory"), String::New(local.obs.c_str()), ReadOnly);
|
---|
2189 | if (local.jsdate>0)
|
---|
2190 | self->Set(String::New("time"), Date::New(local.jsdate), ReadOnly);
|
---|
2191 |
|
---|
2192 | return handle_scope.Close(self);
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | Handle<Object> ConstructRiseSet(const AstroArgs &args, const Nova::RstTime &rst, const bool &rc)
|
---|
2196 | {
|
---|
2197 | Handle<Object> obj = Object::New();
|
---|
2198 | obj->Set(String::New("time"), Date::New(args.jsdate), ReadOnly);
|
---|
2199 | obj->Set(String::New("observatory"), String::New(args.obs.c_str()), ReadOnly);
|
---|
2200 |
|
---|
2201 | const bool isUp = rc>0 ||
|
---|
2202 | (rst.rise<rst.set && (args.jd>rst.rise && args.jd<rst.set)) ||
|
---|
2203 | (rst.rise>rst.set && (args.jd<rst.set || args.jd>rst.rise));
|
---|
2204 |
|
---|
2205 | obj->Set(String::New("isUp"), Boolean::New(rc>=0 && isUp), ReadOnly);
|
---|
2206 |
|
---|
2207 | if (rc!=0)
|
---|
2208 | return obj;
|
---|
2209 |
|
---|
2210 | Handle<Value> rise = Date::New(Time(rst.rise).JavaDate());
|
---|
2211 | Handle<Value> set = Date::New(Time(rst.set).JavaDate());
|
---|
2212 | Handle<Value> trans = Date::New(Time(rst.transit).JavaDate());
|
---|
2213 | if (rise.IsEmpty() || set.IsEmpty() || trans.IsEmpty())
|
---|
2214 | return Handle<Object>();
|
---|
2215 |
|
---|
2216 | obj->Set(String::New("rise"), rise, ReadOnly);
|
---|
2217 | obj->Set(String::New("set"), set, ReadOnly);
|
---|
2218 | obj->Set(String::New("transit"), trans, ReadOnly);
|
---|
2219 |
|
---|
2220 | return obj;
|
---|
2221 | }
|
---|
2222 |
|
---|
2223 | Handle<Value> InterpreterV8::SunHorizon(const Arguments &args)
|
---|
2224 | {
|
---|
2225 | AstroArgs local;
|
---|
2226 | try
|
---|
2227 | {
|
---|
2228 | local = EvalAstroArgs(1, args, 2);
|
---|
2229 | }
|
---|
2230 | catch (const exception &e)
|
---|
2231 | {
|
---|
2232 | return ThrowException(String::New(e.what()));
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | HandleScope handle_scope;
|
---|
2236 |
|
---|
2237 | double hrz = NAN;
|
---|
2238 | if (args.Length()==0 || args[0]->IsNull())
|
---|
2239 | hrz = LN_SOLAR_STANDART_HORIZON;
|
---|
2240 | if (args.Length()>0 && args[0]->IsNumber())
|
---|
2241 | hrz = args[0]->NumberValue();
|
---|
2242 | if (args.Length()>0 && args[0]->IsString())
|
---|
2243 | {
|
---|
2244 | string arg(Tools::Trim(*String::AsciiValue(args[0])));
|
---|
2245 | transform(arg.begin(), arg.end(), arg.begin(), ::tolower);
|
---|
2246 |
|
---|
2247 | if (arg==string("horizon").substr(0, arg.length()))
|
---|
2248 | hrz = LN_SOLAR_STANDART_HORIZON;
|
---|
2249 | if (arg==string("civil").substr(0, arg.length()))
|
---|
2250 | hrz = LN_SOLAR_CIVIL_HORIZON;
|
---|
2251 | if (arg==string("nautical").substr(0, arg.length()))
|
---|
2252 | hrz = LN_SOLAR_NAUTIC_HORIZON;
|
---|
2253 | if (arg==string("fact").substr(0, arg.length()))
|
---|
2254 | hrz = -13;
|
---|
2255 | if (arg==string("astronomical").substr(0, arg.length()))
|
---|
2256 | hrz = LN_SOLAR_ASTRONOMICAL_HORIZON;
|
---|
2257 | }
|
---|
2258 |
|
---|
2259 | if (!finite(hrz))
|
---|
2260 | return ThrowException(String::New("First argument did not yield a valid number."));
|
---|
2261 |
|
---|
2262 | ln_rst_time sun;
|
---|
2263 | const int rc = ln_get_solar_rst_horizon(local.jd-0.5, &local.posn, hrz, &sun);
|
---|
2264 | Handle<Object> rst = ConstructRiseSet(local, sun, rc);
|
---|
2265 | rst->Set(String::New("horizon"), Number::New(hrz));
|
---|
2266 | return handle_scope.Close(rst);
|
---|
2267 | };
|
---|
2268 |
|
---|
2269 | Handle<Value> InterpreterV8::MoonHorizon(const Arguments &args)
|
---|
2270 | {
|
---|
2271 | AstroArgs local;
|
---|
2272 | try
|
---|
2273 | {
|
---|
2274 | local = EvalAstroArgs(0, args, 2);
|
---|
2275 | }
|
---|
2276 | catch (const exception &e)
|
---|
2277 | {
|
---|
2278 | return ThrowException(String::New(e.what()));
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | HandleScope handle_scope;
|
---|
2282 |
|
---|
2283 | ln_rst_time moon;
|
---|
2284 | const int rc = ln_get_lunar_rst(local.jd-0.5, &local.posn, &moon);
|
---|
2285 | Handle<Object> rst = ConstructRiseSet(local, moon, rc);
|
---|
2286 | return handle_scope.Close(rst);
|
---|
2287 | };
|
---|
2288 | #endif
|
---|
2289 |
|
---|
2290 | // ==========================================================================
|
---|
2291 | // Process control
|
---|
2292 | // ==========================================================================
|
---|
2293 |
|
---|
2294 | bool InterpreterV8::HandleException(TryCatch& try_catch, const char *where)
|
---|
2295 | {
|
---|
2296 | if (!try_catch.HasCaught() || !try_catch.CanContinue())
|
---|
2297 | return true;
|
---|
2298 |
|
---|
2299 | const HandleScope handle_scope;
|
---|
2300 |
|
---|
2301 | Handle<Value> except = try_catch.Exception();
|
---|
2302 | if (except.IsEmpty() || except->IsNull())
|
---|
2303 | return true;
|
---|
2304 |
|
---|
2305 | const String::AsciiValue exception(except);
|
---|
2306 |
|
---|
2307 | const Handle<Message> message = try_catch.Message();
|
---|
2308 | if (message.IsEmpty())
|
---|
2309 | return false;
|
---|
2310 |
|
---|
2311 | ostringstream out;
|
---|
2312 |
|
---|
2313 | if (!message->GetScriptResourceName()->IsUndefined())
|
---|
2314 | {
|
---|
2315 | // Print (filename):(line number): (message).
|
---|
2316 | const String::AsciiValue filename(message->GetScriptResourceName());
|
---|
2317 | if (filename.length()>0)
|
---|
2318 | {
|
---|
2319 | out << *filename;
|
---|
2320 | if (message->GetLineNumber()>0)
|
---|
2321 | out << ": l." << message->GetLineNumber();
|
---|
2322 | if (*exception)
|
---|
2323 | out << ": ";
|
---|
2324 | }
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | if (*exception)
|
---|
2328 | out << *exception;
|
---|
2329 |
|
---|
2330 | out << " [" << where << "]";
|
---|
2331 |
|
---|
2332 | JsException(out.str());
|
---|
2333 |
|
---|
2334 | // Print line of source code.
|
---|
2335 | const String::AsciiValue sourceline(message->GetSourceLine());
|
---|
2336 | if (*sourceline)
|
---|
2337 | JsException(*sourceline);
|
---|
2338 |
|
---|
2339 | // Print wavy underline (GetUnderline is deprecated).
|
---|
2340 | const int start = message->GetStartColumn();
|
---|
2341 | const int end = message->GetEndColumn();
|
---|
2342 |
|
---|
2343 | out.str("");
|
---|
2344 | if (start>0)
|
---|
2345 | out << setfill(' ') << setw(start) << ' ';
|
---|
2346 | out << setfill('^') << setw(end-start) << '^';
|
---|
2347 |
|
---|
2348 | JsException(out.str());
|
---|
2349 |
|
---|
2350 | const String::AsciiValue stack_trace(try_catch.StackTrace());
|
---|
2351 | if (stack_trace.length()<=0)
|
---|
2352 | return false;
|
---|
2353 |
|
---|
2354 | if (!*stack_trace)
|
---|
2355 | return false;
|
---|
2356 |
|
---|
2357 | const string trace(*stack_trace);
|
---|
2358 |
|
---|
2359 | typedef boost::char_separator<char> separator;
|
---|
2360 | const boost::tokenizer<separator> tokenizer(trace, separator("\n"));
|
---|
2361 |
|
---|
2362 | // maybe skip: " at internal:"
|
---|
2363 | // maybe skip: " at unknown source:"
|
---|
2364 |
|
---|
2365 | auto it = tokenizer.begin();
|
---|
2366 | JsException("");
|
---|
2367 | while (it!=tokenizer.end())
|
---|
2368 | JsException(*it++);
|
---|
2369 |
|
---|
2370 | return false;
|
---|
2371 | }
|
---|
2372 |
|
---|
2373 | Handle<Value> InterpreterV8::ExecuteInternal(const string &code)
|
---|
2374 | {
|
---|
2375 | // Try/catch and re-throw hides our internal code from
|
---|
2376 | // the displayed exception showing the origin and shows
|
---|
2377 | // the user function instead.
|
---|
2378 | TryCatch exception;
|
---|
2379 |
|
---|
2380 | const Handle<Value> result = ExecuteCode(code);
|
---|
2381 |
|
---|
2382 | // This hides the location of the exception in the internal code,
|
---|
2383 | // which is wanted.
|
---|
2384 | if (exception.HasCaught())
|
---|
2385 | exception.ReThrow();
|
---|
2386 |
|
---|
2387 | return result;
|
---|
2388 | }
|
---|
2389 |
|
---|
2390 | Handle<Value> InterpreterV8::ExecuteCode(const string &code, const string &file)
|
---|
2391 | {
|
---|
2392 | HandleScope handle_scope;
|
---|
2393 |
|
---|
2394 | const Handle<String> source = String::New(code.c_str(), code.size());
|
---|
2395 | const Handle<String> origin = String::New(file.c_str());
|
---|
2396 | if (source.IsEmpty())
|
---|
2397 | return Undefined();
|
---|
2398 |
|
---|
2399 | const Handle<Script> script = Script::Compile(source, origin);
|
---|
2400 | if (script.IsEmpty())
|
---|
2401 | return Undefined();
|
---|
2402 |
|
---|
2403 | const Handle<String> __date__ = String::New("__DATE__");
|
---|
2404 | const Handle<String> __file__ = String::New("__FILE__");
|
---|
2405 |
|
---|
2406 | Handle<Value> save_date;
|
---|
2407 | Handle<Value> save_file;
|
---|
2408 |
|
---|
2409 | Handle<Object> global = Context::GetCurrent()->Global();
|
---|
2410 | if (!global.IsEmpty())
|
---|
2411 | {
|
---|
2412 | struct stat attrib;
|
---|
2413 | if (stat(file.c_str(), &attrib)==0)
|
---|
2414 | {
|
---|
2415 | save_date = global->Get(__date__);
|
---|
2416 | save_file = global->Get(__file__);
|
---|
2417 |
|
---|
2418 | global->Set(__file__, String::New(file.c_str()));
|
---|
2419 |
|
---|
2420 | const Local<Value> date = Date::New(attrib.st_mtime*1000);
|
---|
2421 | if (!date.IsEmpty())
|
---|
2422 | global->Set(__date__, date);
|
---|
2423 | }
|
---|
2424 | }
|
---|
2425 |
|
---|
2426 | const Handle<Value> rc = script->Run();
|
---|
2427 | if (rc.IsEmpty())
|
---|
2428 | return Undefined();
|
---|
2429 |
|
---|
2430 | if (!global.IsEmpty() && !save_date.IsEmpty())
|
---|
2431 | {
|
---|
2432 | global->ForceSet(__date__, save_date);
|
---|
2433 | global->ForceSet(__file__, save_file);
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | return handle_scope.Close(rc);
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | void InterpreterV8::ExecuteConsole()
|
---|
2440 | {
|
---|
2441 | JsSetState(3);
|
---|
2442 |
|
---|
2443 | WindowLog lout;
|
---|
2444 | lout << "\n " << kUnderline << " JavaScript interpreter " << kReset << " (enter '.q' to quit)\n" << endl;
|
---|
2445 |
|
---|
2446 | Readline::StaticPushHistory("java.his");
|
---|
2447 |
|
---|
2448 | string command;
|
---|
2449 | while (1)
|
---|
2450 | {
|
---|
2451 | // Create a local handle scope so that left-overs from single
|
---|
2452 | // console inputs will not fill up the memory
|
---|
2453 | const HandleScope handle_scope;
|
---|
2454 |
|
---|
2455 | // Unlocking is necessary for the preemption to work
|
---|
2456 | const Unlocker global_unlock;
|
---|
2457 |
|
---|
2458 | const string buffer = Tools::Trim(Readline::StaticPrompt(command.empty() ? "JS> " : " \\> "));
|
---|
2459 | if (buffer==".q")
|
---|
2460 | break;
|
---|
2461 |
|
---|
2462 | // buffer empty, do nothing
|
---|
2463 | if (buffer.empty())
|
---|
2464 | continue;
|
---|
2465 |
|
---|
2466 | // Compose command
|
---|
2467 | if (!command.empty())
|
---|
2468 | command += ' ';
|
---|
2469 | command += buffer;
|
---|
2470 |
|
---|
2471 | // If line ends with a backslash, allow addition of next line
|
---|
2472 | auto back = command.rbegin();
|
---|
2473 | if (*back=='\\')
|
---|
2474 | {
|
---|
2475 | *back = ' ';
|
---|
2476 | command = Tools::Trim(command);
|
---|
2477 | continue;
|
---|
2478 | }
|
---|
2479 |
|
---|
2480 | // Locking is necessary to be able to execute java script code
|
---|
2481 | const Locker lock;
|
---|
2482 |
|
---|
2483 | // Catch exceptions during code compilation
|
---|
2484 | TryCatch exception;
|
---|
2485 |
|
---|
2486 | // Execute code which was entered
|
---|
2487 | const Handle<Value> rc = ExecuteCode(command, "console");
|
---|
2488 |
|
---|
2489 | // If all went well and the result wasn't undefined then print
|
---|
2490 | // the returned value.
|
---|
2491 | if (!rc->IsUndefined() && !rc->IsFunction())
|
---|
2492 | JsResult(*String::AsciiValue(rc));
|
---|
2493 |
|
---|
2494 | if (!HandleException(exception, "console"))
|
---|
2495 | lout << endl;
|
---|
2496 |
|
---|
2497 | // Stop all other threads
|
---|
2498 | for (auto it=fThreadIds.begin(); it!=fThreadIds.end(); it++)
|
---|
2499 | V8::TerminateExecution(*it);
|
---|
2500 |
|
---|
2501 | // Allow the java scripts (threads) to run and hence to terminate
|
---|
2502 | const Unlocker unlock;
|
---|
2503 |
|
---|
2504 | // Wait until all threads are terminated
|
---|
2505 | while (!fThreadIds.empty())
|
---|
2506 | usleep(1000);
|
---|
2507 |
|
---|
2508 | // command has been executed, collect new command
|
---|
2509 | command = "";
|
---|
2510 | }
|
---|
2511 |
|
---|
2512 | lout << endl;
|
---|
2513 |
|
---|
2514 | Readline::StaticPopHistory("java.his");
|
---|
2515 | }
|
---|
2516 |
|
---|
2517 | // ==========================================================================
|
---|
2518 | // CORE
|
---|
2519 | // ==========================================================================
|
---|
2520 |
|
---|
2521 | InterpreterV8::InterpreterV8() : fThreadId(-1)
|
---|
2522 | {
|
---|
2523 | const string ver(V8::GetVersion());
|
---|
2524 |
|
---|
2525 | typedef boost::char_separator<char> separator;
|
---|
2526 | const boost::tokenizer<separator> tokenizer(ver, separator("."));
|
---|
2527 |
|
---|
2528 | const vector<string> tok(tokenizer.begin(), tokenizer.end());
|
---|
2529 |
|
---|
2530 | const int major = tok.size()>0 ? stol(tok[0]) : -1;
|
---|
2531 | const int minor = tok.size()>1 ? stol(tok[1]) : -1;
|
---|
2532 | const int build = tok.size()>2 ? stol(tok[2]) : -1;
|
---|
2533 |
|
---|
2534 | if (major>3 || (major==3 && minor>9) || (major==3 && minor==9 && build>10))
|
---|
2535 | {
|
---|
2536 | const string argv = "--use_strict";
|
---|
2537 | V8::SetFlagsFromString(argv.c_str(), argv.size());
|
---|
2538 | }
|
---|
2539 |
|
---|
2540 | /*
|
---|
2541 | const string argv1 = "--prof";
|
---|
2542 | const string argv2 = "--noprof-lazy";
|
---|
2543 |
|
---|
2544 | V8::SetFlagsFromString(argv1.c_str(), argv1.size());
|
---|
2545 | V8::SetFlagsFromString(argv2.c_str(), argv2.size());
|
---|
2546 | */
|
---|
2547 |
|
---|
2548 | This = this;
|
---|
2549 | }
|
---|
2550 |
|
---|
2551 | Handle<Value> InterpreterV8::Constructor(/*Handle<FunctionTemplate> T,*/ const Arguments &args)
|
---|
2552 | {
|
---|
2553 | Handle<Value> argv[args.Length()];
|
---|
2554 |
|
---|
2555 | for (int i=0; i<args.Length(); i++)
|
---|
2556 | argv[i] = args[i];
|
---|
2557 |
|
---|
2558 | return args.Callee()->NewInstance(args.Length(), argv);
|
---|
2559 | }
|
---|
2560 |
|
---|
2561 |
|
---|
2562 | void InterpreterV8::AddFormatToGlobal()// const
|
---|
2563 | {
|
---|
2564 | const string code =
|
---|
2565 | "String.form = function(str, arr)"
|
---|
2566 | "{"
|
---|
2567 | "var i = -1;"
|
---|
2568 | "function callback(exp, p0, p1, p2, p3, p4/*, pos, str*/)"
|
---|
2569 | "{"
|
---|
2570 | "if (exp=='%%')"
|
---|
2571 | "return '%';"
|
---|
2572 | ""
|
---|
2573 | "if (arr[++i]===undefined)"
|
---|
2574 | "return undefined;"
|
---|
2575 | ""
|
---|
2576 | "var exp = p2 ? parseInt(p2.substr(1)) : undefined;"
|
---|
2577 | "var base = p3 ? parseInt(p3.substr(1)) : undefined;"
|
---|
2578 | ""
|
---|
2579 | "var val;"
|
---|
2580 | "switch (p4)"
|
---|
2581 | "{"
|
---|
2582 | "case 's': val = arr[i]; break;"
|
---|
2583 | "case 'c': val = arr[i][0]; break;"
|
---|
2584 | "case 'f': val = parseFloat(arr[i]).toFixed(exp); break;"
|
---|
2585 | "case 'p': val = parseFloat(arr[i]).toPrecision(exp); break;"
|
---|
2586 | "case 'e': val = parseFloat(arr[i]).toExponential(exp); break;"
|
---|
2587 | "case 'x': val = parseInt(arr[i]).toString(base?base:16); break;"
|
---|
2588 | "case 'd': val = parseFloat(parseInt(arr[i], base?base:10).toPrecision(exp)).toFixed(0); break;"
|
---|
2589 | //"default:\n"
|
---|
2590 | //" throw new SyntaxError('Conversion specifier '+p4+' unknown.');\n"
|
---|
2591 | "}"
|
---|
2592 | ""
|
---|
2593 | "val = typeof(val)=='object' ? JSON.stringify(val) : val.toString(base);"
|
---|
2594 | ""
|
---|
2595 | "var sz = parseInt(p1); /* padding size */"
|
---|
2596 | "var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */"
|
---|
2597 | "while (val.length<sz)"
|
---|
2598 | "val = p0 !== undefined ? val+ch : ch+val; /* isminus? */"
|
---|
2599 | ""
|
---|
2600 | "return val;"
|
---|
2601 | "}"
|
---|
2602 | ""
|
---|
2603 | "var regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd])/g;"
|
---|
2604 | "return str.replace(regex, callback);"
|
---|
2605 | "}"
|
---|
2606 | "\n"
|
---|
2607 | "String.prototype.$ = function()"
|
---|
2608 | "{"
|
---|
2609 | "return String.form(this, Array.prototype.slice.call(arguments));"
|
---|
2610 | "}"
|
---|
2611 | "\n"
|
---|
2612 | "String.prototype.count = function(c,i)"
|
---|
2613 | "{"
|
---|
2614 | "return (this.match(new RegExp(c,i?'gi':'g'))||[]).length;"
|
---|
2615 | "}"/*
|
---|
2616 | "\n"
|
---|
2617 | "var format = function()"
|
---|
2618 | "{"
|
---|
2619 | "return dim.format(arguments[0], Array.prototype.slice.call(arguments,1));"
|
---|
2620 | "}"*/;
|
---|
2621 |
|
---|
2622 | // ExcuteInternal does not work properly here...
|
---|
2623 | // If suring compilation an exception is thrown, it will not work
|
---|
2624 | Handle<Script> script = Script::New(String::New(code.c_str()), String::New("internal"));
|
---|
2625 | if (!script.IsEmpty())
|
---|
2626 | script->Run();
|
---|
2627 | }
|
---|
2628 |
|
---|
2629 | void InterpreterV8::JsLoad(const std::string &)
|
---|
2630 | {
|
---|
2631 | Readline::SetScriptDepth(1);
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 | void InterpreterV8::JsEnd(const std::string &)
|
---|
2635 | {
|
---|
2636 | Readline::SetScriptDepth(0);
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
|
---|
2640 | {
|
---|
2641 | const Locker locker;
|
---|
2642 | fThreadId = V8::GetCurrentThreadId();
|
---|
2643 |
|
---|
2644 | JsPrint(string("JavaScript Engine V8 ")+V8::GetVersion());
|
---|
2645 |
|
---|
2646 | JsLoad(filename);
|
---|
2647 |
|
---|
2648 | const HandleScope handle_scope;
|
---|
2649 |
|
---|
2650 | // Create a template for the global object.
|
---|
2651 | Handle<ObjectTemplate> dim = ObjectTemplate::New();
|
---|
2652 | dim->Set(String::New("log"), FunctionTemplate::New(WrapLog), ReadOnly);
|
---|
2653 | dim->Set(String::New("alarm"), FunctionTemplate::New(WrapAlarm), ReadOnly);
|
---|
2654 | dim->Set(String::New("wait"), FunctionTemplate::New(WrapWait), ReadOnly);
|
---|
2655 | dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly);
|
---|
2656 | dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly);
|
---|
2657 | dim->Set(String::New("version"), Integer::New(DIM_VERSION_NUMBER), ReadOnly);
|
---|
2658 | dim->Set(String::New("getStates"), FunctionTemplate::New(WrapGetStates), ReadOnly);
|
---|
2659 | dim->Set(String::New("getDescription"), FunctionTemplate::New(WrapGetDescription), ReadOnly);
|
---|
2660 | dim->Set(String::New("getServices"), FunctionTemplate::New(WrapGetServices), ReadOnly);
|
---|
2661 |
|
---|
2662 | Handle<ObjectTemplate> dimctrl = ObjectTemplate::New();
|
---|
2663 | dimctrl->Set(String::New("defineState"), FunctionTemplate::New(WrapNewState), ReadOnly);
|
---|
2664 | dimctrl->Set(String::New("setState"), FunctionTemplate::New(WrapSetState), ReadOnly);
|
---|
2665 | dimctrl->Set(String::New("getState"), FunctionTemplate::New(WrapGetState), ReadOnly);
|
---|
2666 | dimctrl->Set(String::New("setInterruptHandler"), FunctionTemplate::New(WrapSetInterrupt), ReadOnly);
|
---|
2667 | dimctrl->Set(String::New("triggerInterrupt"), FunctionTemplate::New(WrapTriggerInterrupt), ReadOnly);
|
---|
2668 |
|
---|
2669 | Handle<ObjectTemplate> v8 = ObjectTemplate::New();
|
---|
2670 | v8->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly);
|
---|
2671 | v8->Set(String::New("timeout"), FunctionTemplate::New(WrapTimeout), ReadOnly);
|
---|
2672 | v8->Set(String::New("version"), String::New(V8::GetVersion()), ReadOnly);
|
---|
2673 |
|
---|
2674 | Handle<ObjectTemplate> console = ObjectTemplate::New();
|
---|
2675 | console->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
|
---|
2676 | console->Set(String::New("warn"), FunctionTemplate::New(WrapWarn), ReadOnly);
|
---|
2677 |
|
---|
2678 | Handle<ObjectTemplate> onchange = ObjectTemplate::New();
|
---|
2679 | onchange->SetNamedPropertyHandler(OnChangeGet, WrapOnChangeSet);
|
---|
2680 | dim->Set(String::New("onchange"), onchange);
|
---|
2681 |
|
---|
2682 | Handle<ObjectTemplate> global = ObjectTemplate::New();
|
---|
2683 | global->Set(String::New("v8"), v8, ReadOnly);
|
---|
2684 | global->Set(String::New("dim"), dim, ReadOnly);
|
---|
2685 | global->Set(String::New("dimctrl"), dimctrl, ReadOnly);
|
---|
2686 | global->Set(String::New("console"), console, ReadOnly);
|
---|
2687 | global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly);
|
---|
2688 | global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly);
|
---|
2689 |
|
---|
2690 | Handle<FunctionTemplate> sub = FunctionTemplate::New(WrapSubscription);
|
---|
2691 | sub->SetClassName(String::New("Subscription"));
|
---|
2692 | sub->InstanceTemplate()->SetInternalFieldCount(1);
|
---|
2693 | global->Set(String::New("Subscription"), sub, ReadOnly);
|
---|
2694 |
|
---|
2695 | #ifdef HAVE_SQL
|
---|
2696 | Handle<FunctionTemplate> db = FunctionTemplate::New(WrapDatabase);
|
---|
2697 | db->SetClassName(String::New("Database"));
|
---|
2698 | db->InstanceTemplate()->SetInternalFieldCount(1);
|
---|
2699 | global->Set(String::New("Database"), db, ReadOnly);
|
---|
2700 | #endif
|
---|
2701 |
|
---|
2702 | Handle<FunctionTemplate> thread = FunctionTemplate::New(WrapThread);
|
---|
2703 | thread->SetClassName(String::New("Thread"));
|
---|
2704 | global->Set(String::New("Thread"), thread, ReadOnly);
|
---|
2705 |
|
---|
2706 | Handle<FunctionTemplate> file = FunctionTemplate::New(WrapFile);
|
---|
2707 | file->SetClassName(String::New("File"));
|
---|
2708 | global->Set(String::New("File"), file, ReadOnly);
|
---|
2709 |
|
---|
2710 | Handle<FunctionTemplate> evt = FunctionTemplate::New();
|
---|
2711 | evt->SetClassName(String::New("Event"));
|
---|
2712 | global->Set(String::New("Event"), evt, ReadOnly);
|
---|
2713 |
|
---|
2714 | Handle<FunctionTemplate> desc = FunctionTemplate::New();
|
---|
2715 | desc->SetClassName(String::New("Description"));
|
---|
2716 | global->Set(String::New("Description"), desc, ReadOnly);
|
---|
2717 |
|
---|
2718 | fTemplateEvent = evt;
|
---|
2719 | fTemplateDescription = desc;
|
---|
2720 |
|
---|
2721 | #ifdef HAVE_MAILX
|
---|
2722 | Handle<FunctionTemplate> mail = FunctionTemplate::New(ConstructorMail);
|
---|
2723 | mail->SetClassName(String::New("Mail"));
|
---|
2724 | global->Set(String::New("Mail"), mail, ReadOnly);
|
---|
2725 | #endif
|
---|
2726 |
|
---|
2727 | #ifdef HAVE_CURL
|
---|
2728 | Handle<FunctionTemplate> curl = FunctionTemplate::New(ConstructorCurl);
|
---|
2729 | mail->SetClassName(String::New("Curl"));
|
---|
2730 | global->Set(String::New("Curl"), curl, ReadOnly);
|
---|
2731 | #endif
|
---|
2732 |
|
---|
2733 | #ifdef HAVE_NOVA
|
---|
2734 | Handle<FunctionTemplate> sky = FunctionTemplate::New(ConstructorSky);
|
---|
2735 | sky->SetClassName(String::New("Sky"));
|
---|
2736 | sky->Set(String::New("dist"), FunctionTemplate::New(SkyDist), ReadOnly);
|
---|
2737 | global->Set(String::New("Sky"), sky, ReadOnly);
|
---|
2738 |
|
---|
2739 | Handle<FunctionTemplate> loc = FunctionTemplate::New(ConstructorLocal);
|
---|
2740 | loc->SetClassName(String::New("Local"));
|
---|
2741 | loc->Set(String::New("dist"), FunctionTemplate::New(LocalDist), ReadOnly);
|
---|
2742 | global->Set(String::New("Local"), loc, ReadOnly);
|
---|
2743 |
|
---|
2744 | Handle<FunctionTemplate> moon = FunctionTemplate::New(ConstructorMoon);
|
---|
2745 | moon->SetClassName(String::New("Moon"));
|
---|
2746 | moon->Set(String::New("disk"), FunctionTemplate::New(MoonDisk), ReadOnly);
|
---|
2747 | moon->Set(String::New("horizon"), FunctionTemplate::New(MoonHorizon), ReadOnly);
|
---|
2748 | global->Set(String::New("Moon"), moon, ReadOnly);
|
---|
2749 |
|
---|
2750 | Handle<FunctionTemplate> sun = FunctionTemplate::New();
|
---|
2751 | sun->SetClassName(String::New("Sun"));
|
---|
2752 | sun->Set(String::New("horizon"), FunctionTemplate::New(SunHorizon), ReadOnly);
|
---|
2753 | global->Set(String::New("Sun"), sun, ReadOnly);
|
---|
2754 |
|
---|
2755 | fTemplateLocal = loc;
|
---|
2756 | fTemplateSky = sky;
|
---|
2757 | #endif
|
---|
2758 |
|
---|
2759 | // Persistent
|
---|
2760 | Persistent<Context> context = Context::New(NULL, global);
|
---|
2761 | if (context.IsEmpty())
|
---|
2762 | {
|
---|
2763 | JsException("Creation of global context failed...");
|
---|
2764 | JsEnd(filename);
|
---|
2765 | return false;
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 | // Switch off eval(). It is not possible to track it's exceptions.
|
---|
2769 | context->AllowCodeGenerationFromStrings(false);
|
---|
2770 |
|
---|
2771 | Context::Scope scope(context);
|
---|
2772 |
|
---|
2773 | Handle<Array> args = Array::New(map.size());
|
---|
2774 | for (auto it=map.begin(); it!=map.end(); it++)
|
---|
2775 | args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
|
---|
2776 | context->Global()->Set(String::New("$"), args, ReadOnly);
|
---|
2777 | context->Global()->Set(String::New("arg"), args, ReadOnly);
|
---|
2778 |
|
---|
2779 | const Local<Value> starttime = Date::New(Time().JavaDate());
|
---|
2780 | if (!starttime.IsEmpty())
|
---|
2781 | context->Global()->Set(String::New("__START__"), starttime, ReadOnly);
|
---|
2782 |
|
---|
2783 | //V8::ResumeProfiler();
|
---|
2784 |
|
---|
2785 | TryCatch exception;
|
---|
2786 |
|
---|
2787 | AddFormatToGlobal();
|
---|
2788 |
|
---|
2789 | if (!exception.HasCaught())
|
---|
2790 | {
|
---|
2791 | JsStart(filename);
|
---|
2792 |
|
---|
2793 | Locker::StartPreemption(10);
|
---|
2794 |
|
---|
2795 | if (filename.empty())
|
---|
2796 | ExecuteConsole();
|
---|
2797 | else
|
---|
2798 | {
|
---|
2799 | // We call script->Run because it is the only way to
|
---|
2800 | // catch exceptions.
|
---|
2801 | const Handle<String> source = String::New(("include('"+filename+"');").c_str());
|
---|
2802 | const Handle<String> origin = String::New("main");
|
---|
2803 | const Handle<Script> script = Script::Compile(source, origin);
|
---|
2804 | if (!script.IsEmpty())
|
---|
2805 | {
|
---|
2806 | JsSetState(3);
|
---|
2807 | script->Run();
|
---|
2808 | }
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | Locker::StopPreemption();
|
---|
2812 |
|
---|
2813 | // Stop all other threads
|
---|
2814 | for (auto it=fThreadIds.begin(); it!=fThreadIds.end(); it++)
|
---|
2815 | V8::TerminateExecution(*it);
|
---|
2816 | fThreadIds.clear();
|
---|
2817 | }
|
---|
2818 |
|
---|
2819 | // Handle an exception
|
---|
2820 | /*const bool rc =*/ HandleException(exception, "main");
|
---|
2821 |
|
---|
2822 | // IsProfilerPaused()
|
---|
2823 | // V8::PauseProfiler();
|
---|
2824 |
|
---|
2825 | // -----
|
---|
2826 | // This is how an exit handler could look like, but there is no way to interrupt it
|
---|
2827 | // -----
|
---|
2828 | // Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("dim")));
|
---|
2829 | // if (!obj.IsEmpty())
|
---|
2830 | // {
|
---|
2831 | // Handle<Value> onexit = obj->Get(String::New("onexit"));
|
---|
2832 | // if (!onexit->IsUndefined())
|
---|
2833 | // Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
|
---|
2834 | // // Handle<Object> result = Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
|
---|
2835 | // }
|
---|
2836 |
|
---|
2837 | //context->Exit();
|
---|
2838 |
|
---|
2839 | // The threads are started already and wait to get the lock
|
---|
2840 | // So we have to unlock (manual preemtion) so that they get
|
---|
2841 | // the signal to terminate.
|
---|
2842 | {
|
---|
2843 | const Unlocker unlock;
|
---|
2844 |
|
---|
2845 | for (auto it=fThreads.begin(); it!=fThreads.end(); it++)
|
---|
2846 | it->join();
|
---|
2847 | fThreads.clear();
|
---|
2848 | }
|
---|
2849 |
|
---|
2850 | // Now we can dispose all persistent handles from state callbacks
|
---|
2851 | for (auto it=fStateCallbacks.begin(); it!=fStateCallbacks.end(); it++)
|
---|
2852 | it->second.Dispose();
|
---|
2853 | fStateCallbacks.clear();
|
---|
2854 |
|
---|
2855 | // Now we can dispose the persistent interrupt handler
|
---|
2856 | fInterruptCallback.Dispose();
|
---|
2857 | fInterruptCallback.Clear();
|
---|
2858 |
|
---|
2859 | // Now we can dispose all persistent handles from reverse maps
|
---|
2860 | for (auto it=fReverseMap.begin(); it!=fReverseMap.end(); it++)
|
---|
2861 | it->second.Dispose();
|
---|
2862 | fReverseMap.clear();
|
---|
2863 |
|
---|
2864 | #ifdef HAVE_SQL
|
---|
2865 | // ...and close all database handles
|
---|
2866 | for (auto it=fDatabases.begin(); it!=fDatabases.end(); it++)
|
---|
2867 | delete *it;
|
---|
2868 | fDatabases.clear();
|
---|
2869 | #endif
|
---|
2870 |
|
---|
2871 | fStates.clear();
|
---|
2872 |
|
---|
2873 | context.Dispose();
|
---|
2874 |
|
---|
2875 | JsEnd(filename);
|
---|
2876 |
|
---|
2877 | return true;
|
---|
2878 | }
|
---|
2879 |
|
---|
2880 | void InterpreterV8::JsStop()
|
---|
2881 | {
|
---|
2882 | Locker locker;
|
---|
2883 | V8::TerminateExecution(This->fThreadId);
|
---|
2884 | }
|
---|
2885 |
|
---|
2886 | vector<string> InterpreterV8::JsGetCommandList(const char *, int) const
|
---|
2887 | {
|
---|
2888 | vector<string> rc;
|
---|
2889 |
|
---|
2890 | rc.emplace_back("for (");
|
---|
2891 | rc.emplace_back("while (");
|
---|
2892 | rc.emplace_back("if (");
|
---|
2893 | rc.emplace_back("switch (");
|
---|
2894 | rc.emplace_back("case ");
|
---|
2895 | rc.emplace_back("var ");
|
---|
2896 | rc.emplace_back("function ");
|
---|
2897 | rc.emplace_back("Date(");
|
---|
2898 | rc.emplace_back("new Date(");
|
---|
2899 | rc.emplace_back("'use strict';");
|
---|
2900 | rc.emplace_back("undefined");
|
---|
2901 | rc.emplace_back("null");
|
---|
2902 | rc.emplace_back("delete ");
|
---|
2903 | rc.emplace_back("JSON.stringify(");
|
---|
2904 | rc.emplace_back("JSON.parse(");
|
---|
2905 |
|
---|
2906 | rc.emplace_back("dim.log(");
|
---|
2907 | rc.emplace_back("dim.alarm(");
|
---|
2908 | rc.emplace_back("dim.wait(");
|
---|
2909 | rc.emplace_back("dim.send(");
|
---|
2910 | rc.emplace_back("dim.state(");
|
---|
2911 | rc.emplace_back("dim.version");
|
---|
2912 | rc.emplace_back("dim.getStates(");
|
---|
2913 | rc.emplace_back("dim.getDescription(");
|
---|
2914 | rc.emplace_back("dim.getServices(");
|
---|
2915 |
|
---|
2916 | rc.emplace_back("dimctrl.defineState(");
|
---|
2917 | rc.emplace_back("dimctrl.setState(");
|
---|
2918 | rc.emplace_back("dimctrl.getState(");
|
---|
2919 | rc.emplace_back("dimctrl.setInterruptHandler(");
|
---|
2920 | rc.emplace_back("dimctrl.triggerInterrupt(");
|
---|
2921 |
|
---|
2922 | rc.emplace_back("v8.sleep(");
|
---|
2923 | rc.emplace_back("v8.timeout(");
|
---|
2924 | rc.emplace_back("v8.version()");
|
---|
2925 |
|
---|
2926 | rc.emplace_back("console.out(");
|
---|
2927 | rc.emplace_back("console.warn(");
|
---|
2928 |
|
---|
2929 | rc.emplace_back("include(");
|
---|
2930 | rc.emplace_back("exit()");
|
---|
2931 |
|
---|
2932 | #ifdef HAVE_SQL
|
---|
2933 | rc.emplace_back("Database(");
|
---|
2934 | rc.emplace_back("new Database(");
|
---|
2935 |
|
---|
2936 | rc.emplace_back(".table");
|
---|
2937 | rc.emplace_back(".user");
|
---|
2938 | rc.emplace_back(".database");
|
---|
2939 | rc.emplace_back(".port");
|
---|
2940 | rc.emplace_back(".query");
|
---|
2941 | #endif
|
---|
2942 |
|
---|
2943 | rc.emplace_back("Subscription(");
|
---|
2944 | rc.emplace_back("new Subscription(");
|
---|
2945 |
|
---|
2946 | rc.emplace_back("Thread(");
|
---|
2947 | rc.emplace_back("new Thread(");
|
---|
2948 |
|
---|
2949 | rc.emplace_back("File(");
|
---|
2950 | rc.emplace_back("new File(");
|
---|
2951 |
|
---|
2952 | rc.emplace_back("Event(");
|
---|
2953 | rc.emplace_back("new Event(");
|
---|
2954 |
|
---|
2955 | rc.emplace_back("Description(");
|
---|
2956 | rc.emplace_back("new Description(");
|
---|
2957 |
|
---|
2958 | #ifdef HAVE_MAILX
|
---|
2959 | rc.emplace_back("Mail(");
|
---|
2960 | rc.emplace_back("new Mail(");
|
---|
2961 |
|
---|
2962 | rc.emplace_back(".subject");
|
---|
2963 | rc.emplace_back(".receipients");
|
---|
2964 | rc.emplace_back(".attachments");
|
---|
2965 | rc.emplace_back(".bcc");
|
---|
2966 | rc.emplace_back(".cc");
|
---|
2967 | rc.emplace_back(".text");
|
---|
2968 | rc.emplace_back(".send(");
|
---|
2969 | #endif
|
---|
2970 |
|
---|
2971 | #ifdef HAVE_CURL
|
---|
2972 | rc.emplace_back("Curl(");
|
---|
2973 | rc.emplace_back("new Curl(");
|
---|
2974 |
|
---|
2975 | rc.emplace_back(".url");
|
---|
2976 | rc.emplace_back(".user");
|
---|
2977 | rc.emplace_back(".data");
|
---|
2978 | // rc.emplace_back(".send("); -> MAILX
|
---|
2979 | #endif
|
---|
2980 |
|
---|
2981 | #ifdef HAVE_NOVA
|
---|
2982 | rc.emplace_back("Sky(");
|
---|
2983 | rc.emplace_back("new Sky(");
|
---|
2984 |
|
---|
2985 | rc.emplace_back("Sky.dist");
|
---|
2986 | rc.emplace_back("Local(");
|
---|
2987 |
|
---|
2988 | rc.emplace_back("new Local(");
|
---|
2989 | rc.emplace_back("Local.dist");
|
---|
2990 |
|
---|
2991 | rc.emplace_back("Moon(");
|
---|
2992 | rc.emplace_back("new Moon(");
|
---|
2993 | rc.emplace_back("Moon.disk(");
|
---|
2994 | rc.emplace_back("Moon.horizon(");
|
---|
2995 |
|
---|
2996 | rc.emplace_back("Sun.horizon(");
|
---|
2997 |
|
---|
2998 | rc.emplace_back(".zd");
|
---|
2999 | rc.emplace_back(".az");
|
---|
3000 | rc.emplace_back(".ra");
|
---|
3001 | rc.emplace_back(".dec");
|
---|
3002 |
|
---|
3003 | rc.emplace_back(".toLocal(");
|
---|
3004 | rc.emplace_back(".toSky(");
|
---|
3005 | rc.emplace_back(".rise");
|
---|
3006 | rc.emplace_back(".set");
|
---|
3007 | rc.emplace_back(".transit");
|
---|
3008 | rc.emplace_back(".isUp");
|
---|
3009 |
|
---|
3010 | rc.emplace_back("horizon");
|
---|
3011 | rc.emplace_back("civil");
|
---|
3012 | rc.emplace_back("nautical");
|
---|
3013 | rc.emplace_back("astronomical");
|
---|
3014 | #endif
|
---|
3015 |
|
---|
3016 | rc.emplace_back(".server");
|
---|
3017 | rc.emplace_back(".service");
|
---|
3018 | rc.emplace_back(".name");
|
---|
3019 | rc.emplace_back(".isCommand");
|
---|
3020 | rc.emplace_back(".format");
|
---|
3021 | rc.emplace_back(".description");
|
---|
3022 | rc.emplace_back(".unit");
|
---|
3023 | rc.emplace_back(".delim");
|
---|
3024 | rc.emplace_back(".isOpen");
|
---|
3025 |
|
---|
3026 | rc.emplace_back(".qos");
|
---|
3027 | rc.emplace_back(".size");
|
---|
3028 | rc.emplace_back(".counter");
|
---|
3029 | rc.emplace_back(".type");
|
---|
3030 | rc.emplace_back(".obj");
|
---|
3031 | rc.emplace_back(".data");
|
---|
3032 | rc.emplace_back(".comment");
|
---|
3033 | rc.emplace_back(".index");
|
---|
3034 | rc.emplace_back(".time");
|
---|
3035 | rc.emplace_back(".close()");
|
---|
3036 | rc.emplace_back(".onchange");
|
---|
3037 | rc.emplace_back(".get(");
|
---|
3038 |
|
---|
3039 |
|
---|
3040 | rc.emplace_back("__DATE__");
|
---|
3041 | rc.emplace_back("__FILE__");
|
---|
3042 |
|
---|
3043 | return rc;
|
---|
3044 | }
|
---|
3045 |
|
---|
3046 | #endif
|
---|
3047 |
|
---|
3048 | InterpreterV8 *InterpreterV8::This = 0;
|
---|