test_lua_classes.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright (c) 2005 Daniel Wallin, Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. #include <luabind/wrapper_base.hpp>
  22. #include <luabind/adopt_policy.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. namespace luabind {
  25. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  26. template<class T>
  27. T* get_pointer(boost::shared_ptr<T> const& p) { return p.get(); }
  28. #endif
  29. }
  30. using namespace luabind;
  31. struct A : counted_type<A>
  32. {
  33. virtual ~A() {}
  34. virtual std::string f()
  35. { return "A:f()"; }
  36. virtual std::string g() const
  37. { return "A:g()"; }
  38. };
  39. struct A_wrap : A, wrap_base
  40. {
  41. std::string f()
  42. {
  43. return call_member<std::string>(this, "f");
  44. }
  45. static std::string default_f(A* p)
  46. { return p->A::f(); }
  47. std::string g() const
  48. {
  49. return call_member<std::string>(this, "g");
  50. }
  51. static std::string default_g(A const* p)
  52. { return p->A::g(); }
  53. };
  54. struct B : A
  55. {
  56. virtual std::string f()
  57. { return "B:f()"; }
  58. };
  59. struct B_wrap : B, wrap_base
  60. {
  61. virtual std::string f()
  62. { return call_member<std::string>(this, "f"); }
  63. static std::string default_f(B* p)
  64. { return p->B::f(); }
  65. virtual std::string g() const
  66. { return call_member<std::string>(this, "g"); }
  67. static std::string default_g(B const* p)
  68. { return p->B::g(); }
  69. };
  70. struct base : counted_type<base>
  71. {
  72. virtual ~base() {}
  73. virtual std::string f()
  74. {
  75. return "base:f()";
  76. }
  77. virtual std::string g() const { return ""; }
  78. };
  79. base* filter(base* p) { return p; }
  80. struct base_wrap : base, wrap_base
  81. {
  82. virtual std::string f()
  83. {
  84. return call_member<std::string>(this, "f");
  85. }
  86. static std::string default_f(base* p)
  87. {
  88. return p->base::f();
  89. }
  90. virtual std::string g() const
  91. {
  92. return call_member<std::string>(this, "g");
  93. }
  94. };
  95. struct T_ // vc6.5, don't name your types T!
  96. {
  97. int f(int) { return 1; }
  98. };
  99. struct U : T_
  100. {
  101. int g() { return 3; }
  102. int f(int, int) { return 2; }
  103. };
  104. void test_main(lua_State* L)
  105. {
  106. module(L)
  107. [
  108. class_<A, A_wrap, boost::shared_ptr<A> >("A")
  109. .def(constructor<>())
  110. .def("f", &A::f, &A_wrap::default_f)
  111. .def("g", &A::g, &A_wrap::default_g),
  112. class_<B, A, B_wrap, boost::shared_ptr<A> >("B")
  113. .def(constructor<>())
  114. .def("f", &B::f, &B_wrap::default_f)
  115. .def("g", &B::g, &B_wrap::default_g),
  116. def("filter", &filter),
  117. class_<base, base_wrap>("base")
  118. .def(constructor<>())
  119. .def("f", &base::f, &base_wrap::default_f)
  120. .def("g", &base::g),
  121. class_<T_>("T")
  122. .def("f", &T_::f),
  123. class_<U, T_>("U")
  124. .def(constructor<>())
  125. .def("f", &T_::f)
  126. .def("f", &U::f)
  127. .def("g", &U::g)
  128. ];
  129. DOSTRING(L,
  130. "u = U()\n"
  131. "assert(u:f(0) == 1)\n"
  132. "assert(u:f(0,0) == 2)\n"
  133. "assert(u:g() == 3)\n");
  134. DOSTRING(L,
  135. "u = U()\n"
  136. "assert(u:f(0) == 1)\n"
  137. "assert(u:f(0,0) == 2)\n"
  138. "assert(u:g() == 3)\n");
  139. DOSTRING(L,
  140. "function base:fun()\n"
  141. " return 4\n"
  142. "end\n"
  143. "ba = base()\n"
  144. "assert(ba:fun() == 4)");
  145. DOSTRING(L,
  146. "class 'derived' (base)\n"
  147. " function derived:__init() base.__init(self) end\n"
  148. " function derived:f()\n"
  149. " return 'derived:f() : ' .. base.f(self)\n"
  150. " end\n"
  151. "class 'empty_derived' (base)\n"
  152. " function empty_derived:__init() base.__init(self) end\n"
  153. "class 'C' (B)\n"
  154. " function C:__init() B.__init(self) end\n"
  155. " function C:f() return 'C:f()' end\n"
  156. "function make_derived()\n"
  157. " return derived()\n"
  158. "end\n"
  159. "function make_empty_derived()\n"
  160. " return empty_derived()\n"
  161. "end\n"
  162. "function adopt_ptr(x)\n"
  163. " a = x\n"
  164. "end\n");
  165. DOSTRING(L,
  166. "function gen_error()\n"
  167. " assert(0 == 1)\n"
  168. "end\n");
  169. DOSTRING(L,
  170. "a = A()\n"
  171. "b = B()\n"
  172. "c = C()\n"
  173. "assert(c:f() == 'C:f()')\n"
  174. "assert(b:f() == 'B:f()')\n"
  175. "assert(a:f() == 'A:f()')\n"
  176. "assert(b:g() == 'A:g()')\n"
  177. "assert(c:g() == 'A:g()')\n"
  178. "assert(C.f(c) == 'C:f()')\n"
  179. "assert(B.f(c) == 'B:f()')\n"
  180. "assert(A.f(c) == 'A:f()')\n"
  181. "assert(A.g(c) == 'A:g()')\n");
  182. #ifndef LUABIND_NO_EXCEPTONS
  183. {
  184. LUABIND_CHECK_STACK(L);
  185. try { call_function<int>(L, "gen_error"); }
  186. catch (luabind::error&)
  187. {
  188. bool result(
  189. lua_tostring(L, -1) == std::string("[string \"function "
  190. "gen_error()...\"]:2: assertion failed!"));
  191. TEST_CHECK(result);
  192. lua_pop(L, 1);
  193. }
  194. }
  195. {
  196. A a;
  197. DOSTRING(L, "function test_ref(x) end");
  198. call_function<void>(L, "test_ref", boost::ref(a));
  199. }
  200. {
  201. LUABIND_CHECK_STACK(L);
  202. try { call_function<void>(L, "gen_error"); }
  203. catch (luabind::error&)
  204. {
  205. bool result(
  206. lua_tostring(L, -1) == std::string("[string \"function "
  207. "gen_error()...\"]:2: assertion failed!"));
  208. TEST_CHECK(result);
  209. lua_pop(L, 1);
  210. }
  211. }
  212. {
  213. LUABIND_CHECK_STACK(L);
  214. try { call_function<void>(L, "gen_error") [ adopt(result) ]; }
  215. catch (luabind::error&)
  216. {
  217. bool result(
  218. lua_tostring(L, -1) == std::string("[string \"function "
  219. "gen_error()...\"]:2: assertion failed!"));
  220. TEST_CHECK(result);
  221. lua_pop(L, 1);
  222. }
  223. }
  224. #endif
  225. base* ptr;
  226. {
  227. LUABIND_CHECK_STACK(L);
  228. TEST_NOTHROW(
  229. object a = globals(L)["ba"];
  230. TEST_CHECK(call_member<int>(a, "fun") == 4);
  231. );
  232. }
  233. {
  234. LUABIND_CHECK_STACK(L);
  235. object make_derived = globals(L)["make_derived"];
  236. TEST_NOTHROW(
  237. call_function<void>(make_derived)
  238. );
  239. }
  240. std::auto_ptr<base> own_ptr;
  241. {
  242. LUABIND_CHECK_STACK(L);
  243. TEST_NOTHROW(
  244. own_ptr = std::auto_ptr<base>(
  245. call_function<base*>(L, "make_derived") [ adopt(result) ])
  246. );
  247. }
  248. // make sure the derived lua-part is still referenced by
  249. // the adopted c++ pointer
  250. DOSTRING(L,
  251. "collectgarbage()\n"
  252. "collectgarbage()\n"
  253. "collectgarbage()\n"
  254. "collectgarbage()\n"
  255. "collectgarbage()\n");
  256. TEST_NOTHROW(
  257. TEST_CHECK(own_ptr->f() == "derived:f() : base:f()")
  258. );
  259. own_ptr = std::auto_ptr<base>();
  260. // test virtual functions that are not overridden by lua
  261. TEST_NOTHROW(
  262. own_ptr = std::auto_ptr<base>(
  263. call_function<base*>(L, "make_empty_derived") [ adopt(result) ])
  264. );
  265. TEST_NOTHROW(
  266. TEST_CHECK(own_ptr->f() == "base:f()")
  267. );
  268. TEST_NOTHROW(
  269. call_function<void>(L, "adopt_ptr", own_ptr.get()) [ adopt(_1) ]
  270. );
  271. own_ptr.release();
  272. // test virtual functions that are overridden by lua
  273. TEST_NOTHROW(
  274. ptr = call_function<base*>(L, "derived")
  275. );
  276. TEST_NOTHROW(
  277. TEST_CHECK(ptr->f() == "derived:f() : base:f()")
  278. );
  279. // test virtual function dispatch from within lua
  280. DOSTRING(L,
  281. "a = derived()\n"
  282. "b = filter(a)\n"
  283. "assert(b:f() == 'derived:f() : base:f()')\n");
  284. // test back references
  285. DOSTRING(L,
  286. "a = derived()\n"
  287. "assert(a == filter(a))\n");
  288. }