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

Last change on this file since 14574 was 14572, checked in by tbretz, 12 years ago
Allow double subscription to a service.
File size: 38.0 KB
Line 
1#include "InterpreterV8.h"
2
3#ifdef HAVE_SQL
4#include "Database.h"
5#endif
6
7#include <boost/tokenizer.hpp>
8
9InterpreterV8 *InterpreterV8::This = 0;
10
11#ifdef HAVE_V8
12
13#include <v8.h>
14#include <fstream>
15#include <sstream>
16#include <iomanip>
17
18using namespace std;
19using namespace v8;
20
21bool InterpreterV8::ReportException(TryCatch* try_catch)
22{
23 if (!try_catch->CanContinue())
24 return false;
25
26 const HandleScope handle_scope;
27
28 const String::Utf8Value exception(try_catch->Exception());
29
30 if (*exception && string(*exception)=="exit")
31 return true;
32 if (*exception && string(*exception)=="null")
33 return false;
34
35 const Handle<Message> message = try_catch->Message();
36 if (message.IsEmpty())
37 return false;
38
39 // Print (filename):(line number): (message).
40 const String::Utf8Value filename(message->GetScriptResourceName());
41
42 ostringstream out;
43
44 if (*filename)
45 out << *filename << ": ";
46 out << "l." << message->GetLineNumber();
47 if (*exception)
48 out << ": " << *exception;
49
50 JsException(out.str());
51
52 // Print line of source code.
53 const String::Utf8Value sourceline(message->GetSourceLine());
54 if (*sourceline)
55 JsException(*sourceline);
56
57 // Print wavy underline (GetUnderline is deprecated).
58 const int start = message->GetStartColumn();
59 const int end = message->GetEndColumn();
60
61 out.str("");
62 if (start>0)
63 out << setfill(' ') << setw(start) << ' ';
64 out << setfill('^') << setw(end-start) << '^';
65
66 JsException(out.str());
67
68 String::Utf8Value stack_trace(try_catch->StackTrace());
69 if (stack_trace.length()<=0)
70 return false;
71
72 //if (*stack_trace)
73 // JsException(string("\n")+*stack_trace);
74
75 return false;
76}
77
78// Executes a string within the current v8 context.
79bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file)
80{
81 if (code.IsEmpty())
82 return true;
83
84 const HandleScope handle_scope;
85
86 const Handle<Script> script = Script::Compile(code, file);
87 if (script.IsEmpty())
88 return false;
89
90 JsSetState(3);
91
92 const Handle<Value> result = script->Run();
93 if (result.IsEmpty())
94 return false;
95
96 // If all went well and the result wasn't undefined then print
97 // the returned value.
98 if (!result->IsUndefined())
99 JsResult(*String::Utf8Value(result));
100
101 return true;
102}
103
104bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file)
105{
106 TryCatch exception;
107
108 const bool rc = ExecuteStringNT(code, file);
109
110 // Check if this is a termination exception
111 //if (!exception.CanContinue())
112 // return false;
113
114 if (exception.HasCaught())
115 return ReportException(&exception);
116
117 return rc;
118}
119
120bool InterpreterV8::ExecuteCode(const string &code, const string &file)
121{
122 return ExecuteCode(String::New(code.c_str(), code.size()),
123 String::New(file.c_str()));
124}
125
126bool InterpreterV8::ExecuteFile(const string &name)
127{
128 ifstream fin(name.c_str());
129 if (!fin)
130 {
131 JsException("Error - Could not open file '"+name+"'");
132 return false;
133 }
134
135 string buffer;
136 if (!getline(fin, buffer, '\0'))
137 return true;
138
139 if (fin.fail())
140 {
141 JsException("Error - reading file.");
142 return false;
143 }
144
145 return ExecuteCode(buffer, name);
146}
147
148Handle<Value> InterpreterV8::FuncWait(const Arguments& args)
149{
150 if (args.Length()!=2 && args.Length()!=3)
151 return ThrowException(String::New("Number of arguments must be 2 or 3."));
152
153 if (!args[0]->IsString())
154 return ThrowException(String::New("Argument 1 not a string."));
155
156 if (!args[1]->IsInt32() && !args[1]->IsString())
157 return ThrowException(String::New("Argument 2 not an int32 and not a string."));
158
159 if (args.Length()==3 && !args[2]->IsUint32())
160 return ThrowException(String::New("Argument 3 not an uint32."));
161
162 // Using a Javascript function has the advantage that it is fully
163 // interruptable without the need of C++ code
164
165 const string index = args[1]->IsInt32() ? "s.index" : "s.name";
166 const bool timeout = args.Length()==3;
167 const string arg0 = *String::Utf8Value(args[0]);
168 const string state = args[1]->IsString() ? *String::Utf8Value(args[1]) : "";
169 const string arg1 = args[1]->IsString() ? ("\""+state+"\"") : to_string(args[1]->Int32Value());
170
171 if (arg0.find_first_of("\"'")!=string::npos)
172 return ThrowException(String::New("Server name must not contain quotation marks."));
173
174 if (args[1]->IsString())
175 if (state.find_first_of("\"'")!=string::npos)
176 return ThrowException(String::New("State name must not contain quotation marks."));
177
178 string code = "(function(name,state,ms)"
179 "{";
180 if (timeout)
181 code += "var t = new Date();";
182 code += "while (1)"
183 "{"
184 "var s = dim.state(name);"
185 "if(!"+index+")throw 'Waitig for state "+arg1+" of server "+arg0+" failed.';"
186 "if(state=="+index+")return true;";
187 if (timeout)
188 code += "if((new Date()-t)>ms)return false;";
189
190 code += "dim.sleep();"
191 "}"
192 "})('"+arg0+"',"+arg1;
193 if (timeout)
194 code += "," + to_string(args[2]->Int32Value());
195 code += ");";
196
197 const HandleScope handle_scope;
198
199 // It is not strictly necessary to catch the exception, instead
200 // script->Run() could just be returned, but catching the
201 // exception allow to print the position in the file in
202 // the exception handler instead of just the posiiton in the script.
203 TryCatch exception;
204
205 const Handle<Script> script = Script::Compile(String::New(code.c_str()));
206 const Handle<Value> result = script->Run();
207
208 return exception.HasCaught() ? exception.ReThrow() : result;
209
210 /*
211 const string server = *String::Utf8Value(args[0]);
212 const int32_t state = args[1]->Int32Value();
213 const uint32_t millisec = args.Length()==3 ? args[2]->Int32Value() : 0;
214
215 const int rc = JsWait(server, state, millisec);
216
217 if (rc==0 || rc==1)
218 return Boolean::New(rc);
219
220 return ThrowException(String::New(("Waitig for state "+to_string(state)+" of server '"+server+"' failed.").c_str()));
221 */
222}
223
224Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
225{
226 if (args.Length()==0)
227 return ThrowException(String::New("Number of arguments must be at least 1."));
228
229 if (!args[0]->IsString())
230 return ThrowException(String::New("Argument 1 must be a string."));
231
232 const HandleScope handle_scope;
233
234 const String::Utf8Value str(args[0]);
235
236 string command = *str;
237
238 // Escape all string arguments. All others can be kept as they are.
239 for (int i=1; i<args.Length(); i++)
240 {
241 const String::Utf8Value arg(args[i]);
242 if (args[i]->IsString())
243 command += " \""+string(*arg)+"\"";
244 else
245 command += " "+string(*arg);
246 }
247
248 return Boolean::New(JsSend(command));
249}
250
251Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
252{
253 if (args.Length()==0)
254 {
255 // Theoretically, the CPU usage can be reduced by maybe a factor
256 // of four using a larger value, but this also means that the
257 // JavaScript is locked for a longer time.
258 usleep(1000);
259 return Undefined();
260 }
261
262 if (args.Length()!=1)
263 return ThrowException(String::New("Number of arguments must be exactly 1."));
264
265 if (!args[0]->IsUint32())
266 return ThrowException(String::New("Argument 1 must be an uint32."));
267
268 // Using a Javascript function has the advantage that it is fully
269 // interruptable without the need of C++ code
270
271 const string code =
272 "(function(){"
273 "var t=new Date();"
274 "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();"
275 "})();";
276
277 const HandleScope handle_scope;
278
279 const Handle<Script> script = Script::Compile(String::New(code.c_str()));
280 return script->Run();
281
282 //JsSleep(args[0]->Int32Value());
283 //return Undefined();
284}
285
286Handle<Value> InterpreterV8::FuncState(const Arguments& args)
287{
288 if (args.Length()!=1)
289 return ThrowException(String::New("Number of arguments must be exactly 1."));
290
291 if (!args[0]->IsString())
292 return ThrowException(String::New("Argument 1 must be a string."));
293
294 // Return state.name/state.index
295
296 const String::Utf8Value str(args[0]);
297
298 const State rc = JsState(*str);
299
300 //if (rc.first<=-256)
301 // return Undefined();
302
303 HandleScope handle_scope;
304
305 // It is important to catch the exception thrown
306 // by Date::New in case of thread termination!
307 Local<Value> date;
308 {
309 TryCatch exception;
310 date = Date::New(rc.time.JavaDate());
311 if (exception.HasCaught())
312 return exception.ReThrow();
313 }
314
315 Handle<ObjectTemplate> obj = ObjectTemplate::New();
316 obj->Set(String::New("index"), rc.index<=-256?Undefined():Integer::New(rc.index), ReadOnly);
317 obj->Set(String::New("name"), rc.index<=-256?Undefined():String::New(rc.name.c_str()), ReadOnly);
318 if (rc.index>-256)
319 obj->Set(String::New("time"), date);
320 //obj->Set(String::New("toString"), String::New(("[Object state "+string(*str)+":"+to_string(rc.index)+"]").c_str()));
321
322 return handle_scope.Close(obj->NewInstance());
323}
324
325Handle<Value> InterpreterV8::FuncNewState(const Arguments& args)
326{
327 if (args.Length()<1 || args.Length()>3)
328 return ThrowException(String::New("Number of arguments must be 1, 2 or 3."));
329
330 if (!args[0]->IsUint32())
331 return ThrowException(String::New("Argument 1 must be an uint32."));
332 if (args.Length()>1 && !args[1]->IsString())
333 return ThrowException(String::New("Argument 2 must be a string."));
334 if (args.Length()>2 && !args[2]->IsString())
335 return ThrowException(String::New("Argument 3 must be a string."));
336
337 const HandleScope handle_scope;
338
339 const uint32_t index = args[0]->Int32Value();
340 const string name = *String::Utf8Value(args[1]);
341 const string comment = *String::Utf8Value(args[2]);
342
343 if (index<10 || index>255)
344 return ThrowException(String::New("State must be in the range [10, 255]."));
345
346 if (name.empty())
347 return ThrowException(String::New("State name must not be empty."));
348
349 if (name.find_first_of(':')!=string::npos || name.find_first_of('=')!=string::npos)
350 return ThrowException(String::New("State name must not contain : or =."));
351
352 if (JsHasState(index) || JsHasState(name))
353 {
354 const string what =
355 "State index ["+to_string(index)+"] or name ["+name+"] already defined.";
356
357 return ThrowException(String::New(what.c_str()));
358 }
359
360 return Boolean::New(JsNewState(index, name, comment));
361}
362
363Handle<Value> InterpreterV8::FuncSetState(const Arguments& args)
364{
365 if (args.Length()!=1)
366 return ThrowException(String::New("Number of arguments must be exactly 1."));
367
368 if (!args[0]->IsUint32() && !args[0]->IsString())
369 return ThrowException(String::New("Argument must be an unint32 or a string."));
370
371 const HandleScope handle_scope;
372
373 int index = -2;
374 if (args[0]->IsUint32())
375 {
376 index = args[0]->Int32Value();
377 }
378 else
379 {
380 const string name = *String::Utf8Value(args[0]);
381 index = JsGetState(name);
382 if (index==-2)
383 return ThrowException(String::New(("State '"+name+"' not found.").c_str()));
384 }
385
386 if (index<10 || index>255)
387 return ThrowException(String::New("State must be in the range [10, 255]."));
388
389 return Boolean::New(JsSetState(index));
390}
391
392Handle<Value> InterpreterV8::FuncExit(const Arguments &)
393{
394 V8::TerminateExecution(fThreadId);
395 return ThrowException(String::New("exit"));
396/*
397 if (args.Length()!=1)
398 return ThrowException(String::New("Number of arguments must be exactly 1."));
399
400 if (!args[0]->IsUint32())
401 return ThrowException(String::New("Argument 1 must be an uint32."));
402
403 const HandleScope handle_scope;
404
405 JsSleep(args[0]->Int32Value());
406*/
407 return Undefined();
408}
409
410// The callback that is invoked by v8 whenever the JavaScript 'print'
411// function is called. Prints its arguments on stdout separated by
412// spaces and ending with a newline.
413Handle<Value> InterpreterV8::FuncPrint(const Arguments& args)
414{
415 for (int i=0; i<args.Length(); i++)
416 {
417 const HandleScope handle_scope;
418
419 const String::Utf8Value str(args[i]);
420 if (*str)
421 JsPrint(*str);
422 }
423 return Undefined();
424}
425
426Handle<Value> InterpreterV8::FuncAlarm(const Arguments& args)
427{
428 for (int i=0; i<args.Length(); i++)
429 {
430 const HandleScope handle_scope;
431
432 const String::Utf8Value str(args[i]);
433 if (*str)
434 JsAlarm(*str);
435 }
436
437 if (args.Length()==0)
438 JsAlarm();
439
440 return Undefined();
441}
442
443Handle<Value> InterpreterV8::FuncOut(const Arguments& args)
444{
445 for (int i=0; i<args.Length(); i++)
446 {
447 const HandleScope handle_scope;
448
449 const String::Utf8Value str(args[i]);
450 if (*str)
451 JsOut(*str);
452 }
453 return Undefined();
454}
455
456// The callback that is invoked by v8 whenever the JavaScript 'load'
457// function is called. Loads, compiles and executes its argument
458// JavaScript file.
459Handle<Value> InterpreterV8::FuncInclude(const Arguments& args)
460{
461 for (int i=0; i<args.Length(); i++)
462 {
463 const HandleScope handle_scope;
464
465 const String::Utf8Value file(args[i]);
466 if (*file == NULL)
467 return ThrowException(String::New(("Error loading file '"+string(*file)+"'").c_str()));
468
469 if (!ExecuteFile(*file))
470 return ThrowException(String::New(("Execution of '"+string(*file)+"' failed").c_str()));
471 }
472 return Undefined();
473}
474
475Handle<Value> InterpreterV8::FuncVersion(const Arguments&)
476{
477 return String::New(V8::GetVersion());
478}
479
480Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
481{
482 HandleScope handle_scope;
483
484 void *ptr = Handle<External>::Cast(args.This()->GetInternalField(0))->Value();
485 if (!ptr)
486 return Boolean::New(false);
487
488#ifdef HAVE_SQL
489 Database *db = reinterpret_cast<Database*>(ptr);
490 auto it = find(fDatabases.begin(), fDatabases.end(), db);
491 fDatabases.erase(it);
492 delete db;
493#endif
494
495 args.This()->SetInternalField(0, External::New(0));
496
497 return Boolean::New(true);
498}
499Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
500{
501 if (args.Length()!=1)
502 return ThrowException(String::New("Number of arguments must be exactly 1."));
503
504 if (!args[0]->IsString())
505 return ThrowException(String::New("Both arguments must be a string."));
506
507 HandleScope handle_scope;
508
509 void *ptr = Handle<External>::Cast(args.This()->GetInternalField(0))->Value();
510 if (!ptr)
511 return Undefined();
512
513 const String::Utf8Value query(args[0]);
514
515#ifdef HAVE_SQL
516 try
517 {
518 Database *db = reinterpret_cast<Database*>(ptr);
519
520 const mysqlpp::StoreQueryResult res = db->query(*query).store();
521
522 Handle<Array> ret = Array::New();
523 ret->Set(String::New("table"), String::New(res.table()), ReadOnly);
524
525 Handle<Array> cols = Array::New();
526
527 int irow=0;
528 for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++)
529 {
530 Handle<Array> row = Array::New();
531
532 const mysqlpp::FieldNames *list = it->field_list().list;
533
534 for (size_t i=0; i<it->size(); i++)
535 {
536 const Handle<Value> name = String::New((*list)[i].c_str());
537 if (irow==0)
538 cols->Set(Integer::NewFromUnsigned(i), name, ReadOnly);
539
540 if ((*it)[i].is_null())
541 {
542 row->Set(name, Undefined(), ReadOnly);
543 continue;
544 }
545
546 const string sql_type = (*it)[i].type().sql_name();
547
548 const bool uns = sql_type.find("UNSIGNED")==string::npos;
549
550 if (sql_type.find("BIGINT")!=string::npos)
551 {
552 if (uns)
553 {
554 const uint64_t val = (uint64_t)(*it)[i];
555 if (val>UINT32_MAX)
556 row->Set(name, Number::New(val), ReadOnly);
557 else
558 row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
559 }
560 else
561 {
562 const int64_t val = (int64_t)(*it)[i];
563 if (val<INT32_MIN || val>INT32_MAX)
564 row->Set(name, Number::New(val), ReadOnly);
565 else
566 row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
567 }
568 continue;
569 }
570
571 // 32 bit
572 if (sql_type.find("INT")!=string::npos)
573 {
574 if (uns)
575 row->Set(name, Integer::NewFromUnsigned((uint32_t)(*it)[i]), ReadOnly);
576 else
577 row->Set(name, Integer::New((int32_t)(*it)[i]), ReadOnly);
578 }
579
580 if (sql_type.find("BOOL")!=string::npos )
581 {
582 row->Set(name, Boolean::New((bool)(*it)[i]), ReadOnly);
583 continue;
584 }
585
586 if (sql_type.find("FLOAT")!=string::npos)
587 {
588 ostringstream val;
589 val << setprecision(7) << (float)(*it)[i];
590 row->Set(name, Number::New(stod(val.str())), ReadOnly);
591 continue;
592
593 }
594 if (sql_type.find("DOUBLE")!=string::npos)
595 {
596 row->Set(name, Number::New((double)(*it)[i]), ReadOnly);
597 continue;
598 }
599
600 if (sql_type.find("CHAR")!=string::npos ||
601 sql_type.find("TEXT")!=string::npos)
602 {
603 row->Set(name, String::New((const char*)(*it)[i]), ReadOnly);
604 continue;
605 }
606
607 time_t date = 0;
608 if (sql_type.find("TIMESTAMP")!=string::npos)
609 date = mysqlpp::Time((*it)[i]);
610
611 if (sql_type.find("DATETIME")!=string::npos)
612 date = mysqlpp::DateTime((*it)[i]);
613
614 if (sql_type.find(" DATE ")!=string::npos)
615 date = mysqlpp::Date((*it)[i]);
616
617 if (date>0)
618 {
619 // It is important to catch the exception thrown
620 // by Date::New in case of thread termination!
621 TryCatch exception;
622 Local<Value> val = Date::New(date*1000);
623 if (exception.HasCaught())
624 return exception.ReThrow();
625 //if (V8::IsExecutionTerminating())
626 // return Undefined();
627
628 row->Set(name, val, ReadOnly);
629 }
630 }
631
632 ret->Set(Integer::NewFromUnsigned(irow++), row, ReadOnly);
633 }
634
635 if (irow>0)
636 ret->Set(String::New("cols"), cols, ReadOnly);
637
638 return handle_scope.Close(ret);
639 }
640 catch (const exception &e)
641 {
642 return ThrowException(String::New(e.what()));
643 }
644#endif
645}
646
647Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
648{
649 if (args.Length()!=1)
650 return ThrowException(String::New("Number of arguments must be exactly 1."));
651
652 if (!args[0]->IsString())
653 return ThrowException(String::New("Argument 1 must be a string."));
654
655 HandleScope handle_scope;
656
657 const String::Utf8Value database(args[0]);
658 const String::Utf8Value query (args[1]);
659
660#ifdef HAVE_SQL
661 try
662 {
663 Database *db = new Database(*database);
664 fDatabases.push_back(db);
665
666 Handle<ObjectTemplate> tem = ObjectTemplate::New();
667 tem->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly);
668 tem->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly);
669 tem->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly);
670 tem->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly);
671 tem->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery), ReadOnly);
672 tem->Set(String::New("close"), FunctionTemplate::New(WrapDbClose), ReadOnly);
673 tem->SetInternalFieldCount(1);
674
675 Handle<Object> obj = tem->NewInstance();
676 obj->SetInternalField(0, External::New(db));
677
678 return handle_scope.Close(obj);
679 }
680 catch (const exception &e)
681 {
682 return ThrowException(String::New(e.what()));
683 }
684#endif
685}
686
687Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
688{
689 // Dim values are always unsigned per (FACT++) definition
690 switch (type)
691 {
692 case 'F':
693 {
694 // Remove the "imprecision" effect coming from casting a float to
695 // a double and then showing it with double precision
696 ostringstream val;
697 val << setprecision(7) << *reinterpret_cast<const float*>(ptr);
698 Handle<Value> v=Number::New(stod(val.str()));
699 ptr+=4;
700 return v;
701 }
702 case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; }
703 case 'I':
704 case 'L': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; }
705 case 'X':
706 {
707 const uint64_t val = *reinterpret_cast<const uint64_t*>(ptr);
708 ptr += 8;
709 if (val>UINT32_MAX)
710 return Number::New(val);
711 return Integer::NewFromUnsigned(val);
712 }
713 case 'S': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
714 case 'C': { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; }
715 case ':': { Handle<Value> v=String::New(ptr); return v; }
716 }
717 return Undefined();
718}
719
720Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
721{
722 const HandleScope handle_scope;
723
724 //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value();
725
726 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
727
728 const auto it = fReverseMap.find(*str);
729 if (it!=fReverseMap.end())
730 {
731 it->second.Dispose();
732 fReverseMap.erase(it);
733 }
734
735 args.Holder()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
736
737 return Boolean::New(JsUnsubscribe(*str));
738}
739
740Handle<Value> InterpreterV8::ConvertEvent(const EventImp *evt, uint64_t counter, const char *str)
741{
742 Local<Value> date;
743
744 // It is important to catch the exception thrown
745 // by Date::New in case of thread termination!
746 {
747 TryCatch exception;
748 date = Date::New(evt->GetJavaDate());
749 if (exception.HasCaught())
750 return exception.ReThrow();
751 }
752
753 const vector<Description> vec = JsDescription(str);
754
755 Handle<Array> ret = Array::New();
756 ret->Set(String::New("name"), String::New(str), ReadOnly);
757 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly);
758 ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly);
759 ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly);
760 ret->Set(String::New("size"), Integer::New(evt->GetSize()), ReadOnly);
761 ret->Set(String::New("counter"), Integer::New(counter), ReadOnly);
762 ret->Set(String::New("time"), date, ReadOnly);
763
764 // If no event was received (usually a disconnection event in
765 // the context of FACT++), no data is returned
766 if (evt->IsEmpty())
767 return ret;
768
769 // If valid data was received, but the size was zero, then
770 // null is returned as data
771 if (evt->GetSize()==0 || evt->GetFormat().empty())
772 {
773 ret->Set(String::New("data"), Null(), ReadOnly);
774 return ret;
775 }
776
777 typedef boost::char_separator<char> separator;
778 const boost::tokenizer<separator> tokenizer(evt->GetFormat(), separator(";:"));
779
780 const vector<string> tok(tokenizer.begin(), tokenizer.end());
781
782 Handle<Array> obj = tok.size()>1 ? Array::New() : ret;
783
784 const char *ptr = evt->GetText();
785 const char *end = evt->GetText()+evt->GetSize();
786
787 try
788 {
789 size_t pos = 1;
790 for (auto it=tok.begin(); it!=tok.end() && ptr<end; it++, pos++)
791 {
792 char type = (*it)[0];
793 it++;
794
795 if (it==tok.end() && type=='C')
796 type = ':';
797
798 if (it==tok.end() && type!=':')
799 return Exception::Error(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str()));
800
801 string name = pos<vec.size() ? vec[pos].name : "";
802 if (tok.size()==1)
803 name = "data";
804
805 const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str());
806
807 if (cnt==1)
808 {
809 const Handle<Value> v = Convert(type, ptr);
810 if (tok.size()>1)
811 obj->Set(pos-1, v);
812 if (!name.empty())
813 obj->Set(String::New(name.c_str()), v);
814 }
815 else
816 {
817 Handle<Array> a = Array::New(cnt);
818 for (uint32_t i=0; i<cnt; i++)
819 a->Set(i, Convert(type, ptr));
820 if (tok.size()>1)
821 obj->Set(pos-1, a);
822 if (!name.empty())
823 obj->Set(String::New(name.c_str()), a);
824 }
825
826 if (it==tok.end())
827 break;
828 }
829
830 if (tok.size()>1)
831 ret->Set(String::New("data"), obj, ReadOnly);
832
833 return ret;
834 }
835 catch (...)
836 {
837 return Exception::Error(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str()));
838 }
839}
840/*
841Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
842{
843 HandleScope handle_scope;
844
845 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
846
847 const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
848
849 const EventImp *evt = p.second;
850 if (!evt)
851 return Undefined();
852
853 //if (counter==cnt)
854 // return info.Holder();//Holder()->Get(String::New("data"));
855
856 Handle<Value> ret = ConvertEvent(evt, p.first, *str);
857 return ret->IsNativeError() ? ThrowException(ret) : handle_scope.Close(ret);
858}
859*/
860Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
861{
862 if (args.Length()>1)
863 return ThrowException(String::New("Number of arguments must not be greatr than 1."));
864
865 if (args.Length()==1 && !args[0]->IsInt32() && !args[0]->IsNull())
866 return ThrowException(String::New("Argument 1 not an int32."));
867
868 // Using a Javascript function has the advantage that it is fully
869 // interruptable without the need of C++ code
870 const bool null = args.Length()==1 && args[0]->IsNull();
871 const int32_t timeout = args.Length()==1 ? args[0]->Int32Value() : 0;
872
873 HandleScope handle_scope;
874
875 const Handle<Script> sleep = Script::Compile(String::New("dim.sleep();"));
876
877 const Handle<String> data = String::New("data");
878 const Handle<String> named = String::New("named");
879
880 const String::Utf8Value name(args.Holder()->Get(String::New("name")));
881
882 TryCatch exception;
883
884 Time t;
885 while (!exception.HasCaught())
886 {
887 const pair<uint64_t, EventImp *> p = JsGetEvent(*name);
888
889 const EventImp *evt = p.second;
890 if (evt)
891 {
892 const Handle<Value> val = ConvertEvent(evt, p.first, *name);
893 if (val->IsNativeError())
894 return ThrowException(val);
895
896 // Protect against the return of an exception
897 if (!val.IsEmpty() && val->IsObject())
898 {
899 const Handle<Object> obj = val->ToObject();
900 if (obj->Has(data) && obj->Get(named)->ToBoolean()->Value())
901 return handle_scope.Close(val);
902 }
903 }
904
905 if (args.Length()==0)
906 break;
907
908 if (!null && Time()-t>=boost::posix_time::milliseconds(abs(timeout)))
909 break;
910
911 // We cannot sleep directly because we have to give control back to
912 // JavaScript ever now and then. This also allows us to catch
913 // exceptions, either from the preemption or ConvertEvent
914 sleep->Run();
915 }
916
917 if (exception.HasCaught())
918 return exception.ReThrow();
919
920 if (timeout>=0)
921 return Undefined();
922
923 const string str = "Waiting for a valid event of "+string(*name)+" timed out.";
924 return ThrowException(String::New(str.c_str()));
925}
926
927
928// This is a callback from the RemoteControl piping event handling
929// to the java script ---> in test phase!
930void InterpreterV8::JsHandleEvent(const EventImp &evt, uint64_t cnt, const string &service)
931{
932 if (fThreadId<0)
933 return;
934
935 Locker locker;
936
937 const auto it = fReverseMap.find(service);
938 if (it==fReverseMap.end())
939 return;
940
941 const HandleScope handle_scope;
942
943 Handle<Object> obj = it->second;
944 if (obj.IsEmpty())
945 return;
946
947 const Handle<String> onchange = String::New("onchange");
948 if (!obj->Has(onchange))
949 return;
950
951 const Handle<Value> val = obj->Get(onchange);
952 if (!val->IsFunction())
953 return;
954
955 // -------------------------------------------------------------------
956 // We are not in a context... we need to get into one for Array::New
957
958 Persistent<Context> context = Context::New();
959 if (context.IsEmpty())
960 return;
961
962 const Context::Scope scope(context);
963
964 // -------------------------------------------------------------------
965
966 TryCatch exception;
967
968 Handle<Value> ret = ConvertEvent(&evt, cnt, service.c_str());
969 if (ret->IsArray())
970 {
971 Handle<Array> data = Handle<Array>::Cast(ret);
972 Handle<Value> args[] = { data };
973
974 Handle<Function>::Cast(val)->Call(obj, 1, args);
975 }
976
977 if (exception.HasCaught())
978 ReportException(&exception);
979
980 if (ret->IsNativeError())
981 JsException(service+".onchange callback - "+*String::Utf8Value(ret));
982
983 context.Dispose();
984
985 if (ret->IsUndefined() || ret->IsNativeError() || exception.HasCaught())
986 V8::TerminateExecution(fThreadId);
987}
988
989Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local< Value > value, const AccessorInfo &)
990{
991 const HandleScope handle_scope;
992
993 // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
994 const string server = *String::Utf8Value(prop);
995 auto it = fStateCallbacks.find(server);
996
997 if (it!=fStateCallbacks.end())
998 {
999 it->second.Dispose();
1000 fStateCallbacks.erase(it);
1001 }
1002
1003 if (value->IsFunction())
1004 fStateCallbacks[server] = Persistent<Value>::New(value);
1005
1006 return Handle<Value>();
1007}
1008
1009
1010void InterpreterV8::JsHandleState(const std::string &server, const State &state)
1011{
1012 if (fThreadId<0)
1013 return;
1014
1015 Locker locker;
1016
1017 auto it = fStateCallbacks.find(server);
1018 if (it==fStateCallbacks.end())
1019 {
1020 it = fStateCallbacks.find("*");
1021 if (it==fStateCallbacks.end())
1022 return;
1023 }
1024
1025 const HandleScope handle_scope;
1026
1027 if (it->second.IsEmpty() || !it->second->IsFunction())
1028 return;
1029
1030 // -------------------------------------------------------------------
1031 // We are not in a context... we need to get into one for Array::New
1032
1033 Persistent<Context> context = Context::New();
1034 if (context.IsEmpty())
1035 return;
1036
1037 const Context::Scope scope(context);
1038
1039 // -------------------------------------------------------------------
1040
1041 TryCatch exception;
1042
1043 // It is important to catch the exception thrown
1044 // by Date::New in case of thread termination!
1045 Local<Value> date = Date::New(state.time.JavaDate());
1046
1047 if (!exception.HasCaught())
1048 {
1049 Handle<ObjectTemplate> obj = ObjectTemplate::New();
1050 obj->Set(String::New("index"), state.index<=-256?Undefined():Integer::New(state.index), ReadOnly);
1051 obj->Set(String::New("name"), state.index<=-256?Undefined():String::New(state.name.c_str()), ReadOnly);
1052 obj->Set(String::New("comment"), state.index<=-256?Undefined():String::New(state.comment.c_str()), ReadOnly);
1053 obj->Set(String::New("server"), String::New(server.c_str()), ReadOnly);
1054 if (state.index>-256)
1055 obj->Set(String::New("time"), date);
1056
1057 Handle<Value> args[] = { obj->NewInstance() };
1058
1059 Handle<Function> fun = Handle<Function>::Cast(it->second);
1060 fun->Call(fun, 1, args);
1061 }
1062
1063 if (exception.HasCaught())
1064 ReportException(&exception);
1065
1066 context.Dispose();
1067
1068 if (exception.HasCaught())
1069 V8::TerminateExecution(fThreadId);
1070}
1071
1072/*
1073void Cleanup( Persistent<Value> object, void *parameter )
1074{
1075 cout << "======================> RemoveMyObj()" << endl;
1076}*/
1077
1078Handle<Value> InterpreterV8::FuncSubscribe(const Arguments &args)
1079{
1080 if (args.Length()!=1)
1081 return ThrowException(String::New("Number of arguments must be exactly 1."));
1082
1083 if (!args[0]->IsString())
1084 return ThrowException(String::New("Argument 1 must be a string."));
1085
1086 //if (!args.IsConstructCall())
1087 // return ThrowException(String::New("Must be used as constructor."));
1088
1089 HandleScope handle_scope;
1090
1091 const String::Utf8Value str(args[0]);
1092
1093 const auto it = fReverseMap.find(*str);
1094 if (it!=fReverseMap.end())
1095 return handle_scope.Close(it->second);
1096
1097 void *ptr = JsSubscribe(*str);
1098 if (ptr==0)
1099 return ThrowException(String::New(("Subscription to '"+string(*str)+"' already exists.").c_str()));
1100
1101 Handle<ObjectTemplate> tem = ObjectTemplate::New();
1102 tem->Set(String::New("get"), FunctionTemplate::New(WrapGetData), ReadOnly);
1103 tem->Set(String::New("close"), FunctionTemplate::New(WrapClose), ReadOnly);
1104 tem->Set(String::New("name"), String::New(*str), ReadOnly);
1105 tem->Set(String::New("isOpen"), Boolean::New(true));
1106 tem->SetInternalFieldCount(1);
1107 //tem->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly);
1108
1109 Handle<Object> obj = tem->NewInstance();
1110 obj->SetInternalField(0, External::New(ptr));
1111
1112 fReverseMap[*str] = Persistent<Object>::New(obj);
1113
1114 return handle_scope.Close(obj);
1115
1116 // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance());
1117 // obj.MakeWeak((void*)1, Cleanup);
1118 // return obj;
1119}
1120
1121bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
1122{
1123 Locker locker;
1124 fThreadId = V8::GetCurrentThreadId();
1125
1126 JsPrint(string("JavaScript V8 ")+V8::GetVersion());
1127
1128 JsLoad(filename);
1129
1130 HandleScope handle_scope;
1131
1132 // Create a template for the global object.
1133 Handle<ObjectTemplate> dim = ObjectTemplate::New();
1134 dim->Set(String::New("print"), FunctionTemplate::New(WrapPrint), ReadOnly);
1135 dim->Set(String::New("alarm"), FunctionTemplate::New(WrapAlarm), ReadOnly);
1136 dim->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
1137 dim->Set(String::New("wait"), FunctionTemplate::New(WrapWait), ReadOnly);
1138 dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly);
1139 dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly);
1140 dim->Set(String::New("newState"), FunctionTemplate::New(WrapNewState), ReadOnly);
1141 dim->Set(String::New("setState"), FunctionTemplate::New(WrapSetState), ReadOnly);
1142 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly);
1143 dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly);
1144 dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
1145
1146 Handle<ObjectTemplate> onchange = ObjectTemplate::New();
1147 onchange->SetNamedPropertyHandler(OnChangeGet, WrapOnChangeSet);
1148 dim->Set(v8::String::New("onchange"), onchange);
1149
1150 Handle<ObjectTemplate> global = ObjectTemplate::New();
1151 global->Set(String::New("dim"), dim, ReadOnly);
1152 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly);
1153 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly);
1154 global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
1155
1156 // Persistent
1157 Persistent<Context> context = Context::New(NULL, global);
1158 if (context.IsEmpty())
1159 {
1160 //printf("Error creating context\n");
1161 return false;
1162 }
1163
1164 Context::Scope scope(context);
1165
1166 Handle<Array> args = Array::New(map.size());
1167 for (auto it=map.begin(); it!=map.end(); it++)
1168 args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
1169 context->Global()->Set(String::New("$"), args, ReadOnly);
1170 context->Global()->Set(String::New("arg"), args, ReadOnly);
1171
1172 JsStart(filename);
1173
1174 //context->Enter();
1175 Locker::StartPreemption(10);
1176 const bool rc = ExecuteFile(filename);
1177
1178 // -----
1179 // This is how an exit handler could look like, but there is no way to interrupt it
1180 // -----
1181 // Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("dim")));
1182 // if (!obj.IsEmpty())
1183 // {
1184 // Handle<Value> onexit = obj->Get(String::New("onexit"));
1185 // if (!onexit->IsUndefined())
1186 // Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
1187 // // Handle<Object> result = Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
1188 // }
1189
1190 Locker::StopPreemption();
1191 //context->Exit();
1192
1193 for (auto it=fStateCallbacks.begin(); it!=fStateCallbacks.end(); it++)
1194 it->second.Dispose();
1195 fStateCallbacks.clear();
1196
1197 for (auto it=fReverseMap.begin(); it!=fReverseMap.end(); it++)
1198 it->second.Dispose();
1199 fReverseMap.clear();
1200
1201 context.Dispose();
1202
1203#ifdef HAVE_SQL
1204 for (auto it=fDatabases.begin(); it!=fDatabases.end(); it++)
1205 delete *it;
1206 fDatabases.clear();
1207#endif
1208
1209 JsEnd(filename);
1210
1211 return rc;
1212}
1213
1214void InterpreterV8::JsStop()
1215{
1216 Locker locker;
1217
1218 //cout << "Terminate " << fThreadId << endl;
1219 if (This->fThreadId>=0)
1220 V8::TerminateExecution(This->fThreadId);
1221 //cout << "Terminate " << fThreadId << endl;
1222
1223 //Unlocker unlocker;
1224}
1225
1226#endif
Note: See TracBrowser for help on using the repository browser.