source: trunk/FACT++/src/InterpreterV8-4.h@ 20030

Last change on this file since 20030 was 20023, checked in by tbretz, 4 years ago
This adds a version that was tested at least with V8 6.8 and a wrapper to switch between both -- using ifdefs in the code is not reaonable as there would be too many.
File size: 11.5 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 "Service.h"
16#include "Description.h"
17#include "EventImp.h"
18
19class Database;
20namespace v8
21{
22 class Platform;
23};
24
25#ifdef HAVE_NOVA
26struct ln_rst_time;
27#endif
28
29class InterpreterV8
30{
31 template<class T>
32 using PersistentCopy = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;
33
34 template<class T, class S>
35 using PersistentMap = std::map<T, PersistentCopy<S>>;
36
37 static InterpreterV8 *This;
38 static std::unique_ptr<v8::Platform> fPlatform;
39
40 // The main thread id, needed to be able to terminate
41 // the thread forcefully from 'the outside'
42 v8::Isolate *fMainThread;
43 std::set<v8::Isolate*> fThreadIsolates;
44
45 // A loookup table which allows to indentify the
46 // the JavaScript object corrsponding to the
47 // service name (for checking of an .onchange
48 // subscription exists for that object)
49 PersistentMap<std::string, v8::Object> fReverseMap;
50
51 // Lookup table for the callbacks in cases of state changes
52 PersistentMap<std::string, v8::Object> fStateCallbacks;
53
54 // List of all threads
55 std::vector<std::thread> fThreads;
56
57 // List of all states already set
58 std::vector<std::pair<int, std::string>> fStates;
59
60 // Interrupt handler
61 PersistentCopy<v8::Object> fInterruptCallback;
62
63 static v8::Handle<v8::FunctionTemplate> fTemplateLocal;
64 static v8::Handle<v8::FunctionTemplate> fTemplateSky;
65 static v8::Handle<v8::FunctionTemplate> fTemplateEvent;
66 static v8::Handle<v8::FunctionTemplate> fTemplateDescription;
67
68#ifdef HAVE_SQL
69 std::list<Database*> fDatabases;
70#endif
71
72 std::string fIncludePath;
73
74#ifdef HAVE_V8
75 bool HandleException(v8::TryCatch &try_catch, const char *where);
76 void ExecuteConsole();
77 v8::Handle<v8::Value> ExecuteCode(const std::string &code, const std::string &file="internal");
78 v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
79
80 void Thread(int &id, v8::Persistent<v8::Object> _this, v8::Persistent<v8::Function> func, uint32_t ms);
81
82 std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val, bool only=true);
83
84 //typedef v8::internal::Arguments FactArguments;
85 //typedef v8::FunctionCallback FactArguments;
86 typedef v8::FunctionCallbackInfo<v8::Value> FactArguments;
87
88 static void TerminateExecution(v8::Isolate*);
89
90 void FuncWait(const FactArguments& args);
91 void FuncSend(const FactArguments& args);
92 void FuncSleep(const FactArguments& args);
93 void FuncTimeout(const FactArguments& args);
94 void FuncThread(const FactArguments& args);
95 void FuncKill(const FactArguments& args);
96 void FuncLog(const FactArguments& args);
97 void FuncAlarm(const FactArguments& args);
98 void FuncOut(const FactArguments& args);
99 void FuncWarn(const FactArguments& args);
100 void FuncFile(const FactArguments& args);
101 void FuncSendMail(const FactArguments& args);
102 void FuncSendCurl(const FactArguments& args);
103 void FuncInclude(const FactArguments& args);
104 void FuncExit(const FactArguments& args);
105 void FuncState(const FactArguments& args);
106 void FuncSetState(const FactArguments& args);
107 void FuncGetState(const FactArguments& args);
108 void FuncGetStates(const FactArguments& args);
109 void FuncGetDescription(const FactArguments& args);
110 void FuncGetServices(const FactArguments& args);
111 void FuncNewState(const FactArguments& args);
112 void FuncSetInterrupt(const FactArguments& args);
113 void FuncTriggerInterrupt(const FactArguments& args);
114 //v8::Handle<v8::Value> FuncOpen(const FactArguments& args);
115 void FuncSubscription(const FactArguments& args);
116 void FuncGetData(const FactArguments &args);
117 void FuncClose(const FactArguments &args);
118 void FuncQuery(const FactArguments &args);
119 void FuncDatabase(const FactArguments &args);
120 void FuncDbQuery(const FactArguments &args);
121 void FuncDbClose(const FactArguments &args);
122 void OnChangeSet(v8::Local<v8::Name>, v8::Local<v8::Value>, const v8::PropertyCallbackInfo<v8::Value> &);
123
124 static v8::Handle<v8::Value> Constructor(const FactArguments &args);
125
126 static void ConstructorMail(const FactArguments &args);
127 static void ConstructorCurl(const FactArguments &args);
128
129#ifdef HAVE_NOVA
130 static double GetDataMember(const FactArguments &args, const char *name);
131
132 static void CalcDist(const FactArguments &args, const bool);
133
134 static void LocalToString(const FactArguments &args);
135 static void SkyToString(const FactArguments &args);
136 static void MoonToString(const FactArguments &args);
137 static void LocalDist(const FactArguments &args);
138 static void SkyDist(const FactArguments &args);
139 static void MoonDisk(const FactArguments &args);
140 static void LocalToSky(const FactArguments &args);
141 static void SkyToLocal(const FactArguments &args);
142 static void MoonToLocal(const FactArguments &args);
143 static void ConstructorMoon(const FactArguments &args);
144 static void ConstructorSky(const FactArguments &args);
145 static void ConstructorLocal(const FactArguments &args);
146 static void MoonHorizon(const FactArguments &args);
147 static void SunHorizon(const FactArguments &args);
148#endif
149
150 static void WrapInclude(const FactArguments &args) { if (This) This->FuncInclude(args); }
151 static void WrapFile(const FactArguments &args) { if (This) This->FuncFile(args); }
152 static void WrapSendMail(const FactArguments &args) { if (This) This->FuncSendMail(args); }
153 static void WrapSendCurl(const FactArguments &args) { if (This) This->FuncSendCurl(args); }
154 static void WrapLog(const FactArguments &args) { if (This) This->FuncLog(args); }
155 static void WrapAlarm(const FactArguments &args) { if (This) This->FuncAlarm(args); }
156 static void WrapOut(const FactArguments &args) { if (This) This->FuncOut(args); }
157 static void WrapWarn(const FactArguments &args) { if (This) This->FuncWarn(args); }
158 static void WrapWait(const FactArguments &args) { if (This) This->FuncWait(args); }
159 static void WrapSend(const FactArguments &args) { if (This) This->FuncSend(args); }
160 static void WrapSleep(const FactArguments &args) { if (This) This->FuncSleep(args); }
161 static void WrapTimeout(const FactArguments &args) { if (This) This->FuncTimeout(args); }
162 static void WrapThread(const FactArguments &args) { if (This) This->FuncThread(args); }
163 static void WrapKill(const FactArguments &args) { if (This) This->FuncKill(args); }
164 static void WrapExit(const FactArguments &args) { if (This) This->FuncExit(args); }
165 static void WrapState(const FactArguments &args) { if (This) This->FuncState(args); }
166 static void WrapNewState(const FactArguments &args) { if (This) This->FuncNewState(args); }
167 static void WrapSetState(const FactArguments &args) { if (This) This->FuncSetState(args); }
168 static void WrapGetState(const FactArguments &args) { if (This) This->FuncGetState(args); }
169 static void WrapGetStates(const FactArguments &args){ if (This) This->FuncGetStates(args);}
170 static void WrapGetDescription(const FactArguments &args){ if (This) This->FuncGetDescription(args); }
171 static void WrapGetServices(const FactArguments &args){ if (This) This->FuncGetServices(args); }
172 static void WrapSetInterrupt(const FactArguments &args){ if (This)This->FuncSetInterrupt(args); }
173 static void WrapTriggerInterrupt(const FactArguments &args){ if (This) This->FuncTriggerInterrupt(args); }
174 //static v8::Handle<v8::Value> WrapOpen(const FactArguments &args) { if (This) return This->FuncOpen(args); else return Undefined(v8::Isolate::GetCurrent()); }
175 static void WrapSubscription(const FactArguments &args){ if (This) This->FuncSubscription(args); }
176 static void WrapGetData(const FactArguments &args) { if (This) This->FuncGetData(args); }
177 static void WrapClose(const FactArguments &args) { if (This) This->FuncClose(args); }
178 static void WrapQuery(const FactArguments &args) { if (This) This->FuncQuery(args); }
179 static void WrapDatabase(const FactArguments &args) { if (This) This->FuncDatabase(args); }
180 static void WrapDbQuery(const FactArguments &args) { if (This) This->FuncDbQuery(args); }
181 static void WrapDbClose(const FactArguments &args) { if (This) This->FuncDbClose(args); }
182 static void WrapOnChangeSet(v8::Local<v8::Name> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info)
183 {
184 if (This) This->OnChangeSet(name, value, info);
185 }
186
187 static void OnChangeGet(v8::Local<v8::Name> /*property*/, const v8::PropertyCallbackInfo<v8::Value> &/*info*/)
188 {
189 //return Undefined(v8::Isolate::GetCurrent());
190 }
191
192 static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
193 v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
194#endif
195
196 v8::Handle<v8::Value> HandleInterruptImp(std::string, uint64_t);
197
198public:
199 InterpreterV8();
200 virtual ~InterpreterV8()
201 {
202 This = 0;
203
204#ifdef HAVE_V8
205 //v8::Locker locker;
206 v8::V8::Dispose();
207#endif
208 }
209
210 std::vector<std::string> JsGetCommandList(const char *, int) const;
211
212 virtual void JsLoad(const std::string & = "");
213 virtual void JsStart(const std::string &) { }
214 virtual void JsEnd(const std::string & = "");
215 virtual void JsPrint(const std::string & = "") { }
216 virtual void JsAlarm(const std::string & = "") { }
217 virtual void JsOut(const std::string &) { }
218 virtual void JsWarn(const std::string &) { }
219 virtual void JsResult(const std::string &) { }
220 virtual void JsException(const std::string &) { }
221 virtual bool JsSend(const std::string &) { return true; }
222 //virtual void JsSleep(uint32_t) { }
223 //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
224 virtual State JsState(const std::string &) { return State(); };
225 virtual void *JsSubscribe(const std::string &) { return 0; };
226 virtual bool JsUnsubscribe(const std::string &) { return false; };
227
228 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
229 virtual bool JsSetState(int) { return false; }
230 virtual bool JsHasState(int) const { return false; }
231 virtual bool JsHasState(const std::string &) const { return false; }
232 virtual int JsGetState(const std::string &) const { return -2; }
233 virtual State JsGetCurrentState() const { return State(); }
234 virtual std::vector<State> JsGetStates(const std::string &) { return std::vector<State>(); }
235 virtual std::set<Service> JsGetServices() { return std::set<Service>(); }
236 virtual std::vector<Description> JsGetDescription(const std::string &) { return std::vector<Description>(); }
237
238 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
239 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
240
241 int JsHandleInterrupt(const EventImp &);
242 void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
243 void JsHandleState(const std::string &, const State &);
244
245 void AddFormatToGlobal();
246
247 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
248 static void JsStop();
249};
250
251#ifndef HAVE_V8
252inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
253inline void InterpreterV8::JsStop() { }
254#endif
255
256#endif
Note: See TracBrowser for help on using the repository browser.