2 #include <luabind/functor.hpp>
7 LUABIND_ANONYMOUS_FIX
int feedback
= 0;
9 luabind::functor
<int> functor_test
;
11 void set_functor(luabind::functor
<int> f
)
18 ~base() { feedback
= 99; }
36 int g() { feedback
= 3; return 4; }
43 void test_functor(const luabind::functor
<int>& fun
)
48 void test_value_converter(const std::string str
)
53 void test_pointer_converter(const char* const str
)
62 void take_by_value(copy_me m
)
66 int function_should_never_be_called(lua_State
*)
72 } // anonymous namespace
74 namespace luabind
{ namespace converters
76 yes_t
is_user_defined(by_value
<int>);
78 int convert_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
80 return static_cast<int>(lua_tonumber(L
, index
));
83 int match_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
85 if (lua_isnumber(L
, index
)) return 0; else return -1;
88 void convert_cpp_to_lua(lua_State
* L
, const int& v
)
95 bool test_free_functions()
97 using namespace luabind
;
99 lua_State
* L
= lua_open();
101 int top
= lua_gettop(L
);
105 lua_pushstring(L
, "f");
106 lua_pushcclosure(L
, &function_should_never_be_called
, 0);
107 lua_settable(L
, LUA_GLOBALSINDEX
);
109 if (dostring(L
, "f()")) return false;
110 if (feedback
!= -1) return false;
114 class_
<copy_me
>("copy_me")
115 .def(constructor
<>()),
121 def("by_value", &take_by_value
),
123 def("f", (void(*)(int)) &f
),
124 def("f", (void(*)(int, int)) &f
),
126 def("create", &create_base
, adopt(return_value
)),
127 def("test_functor", &test_functor
),
128 def("set_functor", &set_functor
)
131 #if !(BOOST_MSVC < 1300)
133 def("test_value_converter", &test_value_converter
),
134 def("test_pointer_converter", &test_pointer_converter
)
139 if (dostring(L
, "e = create()")) return false;
140 if (dostring(L
, "e:f()")) return false;
141 if (feedback
!= 5) return false;
143 if (dostring(L
, "f(7)")) return false;
144 if (feedback
!= 7) return false;
146 if (dostring(L
, "f(3, 9)")) return false;
147 if (feedback
!= 12) return false;
149 if (dostring(L
, "a = g()")) return false;
150 if (feedback
!= 3) return false;
151 lua_pushstring(L
, "a");
152 lua_gettable(L
, LUA_GLOBALSINDEX
);
153 if (lua_tonumber(L
, -1) != 4) return false;
156 if (dostring(L
, "test_functor(function(x) return x * 10 end)")) return false;
157 if (feedback
!= 50) return false;
159 functor
<int> test_f(L
, "g");
161 if (a
!= 4) return false;
162 if (feedback
!= 3) return false;
163 if (dostring(L
, "set_functor(nil)")) return false;
165 if (top
!= lua_gettop(L
)) return false;
167 if (dostring(L
, "function lua_create() return create() end")) return false;
169 base
* ptr
= call_function
<base
*>(L
, "lua_create") [ adopt(result
) ];
173 #if !(BOOST_MSVC < 1300)
174 dostring(L
, "test_value_converter('foobar')");
175 if (feedback
!= 9) return false;
176 dostring(L
, "test_pointer_converter('foobar')");
177 if (feedback
!= 6) return false;
180 dostring(L
, "function functor_test(a) glob = a\n return 'foobar'\nend");
181 functor
<std::string
> functor_test
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
183 std::string str
= functor_test(6)[detail::null_type()];
184 if (str
!= "foobar") return false;
185 if (object_cast
<int>(get_globals(L
)["glob"]) != 6) return false;
187 functor
<std::string
> functor_test2
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
189 if (functor_test
!= functor_test2
) return false;
192 if (feedback
!= 99) return false;