#pragma once #include "../config.hpp" #ifdef SCRIPT_ENGINE_SM #include "Base.h" #include namespace se { class Object; class Class; class Value; extern Class* __jsb_CCPrivateData_class; class AutoHandleScope { public: AutoHandleScope(); ~AutoHandleScope(); }; class ScriptEngine final { private: ScriptEngine(); ~ScriptEngine(); public: static ScriptEngine* getInstance(); static void destroyInstance(); Object* getGlobalObject(); typedef bool (*RegisterCallback)(Object*); void addRegisterCallback(RegisterCallback cb); bool start(); bool init(); void cleanup(); /** * @brief Adds a hook function before initializing script engine. * @param[in] hook A hook function to be invoked before initializing script engine. * @note Multiple hook functions could be added, they will be invoked by the order of adding. */ void addBeforeInitHook(const std::function& hook); /** * @brief Adds a hook function after initializing script engine. * @param[in] hook A hook function to be invoked before initializing script engine. * @note Multiple hook functions could be added, they will be invoked by the order of adding. */ void addAfterInitHook(const std::function& hook); void addBeforeCleanupHook(const std::function& hook); void addAfterCleanupHook(const std::function& hook); bool executeScriptBuffer(const char *string, Value *data = nullptr, const char *fileName = nullptr); bool executeScriptBuffer(const char *string, size_t length, Value *data = nullptr, const char *fileName = nullptr); JSContext* _getContext() { return _cx; } bool isInGC(); void _setInGC(bool isInGC); void gc() { JS_GC( _cx ); } bool isValid() { return _isValid; } void clearException(); const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } void _retainScriptObject(void* owner, void* target); void _releaseScriptObject(void* owner, void* target); enum class NodeEventType { ENTER, EXIT, ENTER_TRANSITION_DID_FINISH, EXIT_TRANSITION_DID_START, CLEANUP }; bool _onReceiveNodeEvent(void* node, NodeEventType type); using NodeEventListener = bool(*)(void*, NodeEventType); bool _setNodeEventListener(NodeEventListener listener); private: static void myWeakPointerCompartmentCallback(JSContext* cx, JSCompartment* comp, void* data); static void myWeakPointerZoneGroupCallback(JSContext* cx, void* data); JSContext* _cx; JSCompartment* _oldCompartment; Object* _globalObj; bool _isInGC; bool _isValid; bool _isInCleanup; NodeEventListener _nodeEventListener; std::vector _registerCallbackArray; std::chrono::steady_clock::time_point _startTime; std::vector> _beforeInitHookArray; std::vector> _afterInitHookArray; std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; }; } // namespace se { #endif // SCRIPT_ENGINE_SM