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

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