scope.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2004 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/scope.hpp>
  22. #include <luabind/detail/debug.hpp>
  23. #include <luabind/detail/stack_utils.hpp>
  24. #include <cassert>
  25. namespace luabind { namespace detail {
  26. registration::registration()
  27. : m_next(0)
  28. {
  29. }
  30. registration::~registration()
  31. {
  32. delete m_next;
  33. }
  34. } // namespace detail
  35. scope::scope()
  36. : m_chain(0)
  37. {
  38. }
  39. scope::scope(std::auto_ptr<detail::registration> reg)
  40. : m_chain(reg.release())
  41. {
  42. }
  43. scope::scope(scope const& other)
  44. : m_chain(other.m_chain)
  45. {
  46. const_cast<scope&>(other).m_chain = 0;
  47. }
  48. scope& scope::operator=(scope const& other_)
  49. {
  50. delete m_chain;
  51. m_chain = other_.m_chain;
  52. const_cast<scope&>(other_).m_chain = 0;
  53. return *this;
  54. }
  55. scope::~scope()
  56. {
  57. delete m_chain;
  58. }
  59. scope& scope::operator,(scope s)
  60. {
  61. if (!m_chain)
  62. {
  63. m_chain = s.m_chain;
  64. s.m_chain = 0;
  65. return *this;
  66. }
  67. for (detail::registration* c = m_chain;; c = c->m_next)
  68. {
  69. if (!c->m_next)
  70. {
  71. c->m_next = s.m_chain;
  72. s.m_chain = 0;
  73. break;
  74. }
  75. }
  76. return *this;
  77. }
  78. void scope::register_(lua_State* L) const
  79. {
  80. for (detail::registration* r = m_chain; r != 0; r = r->m_next)
  81. {
  82. LUABIND_CHECK_STACK(L);
  83. r->register_(L);
  84. }
  85. }
  86. } // namespace luabind
  87. namespace luabind {
  88. namespace {
  89. struct lua_pop_stack
  90. {
  91. lua_pop_stack(lua_State* L)
  92. : m_state(L)
  93. {
  94. }
  95. ~lua_pop_stack()
  96. {
  97. lua_pop(m_state, 1);
  98. }
  99. lua_State* m_state;
  100. };
  101. } // namespace unnamed
  102. module_::module_(lua_State* L, char const* name = 0)
  103. : m_state(L)
  104. , name_(name)
  105. {
  106. }
  107. void module_::operator[](scope s)
  108. {
  109. if (name_)
  110. {
  111. lua_pushstring(m_state, name_);
  112. lua_gettable(m_state, LUA_GLOBALSINDEX);
  113. if (!lua_istable(m_state, -1))
  114. {
  115. lua_pop(m_state, 1);
  116. lua_newtable(m_state);
  117. lua_pushstring(m_state, name_);
  118. lua_pushvalue(m_state, -2);
  119. lua_settable(m_state, LUA_GLOBALSINDEX);
  120. }
  121. }
  122. else
  123. {
  124. lua_pushvalue(m_state, LUA_GLOBALSINDEX);
  125. }
  126. lua_pop_stack guard(m_state);
  127. s.register_(m_state);
  128. }
  129. struct namespace_::registration_ : detail::registration
  130. {
  131. registration_(char const* name)
  132. : name_(name)
  133. {
  134. }
  135. void register_(lua_State* L) const
  136. {
  137. LUABIND_CHECK_STACK(L);
  138. assert(lua_gettop(L) >= 1);
  139. lua_pushstring(L, name_);
  140. lua_gettable(L, -2);
  141. detail::stack_pop p(L, 1); // pops the table on exit
  142. if (!lua_istable(L, -1))
  143. {
  144. lua_pop(L, 1);
  145. lua_newtable(L);
  146. lua_pushstring(L, name_);
  147. lua_pushvalue(L, -2);
  148. lua_settable(L, -4);
  149. }
  150. m_scope.register_(L);
  151. }
  152. char const* name_;
  153. scope m_scope;
  154. };
  155. namespace_::namespace_(char const* name)
  156. : scope(std::auto_ptr<detail::registration>(
  157. m_registration = new registration_(name)))
  158. {
  159. }
  160. namespace_& namespace_::operator[](scope s)
  161. {
  162. m_registration->m_scope.operator,(s);
  163. return *this;
  164. }
  165. } // namespace luabind