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

Last change on this file since 14622 was 14607, checked in by tbretz, 12 years ago
Some improvements to internal exception throwing; some improvements to HandleScopes; fixed the problem that with included file JsSetStat(3) was called several times.
File size: 9.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 "Description.h"
16#include "EventImp.h"
17
18class Database;
19
20class InterpreterV8
21{
22 static InterpreterV8 *This;
23
24 // The main thread id, needed to be able to terminate
25 // the thread forcefully from 'the outside'
26 int fThreadId;
27 std::set<int> fThreadIds;
28
29 v8::Persistent<v8::Context> fGlobalContext;
30
31 // A loookup table which allows to indentify the
32 // the JavaScript object corrsponding to the
33 // service name (for checking of an .onchange
34 // subscription exists for that object)
35 std::map<std::string, v8::Persistent<v8::Object>> fReverseMap;
36
37 // Lookup table for the callbacks in cases of state changes
38 std::map<std::string, v8::Persistent<v8::Value>> fStateCallbacks;
39
40 // List of all timeout threads
41 std::vector<std::thread> fTimeout;
42
43#ifdef HAVE_SQL
44 std::list<Database*> fDatabases;
45#endif
46
47#ifdef HAVE_V8
48 void Terminate();
49
50 bool ReportException(v8::TryCatch* try_catch);
51 bool ExecuteFile(const std::string &name, bool main=false);
52 v8::Handle<v8::Value> ExecuteCode(const std::string &code, const std::string &file="internal", bool main=false);
53 v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
54
55 void ThreadTimeout(v8::Persistent<v8::Function> func, uint32_t ms);
56
57 v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
58 v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
59 v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
60 v8::Handle<v8::Value> FuncTimeout(const v8::Arguments& args);
61 v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args);
62 v8::Handle<v8::Value> FuncAlarm(const v8::Arguments& args);
63 v8::Handle<v8::Value> FuncOut(const v8::Arguments& args);
64 v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
65 v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
66 v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
67 v8::Handle<v8::Value> FuncSetState(const v8::Arguments& args);
68 v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args);
69 //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
70 v8::Handle<v8::Value> FuncSubscribe(const v8::Arguments& args);
71 v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
72 v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
73 v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
74 v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
75 v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
76 v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
77 v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
78
79 static double GetDataMember(const v8::Arguments &args, const char *name);
80
81 static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
82 static v8::Handle<v8::Value> SkyToString(const v8::Arguments &args);
83 static v8::Handle<v8::Value> MoonToString(const v8::Arguments &args);
84 static v8::Handle<v8::Value> ConstructLocal(double zd, double az, v8::Handle<v8::Value> time=v8::Handle<v8::Value>());
85 static v8::Handle<v8::Value> ConstructSky(double ra, double dec, v8::Handle<v8::Value> time=v8::Handle<v8::Value>(), bool ismoon=false);
86 static v8::Handle<v8::Value> LocalDist(const v8::Arguments &args);
87 static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args);
88 static v8::Handle<v8::Value> LocalToSky(const v8::Arguments &args);
89 static v8::Handle<v8::Value> SkyToLocal(const v8::Arguments &args);
90 static v8::Handle<v8::Value> MoonToLocal(const v8::Arguments &args);
91 static v8::Handle<v8::Value> ConstructorMoon(const v8::Arguments &args);
92 static v8::Handle<v8::Value> ConstructorSky(const v8::Arguments &args);
93 static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
94
95 static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
96 static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
97 static v8::Handle<v8::Value> WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); }
98 static v8::Handle<v8::Value> WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); }
99 static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
100 static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
101 static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
102 static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
103 static v8::Handle<v8::Value> WrapTimeout(const v8::Arguments &args) { if (This) return This->FuncTimeout(args); else return v8::Undefined(); }
104 static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
105 static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
106 static v8::Handle<v8::Value> WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); }
107 static v8::Handle<v8::Value> WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); }
108 //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
109 static v8::Handle<v8::Value> WrapSubscribe(const v8::Arguments &args){ if (This) return This->FuncSubscribe(args);else return v8::Undefined(); }
110 static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
111 static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
112 static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
113 static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
114 static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
115 static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
116 static v8::Handle<v8::Value> WrapOnChangeSet(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info)
117 {
118 if (This) return This->OnChangeSet(prop, value, info); else return v8::Undefined();
119 }
120
121 static v8::Handle<v8::Value> OnChangeGet(v8::Local<v8::String>, const v8::AccessorInfo &)
122 {
123 return v8::Handle<v8::Value>();
124 }
125
126 static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
127 v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
128#endif
129
130public:
131 InterpreterV8() : fThreadId(-1)
132 {
133 This = this;
134 }
135 virtual ~InterpreterV8()
136 {
137 This = 0;
138
139#ifdef HAVE_V8
140 v8::Locker locker;
141 v8::V8::Dispose();
142#endif
143 }
144
145 virtual void JsLoad(const std::string &) { }
146 virtual void JsStart(const std::string &) { }
147 virtual void JsEnd(const std::string &) { }
148 virtual void JsPrint(const std::string &) { }
149 virtual void JsAlarm(const std::string & = "") { }
150 virtual void JsOut(const std::string &) { }
151 virtual void JsResult(const std::string &) { }
152 virtual void JsException(const std::string &) { }
153 virtual bool JsSend(const std::string &) { return true; }
154 //virtual void JsSleep(uint32_t) { }
155 //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
156 virtual State JsState(const std::string &) { return State(); };
157 virtual void *JsSubscribe(const std::string &) { return 0; };
158 virtual bool JsUnsubscribe(const std::string &) { return false; };
159
160 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
161 virtual bool JsSetState(int) { return false; }
162 virtual bool JsHasState(int) const { return false; }
163 virtual bool JsHasState(const std::string &) const { return false; }
164 virtual int JsGetState(const std::string &) const { return -2; }
165
166 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
167 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
168
169 void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
170 void JsHandleState(const std::string &, const State &);
171
172 void AddFormatToGlobal() const;
173
174 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
175 static void JsStop();
176};
177
178#ifndef HAVE_V8
179inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
180inline void InterpreterV8::JsStop() { }
181#endif
182
183#endif
Note: See TracBrowser for help on using the repository browser.