From 51889cb0ba5cef80361751c42ada8386e2816035 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 23 Apr 2003 20:08:16 +0000 Subject: [PATCH] added set_error_callback() and set_cast_failed_callback() and improved the exceptions a bit. --- luabind/detail/call_function.hpp | 18 +++++++++--------- luabind/detail/call_member.hpp | 18 +++++++++--------- luabind/detail/error.hpp | 35 +++++++++++++++++------------------ luabind/detail/object_funs.hpp | 4 ++-- luabind/functor.hpp | 18 +++++++++--------- luabind/object.hpp | 8 ++++---- test/test_attributes.cpp | 13 +++++-------- 7 files changed, 55 insertions(+), 59 deletions(-) diff --git a/luabind/detail/call_function.hpp b/luabind/detail/call_function.hpp index aaab3e7..4adc557 100644 --- a/luabind/detail/call_function.hpp +++ b/luabind/detail/call_function.hpp @@ -87,7 +87,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -116,7 +116,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -132,10 +132,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." " If you want to handle the error you can use luabind::set_error_callback()"); @@ -165,7 +165,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -181,10 +181,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." " If you want to handle the error you can use luabind::set_error_callback()"); @@ -244,7 +244,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -270,7 +270,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); diff --git a/luabind/detail/call_member.hpp b/luabind/detail/call_member.hpp index 7130f50..da8b329 100644 --- a/luabind/detail/call_member.hpp +++ b/luabind/detail/call_member.hpp @@ -82,7 +82,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value + 1, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -114,7 +114,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value + 1, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -130,10 +130,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." "If you want to handle this error use luabind::set_error_callback()"); @@ -166,7 +166,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value + 1, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -182,10 +182,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." "If you want to handle this error use luabind::set_error_callback()"); @@ -249,7 +249,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value + 1, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -281,7 +281,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value + 1, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); diff --git a/luabind/detail/error.hpp b/luabind/detail/error.hpp index 0d6cc46..a973a2b 100644 --- a/luabind/detail/error.hpp +++ b/luabind/detail/error.hpp @@ -39,40 +39,38 @@ namespace luabind // may throw, if the copy constructor of an exception that is // being thrown throws another exception, terminate will be called // and the entire application is killed. - struct error : public std::exception - { - virtual const char* what() const throw() - { - return "lua runtime error"; - } - }; - - // this exception is thrown when you call a lua function - // from C++, and the return value on the lua stack cannot - // be converted to the type you specified in the C++ call - // (as a template parameter) - class cant_convert_return_value: public std::exception + class error : public std::exception { public: - + error(lua_State* L): m_L(L) {} + lua_State* state() const throw() { return m_L; } virtual const char* what() const throw() { - return "The return value from the called function cannot be converted by the given return value policy"; + return "lua runtime error"; } - + private: + lua_State* m_L; }; - // if a object_cast<>() fails, this is thrown + // if an object_cast<>() fails, this is thrown + // it is also thrown if the return value of + // a lua function cannot be converted class cast_failed : public std::exception { public: + cast_failed(lua_State* L, LUABIND_TYPE_INFO i): m_L(L), m_info(i) {} + lua_State* state() const throw() { return m_L; } + LUABIND_TYPE_INFO info() const throw() { return m_info; } virtual const char* what() const throw() { return "unable to make cast"; } + private: + lua_State* m_L; + LUABIND_TYPE_INFO m_info; }; #else typedef void(*error_callback_fun)(lua_State*); - typedef void(*cast_failed_callback_fun)(lua_State*); + typedef void(*cast_failed_callback_fun)(lua_State*, LUABIND_TYPE_INFO); namespace detail { @@ -109,3 +107,4 @@ namespace luabind } #endif // LUABIND_ERROR_HPP_INCLUDED + diff --git a/luabind/detail/object_funs.hpp b/luabind/detail/object_funs.hpp index d24630d..e2f88e0 100644 --- a/luabind/detail/object_funs.hpp +++ b/luabind/detail/object_funs.hpp @@ -52,10 +52,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(T), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cast_failed(); + throw cast_failed(L, LUABIND_TYPEID(T)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(T)); assert(0 && "object_cast failed. If you want to handle this error use luabind::set_error_callback()"); std::terminate(); diff --git a/luabind/functor.hpp b/luabind/functor.hpp index a37d3e6..f02387c 100644 --- a/luabind/functor.hpp +++ b/luabind/functor.hpp @@ -84,7 +84,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -111,7 +111,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -127,10 +127,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." "if you want to handle this error use luabind::set_error_callback()"); @@ -158,7 +158,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -174,10 +174,10 @@ namespace luabind if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0) { #ifndef LUABIND_NO_EXCEPTIONS - throw cant_convert_return_value(); + throw cast_failed(L, LUABIND_TYPEID(Ret)); #else cast_failed_callback_fun e = detail::error_callback::get().cast; - if (e) e(L); + if (e) e(L, LUABIND_TYPEID(Ret)); assert(0 && "the lua function's return value could not be converted." "if you want to handle this error use luabind::set_error_callback()"); @@ -232,7 +232,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -257,7 +257,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); diff --git a/luabind/object.hpp b/luabind/object.hpp index dba3a7b..1a97274 100644 --- a/luabind/object.hpp +++ b/luabind/object.hpp @@ -169,7 +169,7 @@ namespace luabind if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -1164,7 +1164,7 @@ private: if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw error(); + throw error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -1536,7 +1536,7 @@ private: if (lua_pcall(L, boost::tuples::length::value, 0, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); @@ -1559,7 +1559,7 @@ private: if (lua_pcall(L, boost::tuples::length::value, 1, 0)) { #ifndef LUABIND_NO_EXCEPTIONS - throw luabind::error(); + throw luabind::error(L); #else error_callback_fun e = detail::error_callback::get().err; if (e) e(L); diff --git a/test/test_attributes.cpp b/test/test_attributes.cpp index 702890b..34239d1 100644 --- a/test/test_attributes.cpp +++ b/test/test_attributes.cpp @@ -107,11 +107,13 @@ bool test_attributes() if (feedback != 6) return false; object glob = get_globals(L); - + + if (dostring(L, "a = 4")) return false; + if (glob["a"].type() != LUA_TNUMBER) return false; if (dostring(L, "a = test[nil]")) return false; if (glob["a"].type() != LUA_TNIL) return false; - - // new stuff, doesn work yet + if (dostring(L, "a = test[3.6]")) return false; + if (glob["a"].type() != LUA_TNIL) return false; lua_pushstring(L, "test"); glob["test_string"].set(); @@ -131,11 +133,6 @@ bool test_attributes() if (glob["test_string"].type() != LUA_TNIL) return false; - -// object t = get_globals(L)["test"]["nil"]; -// if (t.type() != LUA_TNIL) return false; - - if (dostring2(L, "test.o = 5") != 1) return false; if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false; lua_pop(L, 1); -- 2.11.4.GIT