Move lua_error out of catch handler to defer longjmp.
[luabind.git] / test / main.cpp
blob3152c7317af4e873264c7ad7a920c09f41ab81b2
1 // Copyright (c) 2005 Daniel Wallin, Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <iostream>
24 #include <cstring>
26 extern "C"
28 #include "lauxlib.h"
29 #include "lualib.h"
32 #include <luabind/open.hpp>
33 #include "test.hpp"
35 extern "C" struct lua_State;
37 void test_main(lua_State*);
39 struct lua_state
41 lua_state();
42 ~lua_state();
44 operator lua_State*() const;
45 void check() const;
47 private:
48 lua_State* m_state;
49 int m_top;
52 lua_state::lua_state()
53 : m_state(lua_open())
55 luaopen_base(m_state);
56 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
57 // lua 5.1 or newer
58 luaL_openlibs(m_state);
59 #else
60 // lua 5.0.2 or older
61 lua_baselibopen(m_state);
62 #endif
63 m_top = lua_gettop(m_state);
64 luabind::open(m_state);
67 lua_state::~lua_state()
69 lua_close(m_state);
72 void lua_state::check() const
74 TEST_CHECK(lua_gettop(m_state) == m_top);
77 lua_state::operator lua_State*() const
79 return m_state;
82 int pcall_handler(lua_State* L)
84 return 1;
87 void dostring(lua_State* state, char const* str)
89 lua_pushcclosure(state, &pcall_handler, 0);
91 if (luaL_loadbuffer(state, str, std::strlen(str), str))
93 std::string err(lua_tostring(state, -1));
94 lua_pop(state, 2);
95 throw err;
98 if (lua_pcall(state, 0, 0, -2))
100 std::string err(lua_tostring(state, -1));
101 lua_pop(state, 2);
102 throw err;
105 lua_pop(state, 1);
108 bool tests_failure = false;
110 void report_failure(char const* err, char const* file, int line)
112 std::cerr << file << ":" << line << "\"" << err << "\"\n";
113 tests_failure = true;
116 int main()
118 lua_state L;
121 test_main(L);
122 L.check();
123 return tests_failure ? 1 : 0;
125 catch (luabind::error const& e)
127 std::cerr << "Terminated with exception: \"" << e.what() << "\"\n"
128 << lua_tostring(e.state(), -1) << "\n";
129 return 1;
131 catch (std::exception const& e)
133 std::cerr << "Terminated with exception: \"" << e.what() << "\"\n";
134 return 1;
136 catch (...)
138 std::cerr << "Terminated with unknown exception\n";
139 return 1;