14b2c3eb9SBrooks Davis // Copyright 2011 Google Inc.
24b2c3eb9SBrooks Davis // All rights reserved.
34b2c3eb9SBrooks Davis //
44b2c3eb9SBrooks Davis // Redistribution and use in source and binary forms, with or without
54b2c3eb9SBrooks Davis // modification, are permitted provided that the following conditions are
64b2c3eb9SBrooks Davis // met:
74b2c3eb9SBrooks Davis //
84b2c3eb9SBrooks Davis // * Redistributions of source code must retain the above copyright
94b2c3eb9SBrooks Davis // notice, this list of conditions and the following disclaimer.
104b2c3eb9SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
114b2c3eb9SBrooks Davis // notice, this list of conditions and the following disclaimer in the
124b2c3eb9SBrooks Davis // documentation and/or other materials provided with the distribution.
134b2c3eb9SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
144b2c3eb9SBrooks Davis // may be used to endorse or promote products derived from this software
154b2c3eb9SBrooks Davis // without specific prior written permission.
164b2c3eb9SBrooks Davis //
174b2c3eb9SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184b2c3eb9SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194b2c3eb9SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204b2c3eb9SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214b2c3eb9SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224b2c3eb9SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234b2c3eb9SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244b2c3eb9SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254b2c3eb9SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264b2c3eb9SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274b2c3eb9SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284b2c3eb9SBrooks Davis
294b2c3eb9SBrooks Davis #include <cassert>
304b2c3eb9SBrooks Davis
314b2c3eb9SBrooks Davis #include <lua.hpp>
324b2c3eb9SBrooks Davis
334b2c3eb9SBrooks Davis #include "c_gate.hpp"
344b2c3eb9SBrooks Davis #include "exceptions.hpp"
354b2c3eb9SBrooks Davis #include "state.ipp"
364b2c3eb9SBrooks Davis
374b2c3eb9SBrooks Davis
384b2c3eb9SBrooks Davis /// Constructs a new error with a plain-text message.
394b2c3eb9SBrooks Davis ///
404b2c3eb9SBrooks Davis /// \param message The plain-text error message.
error(const std::string & message)414b2c3eb9SBrooks Davis lutok::error::error(const std::string& message) :
424b2c3eb9SBrooks Davis std::runtime_error(message)
434b2c3eb9SBrooks Davis {
444b2c3eb9SBrooks Davis }
454b2c3eb9SBrooks Davis
464b2c3eb9SBrooks Davis
474b2c3eb9SBrooks Davis /// Destructor for the error.
~error(void)484b2c3eb9SBrooks Davis lutok::error::~error(void) throw()
494b2c3eb9SBrooks Davis {
504b2c3eb9SBrooks Davis }
514b2c3eb9SBrooks Davis
524b2c3eb9SBrooks Davis
534b2c3eb9SBrooks Davis /// Constructs a new error.
544b2c3eb9SBrooks Davis ///
554b2c3eb9SBrooks Davis /// \param api_function_ The name of the API function that caused the error.
564b2c3eb9SBrooks Davis /// \param message The plain-text error message provided by Lua.
api_error(const std::string & api_function_,const std::string & message)574b2c3eb9SBrooks Davis lutok::api_error::api_error(const std::string& api_function_,
584b2c3eb9SBrooks Davis const std::string& message) :
594b2c3eb9SBrooks Davis error(message),
604b2c3eb9SBrooks Davis _api_function(api_function_)
614b2c3eb9SBrooks Davis {
624b2c3eb9SBrooks Davis }
634b2c3eb9SBrooks Davis
644b2c3eb9SBrooks Davis
654b2c3eb9SBrooks Davis /// Destructor for the error.
~api_error(void)664b2c3eb9SBrooks Davis lutok::api_error::~api_error(void) throw()
674b2c3eb9SBrooks Davis {
684b2c3eb9SBrooks Davis }
694b2c3eb9SBrooks Davis
704b2c3eb9SBrooks Davis
714b2c3eb9SBrooks Davis /// Constructs a new api_error with the message on the top of the Lua stack.
724b2c3eb9SBrooks Davis ///
734b2c3eb9SBrooks Davis /// \pre There is an error message on the top of the stack.
744b2c3eb9SBrooks Davis /// \post The error message is popped from the stack.
754b2c3eb9SBrooks Davis ///
764b2c3eb9SBrooks Davis /// \param state_ The Lua state.
774b2c3eb9SBrooks Davis /// \param api_function_ The name of the Lua API function that caused the error.
784b2c3eb9SBrooks Davis ///
794b2c3eb9SBrooks Davis /// \return A new api_error with the popped message.
804b2c3eb9SBrooks Davis lutok::api_error
from_stack(state & state_,const std::string & api_function_)814b2c3eb9SBrooks Davis lutok::api_error::from_stack(state& state_, const std::string& api_function_)
824b2c3eb9SBrooks Davis {
834b2c3eb9SBrooks Davis lua_State* raw_state = lutok::state_c_gate(state_).c_state();
844b2c3eb9SBrooks Davis
854b2c3eb9SBrooks Davis assert(lua_isstring(raw_state, -1));
864b2c3eb9SBrooks Davis const std::string message = lua_tostring(raw_state, -1);
874b2c3eb9SBrooks Davis lua_pop(raw_state, 1);
884b2c3eb9SBrooks Davis return lutok::api_error(api_function_, message);
894b2c3eb9SBrooks Davis }
904b2c3eb9SBrooks Davis
914b2c3eb9SBrooks Davis
924b2c3eb9SBrooks Davis /// Gets the name of the Lua API function that caused this error.
934b2c3eb9SBrooks Davis ///
944b2c3eb9SBrooks Davis /// \return The name of the function.
954b2c3eb9SBrooks Davis const std::string&
api_function(void) const964b2c3eb9SBrooks Davis lutok::api_error::api_function(void) const
974b2c3eb9SBrooks Davis {
984b2c3eb9SBrooks Davis return _api_function;
994b2c3eb9SBrooks Davis }
1004b2c3eb9SBrooks Davis
1014b2c3eb9SBrooks Davis
1024b2c3eb9SBrooks Davis /// Constructs a new error.
1034b2c3eb9SBrooks Davis ///
1044b2c3eb9SBrooks Davis /// \param filename_ The file that count not be found.
file_not_found_error(const std::string & filename_)1054b2c3eb9SBrooks Davis lutok::file_not_found_error::file_not_found_error(
1064b2c3eb9SBrooks Davis const std::string& filename_) :
1074b2c3eb9SBrooks Davis error("File '" + filename_ + "' not found"),
1084b2c3eb9SBrooks Davis _filename(filename_)
1094b2c3eb9SBrooks Davis {
1104b2c3eb9SBrooks Davis }
1114b2c3eb9SBrooks Davis
1124b2c3eb9SBrooks Davis
1134b2c3eb9SBrooks Davis /// Destructor for the error.
~file_not_found_error(void)1144b2c3eb9SBrooks Davis lutok::file_not_found_error::~file_not_found_error(void) throw()
1154b2c3eb9SBrooks Davis {
1164b2c3eb9SBrooks Davis }
1174b2c3eb9SBrooks Davis
1184b2c3eb9SBrooks Davis
1194b2c3eb9SBrooks Davis /// Gets the name of the file that could not be found.
1204b2c3eb9SBrooks Davis ///
1214b2c3eb9SBrooks Davis /// \return The name of the file.
1224b2c3eb9SBrooks Davis const std::string&
filename(void) const1234b2c3eb9SBrooks Davis lutok::file_not_found_error::filename(void) const
1244b2c3eb9SBrooks Davis {
1254b2c3eb9SBrooks Davis return _filename;
1264b2c3eb9SBrooks Davis }
127