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