#ifndef FACT_InterpreterV8 #define FACT_InterpreterV8 #include #include #include #include #include #ifdef HAVE_V8 #include #endif #include "State.h" #include "Description.h" #include "EventImp.h" class Database; #ifdef HAVE_NOVA struct ln_rst_time; #endif class InterpreterV8 { static InterpreterV8 *This; // The main thread id, needed to be able to terminate // the thread forcefully from 'the outside' int fThreadId; std::set fThreadIds; // 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; // List of all threads std::vector fThreads; // List of all states already set std::vector> fStates; static v8::Handle fTemplateLocal; static v8::Handle fTemplateSky; static v8::Handle fTemplateEvent; #ifdef HAVE_SQL std::list fDatabases; #endif #ifdef HAVE_V8 bool HandleException(v8::TryCatch &try_catch, const char *where); bool ExecuteFile(const std::string &name, bool main=false); v8::Handle ExecuteCode(const std::string &code, const std::string &file="internal", bool main=false); v8::Handle ExecuteInternal(const std::string &code); void Thread(int &id, v8::Persistent func, uint32_t ms); v8::Handle FuncWait(const v8::Arguments& args); v8::Handle FuncSend(const v8::Arguments& args); v8::Handle FuncSleep(const v8::Arguments& args); v8::Handle FuncThread(const v8::Arguments& args); v8::Handle FuncKill(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 FuncFile(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 FuncGetState(const v8::Arguments& args); v8::Handle FuncNewState(const v8::Arguments& args); //v8::Handle FuncOpen(const v8::Arguments& args); v8::Handle FuncSubscription(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 v8::Handle Constructor(const v8::Arguments &args); #ifdef HAVE_NOVA 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 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 MoonHorizon(const v8::Arguments &args); static v8::Handle SunHorizon(const v8::Arguments &args); static v8::Handle ConstructRiseSet(const v8::Handle, const ln_rst_time &, const bool &); #endif static v8::Handle WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); } static v8::Handle WrapFile(const v8::Arguments &args) { if (This) return This->FuncFile(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 WrapThread(const v8::Arguments &args) { if (This) return This->FuncThread(args); else return v8::Undefined(); } static v8::Handle WrapKill(const v8::Arguments &args) { if (This) return This->FuncKill(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 WrapGetState(const v8::Arguments &args) { if (This) return This->FuncGetState(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 WrapSubscription(const v8::Arguments &args){ if (This) return This->FuncSubscription(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(); 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 State JsGetCurrentState() const { return State(); } 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 &); 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