*** empty log message ***
[luabind.git] / test / test_yield.cpp
blob30ee05e7fe19e5674f915c80446b825f8024312a
1 // Copyright (c) 2003 Daniel Wallin and 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.
23 #include "test.hpp"
24 #include <luabind/luabind.hpp>
26 namespace
28 struct test_class : counted_type<test_class>
30 test_class(): n(0) {}
32 int f() const
34 return const_cast<int&>(n)++;
37 int n;
40 int f(int a)
42 return 9;
45 int j(lua_State* L)
47 lua_pushnumber(L, 9);
48 return lua_yield(L, 1);
51 void f() {}
54 #include <iostream>
56 void test_yield()
58 COUNTER_GUARD(test_class);
60 lua_state L;
62 using namespace luabind;
64 module(L)
66 class_<test_class>("test")
67 .def(constructor<>())
68 .def("f", &test_class::f, luabind::yield)
71 DOSTRING(L,
72 "function h()\n"
73 " coroutine.yield(4)"
74 "end");
77 lua_State* thread = lua_newthread(L);
78 BOOST_CHECK(resume_function<int>(thread, "h") == 4);
79 lua_pop(L, 1); // pop thread
82 DOSTRING(L,
83 "function g(str)\n"
84 " assert(str == 'foobar')\n"
85 " a = test()"
86 " for i = 1, 10 do\n"
87 " print(a:f())\n"
88 " end\n"
89 "end");
92 lua_State* thread = lua_newthread(L);
93 resume_function<void>(thread, "g", "foobar");
95 for (int i = 0; i < 10; ++i)
97 resume<void>(thread);
99 lua_pop(L, 1); // pop thread