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.
32 #include <luabind/open.hpp>
35 extern "C" struct lua_State
;
37 void test_main(lua_State
*);
44 operator lua_State
*() const;
52 lua_state::lua_state()
55 luaopen_base(m_state
);
56 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
58 luaL_openlibs(m_state
);
61 lua_baselibopen(m_state
);
63 m_top
= lua_gettop(m_state
);
64 luabind::open(m_state
);
67 lua_state::~lua_state()
72 void lua_state::check() const
74 TEST_CHECK(lua_gettop(m_state
) == m_top
);
77 lua_state::operator lua_State
*() const
82 int pcall_handler(lua_State
* L
)
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));
98 if (lua_pcall(state
, 0, 0, -2))
100 std::string
err(lua_tostring(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;
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";
131 catch (std::exception
const& e
)
133 std::cerr
<< "Terminated with exception: \"" << e
.what() << "\"\n";
138 std::cerr
<< "Terminated with unknown exception\n";