test_yield.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2003 Daniel Wallin and 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 "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. struct test_class : counted_type<test_class>
  22. {
  23. test_class() {}
  24. int f() const
  25. {
  26. return 5;
  27. }
  28. };
  29. int f(int a)
  30. {
  31. return 9;
  32. }
  33. int j(lua_State* L)
  34. {
  35. lua_pushnumber(L, 9);
  36. return lua_yield(L, 1);
  37. }
  38. void f() {}
  39. COUNTER_GUARD(test_class);
  40. void test_main(lua_State* L)
  41. {
  42. using namespace luabind;
  43. module(L)
  44. [
  45. class_<test_class>("test")
  46. .def(constructor<>())
  47. .def("f", &test_class::f, luabind::yield)
  48. ];
  49. DOSTRING(L,
  50. "function h()\n"
  51. " coroutine.yield(4)"
  52. "end");
  53. {
  54. lua_State* thread = lua_newthread(L);
  55. TEST_CHECK(resume_function<int>(thread, "h") == 4);
  56. lua_pop(L, 1); // pop thread
  57. }
  58. DOSTRING(L,
  59. "function g(str)\n"
  60. " assert(str == 'foobar')\n"
  61. " a = test()"
  62. " for i = 1, 10 do\n"
  63. " assert(a:f() == i + 4)\n"
  64. " end\n"
  65. "end");
  66. {
  67. lua_State* thread = lua_newthread(L);
  68. TEST_CHECK(resume_function<int>(thread, "g", "foobar") == 5);
  69. for (int i = 1; i < 10; ++i)
  70. {
  71. TEST_CHECK(resume<int>(thread, i + 4) == 5);
  72. }
  73. lua_pop(L, 1); // pop thread
  74. }
  75. {
  76. lua_State* thread = lua_newthread(L);
  77. object g = globals(thread)["g"];
  78. TEST_CHECK(resume_function<int>(g, "foobar") == 5);
  79. for (int i = 1; i < 10; ++i)
  80. {
  81. TEST_CHECK(resume<int>(thread, i + 4) == 5);
  82. }
  83. lua_pop(L, 1); // pop thread
  84. }
  85. }