test_lua.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "test.h"
  18. #include "config.h"
  19. #include <stdexcept>
  20. extern "C" {
  21. #if USE_LUAJIT
  22. #include <luajit.h>
  23. #else
  24. #include <lua.h>
  25. #endif
  26. #include <lauxlib.h>
  27. }
  28. /*
  29. * This class tests for two common issues that prevent correct error handling
  30. * between Lua and C++.
  31. * Further reading:
  32. * - https://luajit.org/extensions.html#exceptions
  33. * - http://lua-users.org/wiki/ErrorHandlingBetweenLuaAndCplusplus
  34. */
  35. class TestLua : public TestBase
  36. {
  37. public:
  38. TestLua() { TestManager::registerTestModule(this); }
  39. const char *getName() { return "TestLua"; }
  40. void runTests(IGameDef *gamedef);
  41. void testLuaDestructors();
  42. void testCxxExceptions();
  43. };
  44. static TestLua g_test_instance;
  45. void TestLua::runTests(IGameDef *gamedef)
  46. {
  47. TEST(testLuaDestructors);
  48. TEST(testCxxExceptions);
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /*
  52. Check that Lua unwinds the stack correctly when it throws errors internally.
  53. (This is not the case with PUC Lua unless it was compiled as C++.)
  54. */
  55. namespace
  56. {
  57. class DestructorDetector {
  58. bool *did_destruct;
  59. public:
  60. DestructorDetector(bool *did_destruct) : did_destruct(did_destruct)
  61. {
  62. *did_destruct = false;
  63. }
  64. ~DestructorDetector()
  65. {
  66. *did_destruct = true;
  67. }
  68. };
  69. }
  70. void TestLua::testLuaDestructors()
  71. {
  72. bool did_destruct = false;
  73. lua_State *L = luaL_newstate();
  74. lua_cpcall(L, [](lua_State *L) -> int {
  75. DestructorDetector d(reinterpret_cast<bool*>(lua_touserdata(L, 1)));
  76. luaL_error(L, "error");
  77. return 0;
  78. }, &did_destruct);
  79. lua_close(L);
  80. UASSERT(did_destruct);
  81. }
  82. namespace {
  83. int wrapper(lua_State *L, lua_CFunction inner)
  84. {
  85. try {
  86. return inner(L);
  87. } catch (std::exception &e) {
  88. lua_pushstring(L, e.what());
  89. return lua_error(L);
  90. }
  91. }
  92. }
  93. /*
  94. Check that C++ exceptions are caught and re-thrown as Lua errors.
  95. This is handled by a wrapper we define ourselves.
  96. (PUC Lua does not support use of such a wrapper, we have a patched version)
  97. */
  98. void TestLua::testCxxExceptions()
  99. {
  100. lua_State *L = luaL_newstate();
  101. #if USE_LUAJIT
  102. lua_pushlightuserdata(L, reinterpret_cast<void*>(wrapper));
  103. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
  104. lua_pop(L, 1);
  105. #else
  106. lua_atccall(L, wrapper);
  107. #endif
  108. lua_pushcfunction(L, [](lua_State *L) -> int {
  109. throw std::runtime_error("example");
  110. });
  111. int caught = 0;
  112. std::string errmsg;
  113. try {
  114. if (lua_pcall(L, 0, 0, 0) != 0) {
  115. caught = 2;
  116. errmsg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "";
  117. }
  118. } catch (std::exception &e) {
  119. caught = 1;
  120. }
  121. if (caught != 1)
  122. lua_close(L);
  123. UASSERTEQ(int, caught, 2);
  124. UASSERT(errmsg.find("example") != std::string::npos);
  125. }