revised scope documentation and added smart pointer documentation.
[luabind.git] / test / test_yield.cpp
blob857d112623ee677c56e6604d6524f614bec7f5dd
1 #include "test.h"
2 #include <luabind/yield_policy.hpp>
3 #include <stdio.h>
5 namespace
7 LUABIND_ANONYMOUS_FIX int feedback = 0;
9 struct test_class
11 test_class(): n(0) {}
13 int f() const
15 return const_cast<int&>(n)++;
18 int n;
21 int f(int a)
23 return 9;
26 int j(lua_State* L)
28 lua_pushnumber(L, 9);
29 return lua_yield(L, 1);
32 void f() {}
35 #include <iostream>
37 struct ns_test
39 template<class T>
40 ns_test(const T&) {}
42 ns_test(const char*) {}
44 template<class T>
45 void operator=(const T&) {}
48 bool test_yield()
50 using namespace luabind;
52 lua_State* L = lua_open();
53 lua_baselibopen(L);
54 lua_closer c(L);
56 open(L);
58 class_<test_class>("test")
59 .def(constructor<>())
60 .def("f", &test_class::f, yield)
61 .commit(L)
64 dostring(L, "function g() a = test() for i = 1, 10 do print(a:f()) end end");
66 lua_pushstring(L, "j");
67 lua_pushcclosure(L, j, 0);
68 lua_settable(L, LUA_GLOBALSINDEX);
70 lua_State* thread = lua_newthread(L);
71 lua_pushstring(thread, "g");
72 lua_gettable(thread, LUA_GLOBALSINDEX);
74 if (lua_resume(thread, 0))
76 std::cout << "error: " << lua_tostring(thread, -1) << '\n';
79 for (int i = 0; i < 10; ++i)
81 std::cout << "iteration: " << i << ", top: " << lua_gettop(thread) << '\n';
83 lua_resume(thread, lua_gettop(thread));
87 return true;