1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6 Copyright 2008, Nigel Atkinson <suprapilot+LuaCode@gmail.com>
8 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 //==============================================================================
30 class LuaException : public std::exception
37 //----------------------------------------------------------------------------
39 Construct a LuaException after a lua_pcall().
41 LuaException (lua_State* L, int /*code*/)
47 //----------------------------------------------------------------------------
49 LuaException (lua_State *L,
58 //----------------------------------------------------------------------------
60 ~LuaException() throw ()
64 //----------------------------------------------------------------------------
66 char const* what() const throw ()
68 return m_what.c_str();
71 //============================================================================
75 This centralizes all the exceptions thrown, so that we can set
76 breakpoints before the stack is unwound, or otherwise customize the
79 template <class Exception>
80 static void Throw (Exception e)
85 //----------------------------------------------------------------------------
87 Wrapper for lua_pcall that throws.
89 static void pcall (lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0)
91 int code = lua_pcall (L, nargs, nresults, msgh);
93 if (code != LUABRIDGE_LUA_OK)
94 Throw (LuaException (L, code));
97 //----------------------------------------------------------------------------
100 void whatFromStack ()
102 if (lua_gettop (m_L) > 0)
104 char const* s = lua_tostring (m_L, -1);
110 m_what = "missing error";