source: trunk/FACT++/src/InterpreterV8.cc@ 18523

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