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

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