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

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