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