?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  3. // Copyright 2008, Nigel Atkinson <suprapilot+LuaCode@gmail.com>
  4. // SPDX-License-Identifier: MIT
  5.  
  6. #pragma once
  7.  
  8. #include <exception>
  9. #include <string>
  10.  
  11. namespace luabridge {
  12.  
  13. class LuaException : public std::exception
  14. {
  15. private:
  16.     lua_State* m_L;
  17.     std::string m_what;
  18.  
  19. public:
  20.     //----------------------------------------------------------------------------
  21.     /**
  22.         Construct a LuaException after a lua_pcall().
  23.     */
  24.     LuaException(lua_State* L, int /*code*/) : m_L(L) { whatFromStack(); }
  25.  
  26.     //----------------------------------------------------------------------------
  27.  
  28.     LuaException(lua_State* L, char const*, char const*, long) : m_L(L) { whatFromStack(); }
  29.  
  30.     //----------------------------------------------------------------------------
  31.  
  32.     ~LuaException() throw() {}
  33.  
  34.     //----------------------------------------------------------------------------
  35.  
  36.     char const* what() const throw() { return m_what.c_str(); }
  37.  
  38.     //============================================================================
  39.     /**
  40.         Throw an exception.
  41.  
  42.         This centralizes all the exceptions thrown, so that we can set
  43.         breakpoints before the stack is unwound, or otherwise customize the
  44.         behavior.
  45.     */
  46.     template<class Exception>
  47.     static void Throw(Exception e)
  48.     {
  49.         throw e;
  50.     }
  51.  
  52.     //----------------------------------------------------------------------------
  53.     /**
  54.         Wrapper for lua_pcall that throws.
  55.     */
  56.     static void pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0)
  57.     {
  58.         int code = lua_pcall(L, nargs, nresults, msgh);
  59.  
  60.         if (code != LUABRIDGE_LUA_OK)
  61.             Throw(LuaException(L, code));
  62.     }
  63.  
  64.     //----------------------------------------------------------------------------
  65.     /**
  66.         Initializes error handling. Subsequent Lua errors are translated to C++ exceptions.
  67.     */
  68.     static void enableExceptions(lua_State* L) { lua_atpanic(L, throwAtPanic); }
  69.  
  70.     /** Retrieve the lua_State associated with the exception.
  71.  
  72.       @returns A Lua state.
  73.     */
  74.     lua_State* state() const { return m_L; }
  75.  
  76. protected:
  77.     void whatFromStack()
  78.     {
  79.         if (lua_gettop(m_L) > 0)
  80.         {
  81.             char const* s = lua_tostring(m_L, -1);
  82.             m_what = s ? s : "";
  83.         }
  84.         else
  85.         {
  86.             // stack is empty
  87.             m_what = "missing error";
  88.         }
  89.     }
  90.  
  91. private:
  92.     static int throwAtPanic(lua_State* L) { throw LuaException(L, -1); }
  93. };
  94.  
  95. //----------------------------------------------------------------------------
  96. /**
  97.     Initializes error handling. Subsequent Lua errors are translated to C++ exceptions.
  98. */
  99. static void enableExceptions(lua_State* L)
  100. {
  101.     LuaException::enableExceptions(L);
  102. }
  103.  
  104. } // namespace luabridge
  105.