Skip to content

Commit

Permalink
Get rid of the lua.hpp dependency from the exceptions module
Browse files Browse the repository at this point in the history
Make the api_error::from_stack method defined in the exceptions module
take a lutok::state instead of a lua_State.  This finally allows us to
completely drop the exposing of lua.hpp from our public headers (with
the exception of c_gate.hpp, but that's expected).

This change was r12 in Subversion.
  • Loading branch information
jmmv authored and Julio Merino committed Feb 26, 2012
1 parent 3884992 commit d1790df
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion debug.cpp
Expand Up @@ -69,7 +69,7 @@ lutok::debug::get_info(state& s, const std::string& what)
lua_State* raw_state = state_c_gate(s).c_state();

if (lua_getinfo(raw_state, what.c_str(), &_pimpl->lua_debug) == 0)
throw lutok::api_error::from_stack(raw_state, "lua_getinfo");
throw lutok::api_error::from_stack(s, "lua_getinfo");
}


Expand Down
15 changes: 9 additions & 6 deletions exceptions.cpp
Expand Up @@ -30,8 +30,9 @@

#include <lua.hpp>

#include "c_gate.hpp"
#include "exceptions.hpp"
#include "state.hpp"
#include "state.ipp"


/// Constructs a new error with a plain-text message.
Expand Down Expand Up @@ -72,16 +73,18 @@ lutok::api_error::~api_error(void) throw()
/// \pre There is an error message on the top of the stack.
/// \post The error message is popped from the stack.
///
/// \param s The Lua state.
/// \param state_ The Lua state.
/// \param api_function_ The name of the Lua API function that caused the error.
///
/// \return A new api_error with the popped message.
lutok::api_error
lutok::api_error::from_stack(lua_State* s, const std::string& api_function_)
lutok::api_error::from_stack(state& state_, const std::string& api_function_)
{
assert(lua_isstring(s, -1));
const std::string message = lua_tostring(s, -1);
lua_pop(s, 1);
lua_State* raw_state = lutok::state_c_gate(state_).c_state();

assert(lua_isstring(raw_state, -1));
const std::string message = lua_tostring(raw_state, -1);
lua_pop(raw_state, 1);
return lutok::api_error(api_function_, message);
}

Expand Down
4 changes: 1 addition & 3 deletions exceptions.hpp
Expand Up @@ -35,8 +35,6 @@
#include <stdexcept>
#include <string>

#include <lua.hpp>

