#ifndef FACT_InterpreterV8 #define FACT_InterpreterV8 #include #include #include #ifdef HAVE_V8 #include #endif #include "State.h" #include "Description.h" #include "EventImp.h" class Database; class InterpreterV8 { static InterpreterV8 *This; // The main thread id, needed to be able to terminate // the thread forcefully from 'the outside' int fThreadId; // A loookup table which allows to indentify the // the JavaScript object corrsponding to the // service name (for checking of an .onchange // subscription exists for that object) std::map> fReverseMap; // Lookup table for the callbacks in cases of state changes std::map> fStateCallbacks; #ifdef HAVE_SQL std::list fDatabases; #endif #ifdef HAVE_V8 bool ReportException(v8::TryCatch* try_catch); bool ExecuteStringNT(const v8::Handle &code, const v8::Handle &file); bool ExecuteCode(const v8::Handle &code, const v8::Handle &file); bool ExecuteCode(const std::string &code, const std::string &file=""); bool ExecuteFile(const std::string &name); v8::Handle FuncWait(const v8::Arguments& args); v8::Handle FuncSend(const v8::Arguments& args); v8::Handle FuncSleep(const v8::Arguments& args); v8::Handle FuncPrint(const v8::Arguments& args); v8::Handle FuncAlarm(const v8::Arguments& args); v8::Handle FuncOut(const v8::Arguments& args); v8::Handle FuncInclude(const v8::Arguments& args); v8::Handle FuncExit(const v8::Arguments& args); v8::Handle FuncState(const v8::Arguments& args); v8::Handle FuncSetState(const v8::Arguments& args); v8::Handle FuncNewState(const v8::Arguments& args); //v8::Handle FuncOpen(const v8::Arguments& args); v8::Handle FuncSubscribe(const v8::Arguments& args); v8::Handle FuncGetData(const v8::Arguments &args); v8::Handle FuncClose(const v8::Arguments &args); v8::Handle FuncQuery(const v8::Arguments &args); v8::Handle FuncDatabase(const v8::Arguments &args); v8::Handle FuncDbQuery(const v8::Arguments &args); v8::Handle FuncDbClose(const v8::Arguments &args); v8::Handle OnChangeSet(v8::Local, v8::Local, const v8::AccessorInfo &); static double GetDataMember(const v8::Arguments &args, const char *name); static v8::Handle LocalToString(const v8::Arguments &args); static v8::Handle SkyToString(const v8::Arguments &args); static v8::Handle MoonToString(const v8::Arguments &args); static v8::Handle ConstructLocal(double zd, double az, v8::Handle time=v8::Handle()); static v8::Handle ConstructSky(double ra, double dec, v8::Handle time=v8::Handle(), bool ismoon=false); static v8::Handle LocalDist(const v8::Arguments &args); static v8::Handle MoonDisk(const v8::Arguments &args); static v8::Handle LocalToSky(const v8::Arguments &args); static v8::Handle SkyToLocal(const v8::Arguments &args); static v8::Handle MoonToLocal(const v8::Arguments &args); static v8::Handle ConstructorMoon(const v8::Arguments &args); static v8::Handle ConstructorSky(const v8::Arguments &args); static v8::Handle ConstructorLocal(const v8::Arguments &args); static v8::Handle FuncVersion(const v8::Arguments&); static v8::Handle WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); } static v8::Handle WrapPrint(const v8::Arguments &args) { if (This) return This->FuncPrint(args); else return v8::Undefined(); } static v8::Handle WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); } static v8::Handle WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); } static v8::Handle WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); } static v8::Handle WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); } static v8::Handle WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); } static v8::Handle WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); } static v8::Handle WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); } static v8::Handle WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); } static v8::Handle WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); } //static v8::Handle WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); } static v8::Handle WrapSubscribe(const v8::Arguments &args){ if (This) return This->FuncSubscribe(args);else return v8::Undefined(); } static v8::Handle WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); } static v8::Handle WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); } static v8::Handle WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); } static v8::Handle WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); } static v8::Handle WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); } static v8::Handle WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); } static v8::Handle WrapOnChangeSet(v8::Local prop, v8::Local value, const v8::AccessorInfo &info) { if (This) return This->OnChangeSet(prop, value, info); else return v8::Undefined(); } static v8::Handle OnChangeGet(v8::Local, const v8::AccessorInfo &) { return v8::Handle(); } static v8::Handle Convert(char type, const char* &ptr); v8::Handle ConvertEvent(const EventImp *evt, uint64_t, const char *str); #endif public: InterpreterV8() : fThreadId(-1) { This = this; } virtual ~InterpreterV8() { This = 0; #ifdef HAVE_V8 v8::Locker locker; v8::V8::Dispose(); #endif } virtual void JsLoad(const std::string &) { } virtual void JsStart(const std::string &) { } virtual void JsEnd(const std::string &) { } virtual void JsPrint(const std::string &) { } virtual void JsAlarm(const std::string & = "") { } virtual void JsOut(const std::string &) { } virtual void JsResult(const std::string &) { } virtual void JsException(const std::string &) { } virtual bool JsSend(const std::string &) { return true; } //virtual void JsSleep(uint32_t) { } //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; }; virtual State JsState(const std::string &) { return State(); }; virtual void *JsSubscribe(const std::string &) { return 0; }; virtual bool JsUnsubscribe(const std::string &) { return false; }; virtual bool JsNewState(int, const std::string&, const std::string&) { return false; } virtual bool JsSetState(int) { return false; } virtual bool JsHasState(int) const { return false; } virtual bool JsHasState(const std::string &) const { return false; } virtual int JsGetState(const std::string &) const { return -2; } virtual std::vector JsDescription(const std::string &) { return std::vector(); }; virtual std::pair JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); }; void JsHandleEvent(const EventImp &, uint64_t, const std::string &); void JsHandleState(const std::string &, const State &); bool JsRun(const std::string &, const std::map & = std::map()); static void JsStop(); }; #ifndef HAVE_V8 inline bool InterpreterV8::JsRun(const std::string &, const std::map &) { return false; } inline void InterpreterV8::JsStop() { } #endif #endif