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

Last change on this file since 14184 was 14177, checked in by tbretz, 14 years ago
Added a simple database interface.
File size: 22.3 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 const HandleScope handle_scope;
24
25 const String::Utf8Value exception(try_catch->Exception());
26
27 if (*exception && string(*exception)=="exit")
28 return true;
29
30 const Handle<Message> message = try_catch->Message();
31
32 // Print (filename):(line number): (message).
33 const String::Utf8Value filename(message->GetScriptResourceName());
34
35 ostringstream out;
36
37 if (*filename)
38 out << *filename;
39 if (!message.IsEmpty())
40 out << ": l." << message->GetLineNumber();
41 if (*exception)
42 out << ": " << *exception;
43
44 JsException(out.str());
45
46 if (message.IsEmpty())
47 return false;
48
49 // Print line of source code.
50 const String::Utf8Value sourceline(message->GetSourceLine());
51 if (*sourceline)
52 JsException(*sourceline);
53
54 // Print wavy underline (GetUnderline is deprecated).
55 const int start = message->GetStartColumn();
56 const int end = message->GetEndColumn();
57
58 out.str("");
59 if (start>0)
60 out << setfill(' ') << setw(start) << ' ';
61 out << setfill('^') << setw(end-start) << '^';
62
63 JsException(out.str());
64
65 String::Utf8Value stack_trace(try_catch->StackTrace());
66 if (stack_trace.length()<=0)
67 return false;
68
69 //if (*stack_trace)
70 // JsException(string("\n")+*stack_trace);
71
72 return false;
73}
74
75// Executes a string within the current v8 context.
76bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file)
77{
78 if (code.IsEmpty())
79 return true;
80
81 const HandleScope handle_scope;
82
83 const Handle<Script> script = Script::Compile(code, file);
84 if (script.IsEmpty())
85 return false;
86
87 const Handle<Value> result = script->Run();
88 if (result.IsEmpty())
89 return false;
90
91 // If all went well and the result wasn't undefined then print
92 // the returned value.
93 if (!result->IsUndefined())
94 JsResult(*String::Utf8Value(result));
95
96 return true;
97}
98
99bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file)
100{
101 TryCatch exception;
102
103 const bool rc = ExecuteStringNT(code, file);
104
105 if (!exception.CanContinue())
106 return false;
107
108 if (exception.HasCaught())
109 return ReportException(&exception);
110
111 return rc;
112}
113
114bool InterpreterV8::ExecuteCode(const string &code, const string &file)
115{
116 return ExecuteCode(String::New(code.c_str(), code.size()),
117 String::New(file.c_str()));
118}
119
120bool InterpreterV8::ExecuteFile(const string &name)
121{
122 ifstream fin(name.c_str());
123 if (!fin)
124 {
125 JsException("Error - Could not open file '"+name+"'");
126 return false;
127 }
128
129 string buffer;
130 if (!getline(fin, buffer, '\0'))
131 return true;
132
133 if (fin.fail())
134 {
135 JsException("Error - reading file.");
136 return false;
137 }
138
139 return ExecuteCode(buffer, name);
140}
141
142Handle<Value> InterpreterV8::FuncWait(const Arguments& args)
143{
144 if (args.Length()!=2 && args.Length()!=3)
145 return ThrowException(String::New("Number of arguments must be 2 or 3."));
146
147 if (!args[0]->IsString())
148 return ThrowException(String::New("Argument 1 not a string."));
149
150 if (!args[1]->IsInt32())
151 return ThrowException(String::New("Argument 2 not an int32."));
152
153 if (args.Length()==3 && !args[2]->IsUint32())
154 return ThrowException(String::New("Argument 3 not an uint32."));
155
156 const string server = *String::Utf8Value(args[0]);
157 const int32_t state = args[1]->Int32Value();
158 const uint32_t millisec = args.Length()==3 ? args[2]->Int32Value() : 0;
159
160 const int rc = JsWait(server, state, millisec);
161
162 if (rc==0 || rc==1)
163 return Boolean::New(rc);
164
165 return ThrowException(String::New(("Waitig for state "+to_string(state)+" of server '"+server+"' failed.").c_str()));
166}
167
168Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
169{
170 if (args.Length()==0)
171 return ThrowException(String::New("Number of arguments must be at least 1."));
172
173 if (!args[0]->IsString())
174 return ThrowException(String::New("Argument 1 must be a string."));
175
176 const HandleScope handle_scope;
177
178 const String::Utf8Value str(args[0]);
179
180 string command = *str;
181
182 for (int i=1; i<args.Length(); i++)
183 {
184 const String::Utf8Value arg(args[i]);
185 command += " \""+string(*arg)+"\"";
186 }
187
188 return Boolean::New(JsSend(command));
189}
190
191Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
192{
193 if (args.Length()!=1)
194 return ThrowException(String::New("Number of arguments must be exactly 1."));
195
196 if (!args[0]->IsUint32())
197 return ThrowException(String::New("Argument 1 must be an uint32."));
198
199 JsSleep(args[0]->Int32Value());
200
201 return Undefined();
202}
203
204Handle<Value> InterpreterV8::FuncState(const Arguments& args)
205{
206 if (args.Length()!=1)
207 return ThrowException(String::New("Number of arguments must be exactly 1."));
208
209 if (!args[0]->IsString())
210 return ThrowException(String::New("Argument 1 must be a string."));
211
212 // Return state.name/state.index
213
214 HandleScope handle_scope;
215
216 const String::Utf8Value str(args[0]);
217
218 const pair<int32_t, string> rc = JsState(*str);
219
220 Handle<ObjectTemplate> obj = ObjectTemplate::New();
221
222 obj->Set(String::New("index"), rc.first<=-256?Undefined():Integer::New(rc.first), ReadOnly);
223 obj->Set(String::New("name"), rc.first<=-256?Undefined():String::New(rc.second.c_str()), ReadOnly);
224 //obj->Set(String::New("toString"), String::New(("[Object state "+string(*str)+"]").c_str()), ReadOnly);
225
226 return handle_scope.Close(obj->NewInstance());
227}
228
229Handle<Value> InterpreterV8::FuncExit(const Arguments &)
230{
231 v8::V8::TerminateExecution(fThreadId);
232 return ThrowException(String::New("exit"));
233/*
234 if (args.Length()!=1)
235 return ThrowException(String::New("Number of arguments must be exactly 1."));
236
237 if (!args[0]->IsUint32())
238 return ThrowException(String::New("Argument 1 must be an uint32."));
239
240 const HandleScope handle_scope;
241
242 JsSleep(args[0]->Int32Value());
243*/
244 return Undefined();
245}
246
247// The callback that is invoked by v8 whenever the JavaScript 'print'
248// function is called. Prints its arguments on stdout separated by
249// spaces and ending with a newline.
250Handle<Value> InterpreterV8::FuncPrint(const Arguments& args)
251{
252 for (int i=0; i<args.Length(); i++)
253 {
254 const HandleScope handle_scope;
255
256 const String::Utf8Value str(args[i]);
257 if (*str)
258 JsPrint(*str);
259 }
260 return Undefined();
261}
262
263Handle<Value> InterpreterV8::FuncOut(const Arguments& args)
264{
265 for (int i=0; i<args.Length(); i++)
266 {
267 const HandleScope handle_scope;
268
269 const String::Utf8Value str(args[i]);
270 if (*str)
271 JsOut(*str);
272 }
273 return Undefined();
274}
275
276// The callback that is invoked by v8 whenever the JavaScript 'load'
277// function is called. Loads, compiles and executes its argument
278// JavaScript file.
279Handle<Value> InterpreterV8::FuncInclude(const Arguments& args)
280{
281 for (int i = 0; i<args.Length(); i++)
282 {
283 const HandleScope handle_scope;
284
285 const String::Utf8Value file(args[i]);
286 if (*file == NULL)
287 return ThrowException(String::New(("Error loading file '"+string(*file)+"'").c_str()));
288
289 if (!ExecuteFile(*file))
290 return ThrowException(String::New(("Execution of '"+string(*file)+"' failed").c_str()));
291 }
292 return Undefined();
293}
294
295Handle<Value> InterpreterV8::FuncVersion(const Arguments&)
296{
297 return String::New(V8::GetVersion());
298}
299
300Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
301{
302 HandleScope handle_scope;
303
304 void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
305 if (!ptr)
306 return Boolean::New(false);
307
308#ifdef HAVE_SQL
309 Database *db = reinterpret_cast<Database*>(ptr);
310 auto it = find(fDatabases.begin(), fDatabases.end(), db);
311 fDatabases.erase(it);
312 delete db;
313#endif
314
315 args.This()->SetInternalField(0, External::New(0));
316
317 return Boolean::New(true);
318}
319Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
320{
321 if (args.Length()!=1)
322 return ThrowException(String::New("Number of arguments must be exactly 1."));
323
324 if (!args[0]->IsString())
325 return ThrowException(String::New("Both arguments must be a string."));
326
327 HandleScope handle_scope;
328
329 void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
330 if (!ptr)
331 return Undefined();
332
333 const String::Utf8Value query(args[0]);
334
335#ifdef HAVE_SQL
336 try
337 {
338 Database *db = reinterpret_cast<Database*>(ptr);
339
340 const mysqlpp::StoreQueryResult res = db->query(*query).store();
341
342 Handle<Array> ret = Array::New();
343 ret->Set(String::New("table"), String::New(res.table()));
344
345 int irow=0;
346 for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++)
347 {
348 Handle<Array> row = Array::New();
349
350 const mysqlpp::FieldNames *list = it->field_list().list;
351
352 for (size_t i=0; i<it->size(); i++)
353 {
354 const Handle<Value> name = String::New((*list)[i].c_str());
355
356 if ((*it)[i].is_null())
357 {
358 row->Set(name, Undefined());
359 continue;
360 }
361
362 const string sql_type = (*it)[i].type().sql_name();
363
364 const bool uns = sql_type.find("UNSIGNED")==string::npos;
365
366 if (sql_type.find("BIGINT")!=string::npos)
367 {
368 if (uns)
369 {
370 const uint64_t val = (uint64_t)(*it)[i];
371 if (val>UINT32_MAX)
372 row->Set(name, Number::New(val));
373 else
374 row->Set(name, Integer::NewFromUnsigned(val));
375 }
376 else
377 {
378 const int64_t val = (int64_t)(*it)[i];
379 if (val<INT32_MIN || val>INT32_MAX)
380 row->Set(name, Number::New(val));
381 else
382 row->Set(name, Integer::NewFromUnsigned(val));
383 }
384 continue;
385 }
386
387 // 32 bit
388 if (sql_type.find("INT")!=string::npos)
389 {
390 if (uns)
391 row->Set(name, Integer::NewFromUnsigned((uint32_t)(*it)[i]));
392 else
393 row->Set(name, Integer::New((int32_t)(*it)[i]));
394 }
395
396 if (sql_type.find("BOOL")!=string::npos )
397 {
398 row->Set(name, Boolean::New((bool)(*it)[i]));
399 continue;
400 }
401
402 if (sql_type.find("FLOAT")!=string::npos)
403 {
404 ostringstream val;
405 val << setprecision(7) << (float)(*it)[i];
406 row->Set(name, Number::New(stod(val.str())));
407 continue;
408
409 }
410 if (sql_type.find("DOUBLE")!=string::npos)
411 {
412 row->Set(name, Number::New((double)(*it)[i]));
413 continue;
414 }
415
416 if (sql_type.find("CHAR")!=string::npos ||
417 sql_type.find("TEXT")!=string::npos)
418 {
419 row->Set(name, String::New((const char*)(*it)[i]));
420 continue;
421 }
422
423 if (sql_type.find("TIMESTAMP")!=string::npos)
424 {
425 row->Set(name, Date::New(time_t(mysqlpp::Time((*it)[i]))*1000));
426 continue;
427 }
428
429 if (sql_type.find("DATETIME")!=string::npos)
430 {
431 row->Set(name, Date::New(time_t(mysqlpp::DateTime((*it)[i]))*1000));
432 continue;
433 }
434
435 if (sql_type.find(" DATE ")!=string::npos)
436 {
437 row->Set(name, Date::New(time_t((mysqlpp::Date)(*it)[i])*1000));
438 continue;
439 }
440
441 }
442
443 ret->Set(Integer::New(irow++), row);
444 }
445
446 return handle_scope.Close(ret);
447 }
448 catch (const exception &e)
449 {
450 return ThrowException(String::New(e.what()));
451 }
452#endif
453}
454
455Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
456{
457 if (args.Length()!=1)
458 return ThrowException(String::New("Number of arguments must be exactly 1."));
459
460 if (!args[0]->IsString())
461 return ThrowException(String::New("Argument 1 must be a string."));
462
463 HandleScope handle_scope;
464
465 const String::Utf8Value database(args[0]);
466 const String::Utf8Value query (args[1]);
467
468#ifdef HAVE_SQL
469 try
470 {
471 Database *db = new Database(*database);
472 fDatabases.push_back(db);
473
474 Handle<ObjectTemplate> tem = ObjectTemplate::New();
475 tem->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly);
476 tem->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly);
477 tem->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly);
478 tem->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly);
479 tem->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery), ReadOnly);
480 tem->Set(String::New("close"), FunctionTemplate::New(WrapDbClose), ReadOnly);
481 tem->SetInternalFieldCount(1);
482
483 Handle<Object> obj = tem->NewInstance();
484 obj->SetInternalField(0, External::New(db));
485
486 return handle_scope.Close(obj);
487 }
488 catch (const exception &e)
489 {
490 return ThrowException(String::New(e.what()));
491 }
492#endif
493}
494
495Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
496{
497 // Dim values are always unsigned per (FACT++) definition
498 switch (type)
499 {
500 case 'F':
501 {
502 // Remove the "imprecision" effect coming from casting a float to
503 // a double and then showing it with double precision
504 ostringstream val;
505 val << setprecision(7) << *reinterpret_cast<const float*>(ptr);
506 Handle<Value> v=Number::New(stod(val.str()));
507 ptr+=4;
508 return v;
509 }
510 case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; }
511 case 'I':
512 case 'L': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; }
513 case 'X':
514 {
515 const uint64_t val = *reinterpret_cast<const uint64_t*>(ptr);
516 ptr += 8;
517 if (val>UINT32_MAX)
518 return Number::New(val);
519 return Integer::NewFromUnsigned(val);
520 }
521 case 'S': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
522 case 'C': { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; }
523 case ':': { Handle<Value> v=String::New(ptr); return v; }
524 }
525 return Undefined();
526}
527
528Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
529{
530 HandleScope handle_scope;
531
532 //const void *ptr = Local<External>::Cast(info.This()->GetInternalField(0))->Value();
533
534 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
535 return Boolean::New(JsUnsubscribe(*str));
536}
537
538Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
539{
540 HandleScope handle_scope;
541
542 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
543
544 const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
545
546 const EventImp *evt = p.second;
547 if (!evt)
548 return Undefined();
549
550 //if (counter==cnt)
551 // return info.Holder();//Holder()->Get(String::New("data"));
552
553 const vector<Description> vec = JsDescription(*str);
554
555 Handle<Array> ret = Array::New();
556 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly);
557 ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly);
558 ret->Set(String::New("counter"), Integer::New(p.first), ReadOnly);
559 ret->Set(String::New("time"), Date::New(evt->GetJavaDate()), ReadOnly);
560 ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly);
561
562 typedef boost::char_separator<char> separator;
563 const boost::tokenizer<separator> tokenizer(evt->GetFormat(), separator(";:"));
564
565 const vector<string> tok(tokenizer.begin(), tokenizer.end());
566
567 Handle<Array> obj = tok.size()==1 ? ret : Array::New();
568
569 const char *ptr = evt->GetText();
570 try
571 {
572 size_t pos = 1;
573 for (auto it=tok.begin(); it!=tok.end(); it++, pos++)
574 {
575 char type = (*it)[0];
576 it++;
577
578 if (it==tok.end() && type=='C')
579 type = ':';
580
581 if (it==tok.end() && type!=':')
582 return ThrowException(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str()));
583
584 string name = pos<vec.size() ? vec[pos].name : "";
585 if (tok.size()==1)
586 name = "data";
587
588 const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str());
589
590 if (cnt==1)
591 {
592 const Handle<Value> v = Convert(type, ptr);
593 if (tok.size()>1)
594 obj->Set(pos-1, v);
595 if (!name.empty())
596 obj->Set(String::New(name.c_str()), v);
597 }
598 else
599 {
600 Handle<Array> a = Array::New(cnt);
601 for (uint32_t i=0; i<cnt; i++)
602 a->Set(i, Convert(type, ptr));
603 if (tok.size()>1)
604 obj->Set(pos-1, a);
605 if (!name.empty())
606 obj->Set(String::New(name.c_str()), a);
607 }
608
609 if (it==tok.end())
610 break;
611 }
612
613 if (tok.size()>1)
614 ret->Set(String::New("data"), obj, ReadOnly);
615
616 return handle_scope.Close(ret);
617 }
618 catch (...)
619 {
620 return ThrowException(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str()));
621 }
622}
623
624/*
625void Cleanup( Persistent<Value> object, void *parameter )
626{
627 cout << "======================> RemoveMyObj()" << endl;
628}*/
629
630Handle<Value> InterpreterV8::FuncOpen(const Arguments &args)
631{
632 if (args.Length()!=1)
633 return ThrowException(String::New("Number of arguments must be exactly 1."));
634
635 if (!args[0]->IsString())
636 return ThrowException(String::New("Argument 1 must be a string."));
637
638 //if (!args.IsConstructCall())
639 // return ThrowException(String::New("Must be used as constructor."));
640
641 HandleScope handle_scope;
642
643 const String::Utf8Value str(args[0]);
644
645 void *ptr = JsSubscribe(*str);
646 if (ptr==0)
647 return ThrowException(String::New(("Subscription to '"+string(*str)+"' already exists.").c_str()));
648
649 Handle<ObjectTemplate> tem = ObjectTemplate::New();
650 tem->Set(String::New("get"), FunctionTemplate::New(WrapGetData), ReadOnly);
651 tem->Set(String::New("close"), FunctionTemplate::New(WrapClose), ReadOnly);
652 tem->Set(String::New("name"), String::New(*str), ReadOnly);
653 tem->SetInternalFieldCount(1);
654 //tem->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly);
655
656 Handle<Object> obj = tem->NewInstance();
657 obj->SetInternalField(0, External::New(ptr));
658
659 return handle_scope.Close(obj);
660
661 // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance());
662 // obj.MakeWeak((void*)1, Cleanup);
663 // return obj;
664}
665
666bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
667{
668 v8::Locker locker;
669 fThreadId = V8::GetCurrentThreadId();
670
671 JsLoad(filename);
672
673 HandleScope handle_scope;
674
675 // Create a template for the global object.
676 Handle<ObjectTemplate> dim = ObjectTemplate::New();
677 dim->Set(String::New("print"), FunctionTemplate::New(WrapPrint), ReadOnly);
678 dim->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
679 dim->Set(String::New("wait"), FunctionTemplate::New(WrapWait), ReadOnly);
680 dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly);
681 dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly);
682 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly);
683 dim->Set(String::New("open"), FunctionTemplate::New(WrapOpen), ReadOnly);
684 dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
685
686 Handle<ObjectTemplate> global = ObjectTemplate::New();
687 global->Set(String::New("dim"), dim, ReadOnly);
688 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly);
689 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly);
690 global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
691
692 // Persistent
693 Persistent<Context> context = Context::New(NULL, global);
694 if (context.IsEmpty())
695 {
696 //printf("Error creating context\n");
697 return false;
698 }
699
700 v8::Context::Scope scope(context);
701
702 Local<Array> args = Array::New(map.size());
703 for (auto it=map.begin(); it!=map.end(); it++)
704 args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
705 context->Global()->Set(String::New("$"), args, ReadOnly);
706 context->Global()->Set(String::New("arg"), args, ReadOnly);
707
708 JsStart(filename);
709
710 //context->Enter();
711 v8::Locker::StartPreemption(10);
712 const bool rc = ExecuteFile(filename);
713 v8::Locker::StopPreemption();
714 //context->Exit();
715
716 context.Dispose();
717
718#ifdef HAVE_SQL
719 for (auto it=fDatabases.begin(); it!=fDatabases.end(); it++)
720 delete *it;
721 fDatabases.clear();
722#endif
723
724
725 JsEnd(filename);
726
727 return rc;
728}
729
730void InterpreterV8::JsStop()
731{
732 v8::Locker locker;
733 //cout << "Terminate " << fThreadId << endl;
734 if (This->fThreadId>=0)
735 v8::V8::TerminateExecution(This->fThreadId);
736 //cout << "Terminate " << fThreadId << endl;
737 v8::Unlocker unlocker;
738}
739
740#endif
741
Note: See TracBrowser for help on using the repository browser.