1 | #ifndef FACT_InterpreterV8
|
---|
2 | #define FACT_InterpreterV8
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <string>
|
---|
6 |
|
---|
7 | #ifdef HAVE_V8
|
---|
8 | #include <v8.h>
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | #include "Description.h"
|
---|
12 | #include "EventImp.h"
|
---|
13 |
|
---|
14 | class Database;
|
---|
15 |
|
---|
16 | class InterpreterV8
|
---|
17 | {
|
---|
18 | static InterpreterV8 *This;
|
---|
19 |
|
---|
20 | int fThreadId;
|
---|
21 |
|
---|
22 | #ifdef HAVE_SQL
|
---|
23 | std::vector<Database*> fDatabases;
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #ifdef HAVE_V8
|
---|
27 | bool ReportException(v8::TryCatch* try_catch);
|
---|
28 | bool ExecuteStringNT(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
29 | bool ExecuteCode(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
30 | bool ExecuteCode(const std::string &code, const std::string &file="");
|
---|
31 | bool ExecuteFile(const std::string &name);
|
---|
32 |
|
---|
33 | v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
|
---|
34 | v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
|
---|
35 | v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
|
---|
36 | v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args);
|
---|
37 | v8::Handle<v8::Value> FuncAlarm(const v8::Arguments& args);
|
---|
38 | v8::Handle<v8::Value> FuncOut(const v8::Arguments& args);
|
---|
39 | v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
|
---|
40 | v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
|
---|
41 | v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
|
---|
42 | v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
|
---|
43 | v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
|
---|
44 | v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
|
---|
45 | v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
|
---|
46 | v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
|
---|
47 | v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
|
---|
48 | v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
|
---|
49 |
|
---|
50 | static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
|
---|
51 | static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
|
---|
52 | static v8::Handle<v8::Value> WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); }
|
---|
53 | static v8::Handle<v8::Value> WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); }
|
---|
54 | static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
|
---|
55 | static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
|
---|
56 | static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
|
---|
57 | static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
|
---|
58 | static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
|
---|
59 | static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
|
---|
60 | static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
|
---|
61 | static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
|
---|
62 | static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
|
---|
63 | static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
|
---|
64 | static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
|
---|
65 | static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
|
---|
66 | static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
|
---|
67 |
|
---|
68 | static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | public:
|
---|
72 | InterpreterV8() : fThreadId(-1)
|
---|
73 | {
|
---|
74 | This = this;
|
---|
75 | }
|
---|
76 | virtual ~InterpreterV8()
|
---|
77 | {
|
---|
78 | This = 0;
|
---|
79 | #ifdef HAVE_V8
|
---|
80 | v8::V8::Dispose();
|
---|
81 | #endif
|
---|
82 | }
|
---|
83 |
|
---|
84 | virtual void JsLoad(const std::string &) { }
|
---|
85 | virtual void JsStart(const std::string &) { }
|
---|
86 | virtual void JsEnd(const std::string &) { }
|
---|
87 | virtual void JsPrint(const std::string &) { }
|
---|
88 | virtual void JsAlarm(const std::string & = "") { }
|
---|
89 | virtual void JsOut(const std::string &) { }
|
---|
90 | virtual void JsResult(const std::string &) { }
|
---|
91 | virtual void JsException(const std::string &) { }
|
---|
92 | virtual bool JsSend(const std::string &) { return true; }
|
---|
93 | virtual void JsSleep(uint32_t) { }
|
---|
94 | virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
|
---|
95 | virtual std::pair<int32_t, std::string> JsState(const std::string &) { return std::make_pair(-256, ""); };
|
---|
96 | virtual void *JsSubscribe(const std::string &) { return 0; };
|
---|
97 | virtual bool JsUnsubscribe(const std::string &) { return false; };
|
---|
98 | virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
|
---|
99 | virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
|
---|
100 |
|
---|
101 | bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
|
---|
102 | static void JsStop();
|
---|
103 | };
|
---|
104 |
|
---|
105 | #ifndef HAVE_V8
|
---|
106 | inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
|
---|
107 | inline void InterpreterV8::JsStop() { }
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | #endif
|
---|