1 | #ifndef FACT_InterpreterV8
|
---|
2 | #define FACT_InterpreterV8
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <list>
|
---|
6 | #include <string>
|
---|
7 |
|
---|
8 | #ifdef HAVE_V8
|
---|
9 | #include <v8.h>
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | #include "State.h"
|
---|
13 | #include "Description.h"
|
---|
14 | #include "EventImp.h"
|
---|
15 |
|
---|
16 | class Database;
|
---|
17 | #include<iostream>
|
---|
18 | class InterpreterV8
|
---|
19 | {
|
---|
20 | static InterpreterV8 *This;
|
---|
21 |
|
---|
22 | // The main thread id, needed to be able to terminate
|
---|
23 | // the thread forcefully from 'the outside'
|
---|
24 | int fThreadId;
|
---|
25 |
|
---|
26 | // A loookup table which allows to indentify the
|
---|
27 | // the JavaScript object corrsponding to the
|
---|
28 | // service name (for checking of an .onchange
|
---|
29 | // subscription exists for that object)
|
---|
30 | std::map<std::string, v8::Persistent<v8::Object>> fReverseMap;
|
---|
31 |
|
---|
32 | #ifdef HAVE_SQL
|
---|
33 | std::list<Database*> fDatabases;
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #ifdef HAVE_V8
|
---|
37 | bool ReportException(v8::TryCatch* try_catch);
|
---|
38 | bool ExecuteStringNT(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
39 | bool ExecuteCode(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
40 | bool ExecuteCode(const std::string &code, const std::string &file="");
|
---|
41 | bool ExecuteFile(const std::string &name);
|
---|
42 |
|
---|
43 | v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
|
---|
44 | v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
|
---|
45 | v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
|
---|
46 | v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args);
|
---|
47 | v8::Handle<v8::Value> FuncAlarm(const v8::Arguments& args);
|
---|
48 | v8::Handle<v8::Value> FuncOut(const v8::Arguments& args);
|
---|
49 | v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
|
---|
50 | v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
|
---|
51 | v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
|
---|
52 | v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
|
---|
53 | v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
|
---|
54 | v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
|
---|
55 | v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
|
---|
56 | v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
|
---|
57 | v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
|
---|
58 | v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
|
---|
59 |
|
---|
60 | static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
|
---|
61 | static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
|
---|
62 | static v8::Handle<v8::Value> WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); }
|
---|
63 | static v8::Handle<v8::Value> WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); }
|
---|
64 | static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
|
---|
65 | static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
|
---|
66 | static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
|
---|
67 | static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
|
---|
68 | static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
|
---|
69 | static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
|
---|
70 | static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
|
---|
71 | static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
|
---|
72 | static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
|
---|
73 | static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
|
---|
74 | static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
|
---|
75 | static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
|
---|
76 | static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
|
---|
77 |
|
---|
78 | static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
|
---|
79 | v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | public:
|
---|
83 | InterpreterV8() : fThreadId(-1)
|
---|
84 | {
|
---|
85 | This = this;
|
---|
86 | }
|
---|
87 | virtual ~InterpreterV8()
|
---|
88 | {
|
---|
89 | This = 0;
|
---|
90 |
|
---|
91 | #ifdef HAVE_V8
|
---|
92 | v8::Locker locker;
|
---|
93 | v8::V8::Dispose();
|
---|
94 | #endif
|
---|
95 | }
|
---|
96 |
|
---|
97 | virtual void JsLoad(const std::string &) { }
|
---|
98 | virtual void JsStart(const std::string &) { }
|
---|
99 | virtual void JsEnd(const std::string &) { }
|
---|
100 | virtual void JsPrint(const std::string &) { }
|
---|
101 | virtual void JsAlarm(const std::string & = "") { }
|
---|
102 | virtual void JsOut(const std::string &) { }
|
---|
103 | virtual void JsResult(const std::string &) { }
|
---|
104 | virtual void JsException(const std::string &) { }
|
---|
105 | virtual bool JsSend(const std::string &) { return true; }
|
---|
106 | //virtual void JsSleep(uint32_t) { }
|
---|
107 | //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
|
---|
108 | virtual State JsState(const std::string &) { return State(); };
|
---|
109 | virtual void *JsSubscribe(const std::string &) { return 0; };
|
---|
110 | virtual bool JsUnsubscribe(const std::string &) { return false; };
|
---|
111 | virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
|
---|
112 | virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
|
---|
113 |
|
---|
114 | void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
|
---|
115 |
|
---|
116 | bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
|
---|
117 | static void JsStop();
|
---|
118 | };
|
---|
119 |
|
---|
120 | #ifndef HAVE_V8
|
---|
121 | inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
|
---|
122 | inline void InterpreterV8::JsStop() { }
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #endif
|
---|