*** empty log message ***
[luabind.git] / test / test_yield.cpp
blob853c9c7a9e7135a4b59b4d094a0b8e20f1aae04f
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 bool test_yield()
39 using namespace luabind;
41 lua_State* L = lua_open();
42 lua_baselibopen(L);
43 lua_closer c(L);
45 open(L);
47 module(L)
49 class_<test_class>("test")
50 .def(constructor<>())
51 .def("f", &test_class::f, yield)
54 dostring(L, "function g() a = test() for i = 1, 10 do print(a:f()) end end");
56 lua_pushstring(L, "j");
57 lua_pushcclosure(L, j, 0);
58 lua_settable(L, LUA_GLOBALSINDEX);
60 lua_State* thread = lua_newthread(L);
61 lua_pushstring(thread, "g");
62 lua_gettable(thread, LUA_GLOBALSINDEX);
64 if (lua_resume(thread, 0))
66 std::cout << "error: " << lua_tostring(thread, -1) << '\n';
69 for (int i = 0; i < 10; ++i)
71 std::cout << "iteration: " << i << ", top: " << lua_gettop(thread) << '\n';
73 lua_resume(thread, lua_gettop(thread));
77 return true;