source: trunk/FACT++/src/InterpreterV8.h@ 14476

Last change on this file since 14476 was 14177, checked in by tbretz, 12 years ago
Added a simple database interface.
File size: 5.5 KB
Line 
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
14class Database;
15
16class 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> FuncOut(const v8::Arguments& args);
38 v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
39 v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
40 v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
41 v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
42 v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
43 v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
44 v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
45 v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
46 v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
47 v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
48
49 static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
50 static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
51 static v8::Handle<v8::Value> WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); }
52 static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
53 static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
54 static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
55 static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
56 static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
57 static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
58 static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
59 static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
60 static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
61 static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
62 static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
63 static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
64 static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
65
66 static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
67#endif
68
69public:
70 InterpreterV8() : fThreadId(-1)
71 {
72 This = this;
73 }
74 virtual ~InterpreterV8()
75 {
76 This = 0;
77#ifdef HAVE_V8
78 v8::V8::Dispose();
79#endif
80 }
81
82 virtual void JsLoad(const std::string &) { }
83 virtual void JsStart(const std::string &) { }
84 virtual void JsEnd(const std::string &) { }
85 virtual void JsPrint(const std::string &) { }
86 virtual void JsOut(const std::string &) { }
87 virtual void JsResult(const std::string &) { }
88 virtual void JsException(const std::string &) { }
89 virtual bool JsSend(const std::string &) { return true; }
90 virtual void JsSleep(uint32_t) { }
91 virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
92 virtual std::pair<int32_t, std::string> JsState(const std::string &) { return std::make_pair(-256, ""); };
93 virtual void *JsSubscribe(const std::string &) { return 0; };
94 virtual bool JsUnsubscribe(const std::string &) { return false; };
95 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
96 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
97
98 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
99 static void JsStop();
100};
101
102#ifndef HAVE_V8
103inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
104inline void InterpreterV8::JsStop() { }
105#endif
106
107#endif
Note: See TracBrowser for help on using the repository browser.