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

Last change on this file since 20084 was 20083, checked in by tbretz, 4 years ago
Replaced my own PersistentCopy by UniquePersistent, repaired Thread object (with the drawback that individual threads can not be killed anymore as everything is executed effectively in a single thread. So threads can execute code interleaved but not really in a parallel, like a singel core cpu.
File size: 11.3 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(int &id, 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 void FuncWait(const FactArguments& args);
88 void FuncSend(const FactArguments& args);
89 void FuncSleep(const FactArguments& args);
90 void FuncTimeout(const FactArguments& args);
91 void FuncThread(const FactArguments& args);
92 void FuncKill(const FactArguments& args);
93 void FuncLog(const FactArguments& args);
94 void FuncAlarm(const FactArguments& args);
95 void FuncOut(const FactArguments& args);
96 void FuncWarn(const FactArguments& args);
97 void FuncFile(const FactArguments& args);
98 void FuncSendMail(const FactArguments& args);
99 void FuncSendCurl(const FactArguments& args);
100 void FuncInclude(const FactArguments& args);
101 void FuncExit(const FactArguments& args);
102 void FuncState(const FactArguments& args);
103 void FuncSetState(const FactArguments& args);
104 void FuncGetState(const FactArguments& args);
105 void FuncGetStates(const FactArguments& args);
106 void FuncGetDescription(const FactArguments& args);
107 void FuncGetServices(const FactArguments& args);
108 void FuncNewState(const FactArguments& args);
109 void FuncSetInterrupt(const FactArguments& args);
110 void FuncTriggerInterrupt(const FactArguments& args);
111 //v8::Handle<v8::Value> FuncOpen(const FactArguments& args);
112 void FuncSubscription(const FactArguments& args);
113 void FuncGetData(const FactArguments &args);
114 void FuncClose(const FactArguments &args);
115 void FuncQuery(const FactArguments &args);
116 void FuncDatabase(const FactArguments &args);
117 void FuncDbQuery(const FactArguments &args);
118 void FuncDbClose(const FactArguments &args);
119 void OnChangeSet(v8::Local<v8::Name>, v8::Local<v8::Value>, const v8::PropertyCallbackInfo<v8::Value> &);
120
121 static v8::Handle<v8::Value> Constructor(const FactArguments &args);
122
123 static void ConstructorMail(const FactArguments &args);
124 static void ConstructorCurl(const FactArguments &args);
125
126#ifdef HAVE_NOVA
127 static double GetDataMember(const FactArguments &args, const char *name);
128
129 static void CalcDist(const FactArguments &args, const bool);
130
131 static void LocalToString(const FactArguments &args);
132 static void SkyToString(const FactArguments &args);
133 static void MoonToString(const FactArguments &args);
134 static void LocalDist(const FactArguments &args);
135 static void SkyDist(const FactArguments &args);
136 static void MoonDisk(const FactArguments &args);
137 static void LocalToSky(const FactArguments &args);
138 static void SkyToLocal(const FactArguments &args);
139 static void MoonToLocal(const FactArguments &args);
140 static void ConstructorMoon(const FactArguments &args);
141 static void ConstructorSky(const FactArguments &args);
142 static void ConstructorLocal(const FactArguments &args);
143 static void MoonHorizon(const FactArguments &args);
144 static void SunHorizon(const FactArguments &args);
145#endif
146
147 static void WrapInclude(const FactArguments &args) { if (This) This->FuncInclude(args); }
148 static void WrapFile(const FactArguments &args) { if (This) This->FuncFile(args); }
149 static void WrapSendMail(const FactArguments &args) { if (This) This->FuncSendMail(args); }
150 static void WrapSendCurl(const FactArguments &args) { if (This) This->FuncSendCurl(args); }
151 static void WrapLog(const FactArguments &args) { if (This) This->FuncLog(args); }
152 static void WrapAlarm(const FactArguments &args) { if (This) This->FuncAlarm(args); }
153 static void WrapOut(const FactArguments &args) { if (This) This->FuncOut(args); }
154 static void WrapWarn(const FactArguments &args) { if (This) This->FuncWarn(args); }
155 static void WrapWait(const FactArguments &args) { if (This) This->FuncWait(args); }
156 static void WrapSend(const FactArguments &args) { if (This) This->FuncSend(args); }
157 static void WrapSleep(const FactArguments &args) { if (This) This->FuncSleep(args); }
158 static void WrapTimeout(const FactArguments &args) { if (This) This->FuncTimeout(args); }
159 static void WrapThread(const FactArguments &args) { if (This) This->FuncThread(args); }
160 static void WrapKill(const FactArguments &args) { if (This) This->FuncKill(args); }
161 static void WrapExit(const FactArguments &args) { if (This) This->FuncExit(args); }
162 static void WrapState(const FactArguments &args) { if (This) This->FuncState(args); }
163 static void WrapNewState(const FactArguments &args) { if (This) This->FuncNewState(args); }
164 static void WrapSetState(const FactArguments &args) { if (This) This->FuncSetState(args); }
165 static void WrapGetState(const FactArguments &args) { if (This) This->FuncGetState(args); }
166 static void WrapGetStates(const FactArguments &args){ if (This) This->FuncGetStates(args);}
167 static void WrapGetDescription(const FactArguments &args){ if (This) This->FuncGetDescription(args); }
168 static void WrapGetServices(const FactArguments &args){ if (This) This->FuncGetServices(args); }
169 static void WrapSetInterrupt(const FactArguments &args){ if (This)This->FuncSetInterrupt(args); }
170 static void WrapTriggerInterrupt(const FactArguments &args){ if (This) This->FuncTriggerInterrupt(args); }
171 //static v8::Handle<v8::Value> WrapOpen(const FactArguments &args) { if (This) return This->FuncOpen(args); else return Undefined(v8::Isolate::GetCurrent()); }
172 static void WrapSubscription(const FactArguments &args){ if (This) This->FuncSubscription(args); }
173 static void WrapGetData(const FactArguments &args) { if (This) This->FuncGetData(args); }
174 static void WrapClose(const FactArguments &args) { if (This) This->FuncClose(args); }
175 static void WrapQuery(const FactArguments &args) { if (This) This->FuncQuery(args); }
176 static void WrapDatabase(const FactArguments &args) { if (This) This->FuncDatabase(args); }
177 static void WrapDbQuery(const FactArguments &args) { if (This) This->FuncDbQuery(args); }
178 static void WrapDbClose(const FactArguments &args) { if (This) This->FuncDbClose(args); }
179 static void WrapOnChangeSet(v8::Local<v8::Name> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info)
180 {
181 if (This) This->OnChangeSet(name, value, info);
182 }
183
184 static void OnChangeGet(v8::Local<v8::Name> /*property*/, const v8::PropertyCallbackInfo<v8::Value> &/*info*/)
185 {
186 //return Undefined(v8::Isolate::GetCurrent());
187 }
188
189 static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
190 v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
191#endif
192
193 v8::Handle<v8::Value> HandleInterruptImp(std::string, uint64_t);
194
195public:
196 InterpreterV8();
197 virtual ~InterpreterV8()
198 {
199 This = 0;
200
201#ifdef HAVE_V8
202 //v8::Locker locker;
203 v8::V8::Dispose();
204#endif
205 }
206
207 std::vector<std::string> JsGetCommandList(const char *, int) const;
208
209 virtual void JsLoad(const std::string & = "");
210 virtual void JsStart(const std::string &) { }
211 virtual void JsEnd(const std::string & = "");
212 virtual void JsPrint(const std::string & = "") { }
213 virtual void JsAlarm(const std::string & = "") { }
214 virtual void JsOut(const std::string &) { }
215 virtual void JsWarn(const std::string &) { }
216 virtual void JsResult(const std::string &) { }
217 virtual void JsException(const std::string &) { }
218 virtual bool JsSend(const std::string &) { return true; }
219 //virtual void JsSleep(uint32_t) { }
220 //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
221 virtual State JsState(const std::string &) { return State(); };
222 virtual void *JsSubscribe(const std::string &) { return 0; };
223 virtual bool JsUnsubscribe(const std::string &) { return false; };
224
225 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
226 virtual bool JsSetState(int) { return false; }
227 virtual bool JsHasState(int) const { return false; }
228 virtual bool JsHasState(const std::string &) const { return false; }
229 virtual int JsGetState(const std::string &) const { return -2; }
230 virtual State JsGetCurrentState() const { return State(); }
231 virtual std::vector<State> JsGetStates(const std::string &) { return std::vector<State>(); }
232 virtual std::set<Service> JsGetServices() { return std::set<Service>(); }
233 virtual std::vector<Description> JsGetDescription(const std::string &) { return std::vector<Description>(); }
234
235 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
236 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
237
238 int JsHandleInterrupt(const EventImp &);
239 void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
240 void JsHandleState(const std::string &, const State &);
241
242 void AddFormatToGlobal();
243
244 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
245 static void JsStop();
246};
247
248#ifndef HAVE_V8
249inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
250inline void InterpreterV8::JsStop() { }
251#endif
252
253#endif
Note: See TracBrowser for help on using the repository browser.