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

Last change on this file since 15034 was 14983, checked in by tbretz, 12 years ago
Implemented to possibility to run an interactive javascript interpreter.
File size: 10.9 KB
Line 
1#ifndef FACT_InterpreterV8
2#define FACT_InterpreterV8
3
4#include <map>
5#include <set>
6#include <list>
7#include <string>
8#include <thread>
9
10#ifdef HAVE_V8
11#include <v8.h>
12#endif
13
14#include "State.h"
15#include "Description.h"
16#include "EventImp.h"
17
18class Database;
19
20#ifdef HAVE_NOVA
21struct ln_rst_time;
22#endif
23
24class InterpreterV8
25{
26 static InterpreterV8 *This;
27
28 // The main thread id, needed to be able to terminate
29 // the thread forcefully from 'the outside'
30 int fThreadId;
31 std::set<int> fThreadIds;
32
33 // A loookup table which allows to indentify the
34 // the JavaScript object corrsponding to the
35 // service name (for checking of an .onchange
36 // subscription exists for that object)
37 std::map<std::string, v8::Persistent<v8::Object>> fReverseMap;
38
39 // Lookup table for the callbacks in cases of state changes
40 std::map<std::string, v8::Persistent<v8::Object>> fStateCallbacks;
41
42 // List of all threads
43 std::vector<std::thread> fThreads;
44
45 // List of all states already set
46 std::vector<std::pair<int, std::string>> fStates;
47
48 static v8::Handle<v8::FunctionTemplate> fTemplateLocal;
49 static v8::Handle<v8::FunctionTemplate> fTemplateSky;
50 static v8::Handle<v8::FunctionTemplate> fTemplateEvent;
51
52#ifdef HAVE_SQL
53 std::list<Database*> fDatabases;
54#endif
55
56#ifdef HAVE_V8
57 bool HandleException(v8::TryCatch &try_catch, const char *where);
58 bool ExecuteFile(const std::string &name, bool main=false);
59 bool ExecuteConsole();
60 v8::Handle<v8::Value> ExecuteCode(const std::string &code, const std::string &file="internal", bool main=false);
61 v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
62
63 void Thread(int &id, v8::Persistent<v8::Function> func, uint32_t ms);
64
65 v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
66 v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
67 v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
68 v8::Handle<v8::Value> FuncThread(const v8::Arguments& args);
69 v8::Handle<v8::Value> FuncKill(const v8::Arguments& args);
70 v8::Handle<v8::Value> FuncLog(const v8::Arguments& args);
71 v8::Handle<v8::Value> FuncAlarm(const v8::Arguments& args);
72 v8::Handle<v8::Value> FuncOut(const v8::Arguments& args);
73 v8::Handle<v8::Value> FuncFile(const v8::Arguments& args);
74 v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
75 v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
76 v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
77 v8::Handle<v8::Value> FuncSetState(const v8::Arguments& args);
78 v8::Handle<v8::Value> FuncGetState(const v8::Arguments& args);
79 v8::Handle<v8::Value> FuncGetStates(const v8::Arguments& args);
80 v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args);
81 //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
82 v8::Handle<v8::Value> FuncSubscription(const v8::Arguments& args);
83 v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
84 v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
85 v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
86 v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
87 v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
88 v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
89 v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
90
91 static v8::Handle<v8::Value> Constructor(const v8::Arguments &args);
92
93#ifdef HAVE_NOVA
94 static double GetDataMember(const v8::Arguments &args, const char *name);
95
96 static v8::Handle<v8::Value> CalcDist(const v8::Arguments &args, const bool);
97
98 static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
99 static v8::Handle<v8::Value> SkyToString(const v8::Arguments &args);
100 static v8::Handle<v8::Value> MoonToString(const v8::Arguments &args);
101 static v8::Handle<v8::Value> LocalDist(const v8::Arguments &args);
102 static v8::Handle<v8::Value> SkyDist(const v8::Arguments &args);
103 static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args);
104 static v8::Handle<v8::Value> LocalToSky(const v8::Arguments &args);
105 static v8::Handle<v8::Value> SkyToLocal(const v8::Arguments &args);
106 static v8::Handle<v8::Value> MoonToLocal(const v8::Arguments &args);
107 static v8::Handle<v8::Value> ConstructorMoon(const v8::Arguments &args);
108 static v8::Handle<v8::Value> ConstructorSky(const v8::Arguments &args);
109 static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
110 static v8::Handle<v8::Value> MoonHorizon(const v8::Arguments &args);
111 static v8::Handle<v8::Value> SunHorizon(const v8::Arguments &args);
112 static v8::Handle<v8::Object> ConstructRiseSet(const v8::Handle<v8::Value>, const ln_rst_time &, const bool &);
113#endif
114
115 static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
116 static v8::Handle<v8::Value> WrapFile(const v8::Arguments &args) { if (This) return This->FuncFile(args); else return v8::Undefined(); }
117 static v8::Handle<v8::Value> WrapLog(const v8::Arguments &args) { if (This) return This->FuncLog(args); else return v8::Undefined(); }
118 static v8::Handle<v8::Value> WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); }
119 static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
120 static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
121 static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
122 static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
123 static v8::Handle<v8::Value> WrapThread(const v8::Arguments &args) { if (This) return This->FuncThread(args); else return v8::Undefined(); }
124 static v8::Handle<v8::Value> WrapKill(const v8::Arguments &args) { if (This) return This->FuncKill(args); else return v8::Undefined(); }
125 static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
126 static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
127 static v8::Handle<v8::Value> WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); }
128 static v8::Handle<v8::Value> WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); }
129 static v8::Handle<v8::Value> WrapGetState(const v8::Arguments &args) { if (This) return This->FuncGetState(args); else return v8::Undefined(); }
130 static v8::Handle<v8::Value> WrapGetStates(const v8::Arguments &args){ if (This) return This->FuncGetStates(args);else return v8::Undefined(); }
131 //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
132 static v8::Handle<v8::Value> WrapSubscription(const v8::Arguments &args){ if (This) return This->FuncSubscription(args);else return v8::Undefined(); }
133 static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
134 static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
135 static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
136 static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
137 static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
138 static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
139 static v8::Handle<v8::Value> WrapOnChangeSet(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info)
140 {
141 if (This) return This->OnChangeSet(prop, value, info); else return v8::Undefined();
142 }
143
144 static v8::Handle<v8::Value> OnChangeGet(v8::Local<v8::String>, const v8::AccessorInfo &)
145 {
146 return v8::Handle<v8::Value>();
147 }
148
149 static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
150 v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
151#endif
152
153public:
154 InterpreterV8();
155 virtual ~InterpreterV8()
156 {
157 This = 0;
158
159#ifdef HAVE_V8
160 v8::Locker locker;
161 v8::V8::Dispose();
162#endif
163 }
164
165 virtual void JsLoad(const std::string &) { }
166 virtual void JsStart(const std::string &) { }
167 virtual void JsEnd(const std::string &) { }
168 virtual void JsPrint(const std::string &) { }
169 virtual void JsAlarm(const std::string & = "") { }
170 virtual void JsOut(const std::string &) { }
171 virtual void JsResult(const std::string &) { }
172 virtual void JsException(const std::string &) { }
173 virtual bool JsSend(const std::string &) { return true; }
174 //virtual void JsSleep(uint32_t) { }
175 //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
176 virtual State JsState(const std::string &) { return State(); };
177 virtual void *JsSubscribe(const std::string &) { return 0; };
178 virtual bool JsUnsubscribe(const std::string &) { return false; };
179
180 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
181 virtual bool JsSetState(int) { return false; }
182 virtual bool JsHasState(int) const { return false; }
183 virtual bool JsHasState(const std::string &) const { return false; }
184 virtual int JsGetState(const std::string &) const { return -2; }
185 virtual State JsGetCurrentState() const { return State(); }
186 virtual std::vector<State> JsGetStates(const std::string &) { return std::vector<State>(); }
187
188 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
189 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
190
191 void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
192 void JsHandleState(const std::string &, const State &);
193
194 void AddFormatToGlobal();
195
196 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
197 static void JsStop();
198};
199
200#ifndef HAVE_V8
201inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
202inline void InterpreterV8::JsStop() { }
203#endif
204
205#endif
Note: See TracBrowser for help on using the repository browser.