functions.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. static
  4. int noret_environment = 0;
  5. static
  6. void test_function_noret_noparams() {
  7. noret_environment++;
  8. }
  9. TEST_CASE("wrap_function_noret_noparams") {
  10. lua_State* state = luaL_newstate();
  11. // Setup environment
  12. noret_environment = 1337;
  13. // Wrap function
  14. // lua_CFunction cfun = luwra::wrap_function<void(), test_function_noret_noparams>;
  15. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret_noparams);
  16. REQUIRE(cfun != nullptr);
  17. // Register function
  18. luwra::setGlobal(state, "test_function_noret_noparams", cfun);
  19. // Invoke function
  20. REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
  21. REQUIRE(lua_gettop(state) == 0);
  22. REQUIRE(noret_environment == 1338);
  23. lua_close(state);
  24. }
  25. static
  26. void test_function_noret(int a, int b) {
  27. noret_environment = a + b;
  28. }
  29. TEST_CASE("wrap_function_noret") {
  30. lua_State* state = luaL_newstate();
  31. // Test function beforehand
  32. test_function_noret(13, 37);
  33. int req_environemt = noret_environment;
  34. // Wrap function
  35. // lua_CFunction cfun = luwra::wrap_function<void(int, int), test_function_noret>;
  36. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret);
  37. REQUIRE(cfun != nullptr);
  38. // Register function
  39. luwra::setGlobal(state, "test_function_noret", cfun);
  40. // Invoke function
  41. REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
  42. REQUIRE(lua_gettop(state) == 0);
  43. REQUIRE(noret_environment == req_environemt);
  44. lua_close(state);
  45. }
  46. static
  47. int test_function_noparams() {
  48. return 13 * 37;
  49. }
  50. TEST_CASE("wrap_function_noparams") {
  51. lua_State* state = luaL_newstate();
  52. // Wrap function
  53. // lua_CFunction cfun = luwra::wrap_function<int(), test_function_noparams>;
  54. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noparams);
  55. REQUIRE(cfun != nullptr);
  56. // Register function
  57. luwra::setGlobal(state, "test_function_noparams", cfun);
  58. // Invoke function
  59. REQUIRE(luaL_dostring(state, "return test_function_noparams()") == 0);
  60. REQUIRE(lua_gettop(state) == 1);
  61. REQUIRE(luwra::Value<int>::read(state, -1) == test_function_noparams());
  62. lua_close(state);
  63. }
  64. static
  65. int test_function(int a, int b) {
  66. return (a + b) * (a - b);
  67. }
  68. TEST_CASE("wrap_function") {
  69. lua_State* state = luaL_newstate();
  70. // Wrap function
  71. // lua_CFunction cfun = luwra::wrap_function<int(int, int), test_function>;
  72. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function);
  73. REQUIRE(cfun != nullptr);
  74. // Register function
  75. luwra::setGlobal(state, "test_function", cfun);
  76. // Invoke function
  77. REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);
  78. REQUIRE(lua_gettop(state) == 1);
  79. REQUIRE(luwra::Value<int>::read(state, -1) == test_function(37, 13));
  80. lua_close(state);
  81. }
  82. TEST_CASE("NativeFunction") {
  83. luwra::StateWrapper state;
  84. SECTION("with return value") {
  85. REQUIRE(luaL_dostring(state, "return function (x, y) return x + y end") == LUA_OK);
  86. auto fun = luwra::read<luwra::NativeFunction<int(int, int)>>(state, -1);
  87. REQUIRE(fun(13, 37) == 50);
  88. }
  89. SECTION("without return value") {
  90. REQUIRE(luaL_dostring(state, "return function (x, y) returnValue = x + y end") == LUA_OK);
  91. auto fun = luwra::read<luwra::NativeFunction<void(int, int)>>(state, -1);
  92. fun(13, 37);
  93. int returnValue = state["returnValue"];
  94. REQUIRE(returnValue == 50);
  95. }
  96. }