test_extend_class_in_lua.cpp 769 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright Daniel Wallin 2009. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. struct CppClass
  7. {
  8. int f(int x)
  9. {
  10. return x;
  11. }
  12. };
  13. void test_main(lua_State* L)
  14. {
  15. using namespace luabind;
  16. module(L) [
  17. class_<CppClass>("CppClass")
  18. .def(constructor<>())
  19. .def("f", &CppClass::f)
  20. ];
  21. DOSTRING(L,
  22. "function CppClass:f_in_lua(x)\n"
  23. " return self:f(x) * 2\n"
  24. "end\n"
  25. );
  26. DOSTRING(L,
  27. "x = CppClass()\n"
  28. "assert(x:f(1) == 1)\n"
  29. "assert(x:f_in_lua(1) == 2)\n"
  30. );
  31. }