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 InterpreterV8
|
---|
15 | {
|
---|
16 | static InterpreterV8 *This;
|
---|
17 |
|
---|
18 | int fThreadId;
|
---|
19 |
|
---|
20 | #ifdef HAVE_V8
|
---|
21 | bool ReportException(v8::TryCatch* try_catch);
|
---|
22 | bool ExecuteStringNT(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
23 | bool ExecuteCode(const v8::Handle<v8::String> &code, const v8::Handle<v8::Value> &file);
|
---|
24 | bool ExecuteCode(const std::string &code, const std::string &file="");
|
---|
25 | bool ExecuteFile(const std::string &name);
|
---|
26 |
|
---|
27 | v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
|
---|
28 | v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
|
---|
29 | v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
|
---|
30 | v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args);
|
---|
31 | v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
|
---|
32 | v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
|
---|
33 | v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
|
---|
34 | v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
|
---|
35 | v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
|
---|
36 | v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
|
---|
37 |
|
---|
38 | static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
|
---|
39 | static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
|
---|
40 | static v8::Handle<v8::Value> WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); }
|
---|
41 | static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
|
---|
42 | static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
|
---|
43 | static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
|
---|
44 | static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
|
---|
45 | static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
|
---|
46 | static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
|
---|
47 | static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
|
---|
48 | static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | public:
|
---|
52 | InterpreterV8() : fThreadId(-1)
|
---|
53 | {
|
---|
54 | This = this;
|
---|
55 | }
|
---|
56 | virtual ~InterpreterV8()
|
---|
57 | {
|
---|
58 | This = 0;
|
---|
59 | v8::V8::Dispose();
|
---|
60 | }
|
---|
61 |
|
---|
62 | virtual void JsLoad(const std::string &) { }
|
---|
63 | virtual void JsStart(const std::string &) { }
|
---|
64 | virtual void JsEnd(const std::string &) { }
|
---|
65 | virtual void JsPrint(const std::string &) { }
|
---|
66 | virtual void JsResult(const std::string &) { }
|
---|
67 | virtual void JsException(const std::string &) { }
|
---|
68 | virtual bool JsSend(const std::string &) { return true; }
|
---|
69 | virtual void JsSleep(uint32_t) { }
|
---|
70 | virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
|
---|
71 | virtual std::pair<int32_t, std::string> JsState(const std::string &) { return std::make_pair(-256, ""); };
|
---|
72 | virtual void *JsSubscribe(const std::string &) { return 0; };
|
---|
73 | virtual bool JsUnsubscribe(const std::string &) { return false; };
|
---|
74 | virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
|
---|
75 | virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
|
---|
76 |
|
---|
77 | bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
|
---|
78 | static void JsStop();
|
---|
79 | };
|
---|
80 |
|
---|
81 | #ifndef HAVE_V8
|
---|
82 | inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>()) { return false; }
|
---|
83 | inline void InterpreterV8::JsStop() { }
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | #endif
|
---|