#ifndef FACT_InterpreterV8 #define FACT_InterpreterV8 #include #include #include #include #include #ifdef HAVE_V8 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" #include #pragma GCC diagnostic pop #endif #include "State.h" #include "Service.h" #include "Description.h" #include "EventImp.h" class Database; namespace v8 { class Platform; }; #ifdef HAVE_NOVA struct ln_rst_time; #endif class InterpreterV8 { template using PersistentMap = std::map>; static InterpreterV8 *This; static std::unique_ptr fPlatform; // The main thread id, needed to be able to terminate // the thread forcefully from 'the outside' v8::Isolate *fMainThread; // A loookup table which allows to indentify // the JavaScript object corrsponding to the // service name (for checking of an .onchange // subscription exists for that object) PersistentMap fReverseMap; // Lookup table for the callbacks in cases of state changes PersistentMap fStateCallbacks; // List of all threads std::vector fThreads; // List of all states already set std::vector> fStates; // Interrupt handler v8::UniquePersistent fInterruptCallback; static v8::Handle fTemplateLocal; static v8::Handle fTemplateSky; static v8::Handle fTemplateEvent; static v8::Handle fTemplateDescription; #ifdef HAVE_SQL std::list fDatabases; #endif std::string fIncludePath; #ifdef HAVE_V8 bool HandleException(v8::TryCatch &try_catch, const char *where); void ExecuteConsole(); v8::Handle ExecuteCode(const std::string &code, const std::string &file="internal"); v8::Handle ExecuteInternal(const std::string &code); void Thread(v8::UniquePersistent &_this, v8::UniquePersistent &func, uint32_t ms); std::vector ValueToArray(const v8::Handle &val, bool only=true); typedef v8::FunctionCallbackInfo FactArguments; static void TerminateExecution(v8::Isolate*); static void GetterCallback(v8::Local name, const v8::PropertyCallbackInfo &info); static void SetProperty(v8::Handle &obj, const char *name, const v8::Local &val); void FuncWait(const FactArguments& args); void FuncSend(const FactArguments& args); void FuncSleep(const FactArguments& args); void FuncTimeout(const FactArguments& args); void FuncThread(const FactArguments& args); void FuncKill(const FactArguments& args); void FuncLog(const FactArguments& args); void FuncAlarm(const FactArguments& args); void FuncOut(const FactArguments& args); void FuncWarn(const FactArguments& args); void FuncFile(const FactArguments& args); void FuncSendMail(const FactArguments& args); void FuncSendCurl(const FactArguments& args); void FuncInclude(const FactArguments& args); void FuncExit(const FactArguments& args); void FuncState(const FactArguments& args); void FuncSetState(const FactArguments& args); void FuncGetState(const FactArguments& args); void FuncGetStates(const FactArguments& args); void FuncGetDescription(const FactArguments& args); void FuncGetServices(const FactArguments& args); void FuncNewState(const FactArguments& args); void FuncSetInterrupt(const FactArguments& args); void FuncTriggerInterrupt(const FactArguments& args); //v8::Handle FuncOpen(const FactArguments& args); void FuncSubscription(const FactArguments& args); void FuncGetData(const FactArguments &args); void FuncClose(const FactArguments &args); void FuncQuery(const FactArguments &args); void FuncDatabase(const FactArguments &args); void FuncDbQuery(const FactArguments &args); void FuncDbClose(const FactArguments &args); void OnChangeSet(v8::Local, v8::Local, const v8::PropertyCallbackInfo &); static v8::Handle Constructor(const FactArguments &args); static void ConstructorMail(const FactArguments &args); static void ConstructorCurl(const FactArguments &args); #ifdef HAVE_NOVA static double GetDataMember(const FactArguments &args, const char *name); static void CalcDist(const FactArguments &args, const bool); static void LocalToString(const FactArguments &args); static void SkyToString(const FactArguments &args); static void MoonToString(const FactArguments &args); static void LocalDist(const FactArguments &args); static void SkyDist(const FactArguments &args); static void MoonDisk(const FactArguments &args); static void LocalToSky(const FactArguments &args); static void SkyToLocal(const FactArguments &args); static void MoonToLocal(const FactArguments &args); static void ConstructorMoon(const FactArguments &args); static void ConstructorSky(const FactArguments &args); static void ConstructorLocal(const FactArguments &args); static void MoonHorizon(const FactArguments &args); static void SunHorizon(const FactArguments &args); #endif static void WrapInclude(const FactArguments &args) { if (This) This->FuncInclude(args); } static void WrapFile(const FactArguments &args) { if (This) This->FuncFile(args); } static void WrapSendMail(const FactArguments &args) { if (This) This->FuncSendMail(args); } static void WrapSendCurl(const FactArguments &args) { if (This) This->FuncSendCurl(args); } static void WrapLog(const FactArguments &args) { if (This) This->FuncLog(args); } static void WrapAlarm(const FactArguments &args) { if (This) This->FuncAlarm(args); } static void WrapOut(const FactArguments &args) { if (This) This->FuncOut(args); } static void WrapWarn(const FactArguments &args) { if (This) This->FuncWarn(args); } static void WrapWait(const FactArguments &args) { if (This) This->FuncWait(args); } static void WrapSend(const FactArguments &args) { if (This) This->FuncSend(args); } static void WrapSleep(const FactArguments &args) { if (This) This->FuncSleep(args); } static void WrapTimeout(const FactArguments &args) { if (This) This->FuncTimeout(args); } static void WrapThread(const FactArguments &args) { if (This) This->FuncThread(args); } static void WrapKill(const FactArguments &args) { if (This) This->FuncKill(args); } static void WrapExit(const FactArguments &args) { if (This) This->FuncExit(args); } static void WrapState(const FactArguments &args) { if (This) This->FuncState(args); } static void WrapNewState(const FactArguments &args) { if (This) This->FuncNewState(args); } static void WrapSetState(const FactArguments &args) { if (This) This->FuncSetState(args); } static void WrapGetState(const FactArguments &args) { if (This) This->FuncGetState(args); } static void WrapGetStates(const FactArguments &args){ if (This) This->FuncGetStates(args);} static void WrapGetDescription(const FactArguments &args){ if (This) This->FuncGetDescription(args); } static void WrapGetServices(const FactArguments &args){ if (This) This->FuncGetServices(args); } static void WrapSetInterrupt(const FactArguments &args){ if (This)This->FuncSetInterrupt(args); } static void WrapTriggerInterrupt(const FactArguments &args){ if (This) This->FuncTriggerInterrupt(args); } //static v8::Handle WrapOpen(const FactArguments &args) { if (This) return This->FuncOpen(args); else return Undefined(v8::Isolate::GetCurrent()); } static void WrapSubscription(const FactArguments &args){ if (This) This->FuncSubscription(args); } static void WrapGetData(const FactArguments &args) { if (This) This->FuncGetData(args); } static void WrapClose(const FactArguments &args) { if (This) This->FuncClose(args); } static void WrapQuery(const FactArguments &args) { if (This) This->FuncQuery(args); } static void WrapDatabase(const FactArguments &args) { if (This) This->FuncDatabase(args); } static void WrapDbQuery(const FactArguments &args) { if (This) This->FuncDbQuery(args); } static void WrapDbClose(const FactArguments &args) { if (This) This->FuncDbClose(args); } static void WrapOnChangeSet(v8::Local name, v8::Local value, const v8::PropertyCallbackInfo &info) { if (This) This->OnChangeSet(name, value, info); } static void OnChangeGet(v8::Local /*property*/, const v8::PropertyCallbackInfo &/*info*/) { //return Undefined(v8::Isolate::GetCurrent()); } static v8::Handle Convert(char type, const char* &ptr); v8::Handle ConvertEvent(const EventImp *evt, uint64_t, const char *str); #endif v8::Handle HandleInterruptImp(std::string, uint64_t); public: InterpreterV8(); virtual ~InterpreterV8() { This = 0; #ifdef HAVE_V8 //v8::Locker locker; v8::V8::Dispose(); #endif } std::vector JsGetCommandList(const char *, int) const; 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 JsWarn(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 State JsGetCurrentState() const { return State(); } virtual std::vector JsGetStates(const std::string &) { return std::vector(); } virtual std::set JsGetServices() { return std::set(); } virtual std::vector JsGetDescription(const std::string &) { return std::vector(); } virtual std::vector JsDescription(const std::string &) { return std::vector(); }; virtual std::pair JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); }; int JsHandleInterrupt(const EventImp &); void JsHandleEvent(const EventImp &, uint64_t, const std::string &); void JsHandleState(const std::string &, const State &); void AddFormatToGlobal(); 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