doc tweaks
[luabind.git] / test / test_yield.cpp
blob2ccefbb19ea683b07a1105079bb4856476109dba
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)
72 DOSTRING(L, "function g() a = test() for i = 1, 10 do print(a:f()) end end");
74 lua_pushstring(L, "j");
75 lua_pushcclosure(L, j, 0);
76 lua_settable(L, LUA_GLOBALSINDEX);
78 lua_State* thread = lua_newthread(L);
79 lua_pushstring(thread, "g");
80 lua_gettable(thread, LUA_GLOBALSINDEX);
82 if (lua_resume(thread, 0))
84 std::cout << "error: " << lua_tostring(thread, -1) << '\n';
87 for (int i = 0; i < 10; ++i)
89 std::cout << "iteration: " << i << ", top: " << lua_gettop(thread) << '\n';
91 lua_resume(thread, lua_gettop(thread));