text: #pragma once
  
  // Warning: Not thread-safe, only for single VM
  
  class LuaFunction
  {
      lua_State *m_vm;
      void *m_ptr;
      int m_ref;
  
      // Global map for Lua functions references
      struct LuaFunctionData
      {
          int m_ref = 0;
          unsigned int m_refCount = 0U;
      };
      static std::unordered_map<void*, LuaFunctionData> ms_funcMap;
  
      static int RetrieveGlobalReference(lua_State *p_vm, void *p_ptr);
      static void RemoveGlobalReference(lua_State *p_vm, void *p_ptr, int p_ref);
  public:
      LuaFunction();
      LuaFunction(const LuaFunction &p_func);
      ~LuaFunction();
  
      LuaFunction& operator=(const LuaFunction& p_func);
      bool operator==(const LuaFunction& p_func) const;
  
      lua_State* GetVM() const;
      bool IsValid() const;
  protected:
      void Retrieve(lua_State *p_vm, void *p_ptr);
  
      int GetReference() const;
  
      friend class LuaArgReader;
      friend class LuaVM;
  };
  