main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2005 Daniel Wallin, Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include <iostream>
  20. #include <cstring>
  21. extern "C"
  22. {
  23. #include "lauxlib.h"
  24. #include "lualib.h"
  25. }
  26. #include <luabind/open.hpp>
  27. #include "test.hpp"
  28. extern "C" struct lua_State;
  29. void test_main(lua_State*);
  30. struct lua_state
  31. {
  32. lua_state();
  33. ~lua_state();
  34. operator lua_State*() const;
  35. void check() const;
  36. private:
  37. lua_State* m_state;
  38. int m_top;
  39. };
  40. lua_state::lua_state()
  41. : m_state(lua_open())
  42. {
  43. luaopen_base(m_state);
  44. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
  45. // lua 5.1 or newer
  46. luaL_openlibs(m_state);
  47. #else
  48. // lua 5.0.2 or older
  49. lua_baselibopen(m_state);
  50. #endif
  51. m_top = lua_gettop(m_state);
  52. luabind::open(m_state);
  53. }
  54. lua_state::~lua_state()
  55. {
  56. lua_close(m_state);
  57. }
  58. void lua_state::check() const
  59. {
  60. TEST_CHECK(lua_gettop(m_state) == m_top);
  61. }
  62. lua_state::operator lua_State*() const
  63. {
  64. return m_state;
  65. }
  66. int pcall_handler(lua_State* L)
  67. {
  68. return 1;
  69. }
  70. void dostring(lua_State* state, char const* str)
  71. {
  72. lua_pushcclosure(state, &pcall_handler, 0);
  73. if (luaL_loadbuffer(state, str, std::strlen(str), str))
  74. {
  75. std::string err(lua_tostring(state, -1));
  76. lua_pop(state, 2);
  77. throw err;
  78. }
  79. if (lua_pcall(state, 0, 0, -2))
  80. {
  81. std::string err(lua_tostring(state, -1));
  82. lua_pop(state, 2);
  83. throw err;
  84. }
  85. lua_pop(state, 1);
  86. }
  87. bool tests_failure = false;
  88. void report_failure(char const* err, char const* file, int line)
  89. {
  90. std::cerr << file << ":" << line << "\"" << err << "\"\n";
  91. tests_failure = true;
  92. }
  93. int main()
  94. {
  95. lua_state L;
  96. try
  97. {
  98. test_main(L);
  99. L.check();
  100. return tests_failure ? 1 : 0;
  101. }
  102. catch (luabind::error const& e)
  103. {
  104. std::cerr << "Terminated with exception: \"" << e.what() << "\"\n"
  105. << lua_tostring(e.state(), -1) << "\n";
  106. return 1;
  107. }
  108. catch (std::exception const& e)
  109. {
  110. std::cerr << "Terminated with exception: \"" << e.what() << "\"\n";
  111. return 1;
  112. }
  113. catch (...)
  114. {
  115. std::cerr << "Terminated with unknown exception\n";
  116. return 1;
  117. }
  118. }