2 #include <luabind/functor.hpp>
8 LUABIND_ANONYMOUS_FIX
int feedback
= 0;
10 luabind::functor
<int> functor_test
;
12 void set_functor(luabind::functor
<int> f
)
19 ~base() { feedback
= 99; }
37 int g() { feedback
= 3; return 4; }
44 void test_functor(const luabind::functor
<int>& fun
)
49 void test_value_converter(const std::string str
)
54 void test_pointer_converter(const char* const str
)
63 void take_by_value(copy_me m
)
67 int function_should_never_be_called(lua_State
*)
73 } // anonymous namespace
75 namespace luabind
{ namespace converters
77 yes_t
is_user_defined(by_value
<int>);
79 int convert_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
81 return static_cast<int>(lua_tonumber(L
, index
));
84 int match_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
86 if (lua_isnumber(L
, index
)) return 0; else return -1;
89 void convert_cpp_to_lua(lua_State
* L
, const int& v
)
96 bool test_free_functions()
98 using namespace luabind
;
100 lua_State
* L
= lua_open();
102 int top
= lua_gettop(L
);
106 lua_pushstring(L
, "f");
107 lua_pushcclosure(L
, &function_should_never_be_called
, 0);
108 lua_settable(L
, LUA_GLOBALSINDEX
);
110 if (dostring(L
, "f()")) return false;
111 if (feedback
!= -1) return false;
115 class_
<copy_me
>("copy_me")
116 .def(constructor
<>()),
122 def("by_value", &take_by_value
),
124 def("f", (void(*)(int)) &f
),
125 def("f", (void(*)(int, int)) &f
),
127 def("create", &create_base
, adopt(return_value
)),
128 def("test_functor", &test_functor
),
129 def("set_functor", &set_functor
)
132 #if !(BOOST_MSVC < 1300)
134 def("test_value_converter", &test_value_converter
),
135 def("test_pointer_converter", &test_pointer_converter
)
140 if (dostring(L
, "e = create()")) return false;
141 if (dostring(L
, "e:f()")) return false;
142 if (feedback
!= 5) return false;
144 if (dostring(L
, "f(7)")) return false;
145 if (feedback
!= 7) return false;
147 if (dostring(L
, "f(3, 9)")) return false;
148 if (feedback
!= 12) return false;
150 if (dostring(L
, "a = g()")) return false;
151 if (feedback
!= 3) return false;
152 lua_pushstring(L
, "a");
153 lua_gettable(L
, LUA_GLOBALSINDEX
);
154 if (lua_tonumber(L
, -1) != 4) return false;
157 if (dostring(L
, "test_functor(function(x) return x * 10 end)")) return false;
158 if (feedback
!= 50) return false;
160 functor
<int> test_f(L
, "g");
162 if (a
!= 4) return false;
163 if (feedback
!= 3) return false;
164 if (dostring(L
, "set_functor(nil)")) return false;
166 if (top
!= lua_gettop(L
)) return false;
168 if (dostring(L
, "function lua_create() return create() end")) return false;
170 base
* ptr
= call_function
<base
*>(L
, "lua_create") [ adopt(result
) ];
174 #if !(BOOST_MSVC < 1300)
175 dostring(L
, "test_value_converter('foobar')");
176 if (feedback
!= 9) return false;
177 dostring(L
, "test_pointer_converter('foobar')");
178 if (feedback
!= 6) return false;
181 if (!dostring2(L
, "f('incorrect', 'parameters')")) return false;
182 std::cout
<< lua_tostring(L
, -1) << "\n";
185 dostring(L
, "function functor_test(a) glob = a\n return 'foobar'\nend");
186 functor
<std::string
> functor_test
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
188 std::string str
= functor_test(6)[detail::null_type()];
189 if (str
!= "foobar") return false;
190 if (object_cast
<int>(get_globals(L
)["glob"]) != 6) return false;
192 functor
<std::string
> functor_test2
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
194 if (functor_test
!= functor_test2
) return false;
197 if (feedback
!= 99) return false;