create_class.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2003 Daniel Wallin and 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. #define LUABIND_BUILDING
  20. #include <luabind/lua_include.hpp>
  21. #include <luabind/luabind.hpp>
  22. namespace luabind { namespace detail
  23. {
  24. namespace
  25. {
  26. // expects two tables on the lua stack:
  27. // 1: destination
  28. // 2: source
  29. void copy_member_table(lua_State* L)
  30. {
  31. lua_pushnil(L);
  32. while (lua_next(L, -2))
  33. {
  34. lua_pushstring(L, "__init");
  35. if (lua_equal(L, -1, -3))
  36. {
  37. lua_pop(L, 2);
  38. continue;
  39. }
  40. else lua_pop(L, 1); // __init string
  41. lua_pushstring(L, "__finalize");
  42. if (lua_equal(L, -1, -3))
  43. {
  44. lua_pop(L, 2);
  45. continue;
  46. }
  47. else lua_pop(L, 1); // __finalize string
  48. lua_pushvalue(L, -2); // copy key
  49. lua_insert(L, -2);
  50. lua_settable(L, -5);
  51. }
  52. }
  53. }
  54. int create_class::stage2(lua_State* L)
  55. {
  56. class_rep* crep = static_cast<class_rep*>(lua_touserdata(L, lua_upvalueindex(1)));
  57. assert((crep != 0) && "internal error, please report");
  58. assert((is_class_rep(L, lua_upvalueindex(1))) && "internal error, please report");
  59. #ifndef LUABIND_NO_ERROR_CHECKING
  60. if (!is_class_rep(L, 1))
  61. {
  62. lua_pushstring(L, "expected class to derive from or a newline");
  63. lua_error(L);
  64. }
  65. #endif
  66. class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
  67. class_rep::base_info binfo;
  68. binfo.pointer_offset = 0;
  69. binfo.base = base;
  70. crep->add_base_class(binfo);
  71. // copy base class members
  72. crep->get_table(L);
  73. base->get_table(L);
  74. copy_member_table(L);
  75. crep->get_default_table(L);
  76. base->get_default_table(L);
  77. copy_member_table(L);
  78. crep->set_type(base->type());
  79. return 0;
  80. }
  81. int create_class::stage1(lua_State* L)
  82. {
  83. #ifndef LUABIND_NO_ERROR_CHECKING
  84. if (lua_gettop(L) != 1 || lua_type(L, 1) != LUA_TSTRING || lua_isnumber(L, 1))
  85. {
  86. lua_pushstring(L, "invalid construct, expected class name");
  87. lua_error(L);
  88. }
  89. if (std::strlen(lua_tostring(L, 1)) != lua_strlen(L, 1))
  90. {
  91. lua_pushstring(L, "luabind does not support class names with extra nulls");
  92. lua_error(L);
  93. }
  94. #endif
  95. const char* name = lua_tostring(L, 1);
  96. void* c = lua_newuserdata(L, sizeof(class_rep));
  97. new(c) class_rep(L, name);
  98. // make the class globally available
  99. lua_pushstring(L, name);
  100. lua_pushvalue(L, -2);
  101. lua_settable(L, LUA_GLOBALSINDEX);
  102. // also add it to the closure as return value
  103. lua_pushcclosure(L, &stage2, 1);
  104. return 1;
  105. }
  106. }}