*** empty log message ***
[luabind.git] / test / test_free_functions.cpp
blob974a87a28d36cc4180761c76e517aefee456fca9
1 #include "test.h"
2 #include <luabind/functor.hpp>
4 namespace
7 LUABIND_ANONYMOUS_FIX int feedback = 0;
9 struct base
11 ~base() { feedback = 99; }
13 void f()
15 feedback = 5;
19 void f(int x)
21 feedback = x;
24 void f(int x, int y)
26 feedback = x + y;
29 int g() { feedback = 3; return 4; }
31 base* create_base()
33 return new base();
36 void test_functor(const luabind::functor<int>& fun)
38 feedback = fun(5);
41 void test_value_converter(const std::string str)
43 feedback = 9;
46 void test_pointer_converter(const char* const str)
48 feedback = 6;
51 struct copy_me
55 void take_by_value(copy_me m)
59 int function_should_never_be_called(lua_State*)
61 feedback = -1;
62 return 0;
65 } // anonymous namespace
67 namespace luabind { namespace converters
69 yes_t is_user_defined(by_value<int>);
71 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
73 return lua_tonumber(L, index);
76 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
78 if (lua_isnumber(L, index)) return 0; else return -1;
81 void convert_cpp_to_lua(lua_State* L, const int& v)
83 lua_pushnumber(L, v);
88 bool test_free_functions()
90 using namespace luabind;
92 lua_State* L = lua_open();
93 lua_closer c(L);
94 int top = lua_gettop(L);
96 open(L);
98 lua_pushstring(L, "f");
99 lua_pushcclosure(L, &function_should_never_be_called, 0);
100 lua_settable(L, LUA_GLOBALSINDEX);
102 if (dostring(L, "f()")) return false;
103 if (feedback != -1) return false;
105 module(L)
107 class_<copy_me>("copy_me")
108 .def(constructor<>()),
110 class_<base>("base")
111 .def("f", &base::f),
114 def("by_value", &take_by_value),
116 def("f", (void(*)(int)) &f),
117 def("f", (void(*)(int, int)) &f),
118 def("g", &g),
119 def("create", &create_base, adopt(return_value)),
120 def("test_functor", &test_functor)
122 #if !(BOOST_MSVC < 1300)
124 def("test_value_converter", &test_value_converter),
125 def("test_pointer_converter", &test_pointer_converter)
126 #endif
130 if (dostring(L, "e = create()")) return false;
131 if (dostring(L, "e:f()")) return false;
132 if (feedback != 5) return false;
134 if (dostring(L, "f(7)")) return false;
135 if (feedback != 7) return false;
137 if (dostring(L, "f(3, 9)")) return false;
138 if (feedback != 12) return false;
140 if (dostring(L, "a = g()")) return false;
141 if (feedback != 3) return false;
142 lua_pushstring(L, "a");
143 lua_gettable(L, LUA_GLOBALSINDEX);
144 if (lua_tonumber(L, -1) != 4) return false;
145 lua_pop(L, 1);
147 if (dostring(L, "test_functor(function(x) return x * 10 end)")) return false;
148 if (feedback != 50) return false;
150 functor<int> test_f(L, "g");
151 int a = test_f();
152 if (a != 4) return false;
153 if (feedback != 3) return false;
155 if (top != lua_gettop(L)) return false;
157 if (dostring(L, "function lua_create() return create() end")) return false;
159 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
161 delete ptr;
163 #if !(BOOST_MSVC < 1300)
164 dostring(L, "test_value_converter('foobar')");
165 if (feedback != 9) return false;
166 dostring(L, "test_pointer_converter('foobar')");
167 if (feedback != 6) return false;
168 #endif
170 dostring(L, "function functor_test(a) glob = a\n return 'foobar'\nend");
171 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
173 std::string str = functor_test(6)[detail::null_type()];
174 if (str != "foobar") return false;
175 if (object_cast<int>(get_globals(L)["glob"]) != 6) return false;
177 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
179 if (functor_test != functor_test2) return false;
182 if (feedback != 99) return false;
184 return true;