namespace lutok {


Expand All @@ -59,7 +57,7 @@ class api_error : public error {
explicit api_error(const std::string&, const std::string&);
virtual ~api_error(void) throw();

static api_error from_stack(lua_State*, const std::string&);
static api_error from_stack(state&, const std::string&);

const std::string& api_function(void) const;
};
Expand Down
17 changes: 9 additions & 8 deletions exceptions_test.cpp
Expand Up @@ -29,9 +29,9 @@
#include <cstring>

#include <atf-c++.hpp>
#include <lua.hpp>

#include "exceptions.hpp"
#include "state.ipp"


ATF_TEST_CASE_WITHOUT_HEAD(error);
Expand All @@ -54,13 +54,14 @@ ATF_TEST_CASE_BODY(api_error__explicit)
ATF_TEST_CASE_WITHOUT_HEAD(api_error__from_stack);
ATF_TEST_CASE_BODY(api_error__from_stack)
{
lua_State* s = lua_open();
lua_pushinteger(s, 123);
lua_pushstring(s, "The error message");
const lutok::api_error e = lutok::api_error::from_stack(s, "the_function");
ATF_REQUIRE_EQ(1, lua_gettop(s));
ATF_REQUIRE_EQ(123, lua_tointeger(s, -1));
lua_pop(s, 1);
lutok::state state;
state.push_integer(123);
state.push_string("The error message");
const lutok::api_error e = lutok::api_error::from_stack(state,
"the_function");
ATF_REQUIRE_EQ(1, state.get_top());
ATF_REQUIRE_EQ(123, state.to_integer());
state.pop(1);
ATF_REQUIRE(std::strcmp("The error message", e.what()) == 0);
ATF_REQUIRE_EQ("the_function", e.api_function());
}
Expand Down
2 changes: 2 additions & 0 deletions operations.cpp
Expand Up @@ -28,6 +28,8 @@

#include <cassert>

#include <lua.hpp>

#include "exceptions.hpp"
#include "operations.hpp"
#include "stack_cleaner.hpp"
Expand Down
22 changes: 11 additions & 11 deletions state.cpp
Expand Up @@ -301,7 +301,7 @@ lutok::state::get_global(const std::string& name)
lua_pushcfunction(_pimpl->lua_state, protected_getglobal);
lua_pushstring(_pimpl->lua_state, name.c_str());
if (lua_pcall(_pimpl->lua_state, 1, 1, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_getglobal");
throw lutok::api_error::from_stack(*this, "lua_getglobal");
}


Expand All @@ -321,7 +321,7 @@ lutok::state::get_table(const int index)
lua_pushvalue(_pimpl->lua_state, index < 0 ? index - 1 : index);
lua_pushvalue(_pimpl->lua_state, -3);
if (lua_pcall(_pimpl->lua_state, 2, 1, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_gettable");
throw lutok::api_error::from_stack(*this, "lua_gettable");
lua_remove(_pimpl->lua_state, -2);
}

Expand Down Expand Up @@ -434,7 +434,7 @@ lutok::state::load_file(const std::string& file)
if (!::access(file.c_str(), R_OK) == 0)
throw lutok::file_not_found_error(file);
if (luaL_loadfile(_pimpl->lua_state, file.c_str()) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaL_loadfile");
throw lutok::api_error::from_stack(*this, "luaL_loadfile");
}


Expand All @@ -449,7 +449,7 @@ void
lutok::state::load_string(const std::string& str)
{
if (luaL_loadstring(_pimpl->lua_state, str.c_str()) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaL_loadstring");
throw lutok::api_error::from_stack(*this, "luaL_loadstring");
}


Expand Down Expand Up @@ -496,7 +496,7 @@ lutok::state::next(const int index)
lua_pushvalue(_pimpl->lua_state, index < 0 ? index - 1 : index);
lua_pushvalue(_pimpl->lua_state, -3);
if (lua_pcall(_pimpl->lua_state, 2, LUA_MULTRET, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_next");
throw lutok::api_error::from_stack(*this, "lua_next");
const bool more = lua_toboolean(_pimpl->lua_state, -1);
lua_pop(_pimpl->lua_state, 1);
if (more)
Expand All @@ -517,7 +517,7 @@ lutok::state::open_base(void)
{
lua_pushcfunction(_pimpl->lua_state, luaopen_base);
if (lua_pcall(_pimpl->lua_state, 0, 0, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaopen_base");
throw lutok::api_error::from_stack(*this, "luaopen_base");
}


Expand All @@ -531,7 +531,7 @@ lutok::state::open_string(void)
{
lua_pushcfunction(_pimpl->lua_state, luaopen_string);
if (lua_pcall(_pimpl->lua_state, 0, 0, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaopen_string");
throw lutok::api_error::from_stack(*this, "luaopen_string");
}


Expand All @@ -545,7 +545,7 @@ lutok::state::open_table(void)
{
lua_pushcfunction(_pimpl->lua_state, luaopen_table);
if (lua_pcall(_pimpl->lua_state, 0, 0, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaopen_table");
throw lutok::api_error::from_stack(*this, "luaopen_table");
}


Expand All @@ -560,7 +560,7 @@ void
lutok::state::pcall(const int nargs, const int nresults, const int errfunc)
{
if (lua_pcall(_pimpl->lua_state, nargs, nresults, errfunc) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_pcall");
throw lutok::api_error::from_stack(*this, "lua_pcall");
}


Expand Down Expand Up @@ -664,7 +664,7 @@ lutok::state::set_global(const std::string& name)
lua_pushstring(_pimpl->lua_state, name.c_str());
lua_pushvalue(_pimpl->lua_state, -3);
if (lua_pcall(_pimpl->lua_state, 2, 0, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_setglobal");
throw lutok::api_error::from_stack(*this, "lua_setglobal");
lua_pop(_pimpl->lua_state, 1);
}

Expand Down Expand Up @@ -695,7 +695,7 @@ lutok::state::set_table(const int index)
lua_pushvalue(_pimpl->lua_state, -4);
lua_pushvalue(_pimpl->lua_state, -4);
if (lua_pcall(_pimpl->lua_state, 3, 0, 0) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "lua_settable");
throw lutok::api_error::from_stack(*this, "lua_settable");
lua_pop(_pimpl->lua_state, 2);
}

Expand Down

0 comments on commit d1790df

Please sign in to comment.